blob: 601d1deb66477715c96360993595a37afc5170fc [file] [log] [blame]
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <assert.h>
7
8#include <xcb/xcb.h>
9#include <xgl.h>
10#include <xglDbg.h>
11#include <xglWsiX11Ext.h>
12
Cody Northropfe3d8bc2015-03-17 14:54:35 -060013#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060014
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060016#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060017
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060018#define DEMO_BUFFER_COUNT 2
19#define DEMO_TEXTURE_COUNT 1
20
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060021/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070022 * structure to track all objects related to a texture.
23 */
24struct texture_objects {
25 XGL_SAMPLER sampler;
26
27 XGL_IMAGE image;
28 uint32_t num_mem;
29 XGL_GPU_MEMORY *mem;
30 XGL_IMAGE_VIEW view;
31 int32_t tex_width, tex_height;
32};
33
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060034static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060035 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060036};
37
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060038struct xglcube_vs_uniform {
39 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060040 float mvp[4][4];
41 float position[12*3][4];
42 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060043};
44
45struct xgltexcube_vs_uniform {
46 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060047 float mvp[4][4];
48 float position[12*3][4];
49 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060050};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060051
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052//--------------------------------------------------------------------------------------
53// Mesh and VertexFormat Data
54//--------------------------------------------------------------------------------------
55struct Vertex
56{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060057 float posX, posY, posZ, posW; // Position data
58 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060059};
60
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060061struct VertexPosTex
62{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060063 float posX, posY, posZ, posW; // Position data
64 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060065};
66
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060067#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060068#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060069
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060070static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060071 -1.0f,-1.0f,-1.0f, // Vertex 0
72 -1.0f,-1.0f, 1.0f,
73 -1.0f, 1.0f, 1.0f,
74
75 -1.0f, 1.0f, 1.0f, // Vertex 1
76 -1.0f, 1.0f,-1.0f,
77 -1.0f,-1.0f,-1.0f,
78
79 -1.0f,-1.0f,-1.0f, // Vertex 2
80 1.0f, 1.0f,-1.0f,
81 1.0f,-1.0f,-1.0f,
82
83 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060084 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060085 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060086
87 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060088 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060089 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060090
91 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060092 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060093 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060094
95 -1.0f, 1.0f,-1.0f, // Vertex 6
96 -1.0f, 1.0f, 1.0f,
97 1.0f, 1.0f, 1.0f,
98
99 -1.0f, 1.0f,-1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600100 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600101 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600102
103 1.0f, 1.0f,-1.0f, // Vertex 8
104 1.0f, 1.0f, 1.0f,
105 1.0f,-1.0f, 1.0f,
106
107 1.0f,-1.0f, 1.0f, // Vertex 9
108 1.0f,-1.0f,-1.0f,
109 1.0f, 1.0f,-1.0f,
110
111 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600112 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600113 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600114
115 -1.0f,-1.0f, 1.0f, // Vertex 11
116 1.0f,-1.0f, 1.0f,
117 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600118};
119
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600120static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600121 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600122 0.0f, 0.0f,
123 0.0f, 1.0f,
124
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600125 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600126 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600127 1.0f, 0.0f,
128
129// 0.0f, 1.0f, // Vertex 2
130// 1.0f, 0.0f,
131// 0.0f, 0.0f,
132
133// 0.0f, 1.0f, // Vertex 3
134// 1.0f, 0.0f,
135// 1.0f, 1.0f,
136
137 0.0f, 0.0f, // Vertex 2
138 1.0f, 1.0f,
139 1.0f, 0.0f,
140
141 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600142 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600143 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600144
145 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600146 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600147 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600148
149 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600150 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600151 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152
153 0.0f, 1.0f, // Vertex 6
154 1.0f, 1.0f,
155 1.0f, 0.0f,
156
157 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600158 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600159 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600161 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600164
165 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 0.0f, 1.0f,
168
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600169 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600171 0.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600172
173 1.0f, 0.0f, // Vertex 11
174 0.0f, 0.0f,
175 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600176};
177
178void dumpMatrix(const char *note, mat4x4 MVP)
179{
180 int i;
181
182 printf("%s: \n", note);
183 for (i=0; i<4; i++) {
184 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
185 }
186 printf("\n");
187 fflush(stdout);
188}
189
190void dumpVec4(const char *note, vec4 vector)
191{
192 printf("%s: \n", note);
193 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
194 printf("\n");
195 fflush(stdout);
196}
197
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600198struct demo {
199 xcb_connection_t *connection;
200 xcb_screen_t *screen;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700201 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600202
Jon Ashburn93cfc432015-01-29 15:47:01 -0700203 XGL_INSTANCE inst;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600204 XGL_PHYSICAL_GPU gpu;
205 XGL_DEVICE device;
206 XGL_QUEUE queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700207 uint32_t graphics_queue_node_index;
208 XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600209
210 int width, height;
211 XGL_FORMAT format;
212
213 struct {
214 XGL_IMAGE image;
215 XGL_GPU_MEMORY mem;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700216 XGL_CMD_BUFFER cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600217
218 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800219 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600220 } buffers[DEMO_BUFFER_COUNT];
221
222 struct {
223 XGL_FORMAT format;
224
225 XGL_IMAGE image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600226 uint32_t num_mem;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700227 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600228 XGL_DEPTH_STENCIL_VIEW view;
229 } depth;
230
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700231 struct texture_objects textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600232
233 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800234 XGL_BUFFER buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600235 uint32_t num_mem;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700236 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800237 XGL_BUFFER_VIEW view;
238 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600239 } uniform_data;
240
Chia-I Wu87544e72015-02-23 10:41:08 -0700241 XGL_DESCRIPTOR_SET_LAYOUT desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600242 XGL_PIPELINE pipeline;
243
Tony Barbour29645d02015-01-16 14:27:35 -0700244 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
245 XGL_DYNAMIC_RS_STATE_OBJECT raster;
246 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
247 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600248
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600249 mat4x4 projection_matrix;
250 mat4x4 view_matrix;
251 mat4x4 model_matrix;
252
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600253 float spin_angle;
254 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600255 bool pause;
256
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800257 XGL_DESCRIPTOR_REGION desc_region;
258 XGL_DESCRIPTOR_SET desc_set;
259
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600260 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700261 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600262
263 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600264 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600265};
266
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700267static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600268{
269 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
270 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000271 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600272 };
273 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
274 .view = demo->depth.view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000275 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600276 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600277 const float clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
278 const float clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600279 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700280 XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = {
281 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO,
282 .pNext = NULL,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700283 };
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700284 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
285 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700286 .pNext = &graphics_cmd_buf_info,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700287 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
288 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
289 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600290 XGL_RESULT err;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700291 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
292 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
293 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
294 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
295 .pNext = NULL,
296 .colorAttachmentCount = 1,
297 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
298 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
299 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600300 .width = demo->width,
301 .height = demo->height,
302 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700303 };
304 XGL_RENDER_PASS_CREATE_INFO rp_info;
305
306 memset(&rp_info, 0 , sizeof(rp_info));
307 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
308 assert(!err);
309 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
310 rp_info.renderArea.extent.width = demo->width;
311 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchterc17d7de2015-02-10 14:06:25 -0700312 rp_info.colorAttachmentCount = 1;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700313 rp_info.pColorLoadOps = &load_op;
314 rp_info.pColorStoreOps = &store_op;
315 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
316 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
317 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
318 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
319 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
320 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700322 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323 assert(!err);
324
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700325 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326 demo->pipeline);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700327 xglCmdBindDescriptorSet(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800328 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700330 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
331 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
332 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333 demo->color_blend);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700334 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335 demo->depth_stencil);
336
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700337 xglCmdBeginRenderPass(cmd_buf, graphics_cmd_buf_info.renderPass);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600338 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
339 clear_range.baseMipLevel = 0;
340 clear_range.mipLevels = 1;
341 clear_range.baseArraySlice = 0;
342 clear_range.arraySize = 1;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700343 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600344 demo->buffers[demo->current_buffer].image,
345 clear_color, 1, &clear_range);
346
347 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700348 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600349 clear_depth, 0, 1, &clear_range);
350
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700351 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700352 xglCmdEndRenderPass(cmd_buf, graphics_cmd_buf_info.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600353
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700354 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700356
357 xglDestroyObject(graphics_cmd_buf_info.renderPass);
358 xglDestroyObject(rp_info.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600359}
360
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600361
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600362void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600364 mat4x4 MVP, Model, VP;
365 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600366 uint8_t *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600367 XGL_RESULT err;
368
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600369 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600371 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600372 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700373 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600374 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600375
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700376 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600377 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600378 assert(!err);
379
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600380 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600381
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700382 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383 assert(!err);
384}
385
386static void demo_draw(struct demo *demo)
387{
388 const XGL_WSI_X11_PRESENT_INFO present = {
389 .destWindow = demo->window,
390 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800391 .async = true,
392 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600393 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800394 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395 XGL_RESULT err;
396
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600397 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wubb57f642014-11-07 14:30:34 +0800398 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
399
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -0600400 uint32_t i, idx = 0;
401 XGL_MEMORY_REF *memRefs;
402 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
403 demo->depth.num_mem +
404 demo->textures[0].num_mem +
405 demo->uniform_data.num_mem));
406 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
407 memRefs[idx].mem = demo->depth.mem[i];
408 memRefs[idx].flags = 0;
409 }
410 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
411 memRefs[idx].mem = demo->textures[0].mem[i];
412 memRefs[idx].flags = 0;
413 }
414 memRefs[idx].mem = demo->buffers[0].mem;
415 memRefs[idx++].flags = 0;
416 memRefs[idx].mem = demo->buffers[1].mem;
417 memRefs[idx++].flags = 0;
418 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
419 memRefs[idx].mem = demo->uniform_data.mem[i];
420 memRefs[idx].flags = 0;
421 }
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700422 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Piers Daniell735ee532015-02-23 16:23:13 -0700423 idx, memRefs, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600424 assert(!err);
425
Chia-I Wubb57f642014-11-07 14:30:34 +0800426 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600427 assert(!err);
428
429 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
430}
431
432static void demo_prepare_buffers(struct demo *demo)
433{
434 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
435 .format = demo->format,
436 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
437 .extent = {
438 .width = demo->width,
439 .height = demo->height,
440 },
441 .flags = 0,
442 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800443 const XGL_FENCE_CREATE_INFO fence = {
444 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
445 .pNext = NULL,
446 .flags = 0,
447 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600448 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600449 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600450
451 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
452 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
453 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
454 .pNext = NULL,
455 .format = demo->format,
456 .mipLevel = 0,
457 .baseArraySlice = 0,
458 .arraySize = 1,
459 };
460
461 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
462 &demo->buffers[i].image, &demo->buffers[i].mem);
463 assert(!err);
464
465 color_attachment_view.image = demo->buffers[i].image;
466
467 err = xglCreateColorAttachmentView(demo->device,
468 &color_attachment_view, &demo->buffers[i].view);
469 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800470
471 err = xglCreateFence(demo->device,
472 &fence, &demo->buffers[i].fence);
473 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600474 }
475}
476
477static void demo_prepare_depth(struct demo *demo)
478{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700479 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600480 const XGL_IMAGE_CREATE_INFO image = {
481 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
482 .pNext = NULL,
483 .imageType = XGL_IMAGE_2D,
484 .format = depth_format,
485 .extent = { demo->width, demo->height, 1 },
486 .mipLevels = 1,
487 .arraySize = 1,
488 .samples = 1,
489 .tiling = XGL_OPTIMAL_TILING,
490 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
491 .flags = 0,
492 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700493 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
494 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
495 .pNext = NULL,
496 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600497 XGL_MEMORY_ALLOC_INFO mem_alloc = {
498 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700499 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600500 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700501 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700502 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600503 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
504 };
505 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
506 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
507 .pNext = NULL,
508 .image = XGL_NULL_HANDLE,
509 .mipLevel = 0,
510 .baseArraySlice = 0,
511 .arraySize = 1,
512 .flags = 0,
513 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700514 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600515 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700516 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600517 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600518 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600519 uint32_t num_allocations = 0;
520 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600521
522 demo->depth.format = depth_format;
523
524 /* create image */
525 err = xglCreateImage(demo->device, &image,
526 &demo->depth.image);
527 assert(!err);
528
Jon Ashburnb2a66652015-01-16 09:37:43 -0700529
530 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
531 assert(!err && num_alloc_size == sizeof(num_allocations));
532 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
533 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
534 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700536 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
537 &mem_reqs_size, mem_reqs);
538 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700539 err = xglGetObjectInfo(demo->depth.image,
540 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
541 &img_reqs_size, &img_reqs);
542 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
543 img_alloc.usage = img_reqs.usage;
544 img_alloc.formatClass = img_reqs.formatClass;
545 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600546 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700547 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600548
Jon Ashburnb2a66652015-01-16 09:37:43 -0700549 /* allocate memory */
550 err = xglAllocMemory(demo->device, &mem_alloc,
551 &(demo->depth.mem[i]));
552 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553
Jon Ashburnb2a66652015-01-16 09:37:43 -0700554 /* bind memory */
555 err = xglBindObjectMemory(demo->depth.image, i,
556 demo->depth.mem[i], 0);
557 assert(!err);
558 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600559
560 /* create image view */
561 view.image = demo->depth.image;
562 err = xglCreateDepthStencilView(demo->device, &view,
563 &demo->depth.view);
564 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600565}
566
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600567/** loadTexture
568 * loads a png file into an memory object, using cstdio , libpng.
569 *
570 * \param demo : Needed to access XGL calls
571 * \param filename : the png file to be loaded
572 * \param width : width of png, to be updated as a side effect of this function
573 * \param height : height of png, to be updated as a side effect of this function
574 *
575 * \return bool : an opengl texture id. true if successful?,
576 * should be validated by the client of this function.
577 *
578 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
579 * Modified to copy image to memory
580 *
581 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700582bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600583 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600584 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600585{
586 //header for testing if it is a png
587 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700588 int is_png, bit_depth, color_type,rowbytes;
589 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600590 png_structp png_ptr;
591 png_infop info_ptr, end_info;
592 png_byte *image_data;
593 png_bytep *row_pointers;
594
595 //open file as binary
596 FILE *fp = fopen(filename, "rb");
597 if (!fp) {
598 return false;
599 }
600
601 //read the header
602 fread(header, 1, 8, fp);
603
604 //test if png
605 is_png = !png_sig_cmp(header, 0, 8);
606 if (!is_png) {
607 fclose(fp);
608 return false;
609 }
610
611 //create png struct
612 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
613 NULL, NULL);
614 if (!png_ptr) {
615 fclose(fp);
616 return (false);
617 }
618
619 //create png info struct
620 info_ptr = png_create_info_struct(png_ptr);
621 if (!info_ptr) {
622 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
623 fclose(fp);
624 return (false);
625 }
626
627 //create png info struct
628 end_info = png_create_info_struct(png_ptr);
629 if (!end_info) {
630 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
631 fclose(fp);
632 return (false);
633 }
634
635 //png error stuff, not sure libpng man suggests this.
636 if (setjmp(png_jmpbuf(png_ptr))) {
637 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
638 fclose(fp);
639 return (false);
640 }
641
642 //init png reading
643 png_init_io(png_ptr, fp);
644
645 //let libpng know you already read the first 8 bytes
646 png_set_sig_bytes(png_ptr, 8);
647
648 // read all the info up to the image data
649 png_read_info(png_ptr, info_ptr);
650
651 // get info about png
652 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
653 NULL, NULL, NULL);
654
655 //update width and height based on png info
656 *width = twidth;
657 *height = theight;
658
659 // Require that incoming texture be 8bits per color component
660 // and 4 components (RGBA).
661 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
662 png_get_channels(png_ptr, info_ptr) != 4) {
663 return false;
664 }
665
666 if (rgba_data == NULL) {
667 // If data pointer is null, we just want the width & height
668 // clean up memory and close stuff
669 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
670 fclose(fp);
671
672 return true;
673 }
674
675 // Update the png info struct.
676 png_read_update_info(png_ptr, info_ptr);
677
678 // Row size in bytes.
679 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
680
681 // Allocate the image_data as a big block, to be given to opengl
682 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
683 if (!image_data) {
684 //clean up memory and close stuff
685 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
686 fclose(fp);
687 return false;
688 }
689
690 // row_pointers is for pointing to image_data for reading the png with libpng
691 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
692 if (!row_pointers) {
693 //clean up memory and close stuff
694 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
695 // delete[] image_data;
696 fclose(fp);
697 return false;
698 }
699 // set the individual row_pointers to point at the correct offsets of image_data
700 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700701 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600702
703 // read the png into image_data through row_pointers
704 png_read_image(png_ptr, row_pointers);
705
706 // clean up memory and close stuff
707 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
708 free(row_pointers);
709 free(image_data);
710 fclose(fp);
711
712 return true;
713}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600714
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700715static void demo_prepare_texture_image(struct demo *demo,
716 const char *filename,
717 struct texture_objects *tex_objs,
718 XGL_IMAGE_TILING tiling,
719 XGL_FLAGS mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600720{
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700721 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600722 int32_t tex_width;
723 int32_t tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600724 XGL_RESULT err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700725
726 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
727 assert(err);
728
729 tex_objs->tex_width = tex_width;
730 tex_objs->tex_height = tex_height;
731
732 const XGL_IMAGE_CREATE_INFO image_create_info = {
733 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
734 .pNext = NULL,
735 .imageType = XGL_IMAGE_2D,
736 .format = tex_format,
737 .extent = { tex_width, tex_height, 1 },
738 .mipLevels = 1,
739 .arraySize = 1,
740 .samples = 1,
741 .tiling = tiling,
742 .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
743 .flags = 0,
744 };
Piers Daniell735ee532015-02-23 16:23:13 -0700745 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
746 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
747 .pNext = NULL,
748 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700749 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
750 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
Piers Daniell735ee532015-02-23 16:23:13 -0700751 .pNext = &buf_alloc,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700752 };
753 XGL_MEMORY_ALLOC_INFO mem_alloc = {
754 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
755 .pNext = &img_alloc,
756 .allocationSize = 0,
757 .memProps = mem_props,
758 .memType = XGL_MEMORY_TYPE_IMAGE,
759 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
760 };
761
762 XGL_MEMORY_REQUIREMENTS *mem_reqs;
763 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Piers Daniell735ee532015-02-23 16:23:13 -0700764 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
765 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700766 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
767 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
768 uint32_t num_allocations = 0;
769 size_t num_alloc_size = sizeof(num_allocations);
770
771 err = xglCreateImage(demo->device, &image_create_info,
772 &tex_objs->image);
773 assert(!err);
774
775 err = xglGetObjectInfo(tex_objs->image,
776 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
777 &num_alloc_size, &num_allocations);
778 assert(!err && num_alloc_size == sizeof(num_allocations));
779 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
780 tex_objs->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
781 err = xglGetObjectInfo(tex_objs->image,
782 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
783 &mem_reqs_size, mem_reqs);
784 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
785 err = xglGetObjectInfo(tex_objs->image,
786 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
787 &img_reqs_size, &img_reqs);
788 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
789 img_alloc.usage = img_reqs.usage;
790 img_alloc.formatClass = img_reqs.formatClass;
791 img_alloc.samples = img_reqs.samples;
792 mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
793 for (uint32_t j = 0; j < num_allocations; j ++) {
Courtney Goeltzenleuchter02ba99a2015-02-25 11:48:28 -0700794 mem_alloc.memType = mem_reqs[j].memType;
Piers Daniell735ee532015-02-23 16:23:13 -0700795 mem_alloc.allocationSize = mem_reqs[j].size;
796
797 if (mem_alloc.memType == XGL_MEMORY_TYPE_BUFFER) {
798 err = xglGetObjectInfo(tex_objs->image,
799 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
800 &buf_reqs_size, &buf_reqs);
801 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
802 buf_alloc.usage = buf_reqs.usage;
803 img_alloc.pNext = &buf_alloc;
804 } else {
805 img_alloc.pNext = 0;
806 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700807
808 /* allocate memory */
809 err = xglAllocMemory(demo->device, &mem_alloc,
810 &(tex_objs->mem[j]));
811 assert(!err);
812
813 /* bind memory */
814 err = xglBindObjectMemory(tex_objs->image, j, tex_objs->mem[j], 0);
815 assert(!err);
816 }
817 free(mem_reqs);
818 mem_reqs = NULL;
819
820 tex_objs->num_mem = num_allocations;
821
822 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
823 const XGL_IMAGE_SUBRESOURCE subres = {
824 .aspect = XGL_IMAGE_ASPECT_COLOR,
825 .mipLevel = 0,
826 .arraySlice = 0,
827 };
828 XGL_SUBRESOURCE_LAYOUT layout;
829 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
830 void *data;
831
832 err = xglGetImageSubresourceInfo(tex_objs->image, &subres,
833 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
834 &layout_size, &layout);
835 assert(!err && layout_size == sizeof(layout));
836 /* Linear texture must be within a single memory object */
837 assert(num_allocations == 1);
838
839 err = xglMapMemory(tex_objs->mem[0], 0, &data);
840 assert(!err);
841
842 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
843 fprintf(stderr, "Error loading texture: %s\n", filename);
844 }
845
846 err = xglUnmapMemory(tex_objs->mem[0]);
847 assert(!err);
848 }
849}
850
851static void demo_destroy_texture_image(struct texture_objects *tex_objs)
852{
853 /* clean up staging resources */
854 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
855 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
856 xglFreeMemory(tex_objs->mem[j]);
857 }
858
859 free(tex_objs->mem);
860 xglDestroyObject(tex_objs->image);
861}
862
863static void demo_prepare_textures(struct demo *demo)
864{
865 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
866 XGL_FORMAT_PROPERTIES props;
867 size_t size = sizeof(props);
868 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600869 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600870
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700871 err = xglGetFormatInfo(demo->device, tex_format,
872 XGL_INFO_TYPE_FORMAT_PROPERTIES,
873 &size, &props);
874 assert(!err);
875
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600876 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700877
878 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
879 /* Device can texture using linear textures */
880 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
881 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
882 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT){
883 /* Must use staging buffer to copy linear texture to optimized */
884 struct texture_objects staging_texture;
885
886 memset(&staging_texture, 0, sizeof(staging_texture));
887 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
888 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
889
890 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
891 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
892
893 XGL_CMD_BUFFER staging_cmd_buf;
894 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {
895 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
896 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700897 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700898 .flags = 0
899 };
900
901 err = xglCreateCommandBuffer(demo->device, &cmd_buf_create_info, &staging_cmd_buf);
902 assert(!err);
903
904 /* Copy staging texture to usable texture */
905 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {
906 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
907 .pNext = NULL,
908 .flags = 0
909 };
910
911 err = xglResetCommandBuffer(staging_cmd_buf);
912 assert(!err);
913
914 err = xglBeginCommandBuffer(staging_cmd_buf, &cmd_buf_begin_info);
915 assert(!err);
916
917 XGL_IMAGE_COPY copy_region = {
918 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
919 .srcOffset = { 0, 0, 0 },
920 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
921 .destOffset = { 0, 0, 0 },
922 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
923 };
924 xglCmdCopyImage(staging_cmd_buf, staging_texture.image, demo->textures[i].image, 1, &copy_region);
925
926 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
927 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
928 .pNext = NULL,
929 .outputMask = XGL_MEMORY_OUTPUT_COPY_BIT,
930 .inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT,
931 .oldLayout = XGL_IMAGE_LAYOUT_GENERAL,
932 .newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
933 .image = staging_texture.image,
934 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
935 };
936 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
937
938 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
939 XGL_PIPELINE_BARRIER pipeline_barrier;
940 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
941 pipeline_barrier.pNext = NULL;
942 pipeline_barrier.eventCount = 1;
943 pipeline_barrier.pEvents = set_events;
944 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
945 pipeline_barrier.memBarrierCount = 1;
946 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
947
948 // write barrier to the command buffer
949 xglCmdPipelineBarrier(staging_cmd_buf, &pipeline_barrier);
950
951 err = xglEndCommandBuffer(staging_cmd_buf);
952 assert(!err);
953
954 const XGL_CMD_BUFFER cmd_bufs[] = { staging_cmd_buf };
955 XGL_MEMORY_REF mem_refs[16];
956 uint32_t num_refs = 0;
957
958 for (uint32_t j = 0; j < staging_texture.num_mem; j++) {
959 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
960 mem_refs[num_refs].mem = staging_texture.mem[j];
961 num_refs++;
962 assert(num_refs < 16);
963 }
964
965 for (uint32_t j = 0; j < demo->textures[i].num_mem; j++) {
966 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
967 mem_refs[num_refs].mem = demo->textures[i].mem[j];
968 num_refs++;
969 assert(num_refs < 16);
970 }
971
972 err = xglQueueSubmit(demo->queue, 1, cmd_bufs,
973 num_refs, mem_refs, XGL_NULL_HANDLE);
974 assert(!err);
975
976 err = xglQueueWaitIdle(demo->queue);
977 assert(!err);
978
979 demo_destroy_texture_image(&staging_texture);
980
981 xglDestroyObject(staging_cmd_buf);
982 } else {
983 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
984 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
985 }
986
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600987 const XGL_SAMPLER_CREATE_INFO sampler = {
988 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
989 .pNext = NULL,
990 .magFilter = XGL_TEX_FILTER_NEAREST,
991 .minFilter = XGL_TEX_FILTER_NEAREST,
992 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600993 .addressU = XGL_TEX_ADDRESS_CLAMP,
994 .addressV = XGL_TEX_ADDRESS_CLAMP,
995 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600996 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700997 .maxAnisotropy = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600998 .compareFunc = XGL_COMPARE_NEVER,
999 .minLod = 0.0f,
1000 .maxLod = 0.0f,
1001 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
1002 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001003
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004 XGL_IMAGE_VIEW_CREATE_INFO view = {
1005 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1006 .pNext = NULL,
1007 .image = XGL_NULL_HANDLE,
1008 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001009 .format = tex_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001010 .channels = { XGL_CHANNEL_SWIZZLE_R,
1011 XGL_CHANNEL_SWIZZLE_G,
1012 XGL_CHANNEL_SWIZZLE_B,
1013 XGL_CHANNEL_SWIZZLE_A, },
1014 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
1015 .minLod = 0.0f,
1016 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001017
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018 /* create sampler */
1019 err = xglCreateSampler(demo->device, &sampler,
1020 &demo->textures[i].sampler);
1021 assert(!err);
1022
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001023 /* create image view */
1024 view.image = demo->textures[i].image;
1025 err = xglCreateImageView(demo->device, &view,
1026 &demo->textures[i].view);
1027 assert(!err);
1028 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001029}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001030
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001031void demo_prepare_cube_data_buffer(struct demo *demo)
1032{
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001033 XGL_BUFFER_CREATE_INFO buf_info;
1034 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001035 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1036 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1037 .pNext = NULL,
1038 };
1039 XGL_MEMORY_ALLOC_INFO alloc_info = {
1040 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1041 .pNext = &buf_alloc,
1042 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -07001043 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -07001044 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001045 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1046 };
1047 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001048 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001049 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001050 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1051 uint32_t num_allocations = 0;
1052 size_t num_alloc_size = sizeof(num_allocations);
1053 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001054 int i;
1055 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001056 XGL_RESULT err;
1057 struct xgltexcube_vs_uniform data;
1058
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001059 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001060 mat4x4_mul(MVP, VP, demo->model_matrix);
1061 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001062// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001063
1064 for (i=0; i<12*3; i++) {
1065 data.position[i][0] = g_vertex_buffer_data[i*3];
1066 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1067 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1068 data.position[i][3] = 1.0f;
1069 data.attr[i][0] = g_uv_buffer_data[2*i];
1070 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1071 data.attr[i][2] = 0;
1072 data.attr[i][3] = 0;
1073 }
1074
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001075 memset(&buf_info, 0, sizeof(buf_info));
1076 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1077 buf_info.size = sizeof(data);
1078 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1079 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1080 assert(!err);
1081
1082 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001083 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1084 &num_alloc_size, &num_allocations);
1085 assert(!err && num_alloc_size == sizeof(num_allocations));
1086 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1087 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1088 demo->uniform_data.num_mem = num_allocations;
1089 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001090 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001091 &mem_reqs_size, mem_reqs);
1092 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1093 err = xglGetObjectInfo(demo->uniform_data.buf,
1094 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1095 &buf_reqs_size, &buf_reqs);
1096 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1097 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001098 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001099 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001100
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001101 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1102 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001103
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001104 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001105 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001106
Piers Daniell735ee532015-02-23 16:23:13 -07001107 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001108
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001109 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1110 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001111
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001112 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1113 demo->uniform_data.mem[i], 0);
1114 assert(!err);
1115 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001116
1117 memset(&view_info, 0, sizeof(view_info));
1118 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1119 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +08001120 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001121 view_info.offset = 0;
1122 view_info.range = sizeof(data);
1123
1124 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1125 assert(!err);
1126
1127 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1128 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001129}
1130
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001131static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001132{
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001133 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
1134 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1135 .pNext = NULL,
1136 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1137 .count = DEMO_TEXTURE_COUNT,
1138 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
1139 .immutableSampler = XGL_NULL_HANDLE,
1140 };
Chia-I Wu87544e72015-02-23 10:41:08 -07001141 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
1142 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1143 .pNext = &descriptor_layout_fs,
1144 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1145 .count = 1,
1146 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
1147 .immutableSampler = XGL_NULL_HANDLE,
1148 };
Chia-I Wu06f82812015-02-23 10:41:08 -07001149
1150 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001151 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001152
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001153 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu06f82812015-02-23 10:41:08 -07001154 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001155 XGL_NULL_HANDLE, &descriptor_layout_vs,
Chia-I Wu87544e72015-02-23 10:41:08 -07001156 &demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001157 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001158}
1159
1160static XGL_SHADER demo_prepare_shader(struct demo *demo,
1161 XGL_PIPELINE_SHADER_STAGE stage,
1162 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001163 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001164{
1165 XGL_SHADER_CREATE_INFO createInfo;
1166 XGL_SHADER shader;
1167 XGL_RESULT err;
1168
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001169
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001170 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1171 createInfo.pNext = NULL;
1172
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001173#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001174 createInfo.codeSize = size;
1175 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001176 createInfo.flags = 0;
1177
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001178 err = xglCreateShader(demo->device, &createInfo, &shader);
1179 if (err) {
1180 free((void *) createInfo.pCode);
1181 }
1182#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001183 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001184 // to the driver "under the covers"
1185 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1186 createInfo.pCode = malloc(createInfo.codeSize);
1187 createInfo.flags = 0;
1188
1189 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001190 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001191 ((uint32_t *) createInfo.pCode)[1] = 0;
1192 ((uint32_t *) createInfo.pCode)[2] = stage;
1193 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1194
1195 err = xglCreateShader(demo->device, &createInfo, &shader);
1196 if (err) {
1197 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001198 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001199 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001200#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001201
1202 return shader;
1203}
1204
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001205char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001206{
1207 long int size;
1208 void *shader_code;
1209
1210 FILE *fp = fopen(filename, "rb");
1211 if (!fp) return NULL;
1212
1213 fseek(fp, 0L, SEEK_END);
1214 size = ftell(fp);
1215
1216 fseek(fp, 0L, SEEK_SET);
1217
1218 shader_code = malloc(size);
1219 fread(shader_code, size, 1, fp);
1220
1221 *psize = size;
1222
1223 return shader_code;
1224}
1225
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001226static XGL_SHADER demo_prepare_vs(struct demo *demo)
1227{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001228#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001229 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001230 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001231
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001232 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001233
1234 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1235 vertShaderCode, size);
1236#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001237 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001238 "#version 140\n"
1239 "#extension GL_ARB_separate_shader_objects : enable\n"
1240 "#extension GL_ARB_shading_language_420pack : enable\n"
1241 "\n"
1242 "layout(binding = 0) uniform buf {\n"
1243 " mat4 MVP;\n"
1244 " vec4 position[12*3];\n"
1245 " vec4 attr[12*3];\n"
1246 "} ubuf;\n"
1247 "\n"
1248 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001249 "\n"
1250 "void main() \n"
1251 "{\n"
1252 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001253 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1254 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001255
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001256 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1257 (const void *) vertShaderText,
1258 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001259#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001260}
1261
1262static XGL_SHADER demo_prepare_fs(struct demo *demo)
1263{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001264#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001265 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001266 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001267
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001268 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001269
1270 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1271 fragShaderCode, size);
1272#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001273 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001274 "#version 140\n"
1275 "#extension GL_ARB_separate_shader_objects : enable\n"
1276 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001277 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001278 "\n"
1279 "layout (location = 0) in vec4 texcoord;\n"
1280 "void main() {\n"
1281 " gl_FragColor = texture(tex, texcoord.xy);\n"
1282 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001283
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001284 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1285 (const void *) fragShaderText,
1286 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001287#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001288}
1289
1290static void demo_prepare_pipeline(struct demo *demo)
1291{
1292 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001293 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1294 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001295 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1296 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001297 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1298 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001299 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1300 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001301 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001302
1303 memset(&pipeline, 0, sizeof(pipeline));
1304 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu87544e72015-02-23 10:41:08 -07001305 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307 memset(&ia, 0, sizeof(ia));
1308 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1309 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1310
1311 memset(&rs, 0, sizeof(rs));
1312 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001313 rs.fillMode = XGL_FILL_SOLID;
Mike Stroyan16b3d982015-03-19 14:29:04 -06001314 rs.cullMode = XGL_CULL_BACK;
Tony Barbour29645d02015-01-16 14:27:35 -07001315 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001316
1317 memset(&cb, 0, sizeof(cb));
1318 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001319 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1320 memset(att_state, 0, sizeof(att_state));
1321 att_state[0].format = demo->format;
1322 att_state[0].channelWriteMask = 0xf;
1323 att_state[0].blendEnable = XGL_FALSE;
1324 cb.attachmentCount = 1;
1325 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001326
Tony Barbour29645d02015-01-16 14:27:35 -07001327 memset(&vp, 0, sizeof(vp));
1328 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001329 vp.numViewports = 1;
Mike Stroyan259bc542015-03-24 15:13:34 -06001330 vp.clipOrigin = XGL_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001331
1332 memset(&ds, 0, sizeof(ds));
1333 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1334 ds.format = demo->depth.format;
1335 ds.depthTestEnable = XGL_TRUE;
1336 ds.depthWriteEnable = XGL_TRUE;
1337 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1338 ds.depthBoundsEnable = XGL_FALSE;
1339 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1340 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1341 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1342 ds.stencilTestEnable = XGL_FALSE;
1343 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001344
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001345 memset(&vs, 0, sizeof(vs));
1346 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1347 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001348 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001349 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001350
1351 memset(&fs, 0, sizeof(fs));
1352 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1353 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1354 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001355 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001356
1357 memset(&ms, 0, sizeof(ms));
1358 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1359 ms.sampleMask = 1;
1360 ms.multisampleEnable = XGL_FALSE;
1361 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001363 pipeline.pNext = (const void *) &ia;
1364 ia.pNext = (const void *) &rs;
1365 rs.pNext = (const void *) &cb;
1366 cb.pNext = (const void *) &ms;
1367 ms.pNext = (const void *) &vp;
1368 vp.pNext = (const void *) &ds;
1369 ds.pNext = (const void *) &vs;
1370 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371
1372 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1373 assert(!err);
1374
1375 xglDestroyObject(vs.shader.shader);
1376 xglDestroyObject(fs.shader.shader);
1377}
1378
1379static void demo_prepare_dynamic_states(struct demo *demo)
1380{
Tony Barbour29645d02015-01-16 14:27:35 -07001381 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1382 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1383 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1384 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001385 XGL_RESULT err;
1386
Tony Barbour29645d02015-01-16 14:27:35 -07001387 memset(&viewport_create, 0, sizeof(viewport_create));
1388 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001389 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001390 XGL_VIEWPORT viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001391 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001392 viewport.height = (float) demo->height;
1393 viewport.width = (float) demo->width;
1394 viewport.minDepth = (float) 0.0f;
1395 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001396 viewport_create.pViewports = &viewport;
1397 XGL_RECT scissor;
1398 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001399 scissor.extent.width = demo->width;
1400 scissor.extent.height = demo->height;
1401 scissor.offset.x = 0;
1402 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001403 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404
1405 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001406 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001407 raster.pointSize = 1.0;
1408 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001409
1410 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001411 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001412 color_blend.blendConst[0] = 1.0f;
1413 color_blend.blendConst[1] = 1.0f;
1414 color_blend.blendConst[2] = 1.0f;
1415 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416
1417 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001418 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001419 depth_stencil.minDepth = 0.0f;
1420 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001421 depth_stencil.stencilBackRef = 0;
1422 depth_stencil.stencilFrontRef = 0;
1423 depth_stencil.stencilReadMask = 0xff;
1424 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425
Tony Barbour29645d02015-01-16 14:27:35 -07001426 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427 assert(!err);
1428
Tony Barbour29645d02015-01-16 14:27:35 -07001429 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001430 assert(!err);
1431
Tony Barbour29645d02015-01-16 14:27:35 -07001432 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001433 &color_blend, &demo->color_blend);
1434 assert(!err);
1435
Tony Barbour29645d02015-01-16 14:27:35 -07001436 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001437 &depth_stencil, &demo->depth_stencil);
1438 assert(!err);
1439}
1440
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001441static void demo_prepare_descriptor_region(struct demo *demo)
1442{
1443 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1444 [0] = {
1445 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1446 .count = 1,
1447 },
1448 [1] = {
1449 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1450 .count = DEMO_TEXTURE_COUNT,
1451 },
1452 };
1453 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1454 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1455 .pNext = NULL,
1456 .count = 2,
1457 .pTypeCount = type_counts,
1458 };
1459 XGL_RESULT err;
1460
1461 err = xglCreateDescriptorRegion(demo->device,
1462 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1463 &descriptor_region, &demo->desc_region);
1464 assert(!err);
1465}
1466
1467static void demo_prepare_descriptor_set(struct demo *demo)
1468{
1469 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1470 &demo->uniform_data.attach;
1471 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1472 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1473 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1474 XGL_UPDATE_BUFFERS update_vs;
1475 XGL_RESULT err;
1476 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001477 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001478
1479 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1480 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1481 view_info[i].pNext = NULL;
1482 view_info[i].view = demo->textures[i].view,
1483 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1484
1485 combined_info[i].pSampler = demo->textures[i].sampler;
1486 combined_info[i].pImageView = &view_info[i];
1487 }
1488
1489 memset(&update_vs, 0, sizeof(update_vs));
1490 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1491 update_vs.pNext = &update_fs;
1492 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1493 update_vs.count = 1;
1494 update_vs.pBufferViews = &view_info_vs;
1495
1496 memset(&update_fs, 0, sizeof(update_fs));
1497 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1498 update_fs.index = 1;
1499 update_fs.count = DEMO_TEXTURE_COUNT;
1500 update_fs.pSamplerImageViews = combined_info;
1501
1502 err = xglAllocDescriptorSets(demo->desc_region,
1503 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001504 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001505 &demo->desc_set, &count);
1506 assert(!err && count == 1);
1507
1508 xglBeginDescriptorRegionUpdate(demo->device,
1509 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1510
1511 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1512 xglUpdateDescriptors(demo->desc_set, &update_vs);
1513
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001514 xglEndDescriptorRegionUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001515}
1516
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001517static void demo_prepare(struct demo *demo)
1518{
1519 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1520 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1521 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001522 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001523 .flags = 0,
1524 };
1525 XGL_RESULT err;
1526
1527 demo_prepare_buffers(demo);
1528 demo_prepare_depth(demo);
1529 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001530 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001531
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001532 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001533 demo_prepare_pipeline(demo);
1534 demo_prepare_dynamic_states(demo);
1535
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001536 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1537 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1538 assert(!err);
1539 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001540
1541 demo_prepare_descriptor_region(demo);
1542 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001543
1544
1545 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1546 demo->current_buffer = i;
1547 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1548 }
1549
1550 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001551}
1552
1553static void demo_handle_event(struct demo *demo,
1554 const xcb_generic_event_t *event)
1555{
Piers Daniell735ee532015-02-23 16:23:13 -07001556 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001557 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001558 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001559 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001560 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001561 case XCB_CLIENT_MESSAGE:
1562 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1563 (*demo->atom_wm_delete_window).atom) {
1564 demo->quit = true;
1565 }
1566 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567 case XCB_KEY_RELEASE:
1568 {
1569 const xcb_key_release_event_t *key =
1570 (const xcb_key_release_event_t *) event;
1571
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001572 switch (key->detail) {
1573 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001574 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001575 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001576 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001577 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001578 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001579 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001580 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001581 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001582 case 0x41:
1583 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001584 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001585 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001586 }
1587 break;
1588 default:
1589 break;
1590 }
1591}
1592
1593static void demo_run(struct demo *demo)
1594{
1595 xcb_flush(demo->connection);
1596
1597 while (!demo->quit) {
1598 xcb_generic_event_t *event;
1599
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001600 if (demo->pause) {
1601 event = xcb_wait_for_event(demo->connection);
1602 } else {
1603 event = xcb_poll_for_event(demo->connection);
1604 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001605 if (event) {
1606 demo_handle_event(demo, event);
1607 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001608 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001609
1610 // Wait for work to finish before updating MVP.
1611 xglDeviceWaitIdle(demo->device);
1612 demo_update_data_buffer(demo);
1613
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001614 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001615
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001616 // Wait for work to finish before updating MVP.
1617 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001618 }
1619}
1620
1621static void demo_create_window(struct demo *demo)
1622{
1623 uint32_t value_mask, value_list[32];
1624
1625 demo->window = xcb_generate_id(demo->connection);
1626
1627 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1628 value_list[0] = demo->screen->black_pixel;
1629 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1630 XCB_EVENT_MASK_EXPOSURE;
1631
1632 xcb_create_window(demo->connection,
1633 XCB_COPY_FROM_PARENT,
1634 demo->window, demo->screen->root,
1635 0, 0, demo->width, demo->height, 0,
1636 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1637 demo->screen->root_visual,
1638 value_mask, value_list);
1639
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001640 /* Magic code that will send notification when window is destroyed */
1641 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1642 "WM_PROTOCOLS");
1643 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1644
1645 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1646 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1647
1648 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1649 demo->window, (*reply).atom, 4, 32, 1,
1650 &(*demo->atom_wm_delete_window).atom);
1651 free(reply);
1652
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001653 xcb_map_window(demo->connection, demo->window);
1654}
1655
1656static void demo_init_xgl(struct demo *demo)
1657{
1658 const XGL_APPLICATION_INFO app = {
1659 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1660 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001661 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001662 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001663 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001664 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001665 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001666 };
1667 const XGL_WSI_X11_CONNECTION_INFO connection = {
1668 .pConnection = demo->connection,
1669 .root = demo->screen->root,
1670 .provider = 0,
1671 };
1672 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1673 .queueNodeIndex = 0,
1674 .queueCount = 1,
1675 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001676 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001677 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001678 };
1679 const XGL_DEVICE_CREATE_INFO device = {
1680 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1681 .pNext = NULL,
1682 .queueRecordCount = 1,
1683 .pRequestedQueues = &queue,
1684 .extensionCount = 1,
1685 .ppEnabledExtensionNames = ext_names,
1686 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1687 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1688 };
1689 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001690 uint32_t gpu_count;
1691 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001692 size_t data_size;
1693 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694
Jon Ashburn93cfc432015-01-29 15:47:01 -07001695 err = xglCreateInstance(&app, NULL, &demo->inst);
Ian Elliott3979e282015-04-03 15:24:55 -06001696 if (err == XGL_ERROR_INCOMPATIBLE_DRIVER) {
1697 printf("Cannot find a compatible Vulkan installable client driver "
1698 "(ICD).\nExiting ...\n");
1699 fflush(stdout);
1700 exit(1);
1701 } else {
1702 assert(!err);
1703 }
Jon Ashburn93cfc432015-01-29 15:47:01 -07001704 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001705 assert(!err && gpu_count == 1);
1706
1707 for (i = 0; i < device.extensionCount; i++) {
1708 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1709 assert(!err);
1710 }
1711
1712 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1713 assert(!err);
1714
1715 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1716 assert(!err);
1717
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001718 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1719 &data_size, NULL);
1720 assert(!err);
1721
1722 demo->queue_props = (XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *) malloc(data_size);
1723 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1724 &data_size, demo->queue_props);
1725 assert(!err);
1726 queue_count = data_size / sizeof(XGL_PHYSICAL_GPU_QUEUE_PROPERTIES);
1727 assert(queue_count >= 1);
1728
1729 for (i = 0; i < queue_count; i++) {
1730 if (demo->queue_props[i].queueFlags & XGL_QUEUE_GRAPHICS_BIT)
1731 break;
1732 }
1733 assert(i < queue_count);
1734 demo->graphics_queue_node_index = i;
1735
1736 err = xglGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001737 0, &demo->queue);
1738 assert(!err);
1739}
1740
1741static void demo_init_connection(struct demo *demo)
1742{
1743 const xcb_setup_t *setup;
1744 xcb_screen_iterator_t iter;
1745 int scr;
1746
1747 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001748 if (demo->connection == NULL) {
1749 printf("Cannot find a compatible Vulkan installable client driver "
1750 "(ICD).\nExiting ...\n");
1751 fflush(stdout);
1752 exit(1);
1753 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001754
1755 setup = xcb_get_setup(demo->connection);
1756 iter = xcb_setup_roots_iterator(setup);
1757 while (scr-- > 0)
1758 xcb_screen_next(&iter);
1759
1760 demo->screen = iter.data;
1761}
1762
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001763static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001764{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001765 vec3 eye = {0.0f, 3.0f, 5.0f};
1766 vec3 origin = {0, 0, 0};
1767 vec3 up = {0.0f, -1.0f, 0.0};
1768
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001769 memset(demo, 0, sizeof(*demo));
1770
Piers Daniell735ee532015-02-23 16:23:13 -07001771 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001772 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1773 demo->use_staging_buffer = true;
1774 }
1775
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001776 demo_init_connection(demo);
1777 demo_init_xgl(demo);
1778
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001779 demo->width = 500;
1780 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001781 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001782
1783 demo->spin_angle = 0.01f;
1784 demo->spin_increment = 0.01f;
1785 demo->pause = false;
1786
Piers Daniell735ee532015-02-23 16:23:13 -07001787 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001788 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1789 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001790}
1791
1792static void demo_cleanup(struct demo *demo)
1793{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001794 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001795
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001796 xglDestroyObject(demo->desc_set);
1797 xglDestroyObject(demo->desc_region);
1798
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001799 xglDestroyObject(demo->viewport);
1800 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001801 xglDestroyObject(demo->color_blend);
1802 xglDestroyObject(demo->depth_stencil);
1803
1804 xglDestroyObject(demo->pipeline);
Chia-I Wu87544e72015-02-23 10:41:08 -07001805 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001806
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001807 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1808 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001809 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001810 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001811 for (j = 0; j < demo->textures[i].num_mem; j++)
1812 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001813 xglDestroyObject(demo->textures[i].sampler);
1814 }
1815
1816 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001817 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001818 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001819 for (j = 0; j < demo->depth.num_mem; j++)
1820 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001821
1822 xglDestroyObject(demo->uniform_data.view);
1823 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001824 xglDestroyObject(demo->uniform_data.buf);
1825 for (j = 0; j < demo->uniform_data.num_mem; j++)
1826 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001827
1828 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001829 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830 xglDestroyObject(demo->buffers[i].view);
1831 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001832 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001833 }
1834
1835 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001836 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001837
1838 xcb_destroy_window(demo->connection, demo->window);
1839 xcb_disconnect(demo->connection);
1840}
1841
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001842int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001843{
1844 struct demo demo;
1845
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001846 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001847
1848 demo_prepare(&demo);
1849 demo_create_window(&demo);
1850 demo_run(&demo);
1851
1852 demo_cleanup(&demo);
1853
1854 return 0;
1855}