blob: 54237e8453d8f57b7a79be62d821dea974bef929 [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
13#include "icd-bil.h"
14
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -060016#include <unistd.h>
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060017#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060018
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060019#define DEMO_BUFFER_COUNT 2
20#define DEMO_TEXTURE_COUNT 1
21
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060022/*
23 * When not defined, code will use built-in GLSL compiler
24 * which may not be supported on all drivers
25 */
26#define EXTERNAL_BIL
27
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060029 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060030};
31
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060032struct xglcube_vs_uniform {
33 // Must start with MVP
34 XGL_FLOAT mvp[4][4];
35 XGL_FLOAT position[12*3][4];
36 XGL_FLOAT color[12*3][4];
37};
38
39struct xgltexcube_vs_uniform {
40 // Must start with MVP
41 XGL_FLOAT mvp[4][4];
42 XGL_FLOAT position[12*3][4];
43 XGL_FLOAT attr[12*3][4];
44};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060046//--------------------------------------------------------------------------------------
47// Mesh and VertexFormat Data
48//--------------------------------------------------------------------------------------
49struct Vertex
50{
51 XGL_FLOAT posX, posY, posZ, posW; // Position data
52 XGL_FLOAT r, g, b, a; // Color
53};
54
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060055struct VertexPosTex
56{
57 XGL_FLOAT posX, posY, posZ, posW; // Position data
58 XGL_FLOAT u, v, s, t; // Texcoord
59};
60
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060061#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060062#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060063
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060064static const XGL_FLOAT g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060065 -1.0f,-1.0f,-1.0f, // Vertex 0
66 -1.0f,-1.0f, 1.0f,
67 -1.0f, 1.0f, 1.0f,
68
69 -1.0f, 1.0f, 1.0f, // Vertex 1
70 -1.0f, 1.0f,-1.0f,
71 -1.0f,-1.0f,-1.0f,
72
73 -1.0f,-1.0f,-1.0f, // Vertex 2
74 1.0f, 1.0f,-1.0f,
75 1.0f,-1.0f,-1.0f,
76
77 -1.0f,-1.0f,-1.0f, // Vertex 3
78 1.0f, 1.0f,-1.0f,
79 -1.0f, 1.0f,-1.0f,
80
81 -1.0f,-1.0f,-1.0f, // Vertex 4
82 1.0f,-1.0f, 1.0f,
83 1.0f,-1.0f,-1.0f,
84
85 -1.0f,-1.0f,-1.0f, // Vertex 5
86 -1.0f,-1.0f, 1.0f,
87 1.0f,-1.0f, 1.0f,
88
89 -1.0f, 1.0f,-1.0f, // Vertex 6
90 -1.0f, 1.0f, 1.0f,
91 1.0f, 1.0f, 1.0f,
92
93 -1.0f, 1.0f,-1.0f, // Vertex 7
94 1.0f, 1.0f,-1.0f,
95 1.0f, 1.0f, 1.0f,
96
97 1.0f, 1.0f,-1.0f, // Vertex 8
98 1.0f, 1.0f, 1.0f,
99 1.0f,-1.0f, 1.0f,
100
101 1.0f,-1.0f, 1.0f, // Vertex 9
102 1.0f,-1.0f,-1.0f,
103 1.0f, 1.0f,-1.0f,
104
105 -1.0f, 1.0f, 1.0f, // Vertex 10
106 1.0f, 1.0f, 1.0f,
107 -1.0f,-1.0f, 1.0f,
108
109 -1.0f,-1.0f, 1.0f, // Vertex 11
110 1.0f,-1.0f, 1.0f,
111 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600112};
113
114static const XGL_FLOAT g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600115 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600116 0.0f, 0.0f,
117 0.0f, 1.0f,
118
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600119 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600120 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600121 1.0f, 0.0f,
122
123// 0.0f, 1.0f, // Vertex 2
124// 1.0f, 0.0f,
125// 0.0f, 0.0f,
126
127// 0.0f, 1.0f, // Vertex 3
128// 1.0f, 0.0f,
129// 1.0f, 1.0f,
130
131 0.0f, 0.0f, // Vertex 2
132 1.0f, 1.0f,
133 1.0f, 0.0f,
134
135 0.0f, 0.0f, // Vertex 3
136 1.0f, 1.0f,
137 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600138
139 0.0f, 1.0f, // Vertex 4
140 1.0f, 0.0f,
141 0.0f, 0.0f,
142
143 0.0f, 1.0f, // Vertex 5
144 1.0f, 1.0f,
145 1.0f, 0.0f,
146
147 0.0f, 1.0f, // Vertex 6
148 1.0f, 1.0f,
149 1.0f, 0.0f,
150
151 0.0f, 1.0f, // Vertex 7
152 0.0f, 0.0f,
153 1.0f, 0.0f,
154
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600155 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600158
159 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 0.0f, 1.0f,
162
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600163 1.0f, 1.0f, // Vertex 10
164 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600166
167 1.0f, 0.0f, // Vertex 11
168 0.0f, 0.0f,
169 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600170};
171
172void dumpMatrix(const char *note, mat4x4 MVP)
173{
174 int i;
175
176 printf("%s: \n", note);
177 for (i=0; i<4; i++) {
178 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
179 }
180 printf("\n");
181 fflush(stdout);
182}
183
184void dumpVec4(const char *note, vec4 vector)
185{
186 printf("%s: \n", note);
187 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
188 printf("\n");
189 fflush(stdout);
190}
191
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600192struct demo {
193 xcb_connection_t *connection;
194 xcb_screen_t *screen;
195
196 XGL_PHYSICAL_GPU gpu;
197 XGL_DEVICE device;
198 XGL_QUEUE queue;
199
200 int width, height;
201 XGL_FORMAT format;
202
203 struct {
204 XGL_IMAGE image;
205 XGL_GPU_MEMORY mem;
206
207 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800208 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600209 } buffers[DEMO_BUFFER_COUNT];
210
211 struct {
212 XGL_FORMAT format;
213
214 XGL_IMAGE image;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700215 XGL_UINT num_mem;
216 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600217 XGL_DEPTH_STENCIL_VIEW view;
218 } depth;
219
220 struct {
221 XGL_SAMPLER sampler;
222
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223 char *filename;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600224 XGL_IMAGE image;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700225 XGL_UINT num_mem;
226 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600227 XGL_IMAGE_VIEW view;
228 } textures[DEMO_TEXTURE_COUNT];
229
230 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800231 XGL_BUFFER buf;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700232 XGL_UINT num_mem;
233 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800234 XGL_BUFFER_VIEW view;
235 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600236 } uniform_data;
237
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800238 XGL_DESCRIPTOR_SET_LAYOUT desc_layout_vs;
239 XGL_DESCRIPTOR_SET_LAYOUT desc_layout_fs;
240 XGL_DESCRIPTOR_SET_LAYOUT *desc_layout_last;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600241 XGL_PIPELINE pipeline;
242
Tony Barbour29645d02015-01-16 14:27:35 -0700243 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
244 XGL_DYNAMIC_RS_STATE_OBJECT raster;
245 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
246 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600247
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600248 mat4x4 projection_matrix;
249 mat4x4 view_matrix;
250 mat4x4 model_matrix;
251
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600252 XGL_FLOAT spin_angle;
253 XGL_FLOAT spin_increment;
254 bool pause;
255
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600256 XGL_CMD_BUFFER cmd;
257
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800258 XGL_DESCRIPTOR_REGION desc_region;
259 XGL_DESCRIPTOR_SET desc_set;
260
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600261 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700262 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600263
264 bool quit;
265 XGL_UINT current_buffer;
266};
267
268static void demo_draw_build_cmd(struct demo *demo)
269{
270 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
271 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000272 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600273 };
274 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
275 .view = demo->depth.view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000276 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600277 };
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600278 const XGL_FLOAT clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600279 const XGL_FLOAT clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600280 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700281 XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = {
282 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO,
283 .pNext = NULL,
284 .operation = XGL_RENDER_PASS_OPERATION_BEGIN_AND_END,
285 };
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700286 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
287 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700288 .pNext = &graphics_cmd_buf_info,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700289 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
290 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
291 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600292 XGL_RESULT err;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700293 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
294 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
295 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
296 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
297 .pNext = NULL,
298 .colorAttachmentCount = 1,
299 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
300 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
301 .sampleCount = 1,
302 };
303 XGL_RENDER_PASS_CREATE_INFO rp_info;
304
305 memset(&rp_info, 0 , sizeof(rp_info));
306 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
307 assert(!err);
308 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
309 rp_info.renderArea.extent.width = demo->width;
310 rp_info.renderArea.extent.height = demo->height;
311 rp_info.pColorLoadOps = &load_op;
312 rp_info.pColorStoreOps = &store_op;
313 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
314 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
315 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
316 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
317 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
318 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700320 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321 assert(!err);
322
323 xglCmdBindPipeline(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
324 demo->pipeline);
325 xglCmdBindDescriptorSet(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800326 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327
Tony Barbour29645d02015-01-16 14:27:35 -0700328 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_VIEWPORT, demo->viewport);
329 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_RASTER, demo->raster);
330 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331 demo->color_blend);
Tony Barbour29645d02015-01-16 14:27:35 -0700332 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333 demo->depth_stencil);
334
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600335 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
336 clear_range.baseMipLevel = 0;
337 clear_range.mipLevels = 1;
338 clear_range.baseArraySlice = 0;
339 clear_range.arraySize = 1;
340 xglCmdClearColorImage(demo->cmd,
341 demo->buffers[demo->current_buffer].image,
342 clear_color, 1, &clear_range);
343
344 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
345 xglCmdClearDepthStencil(demo->cmd, demo->depth.image,
346 clear_depth, 0, 1, &clear_range);
347
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600348 xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349
350 err = xglEndCommandBuffer(demo->cmd);
351 assert(!err);
352}
353
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600354
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600355void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600356{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600357 mat4x4 MVP, Model, VP;
358 int matrixSize = sizeof(MVP);
359 XGL_UINT8 *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360 XGL_RESULT err;
361
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600362 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600364 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600365 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600366 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600367 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600368
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700369 assert(demo->uniform_data.num_mem == 1);
370 err = xglMapMemory(demo->uniform_data.mem[0], 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371 assert(!err);
372
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600373 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700375 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376 assert(!err);
377}
378
379static void demo_draw(struct demo *demo)
380{
381 const XGL_WSI_X11_PRESENT_INFO present = {
382 .destWindow = demo->window,
383 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800384 .async = true,
385 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600386 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800387 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388 XGL_RESULT err;
389
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390 demo_draw_build_cmd(demo);
391
Chia-I Wubb57f642014-11-07 14:30:34 +0800392 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((XGL_UINT64) 0));
393 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
394
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395 err = xglQueueSubmit(demo->queue, 1, &demo->cmd,
396 0, NULL, XGL_NULL_HANDLE);
397 assert(!err);
398
Chia-I Wubb57f642014-11-07 14:30:34 +0800399 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400 assert(!err);
401
402 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
403}
404
405static void demo_prepare_buffers(struct demo *demo)
406{
407 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
408 .format = demo->format,
409 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
410 .extent = {
411 .width = demo->width,
412 .height = demo->height,
413 },
414 .flags = 0,
415 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800416 const XGL_FENCE_CREATE_INFO fence = {
417 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
418 .pNext = NULL,
419 .flags = 0,
420 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600421 XGL_RESULT err;
422 XGL_UINT i;
423
424 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
425 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
426 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
427 .pNext = NULL,
428 .format = demo->format,
429 .mipLevel = 0,
430 .baseArraySlice = 0,
431 .arraySize = 1,
432 };
433
434 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
435 &demo->buffers[i].image, &demo->buffers[i].mem);
436 assert(!err);
437
438 color_attachment_view.image = demo->buffers[i].image;
439
440 err = xglCreateColorAttachmentView(demo->device,
441 &color_attachment_view, &demo->buffers[i].view);
442 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800443
444 err = xglCreateFence(demo->device,
445 &fence, &demo->buffers[i].fence);
446 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600447 }
448}
449
450static void demo_prepare_depth(struct demo *demo)
451{
452 const XGL_FORMAT depth_format = { XGL_CH_FMT_R16, XGL_NUM_FMT_DS };
453 const XGL_IMAGE_CREATE_INFO image = {
454 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
455 .pNext = NULL,
456 .imageType = XGL_IMAGE_2D,
457 .format = depth_format,
458 .extent = { demo->width, demo->height, 1 },
459 .mipLevels = 1,
460 .arraySize = 1,
461 .samples = 1,
462 .tiling = XGL_OPTIMAL_TILING,
463 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
464 .flags = 0,
465 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700466 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
467 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
468 .pNext = NULL,
469 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600470 XGL_MEMORY_ALLOC_INFO mem_alloc = {
471 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700472 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600473 .allocationSize = 0,
474 .alignment = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700475 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600476 .flags = 0,
477 .heapCount = 0,
478 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
479 };
480 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
481 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
482 .pNext = NULL,
483 .image = XGL_NULL_HANDLE,
484 .mipLevel = 0,
485 .baseArraySlice = 0,
486 .arraySize = 1,
487 .flags = 0,
488 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700489 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Jon Ashburnbd75a812014-11-21 13:29:30 -0700490 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700491 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
492 XGL_SIZE img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600493 XGL_RESULT err;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700494 XGL_UINT num_allocations = 0;
495 XGL_SIZE num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600496
497 demo->depth.format = depth_format;
498
499 /* create image */
500 err = xglCreateImage(demo->device, &image,
501 &demo->depth.image);
502 assert(!err);
503
Jon Ashburnb2a66652015-01-16 09:37:43 -0700504
505 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
506 assert(!err && num_alloc_size == sizeof(num_allocations));
507 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
508 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
509 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600510 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700511 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
512 &mem_reqs_size, mem_reqs);
513 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700514 err = xglGetObjectInfo(demo->depth.image,
515 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
516 &img_reqs_size, &img_reqs);
517 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
518 img_alloc.usage = img_reqs.usage;
519 img_alloc.formatClass = img_reqs.formatClass;
520 img_alloc.samples = img_reqs.samples;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700521 for (XGL_UINT i = 0; i < num_allocations; i ++) {
522 mem_alloc.allocationSize = mem_reqs[i].size;
523 mem_alloc.alignment = mem_reqs[i].alignment;
524 mem_alloc.heapCount = mem_reqs[i].heapCount;
525 XGL_UINT heapInfo[mem_reqs[i].heapCount];
526 mem_alloc.pHeaps = (const XGL_UINT *) heapInfo;
527 memcpy(heapInfo, mem_reqs[i].pHeaps,
528 sizeof(mem_reqs[i].pHeaps[0]) * mem_reqs[i].heapCount);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600529
Jon Ashburnb2a66652015-01-16 09:37:43 -0700530 /* allocate memory */
531 err = xglAllocMemory(demo->device, &mem_alloc,
532 &(demo->depth.mem[i]));
533 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600534
Jon Ashburnb2a66652015-01-16 09:37:43 -0700535 /* bind memory */
536 err = xglBindObjectMemory(demo->depth.image, i,
537 demo->depth.mem[i], 0);
538 assert(!err);
539 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600540
541 /* create image view */
542 view.image = demo->depth.image;
543 err = xglCreateDepthStencilView(demo->device, &view,
544 &demo->depth.view);
545 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600546}
547
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600548/** loadTexture
549 * loads a png file into an memory object, using cstdio , libpng.
550 *
551 * \param demo : Needed to access XGL calls
552 * \param filename : the png file to be loaded
553 * \param width : width of png, to be updated as a side effect of this function
554 * \param height : height of png, to be updated as a side effect of this function
555 *
556 * \return bool : an opengl texture id. true if successful?,
557 * should be validated by the client of this function.
558 *
559 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
560 * Modified to copy image to memory
561 *
562 */
563bool loadTexture(char *filename, XGL_UINT8 *rgba_data,
564 XGL_SUBRESOURCE_LAYOUT *layout,
565 XGL_INT *width, XGL_INT *height)
566{
567 //header for testing if it is a png
568 png_byte header[8];
569 int i, is_png, bit_depth, color_type,rowbytes;
570 png_uint_32 twidth, theight;
571 png_structp png_ptr;
572 png_infop info_ptr, end_info;
573 png_byte *image_data;
574 png_bytep *row_pointers;
575
576 //open file as binary
577 FILE *fp = fopen(filename, "rb");
578 if (!fp) {
579 return false;
580 }
581
582 //read the header
583 fread(header, 1, 8, fp);
584
585 //test if png
586 is_png = !png_sig_cmp(header, 0, 8);
587 if (!is_png) {
588 fclose(fp);
589 return false;
590 }
591
592 //create png struct
593 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
594 NULL, NULL);
595 if (!png_ptr) {
596 fclose(fp);
597 return (false);
598 }
599
600 //create png info struct
601 info_ptr = png_create_info_struct(png_ptr);
602 if (!info_ptr) {
603 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
604 fclose(fp);
605 return (false);
606 }
607
608 //create png info struct
609 end_info = png_create_info_struct(png_ptr);
610 if (!end_info) {
611 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
612 fclose(fp);
613 return (false);
614 }
615
616 //png error stuff, not sure libpng man suggests this.
617 if (setjmp(png_jmpbuf(png_ptr))) {
618 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
619 fclose(fp);
620 return (false);
621 }
622
623 //init png reading
624 png_init_io(png_ptr, fp);
625
626 //let libpng know you already read the first 8 bytes
627 png_set_sig_bytes(png_ptr, 8);
628
629 // read all the info up to the image data
630 png_read_info(png_ptr, info_ptr);
631
632 // get info about png
633 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
634 NULL, NULL, NULL);
635
636 //update width and height based on png info
637 *width = twidth;
638 *height = theight;
639
640 // Require that incoming texture be 8bits per color component
641 // and 4 components (RGBA).
642 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
643 png_get_channels(png_ptr, info_ptr) != 4) {
644 return false;
645 }
646
647 if (rgba_data == NULL) {
648 // If data pointer is null, we just want the width & height
649 // clean up memory and close stuff
650 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
651 fclose(fp);
652
653 return true;
654 }
655
656 // Update the png info struct.
657 png_read_update_info(png_ptr, info_ptr);
658
659 // Row size in bytes.
660 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
661
662 // Allocate the image_data as a big block, to be given to opengl
663 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
664 if (!image_data) {
665 //clean up memory and close stuff
666 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
667 fclose(fp);
668 return false;
669 }
670
671 // row_pointers is for pointing to image_data for reading the png with libpng
672 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
673 if (!row_pointers) {
674 //clean up memory and close stuff
675 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
676 // delete[] image_data;
677 fclose(fp);
678 return false;
679 }
680 // set the individual row_pointers to point at the correct offsets of image_data
681 for (i = 0; i < theight; ++i)
682 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
683
684 // read the png into image_data through row_pointers
685 png_read_image(png_ptr, row_pointers);
686
687 // clean up memory and close stuff
688 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
689 free(row_pointers);
690 free(image_data);
691 fclose(fp);
692
693 return true;
694}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600695
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600696static void demo_prepare_textures(struct demo *demo)
697{
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600698 const XGL_FORMAT tex_format = { XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600699 XGL_INT tex_width;
700 XGL_INT tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600701 XGL_RESULT err;
702 XGL_UINT i;
703
704 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
705 const XGL_SAMPLER_CREATE_INFO sampler = {
706 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
707 .pNext = NULL,
708 .magFilter = XGL_TEX_FILTER_NEAREST,
709 .minFilter = XGL_TEX_FILTER_NEAREST,
710 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600711 .addressU = XGL_TEX_ADDRESS_CLAMP,
712 .addressV = XGL_TEX_ADDRESS_CLAMP,
713 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600714 .mipLodBias = 0.0f,
715 .maxAnisotropy = 0,
716 .compareFunc = XGL_COMPARE_NEVER,
717 .minLod = 0.0f,
718 .maxLod = 0.0f,
719 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
720 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600721
722 assert(loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height));
723
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600724 const XGL_IMAGE_CREATE_INFO image = {
725 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
726 .pNext = NULL,
727 .imageType = XGL_IMAGE_2D,
728 .format = tex_format,
729 .extent = { tex_width, tex_height, 1 },
730 .mipLevels = 1,
731 .arraySize = 1,
732 .samples = 1,
733 .tiling = XGL_LINEAR_TILING,
734 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
735 .flags = 0,
736 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700737 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
738 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
739 .pNext = NULL,
740 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600741 XGL_MEMORY_ALLOC_INFO mem_alloc = {
742 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700743 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600744 .allocationSize = 0,
745 .alignment = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700746 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600747 .flags = 0,
748 .heapCount = 0,
Tony Barbour29645d02015-01-16 14:27:35 -0700749 .pHeaps = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600750 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
751 };
752 XGL_IMAGE_VIEW_CREATE_INFO view = {
753 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
754 .pNext = NULL,
755 .image = XGL_NULL_HANDLE,
756 .viewType = XGL_IMAGE_VIEW_2D,
757 .format = image.format,
758 .channels = { XGL_CHANNEL_SWIZZLE_R,
759 XGL_CHANNEL_SWIZZLE_G,
760 XGL_CHANNEL_SWIZZLE_B,
761 XGL_CHANNEL_SWIZZLE_A, },
762 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
763 .minLod = 0.0f,
764 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700765
766 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Jon Ashburnbd75a812014-11-21 13:29:30 -0700767 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700768 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
769 XGL_SIZE img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700770 XGL_UINT num_allocations = 0;
771 XGL_SIZE num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600772
773 /* create sampler */
774 err = xglCreateSampler(demo->device, &sampler,
775 &demo->textures[i].sampler);
776 assert(!err);
777
778 /* create image */
779 err = xglCreateImage(demo->device, &image,
780 &demo->textures[i].image);
781 assert(!err);
782
783 err = xglGetObjectInfo(demo->textures[i].image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700784 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
785 &num_alloc_size, &num_allocations);
786 assert(!err && num_alloc_size == sizeof(num_allocations));
787 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
788 demo->textures[i].mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
789 demo->textures[i].num_mem = num_allocations;
790 err = xglGetObjectInfo(demo->textures[i].image,
791 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
792 &mem_reqs_size, mem_reqs);
793 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700794 err = xglGetObjectInfo(demo->textures[i].image,
795 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
796 &img_reqs_size, &img_reqs);
797 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
798 img_alloc.usage = img_reqs.usage;
799 img_alloc.formatClass = img_reqs.formatClass;
800 img_alloc.samples = img_reqs.samples;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700801 for (XGL_UINT j = 0; j < num_allocations; j ++) {
802 mem_alloc.allocationSize = mem_reqs[j].size;
803 mem_alloc.alignment = mem_reqs[j].alignment;
804 mem_alloc.heapCount = mem_reqs[j].heapCount;
805 XGL_UINT heapInfo[mem_reqs[j].heapCount];
806 mem_alloc.pHeaps = (const XGL_UINT *)heapInfo;
807 memcpy(heapInfo, mem_reqs[j].pHeaps,
808 sizeof(mem_reqs[j].pHeaps[0]) * mem_reqs[j].heapCount);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600809
Jon Ashburnb2a66652015-01-16 09:37:43 -0700810 /* allocate memory */
811 err = xglAllocMemory(demo->device, &mem_alloc,
812 &(demo->textures[i].mem[j]));
813 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600814
Jon Ashburnb2a66652015-01-16 09:37:43 -0700815 /* bind memory */
816 err = xglBindObjectMemory(demo->textures[i].image, j,
817 demo->textures[i].mem[j], 0);
818 assert(!err);
819 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600820
821 /* create image view */
822 view.image = demo->textures[i].image;
823 err = xglCreateImageView(demo->device, &view,
824 &demo->textures[i].view);
825 assert(!err);
826 }
827
828 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
829 const XGL_IMAGE_SUBRESOURCE subres = {
830 .aspect = XGL_IMAGE_ASPECT_COLOR,
831 .mipLevel = 0,
832 .arraySlice = 0,
833 };
834 XGL_SUBRESOURCE_LAYOUT layout;
Chia-I Wu170de772015-01-05 16:27:42 +0800835 XGL_SIZE layout_size = sizeof(layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600836 XGL_VOID *data;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600837
838 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
839 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
840 assert(!err && layout_size == sizeof(layout));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700841 assert(demo->textures[i].num_mem == 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600842
Jon Ashburnb2a66652015-01-16 09:37:43 -0700843 err = xglMapMemory(demo->textures[i].mem[0], 0, &data);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600844 assert(!err);
845
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600846 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847
Jon Ashburnb2a66652015-01-16 09:37:43 -0700848 err = xglUnmapMemory(demo->textures[i].mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600849 assert(!err);
850 }
851}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600852
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600853void demo_prepare_cube_data_buffer(struct demo *demo)
854{
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800855 XGL_BUFFER_CREATE_INFO buf_info;
856 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700857 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
858 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
859 .pNext = NULL,
860 };
861 XGL_MEMORY_ALLOC_INFO alloc_info = {
862 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
863 .pNext = &buf_alloc,
864 .allocationSize = 0,
865 .alignment = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700866 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700867 .flags = 0,
868 .heapCount = 0,
869 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
870 };
871 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800872 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700873 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
874 XGL_SIZE buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
875 XGL_UINT num_allocations = 0;
876 XGL_SIZE num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600877 XGL_UINT8 *pData;
878 int i;
879 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600880 XGL_RESULT err;
881 struct xgltexcube_vs_uniform data;
882
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600883 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600884 mat4x4_mul(MVP, VP, demo->model_matrix);
885 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600886// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600887
888 for (i=0; i<12*3; i++) {
889 data.position[i][0] = g_vertex_buffer_data[i*3];
890 data.position[i][1] = g_vertex_buffer_data[i*3+1];
891 data.position[i][2] = g_vertex_buffer_data[i*3+2];
892 data.position[i][3] = 1.0f;
893 data.attr[i][0] = g_uv_buffer_data[2*i];
894 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
895 data.attr[i][2] = 0;
896 data.attr[i][3] = 0;
897 }
898
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800899 memset(&buf_info, 0, sizeof(buf_info));
900 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
901 buf_info.size = sizeof(data);
902 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
903 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
904 assert(!err);
905
906 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700907 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
908 &num_alloc_size, &num_allocations);
909 assert(!err && num_alloc_size == sizeof(num_allocations));
910 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
911 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
912 demo->uniform_data.num_mem = num_allocations;
913 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800914 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700915 &mem_reqs_size, mem_reqs);
916 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
917 err = xglGetObjectInfo(demo->uniform_data.buf,
918 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
919 &buf_reqs_size, &buf_reqs);
920 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
921 buf_alloc.usage = buf_reqs.usage;
922 for (XGL_UINT i = 0; i < num_allocations; i ++) {
923 alloc_info.allocationSize = mem_reqs[i].size;
924 alloc_info.alignment = mem_reqs[i].alignment;
925 alloc_info.heapCount = mem_reqs[i].heapCount;
926 XGL_UINT heapInfo[mem_reqs[i].heapCount];
927 alloc_info.pHeaps = (const XGL_UINT *)heapInfo;
928 memcpy(heapInfo, mem_reqs[i].pHeaps,
929 sizeof(mem_reqs[i].pHeaps[0]) * mem_reqs[i].heapCount);
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800930
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700931 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
932 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600933
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700934 err = xglMapMemory(demo->uniform_data.mem[i], 0, (XGL_VOID **) &pData);
935 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600936
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700937 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600938
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700939 err = xglUnmapMemory(demo->uniform_data.mem[i]);
940 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600941
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700942 err = xglBindObjectMemory(demo->uniform_data.buf, i,
943 demo->uniform_data.mem[i], 0);
944 assert(!err);
945 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800946
947 memset(&view_info, 0, sizeof(view_info));
948 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
949 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +0800950 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800951 view_info.offset = 0;
952 view_info.range = sizeof(data);
953
954 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
955 assert(!err);
956
957 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
958 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600959}
960
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800961static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600962{
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800963 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
964 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600965 .pNext = NULL,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800966 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
967 .count = 1,
968 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
969 .immutableSampler = XGL_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600970 };
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800971 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
972 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
973 .pNext = NULL,
974 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
975 .count = DEMO_TEXTURE_COUNT,
976 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
977 .immutableSampler = XGL_NULL_HANDLE,
978 };
979 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600980 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600981
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800982 err = xglCreateDescriptorSetLayout(demo->device,
983 XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, &bind_point,
984 XGL_NULL_HANDLE, &descriptor_layout_vs,
985 &demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600986 assert(!err);
987
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800988 err = xglCreateDescriptorSetLayout(demo->device,
989 XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, &bind_point,
990 demo->desc_layout_vs, &descriptor_layout_fs,
991 &demo->desc_layout_fs);
992 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600993
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800994 demo->desc_layout_last = &demo->desc_layout_fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600995}
996
997static XGL_SHADER demo_prepare_shader(struct demo *demo,
998 XGL_PIPELINE_SHADER_STAGE stage,
999 const void *code,
1000 XGL_SIZE size)
1001{
1002 XGL_SHADER_CREATE_INFO createInfo;
1003 XGL_SHADER shader;
1004 XGL_RESULT err;
1005
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001006
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001007 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1008 createInfo.pNext = NULL;
1009
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001010#ifdef EXTERNAL_BIL
1011 createInfo.codeSize = size;
1012 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001013 createInfo.flags = 0;
1014
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001015 err = xglCreateShader(demo->device, &createInfo, &shader);
1016 if (err) {
1017 free((void *) createInfo.pCode);
1018 }
1019#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001020 // Create fake BIL structure to feed GLSL
1021 // to the driver "under the covers"
1022 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1023 createInfo.pCode = malloc(createInfo.codeSize);
1024 createInfo.flags = 0;
1025
1026 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1027 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1028 ((uint32_t *) createInfo.pCode)[1] = 0;
1029 ((uint32_t *) createInfo.pCode)[2] = stage;
1030 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1031
1032 err = xglCreateShader(demo->device, &createInfo, &shader);
1033 if (err) {
1034 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001035 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001036 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001037#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038
1039 return shader;
1040}
1041
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001042char *demo_read_bil(const char *filename, XGL_SIZE *psize)
1043{
1044 long int size;
1045 void *shader_code;
1046
1047 FILE *fp = fopen(filename, "rb");
1048 if (!fp) return NULL;
1049
1050 fseek(fp, 0L, SEEK_END);
1051 size = ftell(fp);
1052
1053 fseek(fp, 0L, SEEK_SET);
1054
1055 shader_code = malloc(size);
1056 fread(shader_code, size, 1, fp);
1057
1058 *psize = size;
1059
1060 return shader_code;
1061}
1062
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001063static XGL_SHADER demo_prepare_vs(struct demo *demo)
1064{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001065#ifdef EXTERNAL_BIL
1066 void *vertShaderCode;
1067 XGL_SIZE size;
1068
1069 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1070
1071 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1072 vertShaderCode, size);
1073#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001074 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001075 "#version 140\n"
1076 "#extension GL_ARB_separate_shader_objects : enable\n"
1077 "#extension GL_ARB_shading_language_420pack : enable\n"
1078 "\n"
1079 "layout(binding = 0) uniform buf {\n"
1080 " mat4 MVP;\n"
1081 " vec4 position[12*3];\n"
1082 " vec4 attr[12*3];\n"
1083 "} ubuf;\n"
1084 "\n"
1085 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001086 "\n"
1087 "void main() \n"
1088 "{\n"
1089 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001090 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1091 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001092
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001093 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1094 (const void *) vertShaderText,
1095 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001096#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001097}
1098
1099static XGL_SHADER demo_prepare_fs(struct demo *demo)
1100{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001101#ifdef EXTERNAL_BIL
1102 void *fragShaderCode;
1103 XGL_SIZE size;
1104
1105 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1106
1107 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1108 fragShaderCode, size);
1109#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001110 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001111 "#version 140\n"
1112 "#extension GL_ARB_separate_shader_objects : enable\n"
1113 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001114 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001115 "\n"
1116 "layout (location = 0) in vec4 texcoord;\n"
1117 "void main() {\n"
1118 " gl_FragColor = texture(tex, texcoord.xy);\n"
1119 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001120
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001121 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1122 (const void *) fragShaderText,
1123 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001124#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001125}
1126
1127static void demo_prepare_pipeline(struct demo *demo)
1128{
1129 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001130 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1131 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001132 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1133 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001134 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1135 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001136 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1137 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001138 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001139
1140 memset(&pipeline, 0, sizeof(pipeline));
1141 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001142 pipeline.lastSetLayout = *demo->desc_layout_last;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001143
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001144 memset(&ia, 0, sizeof(ia));
1145 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1146 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1147
1148 memset(&rs, 0, sizeof(rs));
1149 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001150 rs.fillMode = XGL_FILL_SOLID;
1151 rs.cullMode = XGL_CULL_NONE;
1152 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001153
1154 memset(&cb, 0, sizeof(cb));
1155 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001156 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1157 memset(att_state, 0, sizeof(att_state));
1158 att_state[0].format = demo->format;
1159 att_state[0].channelWriteMask = 0xf;
1160 att_state[0].blendEnable = XGL_FALSE;
1161 cb.attachmentCount = 1;
1162 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001163
Tony Barbour29645d02015-01-16 14:27:35 -07001164 memset(&vp, 0, sizeof(vp));
1165 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1166 vp.scissorEnable = XGL_FALSE;
1167
1168 memset(&ds, 0, sizeof(ds));
1169 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1170 ds.format = demo->depth.format;
1171 ds.depthTestEnable = XGL_TRUE;
1172 ds.depthWriteEnable = XGL_TRUE;
1173 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1174 ds.depthBoundsEnable = XGL_FALSE;
1175 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1176 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1177 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1178 ds.stencilTestEnable = XGL_FALSE;
1179 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001180
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001181 memset(&vs, 0, sizeof(vs));
1182 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1183 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001184 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001185 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001186
1187 memset(&fs, 0, sizeof(fs));
1188 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1189 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1190 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001191 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001192
1193 memset(&ms, 0, sizeof(ms));
1194 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1195 ms.sampleMask = 1;
1196 ms.multisampleEnable = XGL_FALSE;
1197 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001198
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001199 pipeline.pNext = (const XGL_VOID *) &ia;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200 ia.pNext = (const XGL_VOID *) &rs;
1201 rs.pNext = (const XGL_VOID *) &cb;
Tony Barbour29645d02015-01-16 14:27:35 -07001202 cb.pNext = (const XGL_VOID *) &ms;
1203 ms.pNext = (const XGL_VOID *) &vp;
1204 vp.pNext = (const XGL_VOID *) &ds;
1205 ds.pNext = (const XGL_VOID *) &vs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 vs.pNext = (const XGL_VOID *) &fs;
1207
1208 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1209 assert(!err);
1210
1211 xglDestroyObject(vs.shader.shader);
1212 xglDestroyObject(fs.shader.shader);
1213}
1214
1215static void demo_prepare_dynamic_states(struct demo *demo)
1216{
Tony Barbour29645d02015-01-16 14:27:35 -07001217 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1218 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1219 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1220 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001221 XGL_RESULT err;
1222
Tony Barbour29645d02015-01-16 14:27:35 -07001223 memset(&viewport_create, 0, sizeof(viewport_create));
1224 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
1225 viewport_create.viewportCount = 1;
1226 XGL_VIEWPORT viewport;
1227 viewport.height = (XGL_FLOAT) demo->height;
1228 viewport.width = (XGL_FLOAT) demo->width;
1229 viewport.minDepth = (XGL_FLOAT) 0.0f;
1230 viewport.maxDepth = (XGL_FLOAT) 1.0f;
1231 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232
1233 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001234 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001235
1236 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001237 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238
1239 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001240 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1241 depth_stencil.stencilBackRef = 0;
1242 depth_stencil.stencilFrontRef = 0;
1243 depth_stencil.stencilReadMask = 0xff;
1244 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001245
Tony Barbour29645d02015-01-16 14:27:35 -07001246 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247 assert(!err);
1248
Tony Barbour29645d02015-01-16 14:27:35 -07001249 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001250 assert(!err);
1251
Tony Barbour29645d02015-01-16 14:27:35 -07001252 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001253 &color_blend, &demo->color_blend);
1254 assert(!err);
1255
Tony Barbour29645d02015-01-16 14:27:35 -07001256 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001257 &depth_stencil, &demo->depth_stencil);
1258 assert(!err);
1259}
1260
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001261static void demo_prepare_descriptor_region(struct demo *demo)
1262{
1263 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1264 [0] = {
1265 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1266 .count = 1,
1267 },
1268 [1] = {
1269 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1270 .count = DEMO_TEXTURE_COUNT,
1271 },
1272 };
1273 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1274 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1275 .pNext = NULL,
1276 .count = 2,
1277 .pTypeCount = type_counts,
1278 };
1279 XGL_RESULT err;
1280
1281 err = xglCreateDescriptorRegion(demo->device,
1282 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1283 &descriptor_region, &demo->desc_region);
1284 assert(!err);
1285}
1286
1287static void demo_prepare_descriptor_set(struct demo *demo)
1288{
1289 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1290 &demo->uniform_data.attach;
1291 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1292 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1293 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1294 XGL_UPDATE_BUFFERS update_vs;
1295 XGL_RESULT err;
1296 uint32_t count;
1297 XGL_UINT i;
1298
1299 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1300 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1301 view_info[i].pNext = NULL;
1302 view_info[i].view = demo->textures[i].view,
1303 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1304
1305 combined_info[i].pSampler = demo->textures[i].sampler;
1306 combined_info[i].pImageView = &view_info[i];
1307 }
1308
1309 memset(&update_vs, 0, sizeof(update_vs));
1310 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1311 update_vs.pNext = &update_fs;
1312 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1313 update_vs.count = 1;
1314 update_vs.pBufferViews = &view_info_vs;
1315
1316 memset(&update_fs, 0, sizeof(update_fs));
1317 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1318 update_fs.index = 1;
1319 update_fs.count = DEMO_TEXTURE_COUNT;
1320 update_fs.pSamplerImageViews = combined_info;
1321
1322 err = xglAllocDescriptorSets(demo->desc_region,
1323 XGL_DESCRIPTOR_SET_USAGE_STATIC,
1324 1, demo->desc_layout_last,
1325 &demo->desc_set, &count);
1326 assert(!err && count == 1);
1327
1328 xglBeginDescriptorRegionUpdate(demo->device,
1329 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1330
1331 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1332 xglUpdateDescriptors(demo->desc_set, &update_vs);
1333
1334 xglEndDescriptorRegionUpdate(demo->device, demo->cmd);
1335}
1336
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001337static void demo_prepare(struct demo *demo)
1338{
1339 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1340 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1341 .pNext = NULL,
1342 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1343 .flags = 0,
1344 };
1345 XGL_RESULT err;
1346
1347 demo_prepare_buffers(demo);
1348 demo_prepare_depth(demo);
1349 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001350 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001351
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001352 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353 demo_prepare_pipeline(demo);
1354 demo_prepare_dynamic_states(demo);
1355
1356 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
1357 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001358
1359 demo_prepare_descriptor_region(demo);
1360 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001361}
1362
1363static void demo_handle_event(struct demo *demo,
1364 const xcb_generic_event_t *event)
1365{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001366 u_int8_t event_code = event->response_type & 0x7f;
1367 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001368 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001369 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001370 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001371 case XCB_CLIENT_MESSAGE:
1372 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1373 (*demo->atom_wm_delete_window).atom) {
1374 demo->quit = true;
1375 }
1376 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001377 case XCB_KEY_RELEASE:
1378 {
1379 const xcb_key_release_event_t *key =
1380 (const xcb_key_release_event_t *) event;
1381
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001382 switch (key->detail) {
1383 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001384 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001385 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001386 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001387 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001388 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001389 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001390 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001391 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001392 case 0x41:
1393 demo->pause = !demo->pause;
1394 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001395 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396 }
1397 break;
1398 default:
1399 break;
1400 }
1401}
1402
1403static void demo_run(struct demo *demo)
1404{
1405 xcb_flush(demo->connection);
1406
1407 while (!demo->quit) {
1408 xcb_generic_event_t *event;
1409
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001410 if (demo->pause) {
1411 event = xcb_wait_for_event(demo->connection);
1412 } else {
1413 event = xcb_poll_for_event(demo->connection);
1414 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415 if (event) {
1416 demo_handle_event(demo, event);
1417 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001418 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001419
1420 // Wait for work to finish before updating MVP.
1421 xglDeviceWaitIdle(demo->device);
1422 demo_update_data_buffer(demo);
1423
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001424 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001425
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001426 // Wait for work to finish before updating MVP.
1427 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428 }
1429}
1430
1431static void demo_create_window(struct demo *demo)
1432{
1433 uint32_t value_mask, value_list[32];
1434
1435 demo->window = xcb_generate_id(demo->connection);
1436
1437 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1438 value_list[0] = demo->screen->black_pixel;
1439 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1440 XCB_EVENT_MASK_EXPOSURE;
1441
1442 xcb_create_window(demo->connection,
1443 XCB_COPY_FROM_PARENT,
1444 demo->window, demo->screen->root,
1445 0, 0, demo->width, demo->height, 0,
1446 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1447 demo->screen->root_visual,
1448 value_mask, value_list);
1449
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001450 /* Magic code that will send notification when window is destroyed */
1451 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1452 "WM_PROTOCOLS");
1453 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1454
1455 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1456 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1457
1458 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1459 demo->window, (*reply).atom, 4, 32, 1,
1460 &(*demo->atom_wm_delete_window).atom);
1461 free(reply);
1462
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001463 xcb_map_window(demo->connection, demo->window);
1464}
1465
1466static void demo_init_xgl(struct demo *demo)
1467{
1468 const XGL_APPLICATION_INFO app = {
1469 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1470 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001471 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001473 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001474 .engineVersion = 0,
1475 .apiVersion = XGL_MAKE_VERSION(0, 22, 0),
1476 };
1477 const XGL_WSI_X11_CONNECTION_INFO connection = {
1478 .pConnection = demo->connection,
1479 .root = demo->screen->root,
1480 .provider = 0,
1481 };
1482 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1483 .queueNodeIndex = 0,
1484 .queueCount = 1,
1485 };
1486 const XGL_CHAR *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001487 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001488 };
1489 const XGL_DEVICE_CREATE_INFO device = {
1490 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1491 .pNext = NULL,
1492 .queueRecordCount = 1,
1493 .pRequestedQueues = &queue,
1494 .extensionCount = 1,
1495 .ppEnabledExtensionNames = ext_names,
1496 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1497 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1498 };
1499 XGL_RESULT err;
1500 XGL_UINT gpu_count;
1501 XGL_UINT i;
1502
1503 err = xglInitAndEnumerateGpus(&app, NULL, 1, &gpu_count, &demo->gpu);
1504 assert(!err && gpu_count == 1);
1505
1506 for (i = 0; i < device.extensionCount; i++) {
1507 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1508 assert(!err);
1509 }
1510
1511 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1512 assert(!err);
1513
1514 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1515 assert(!err);
1516
1517 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1518 0, &demo->queue);
1519 assert(!err);
1520}
1521
1522static void demo_init_connection(struct demo *demo)
1523{
1524 const xcb_setup_t *setup;
1525 xcb_screen_iterator_t iter;
1526 int scr;
1527
1528 demo->connection = xcb_connect(NULL, &scr);
1529
1530 setup = xcb_get_setup(demo->connection);
1531 iter = xcb_setup_roots_iterator(setup);
1532 while (scr-- > 0)
1533 xcb_screen_next(&iter);
1534
1535 demo->screen = iter.data;
1536}
1537
1538static void demo_init(struct demo *demo)
1539{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001540 vec3 eye = {0.0f, 3.0f, 5.0f};
1541 vec3 origin = {0, 0, 0};
1542 vec3 up = {0.0f, -1.0f, 0.0};
1543
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001544 memset(demo, 0, sizeof(*demo));
1545
1546 demo_init_connection(demo);
1547 demo_init_xgl(demo);
1548
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001549 demo->width = 500;
1550 demo->height = 500;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001551 demo->format.channelFormat = XGL_CH_FMT_B8G8R8A8;
1552 demo->format.numericFormat = XGL_NUM_FMT_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001553
1554 demo->spin_angle = 0.01f;
1555 demo->spin_increment = 0.01f;
1556 demo->pause = false;
1557
1558 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1559 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1560 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561}
1562
1563static void demo_cleanup(struct demo *demo)
1564{
Jon Ashburnb2a66652015-01-16 09:37:43 -07001565 XGL_UINT i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001566
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001567 xglDestroyObject(demo->desc_set);
1568 xglDestroyObject(demo->desc_region);
1569
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570 xglDestroyObject(demo->cmd);
1571
1572 xglDestroyObject(demo->viewport);
1573 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001574 xglDestroyObject(demo->color_blend);
1575 xglDestroyObject(demo->depth_stencil);
1576
1577 xglDestroyObject(demo->pipeline);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001578 xglDestroyObject(demo->desc_layout_fs);
1579 xglDestroyObject(demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001580
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001581// xglFreeMemory(demo->vertices.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582
1583 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1584 xglDestroyObject(demo->textures[i].view);
1585 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001586 for (j = 0; j < demo->textures[i].num_mem; j++)
1587 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001588 xglDestroyObject(demo->textures[i].sampler);
1589 }
1590
1591 xglDestroyObject(demo->depth.view);
1592 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001593 for (j = 0; j < demo->depth.num_mem; j++)
1594 xglFreeMemory(demo->depth.mem[j]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001595 xglDestroyObject(demo->uniform_data.buf);
1596 for (j = 0; j < demo->uniform_data.num_mem; j++)
1597 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001598
1599 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001600 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001601 xglDestroyObject(demo->buffers[i].view);
1602 xglDestroyObject(demo->buffers[i].image);
1603 }
1604
1605 xglDestroyDevice(demo->device);
1606
1607 xcb_destroy_window(demo->connection, demo->window);
1608 xcb_disconnect(demo->connection);
1609}
1610
1611int main(void)
1612{
1613 struct demo demo;
1614
1615 demo_init(&demo);
1616
1617 demo_prepare(&demo);
1618 demo_create_window(&demo);
1619 demo_run(&demo);
1620
1621 demo_cleanup(&demo);
1622
1623 return 0;
1624}