blob: e49916a9e650c63259c44648ea64cbe9abdaec59 [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
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060034 float mvp[4][4];
35 float position[12*3][4];
36 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060037};
38
39struct xgltexcube_vs_uniform {
40 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060041 float mvp[4][4];
42 float position[12*3][4];
43 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060044};
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{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060051 float posX, posY, posZ, posW; // Position data
52 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053};
54
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060055struct VertexPosTex
56{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060057 float posX, posY, posZ, posW; // Position data
58 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060059};
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
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060064static const 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
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600114static const 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
Jon Ashburn93cfc432015-01-29 15:47:01 -0700196 XGL_INSTANCE inst;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600197 XGL_PHYSICAL_GPU gpu;
198 XGL_DEVICE device;
199 XGL_QUEUE queue;
200
201 int width, height;
202 XGL_FORMAT format;
203
204 struct {
205 XGL_IMAGE image;
206 XGL_GPU_MEMORY mem;
207
208 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800209 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600210 } buffers[DEMO_BUFFER_COUNT];
211
212 struct {
213 XGL_FORMAT format;
214
215 XGL_IMAGE image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600216 uint32_t num_mem;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700217 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600218 XGL_DEPTH_STENCIL_VIEW view;
219 } depth;
220
221 struct {
222 XGL_SAMPLER sampler;
223
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 char *filename;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600225 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_IMAGE_VIEW view;
229 } textures[DEMO_TEXTURE_COUNT];
230
231 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800232 XGL_BUFFER buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600233 uint32_t num_mem;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700234 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800235 XGL_BUFFER_VIEW view;
236 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600237 } uniform_data;
238
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800239 XGL_DESCRIPTOR_SET_LAYOUT desc_layout_vs;
240 XGL_DESCRIPTOR_SET_LAYOUT desc_layout_fs;
241 XGL_DESCRIPTOR_SET_LAYOUT *desc_layout_last;
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
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600257 XGL_CMD_BUFFER cmd;
258
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800259 XGL_DESCRIPTOR_REGION desc_region;
260 XGL_DESCRIPTOR_SET desc_set;
261
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600262 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700263 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600264
265 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600266 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600267};
268
269static void demo_draw_build_cmd(struct demo *demo)
270{
271 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
272 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000273 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600274 };
275 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
276 .view = demo->depth.view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000277 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600278 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600279 const float clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
280 const float clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600281 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700282 XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = {
283 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO,
284 .pNext = NULL,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700285 };
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,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600302 .width = demo->width,
303 .height = demo->height,
304 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700305 };
306 XGL_RENDER_PASS_CREATE_INFO rp_info;
307
308 memset(&rp_info, 0 , sizeof(rp_info));
309 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
310 assert(!err);
311 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
312 rp_info.renderArea.extent.width = demo->width;
313 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchterc17d7de2015-02-10 14:06:25 -0700314 rp_info.colorAttachmentCount = 1;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700315 rp_info.pColorLoadOps = &load_op;
316 rp_info.pColorStoreOps = &store_op;
317 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
318 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
319 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
320 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
321 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
322 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700324 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600325 assert(!err);
326
327 xglCmdBindPipeline(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
328 demo->pipeline);
329 xglCmdBindDescriptorSet(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800330 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331
Tony Barbour29645d02015-01-16 14:27:35 -0700332 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_VIEWPORT, demo->viewport);
333 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_RASTER, demo->raster);
334 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335 demo->color_blend);
Tony Barbour29645d02015-01-16 14:27:35 -0700336 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600337 demo->depth_stencil);
338
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600339 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
340 clear_range.baseMipLevel = 0;
341 clear_range.mipLevels = 1;
342 clear_range.baseArraySlice = 0;
343 clear_range.arraySize = 1;
344 xglCmdClearColorImage(demo->cmd,
345 demo->buffers[demo->current_buffer].image,
346 clear_color, 1, &clear_range);
347
348 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
349 xglCmdClearDepthStencil(demo->cmd, demo->depth.image,
350 clear_depth, 0, 1, &clear_range);
351
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600352 xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600353
354 err = xglEndCommandBuffer(demo->cmd);
355 assert(!err);
356}
357
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600358
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600359void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600361 mat4x4 MVP, Model, VP;
362 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600363 uint8_t *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364 XGL_RESULT err;
365
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600366 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600367
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600368 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600369 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600370 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600371 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600372
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700373 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600374 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375 assert(!err);
376
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600377 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600378
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700379 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380 assert(!err);
381}
382
383static void demo_draw(struct demo *demo)
384{
385 const XGL_WSI_X11_PRESENT_INFO present = {
386 .destWindow = demo->window,
387 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800388 .async = true,
389 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800391 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600392 XGL_RESULT err;
393
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600394 demo_draw_build_cmd(demo);
395
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600396 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wubb57f642014-11-07 14:30:34 +0800397 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
398
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600399 err = xglQueueSubmit(demo->queue, 1, &demo->cmd,
400 0, NULL, XGL_NULL_HANDLE);
401 assert(!err);
402
Chia-I Wubb57f642014-11-07 14:30:34 +0800403 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404 assert(!err);
405
406 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
407}
408
409static void demo_prepare_buffers(struct demo *demo)
410{
411 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
412 .format = demo->format,
413 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
414 .extent = {
415 .width = demo->width,
416 .height = demo->height,
417 },
418 .flags = 0,
419 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800420 const XGL_FENCE_CREATE_INFO fence = {
421 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
422 .pNext = NULL,
423 .flags = 0,
424 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600425 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600426 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600427
428 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
429 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
430 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
431 .pNext = NULL,
432 .format = demo->format,
433 .mipLevel = 0,
434 .baseArraySlice = 0,
435 .arraySize = 1,
436 };
437
438 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
439 &demo->buffers[i].image, &demo->buffers[i].mem);
440 assert(!err);
441
442 color_attachment_view.image = demo->buffers[i].image;
443
444 err = xglCreateColorAttachmentView(demo->device,
445 &color_attachment_view, &demo->buffers[i].view);
446 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800447
448 err = xglCreateFence(demo->device,
449 &fence, &demo->buffers[i].fence);
450 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600451 }
452}
453
454static void demo_prepare_depth(struct demo *demo)
455{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700456 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600457 const XGL_IMAGE_CREATE_INFO image = {
458 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
459 .pNext = NULL,
460 .imageType = XGL_IMAGE_2D,
461 .format = depth_format,
462 .extent = { demo->width, demo->height, 1 },
463 .mipLevels = 1,
464 .arraySize = 1,
465 .samples = 1,
466 .tiling = XGL_OPTIMAL_TILING,
467 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
468 .flags = 0,
469 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700470 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
471 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
472 .pNext = NULL,
473 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600474 XGL_MEMORY_ALLOC_INFO mem_alloc = {
475 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700476 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600477 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700478 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700479 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600480 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
481 };
482 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
483 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
484 .pNext = NULL,
485 .image = XGL_NULL_HANDLE,
486 .mipLevel = 0,
487 .baseArraySlice = 0,
488 .arraySize = 1,
489 .flags = 0,
490 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700491 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600492 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700493 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600494 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600495 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600496 uint32_t num_allocations = 0;
497 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600498
499 demo->depth.format = depth_format;
500
501 /* create image */
502 err = xglCreateImage(demo->device, &image,
503 &demo->depth.image);
504 assert(!err);
505
Jon Ashburnb2a66652015-01-16 09:37:43 -0700506
507 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
508 assert(!err && num_alloc_size == sizeof(num_allocations));
509 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
510 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
511 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600512 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700513 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
514 &mem_reqs_size, mem_reqs);
515 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700516 err = xglGetObjectInfo(demo->depth.image,
517 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
518 &img_reqs_size, &img_reqs);
519 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
520 img_alloc.usage = img_reqs.usage;
521 img_alloc.formatClass = img_reqs.formatClass;
522 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600523 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700524 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525
Jon Ashburnb2a66652015-01-16 09:37:43 -0700526 /* allocate memory */
527 err = xglAllocMemory(demo->device, &mem_alloc,
528 &(demo->depth.mem[i]));
529 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600530
Jon Ashburnb2a66652015-01-16 09:37:43 -0700531 /* bind memory */
532 err = xglBindObjectMemory(demo->depth.image, i,
533 demo->depth.mem[i], 0);
534 assert(!err);
535 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600536
537 /* create image view */
538 view.image = demo->depth.image;
539 err = xglCreateDepthStencilView(demo->device, &view,
540 &demo->depth.view);
541 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542}
543
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600544/** loadTexture
545 * loads a png file into an memory object, using cstdio , libpng.
546 *
547 * \param demo : Needed to access XGL calls
548 * \param filename : the png file to be loaded
549 * \param width : width of png, to be updated as a side effect of this function
550 * \param height : height of png, to be updated as a side effect of this function
551 *
552 * \return bool : an opengl texture id. true if successful?,
553 * should be validated by the client of this function.
554 *
555 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
556 * Modified to copy image to memory
557 *
558 */
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600559bool loadTexture(char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600560 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600561 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600562{
563 //header for testing if it is a png
564 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700565 int is_png, bit_depth, color_type,rowbytes;
566 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600567 png_structp png_ptr;
568 png_infop info_ptr, end_info;
569 png_byte *image_data;
570 png_bytep *row_pointers;
571
572 //open file as binary
573 FILE *fp = fopen(filename, "rb");
574 if (!fp) {
575 return false;
576 }
577
578 //read the header
579 fread(header, 1, 8, fp);
580
581 //test if png
582 is_png = !png_sig_cmp(header, 0, 8);
583 if (!is_png) {
584 fclose(fp);
585 return false;
586 }
587
588 //create png struct
589 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
590 NULL, NULL);
591 if (!png_ptr) {
592 fclose(fp);
593 return (false);
594 }
595
596 //create png info struct
597 info_ptr = png_create_info_struct(png_ptr);
598 if (!info_ptr) {
599 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
600 fclose(fp);
601 return (false);
602 }
603
604 //create png info struct
605 end_info = png_create_info_struct(png_ptr);
606 if (!end_info) {
607 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
608 fclose(fp);
609 return (false);
610 }
611
612 //png error stuff, not sure libpng man suggests this.
613 if (setjmp(png_jmpbuf(png_ptr))) {
614 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
615 fclose(fp);
616 return (false);
617 }
618
619 //init png reading
620 png_init_io(png_ptr, fp);
621
622 //let libpng know you already read the first 8 bytes
623 png_set_sig_bytes(png_ptr, 8);
624
625 // read all the info up to the image data
626 png_read_info(png_ptr, info_ptr);
627
628 // get info about png
629 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
630 NULL, NULL, NULL);
631
632 //update width and height based on png info
633 *width = twidth;
634 *height = theight;
635
636 // Require that incoming texture be 8bits per color component
637 // and 4 components (RGBA).
638 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
639 png_get_channels(png_ptr, info_ptr) != 4) {
640 return false;
641 }
642
643 if (rgba_data == NULL) {
644 // If data pointer is null, we just want the width & height
645 // clean up memory and close stuff
646 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
647 fclose(fp);
648
649 return true;
650 }
651
652 // Update the png info struct.
653 png_read_update_info(png_ptr, info_ptr);
654
655 // Row size in bytes.
656 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
657
658 // Allocate the image_data as a big block, to be given to opengl
659 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
660 if (!image_data) {
661 //clean up memory and close stuff
662 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
663 fclose(fp);
664 return false;
665 }
666
667 // row_pointers is for pointing to image_data for reading the png with libpng
668 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
669 if (!row_pointers) {
670 //clean up memory and close stuff
671 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
672 // delete[] image_data;
673 fclose(fp);
674 return false;
675 }
676 // set the individual row_pointers to point at the correct offsets of image_data
677 for (i = 0; i < theight; ++i)
678 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
679
680 // read the png into image_data through row_pointers
681 png_read_image(png_ptr, row_pointers);
682
683 // clean up memory and close stuff
684 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
685 free(row_pointers);
686 free(image_data);
687 fclose(fp);
688
689 return true;
690}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600691
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600692static void demo_prepare_textures(struct demo *demo)
693{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700694 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600695 int32_t tex_width;
696 int32_t tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600697 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600698 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600699
700 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
701 const XGL_SAMPLER_CREATE_INFO sampler = {
702 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
703 .pNext = NULL,
704 .magFilter = XGL_TEX_FILTER_NEAREST,
705 .minFilter = XGL_TEX_FILTER_NEAREST,
706 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600707 .addressU = XGL_TEX_ADDRESS_CLAMP,
708 .addressV = XGL_TEX_ADDRESS_CLAMP,
709 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600710 .mipLodBias = 0.0f,
711 .maxAnisotropy = 0,
712 .compareFunc = XGL_COMPARE_NEVER,
713 .minLod = 0.0f,
714 .maxLod = 0.0f,
715 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
716 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600717
Tony Barbour276d9822015-02-05 14:14:33 -0700718 err = loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height);
719 assert(err);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600720
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600721 const XGL_IMAGE_CREATE_INFO image = {
722 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
723 .pNext = NULL,
724 .imageType = XGL_IMAGE_2D,
725 .format = tex_format,
726 .extent = { tex_width, tex_height, 1 },
727 .mipLevels = 1,
728 .arraySize = 1,
729 .samples = 1,
730 .tiling = XGL_LINEAR_TILING,
731 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
732 .flags = 0,
733 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700734 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
735 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
736 .pNext = NULL,
737 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600738 XGL_MEMORY_ALLOC_INFO mem_alloc = {
739 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700740 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600741 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700742 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700743 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600744 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
745 };
746 XGL_IMAGE_VIEW_CREATE_INFO view = {
747 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
748 .pNext = NULL,
749 .image = XGL_NULL_HANDLE,
750 .viewType = XGL_IMAGE_VIEW_2D,
751 .format = image.format,
752 .channels = { XGL_CHANNEL_SWIZZLE_R,
753 XGL_CHANNEL_SWIZZLE_G,
754 XGL_CHANNEL_SWIZZLE_B,
755 XGL_CHANNEL_SWIZZLE_A, },
756 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
757 .minLod = 0.0f,
758 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700759
760 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600761 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700762 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600763 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
764 uint32_t num_allocations = 0;
765 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600766
767 /* create sampler */
768 err = xglCreateSampler(demo->device, &sampler,
769 &demo->textures[i].sampler);
770 assert(!err);
771
772 /* create image */
773 err = xglCreateImage(demo->device, &image,
774 &demo->textures[i].image);
775 assert(!err);
776
777 err = xglGetObjectInfo(demo->textures[i].image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700778 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
779 &num_alloc_size, &num_allocations);
780 assert(!err && num_alloc_size == sizeof(num_allocations));
781 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
782 demo->textures[i].mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
783 demo->textures[i].num_mem = num_allocations;
784 err = xglGetObjectInfo(demo->textures[i].image,
785 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
786 &mem_reqs_size, mem_reqs);
787 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700788 err = xglGetObjectInfo(demo->textures[i].image,
789 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
790 &img_reqs_size, &img_reqs);
791 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
792 img_alloc.usage = img_reqs.usage;
793 img_alloc.formatClass = img_reqs.formatClass;
794 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600795 for (uint32_t j = 0; j < num_allocations; j ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700796 mem_alloc.allocationSize = mem_reqs[j].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600797
Jon Ashburnb2a66652015-01-16 09:37:43 -0700798 /* allocate memory */
799 err = xglAllocMemory(demo->device, &mem_alloc,
800 &(demo->textures[i].mem[j]));
801 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600802
Jon Ashburnb2a66652015-01-16 09:37:43 -0700803 /* bind memory */
804 err = xglBindObjectMemory(demo->textures[i].image, j,
805 demo->textures[i].mem[j], 0);
806 assert(!err);
807 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600808
809 /* create image view */
810 view.image = demo->textures[i].image;
811 err = xglCreateImageView(demo->device, &view,
812 &demo->textures[i].view);
813 assert(!err);
814 }
815
816 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
817 const XGL_IMAGE_SUBRESOURCE subres = {
818 .aspect = XGL_IMAGE_ASPECT_COLOR,
819 .mipLevel = 0,
820 .arraySlice = 0,
821 };
822 XGL_SUBRESOURCE_LAYOUT layout;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600823 size_t layout_size = sizeof(layout);
824 void *data;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600825
826 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
827 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
828 assert(!err && layout_size == sizeof(layout));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700829 assert(demo->textures[i].num_mem == 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830
Jon Ashburnb2a66652015-01-16 09:37:43 -0700831 err = xglMapMemory(demo->textures[i].mem[0], 0, &data);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600832 assert(!err);
833
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600834 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600835
Jon Ashburnb2a66652015-01-16 09:37:43 -0700836 err = xglUnmapMemory(demo->textures[i].mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600837 assert(!err);
838 }
839}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600840
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600841void demo_prepare_cube_data_buffer(struct demo *demo)
842{
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800843 XGL_BUFFER_CREATE_INFO buf_info;
844 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700845 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
846 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
847 .pNext = NULL,
848 };
849 XGL_MEMORY_ALLOC_INFO alloc_info = {
850 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
851 .pNext = &buf_alloc,
852 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700853 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700854 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700855 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
856 };
857 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600858 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700859 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600860 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
861 uint32_t num_allocations = 0;
862 size_t num_alloc_size = sizeof(num_allocations);
863 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600864 int i;
865 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600866 XGL_RESULT err;
867 struct xgltexcube_vs_uniform data;
868
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600869 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600870 mat4x4_mul(MVP, VP, demo->model_matrix);
871 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600872// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600873
874 for (i=0; i<12*3; i++) {
875 data.position[i][0] = g_vertex_buffer_data[i*3];
876 data.position[i][1] = g_vertex_buffer_data[i*3+1];
877 data.position[i][2] = g_vertex_buffer_data[i*3+2];
878 data.position[i][3] = 1.0f;
879 data.attr[i][0] = g_uv_buffer_data[2*i];
880 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
881 data.attr[i][2] = 0;
882 data.attr[i][3] = 0;
883 }
884
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800885 memset(&buf_info, 0, sizeof(buf_info));
886 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
887 buf_info.size = sizeof(data);
888 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
889 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
890 assert(!err);
891
892 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700893 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
894 &num_alloc_size, &num_allocations);
895 assert(!err && num_alloc_size == sizeof(num_allocations));
896 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
897 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
898 demo->uniform_data.num_mem = num_allocations;
899 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800900 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700901 &mem_reqs_size, mem_reqs);
902 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
903 err = xglGetObjectInfo(demo->uniform_data.buf,
904 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
905 &buf_reqs_size, &buf_reqs);
906 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
907 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600908 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700909 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800910
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700911 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
912 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600913
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600914 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700915 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600916
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700917 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600918
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700919 err = xglUnmapMemory(demo->uniform_data.mem[i]);
920 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600921
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700922 err = xglBindObjectMemory(demo->uniform_data.buf, i,
923 demo->uniform_data.mem[i], 0);
924 assert(!err);
925 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800926
927 memset(&view_info, 0, sizeof(view_info));
928 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
929 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +0800930 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800931 view_info.offset = 0;
932 view_info.range = sizeof(data);
933
934 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
935 assert(!err);
936
937 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
938 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600939}
940
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800941static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600942{
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800943 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
944 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600945 .pNext = NULL,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800946 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
947 .count = 1,
948 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
949 .immutableSampler = XGL_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600950 };
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800951 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
952 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
953 .pNext = NULL,
954 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
955 .count = DEMO_TEXTURE_COUNT,
956 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
957 .immutableSampler = XGL_NULL_HANDLE,
958 };
959 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600960 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600961
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800962 err = xglCreateDescriptorSetLayout(demo->device,
963 XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, &bind_point,
964 XGL_NULL_HANDLE, &descriptor_layout_vs,
965 &demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600966 assert(!err);
967
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800968 err = xglCreateDescriptorSetLayout(demo->device,
969 XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, &bind_point,
970 demo->desc_layout_vs, &descriptor_layout_fs,
971 &demo->desc_layout_fs);
972 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600973
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800974 demo->desc_layout_last = &demo->desc_layout_fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600975}
976
977static XGL_SHADER demo_prepare_shader(struct demo *demo,
978 XGL_PIPELINE_SHADER_STAGE stage,
979 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600980 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600981{
982 XGL_SHADER_CREATE_INFO createInfo;
983 XGL_SHADER shader;
984 XGL_RESULT err;
985
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600986
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600987 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
988 createInfo.pNext = NULL;
989
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600990#ifdef EXTERNAL_BIL
991 createInfo.codeSize = size;
992 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600993 createInfo.flags = 0;
994
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600995 err = xglCreateShader(demo->device, &createInfo, &shader);
996 if (err) {
997 free((void *) createInfo.pCode);
998 }
999#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001000 // Create fake BIL structure to feed GLSL
1001 // to the driver "under the covers"
1002 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1003 createInfo.pCode = malloc(createInfo.codeSize);
1004 createInfo.flags = 0;
1005
1006 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1007 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1008 ((uint32_t *) createInfo.pCode)[1] = 0;
1009 ((uint32_t *) createInfo.pCode)[2] = stage;
1010 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1011
1012 err = xglCreateShader(demo->device, &createInfo, &shader);
1013 if (err) {
1014 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001015 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001016 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001017#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018
1019 return shader;
1020}
1021
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001022char *demo_read_bil(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001023{
1024 long int size;
1025 void *shader_code;
1026
1027 FILE *fp = fopen(filename, "rb");
1028 if (!fp) return NULL;
1029
1030 fseek(fp, 0L, SEEK_END);
1031 size = ftell(fp);
1032
1033 fseek(fp, 0L, SEEK_SET);
1034
1035 shader_code = malloc(size);
1036 fread(shader_code, size, 1, fp);
1037
1038 *psize = size;
1039
1040 return shader_code;
1041}
1042
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001043static XGL_SHADER demo_prepare_vs(struct demo *demo)
1044{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001045#ifdef EXTERNAL_BIL
1046 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001047 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001048
1049 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1050
1051 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1052 vertShaderCode, size);
1053#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001054 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001055 "#version 140\n"
1056 "#extension GL_ARB_separate_shader_objects : enable\n"
1057 "#extension GL_ARB_shading_language_420pack : enable\n"
1058 "\n"
1059 "layout(binding = 0) uniform buf {\n"
1060 " mat4 MVP;\n"
1061 " vec4 position[12*3];\n"
1062 " vec4 attr[12*3];\n"
1063 "} ubuf;\n"
1064 "\n"
1065 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001066 "\n"
1067 "void main() \n"
1068 "{\n"
1069 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001070 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1071 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001072
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001073 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1074 (const void *) vertShaderText,
1075 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001076#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001077}
1078
1079static XGL_SHADER demo_prepare_fs(struct demo *demo)
1080{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001081#ifdef EXTERNAL_BIL
1082 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001083 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001084
1085 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1086
1087 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1088 fragShaderCode, size);
1089#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001090 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001091 "#version 140\n"
1092 "#extension GL_ARB_separate_shader_objects : enable\n"
1093 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001094 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001095 "\n"
1096 "layout (location = 0) in vec4 texcoord;\n"
1097 "void main() {\n"
1098 " gl_FragColor = texture(tex, texcoord.xy);\n"
1099 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001100
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001101 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1102 (const void *) fragShaderText,
1103 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001104#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001105}
1106
1107static void demo_prepare_pipeline(struct demo *demo)
1108{
1109 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001110 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1111 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001112 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1113 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1115 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001116 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1117 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001118 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001119
1120 memset(&pipeline, 0, sizeof(pipeline));
1121 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001122 pipeline.lastSetLayout = *demo->desc_layout_last;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001123
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001124 memset(&ia, 0, sizeof(ia));
1125 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1126 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1127
1128 memset(&rs, 0, sizeof(rs));
1129 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001130 rs.fillMode = XGL_FILL_SOLID;
1131 rs.cullMode = XGL_CULL_NONE;
1132 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001133
1134 memset(&cb, 0, sizeof(cb));
1135 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001136 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1137 memset(att_state, 0, sizeof(att_state));
1138 att_state[0].format = demo->format;
1139 att_state[0].channelWriteMask = 0xf;
1140 att_state[0].blendEnable = XGL_FALSE;
1141 cb.attachmentCount = 1;
1142 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001143
Tony Barbour29645d02015-01-16 14:27:35 -07001144 memset(&vp, 0, sizeof(vp));
1145 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001146
1147 memset(&ds, 0, sizeof(ds));
1148 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1149 ds.format = demo->depth.format;
1150 ds.depthTestEnable = XGL_TRUE;
1151 ds.depthWriteEnable = XGL_TRUE;
1152 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1153 ds.depthBoundsEnable = XGL_FALSE;
1154 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1155 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1156 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1157 ds.stencilTestEnable = XGL_FALSE;
1158 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001159
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001160 memset(&vs, 0, sizeof(vs));
1161 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1162 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001163 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001164 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001165
1166 memset(&fs, 0, sizeof(fs));
1167 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1168 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1169 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001170 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001171
1172 memset(&ms, 0, sizeof(ms));
1173 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1174 ms.sampleMask = 1;
1175 ms.multisampleEnable = XGL_FALSE;
1176 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001177
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001178 pipeline.pNext = (const void *) &ia;
1179 ia.pNext = (const void *) &rs;
1180 rs.pNext = (const void *) &cb;
1181 cb.pNext = (const void *) &ms;
1182 ms.pNext = (const void *) &vp;
1183 vp.pNext = (const void *) &ds;
1184 ds.pNext = (const void *) &vs;
1185 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001186
1187 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1188 assert(!err);
1189
1190 xglDestroyObject(vs.shader.shader);
1191 xglDestroyObject(fs.shader.shader);
1192}
1193
1194static void demo_prepare_dynamic_states(struct demo *demo)
1195{
Tony Barbour29645d02015-01-16 14:27:35 -07001196 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1197 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1198 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1199 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200 XGL_RESULT err;
1201
Tony Barbour29645d02015-01-16 14:27:35 -07001202 memset(&viewport_create, 0, sizeof(viewport_create));
1203 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001204 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001205 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001206 XGL_RECT scissor;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001207 viewport.height = (float) demo->height;
1208 viewport.width = (float) demo->width;
1209 viewport.minDepth = (float) 0.0f;
1210 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001211 scissor.extent.width = demo->width;
1212 scissor.extent.height = demo->height;
1213 scissor.offset.x = 0;
1214 scissor.offset.y = 0;
Tony Barbour29645d02015-01-16 14:27:35 -07001215 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001216 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001217
1218 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001219 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001220
1221 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001222 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001223
1224 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001225 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1226 depth_stencil.stencilBackRef = 0;
1227 depth_stencil.stencilFrontRef = 0;
1228 depth_stencil.stencilReadMask = 0xff;
1229 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001230
Tony Barbour29645d02015-01-16 14:27:35 -07001231 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232 assert(!err);
1233
Tony Barbour29645d02015-01-16 14:27:35 -07001234 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001235 assert(!err);
1236
Tony Barbour29645d02015-01-16 14:27:35 -07001237 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238 &color_blend, &demo->color_blend);
1239 assert(!err);
1240
Tony Barbour29645d02015-01-16 14:27:35 -07001241 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001242 &depth_stencil, &demo->depth_stencil);
1243 assert(!err);
1244}
1245
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001246static void demo_prepare_descriptor_region(struct demo *demo)
1247{
1248 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1249 [0] = {
1250 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1251 .count = 1,
1252 },
1253 [1] = {
1254 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1255 .count = DEMO_TEXTURE_COUNT,
1256 },
1257 };
1258 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1259 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1260 .pNext = NULL,
1261 .count = 2,
1262 .pTypeCount = type_counts,
1263 };
1264 XGL_RESULT err;
1265
1266 err = xglCreateDescriptorRegion(demo->device,
1267 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1268 &descriptor_region, &demo->desc_region);
1269 assert(!err);
1270}
1271
1272static void demo_prepare_descriptor_set(struct demo *demo)
1273{
1274 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1275 &demo->uniform_data.attach;
1276 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1277 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1278 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1279 XGL_UPDATE_BUFFERS update_vs;
1280 XGL_RESULT err;
1281 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001282 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001283
1284 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1285 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1286 view_info[i].pNext = NULL;
1287 view_info[i].view = demo->textures[i].view,
1288 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1289
1290 combined_info[i].pSampler = demo->textures[i].sampler;
1291 combined_info[i].pImageView = &view_info[i];
1292 }
1293
1294 memset(&update_vs, 0, sizeof(update_vs));
1295 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1296 update_vs.pNext = &update_fs;
1297 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1298 update_vs.count = 1;
1299 update_vs.pBufferViews = &view_info_vs;
1300
1301 memset(&update_fs, 0, sizeof(update_fs));
1302 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1303 update_fs.index = 1;
1304 update_fs.count = DEMO_TEXTURE_COUNT;
1305 update_fs.pSamplerImageViews = combined_info;
1306
1307 err = xglAllocDescriptorSets(demo->desc_region,
1308 XGL_DESCRIPTOR_SET_USAGE_STATIC,
1309 1, demo->desc_layout_last,
1310 &demo->desc_set, &count);
1311 assert(!err && count == 1);
1312
1313 xglBeginDescriptorRegionUpdate(demo->device,
1314 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1315
1316 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1317 xglUpdateDescriptors(demo->desc_set, &update_vs);
1318
1319 xglEndDescriptorRegionUpdate(demo->device, demo->cmd);
1320}
1321
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001322static void demo_prepare(struct demo *demo)
1323{
1324 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1325 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1326 .pNext = NULL,
1327 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1328 .flags = 0,
1329 };
1330 XGL_RESULT err;
1331
1332 demo_prepare_buffers(demo);
1333 demo_prepare_depth(demo);
1334 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001335 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001336
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001337 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001338 demo_prepare_pipeline(demo);
1339 demo_prepare_dynamic_states(demo);
1340
1341 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
1342 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001343
1344 demo_prepare_descriptor_region(demo);
1345 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346}
1347
1348static void demo_handle_event(struct demo *demo,
1349 const xcb_generic_event_t *event)
1350{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001351 u_int8_t event_code = event->response_type & 0x7f;
1352 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001354 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001355 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001356 case XCB_CLIENT_MESSAGE:
1357 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1358 (*demo->atom_wm_delete_window).atom) {
1359 demo->quit = true;
1360 }
1361 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362 case XCB_KEY_RELEASE:
1363 {
1364 const xcb_key_release_event_t *key =
1365 (const xcb_key_release_event_t *) event;
1366
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001367 switch (key->detail) {
1368 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001369 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001370 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001371 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001372 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001373 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001374 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001375 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001376 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001377 case 0x41:
1378 demo->pause = !demo->pause;
1379 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001380 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001381 }
1382 break;
1383 default:
1384 break;
1385 }
1386}
1387
1388static void demo_run(struct demo *demo)
1389{
1390 xcb_flush(demo->connection);
1391
1392 while (!demo->quit) {
1393 xcb_generic_event_t *event;
1394
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001395 if (demo->pause) {
1396 event = xcb_wait_for_event(demo->connection);
1397 } else {
1398 event = xcb_poll_for_event(demo->connection);
1399 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 if (event) {
1401 demo_handle_event(demo, event);
1402 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001403 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001404
1405 // Wait for work to finish before updating MVP.
1406 xglDeviceWaitIdle(demo->device);
1407 demo_update_data_buffer(demo);
1408
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001409 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001410
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001411 // Wait for work to finish before updating MVP.
1412 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413 }
1414}
1415
1416static void demo_create_window(struct demo *demo)
1417{
1418 uint32_t value_mask, value_list[32];
1419
1420 demo->window = xcb_generate_id(demo->connection);
1421
1422 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1423 value_list[0] = demo->screen->black_pixel;
1424 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1425 XCB_EVENT_MASK_EXPOSURE;
1426
1427 xcb_create_window(demo->connection,
1428 XCB_COPY_FROM_PARENT,
1429 demo->window, demo->screen->root,
1430 0, 0, demo->width, demo->height, 0,
1431 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1432 demo->screen->root_visual,
1433 value_mask, value_list);
1434
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001435 /* Magic code that will send notification when window is destroyed */
1436 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1437 "WM_PROTOCOLS");
1438 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1439
1440 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1441 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1442
1443 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1444 demo->window, (*reply).atom, 4, 32, 1,
1445 &(*demo->atom_wm_delete_window).atom);
1446 free(reply);
1447
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001448 xcb_map_window(demo->connection, demo->window);
1449}
1450
1451static void demo_init_xgl(struct demo *demo)
1452{
1453 const XGL_APPLICATION_INFO app = {
1454 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1455 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001456 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001457 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001458 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001459 .engineVersion = 0,
1460 .apiVersion = XGL_MAKE_VERSION(0, 22, 0),
1461 };
1462 const XGL_WSI_X11_CONNECTION_INFO connection = {
1463 .pConnection = demo->connection,
1464 .root = demo->screen->root,
1465 .provider = 0,
1466 };
1467 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1468 .queueNodeIndex = 0,
1469 .queueCount = 1,
1470 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001471 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001472 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001473 };
1474 const XGL_DEVICE_CREATE_INFO device = {
1475 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1476 .pNext = NULL,
1477 .queueRecordCount = 1,
1478 .pRequestedQueues = &queue,
1479 .extensionCount = 1,
1480 .ppEnabledExtensionNames = ext_names,
1481 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1482 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1483 };
1484 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001485 uint32_t gpu_count;
1486 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001487
Jon Ashburn93cfc432015-01-29 15:47:01 -07001488 err = xglCreateInstance(&app, NULL, &demo->inst);
1489 assert(!err);
1490 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001491 assert(!err && gpu_count == 1);
1492
1493 for (i = 0; i < device.extensionCount; i++) {
1494 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1495 assert(!err);
1496 }
1497
1498 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1499 assert(!err);
1500
1501 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1502 assert(!err);
1503
1504 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1505 0, &demo->queue);
1506 assert(!err);
1507}
1508
1509static void demo_init_connection(struct demo *demo)
1510{
1511 const xcb_setup_t *setup;
1512 xcb_screen_iterator_t iter;
1513 int scr;
1514
1515 demo->connection = xcb_connect(NULL, &scr);
1516
1517 setup = xcb_get_setup(demo->connection);
1518 iter = xcb_setup_roots_iterator(setup);
1519 while (scr-- > 0)
1520 xcb_screen_next(&iter);
1521
1522 demo->screen = iter.data;
1523}
1524
1525static void demo_init(struct demo *demo)
1526{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001527 vec3 eye = {0.0f, 3.0f, 5.0f};
1528 vec3 origin = {0, 0, 0};
1529 vec3 up = {0.0f, -1.0f, 0.0};
1530
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001531 memset(demo, 0, sizeof(*demo));
1532
1533 demo_init_connection(demo);
1534 demo_init_xgl(demo);
1535
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001536 demo->width = 500;
1537 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001538 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001539
1540 demo->spin_angle = 0.01f;
1541 demo->spin_increment = 0.01f;
1542 demo->pause = false;
1543
1544 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1545 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1546 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001547}
1548
1549static void demo_cleanup(struct demo *demo)
1550{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001551 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001552
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001553 xglDestroyObject(demo->desc_set);
1554 xglDestroyObject(demo->desc_region);
1555
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001556 xglDestroyObject(demo->cmd);
1557
1558 xglDestroyObject(demo->viewport);
1559 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001560 xglDestroyObject(demo->color_blend);
1561 xglDestroyObject(demo->depth_stencil);
1562
1563 xglDestroyObject(demo->pipeline);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001564 xglDestroyObject(demo->desc_layout_fs);
1565 xglDestroyObject(demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001566
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001567// xglFreeMemory(demo->vertices.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001568
1569 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1570 xglDestroyObject(demo->textures[i].view);
1571 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001572 for (j = 0; j < demo->textures[i].num_mem; j++)
1573 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001574 xglDestroyObject(demo->textures[i].sampler);
1575 }
1576
1577 xglDestroyObject(demo->depth.view);
1578 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001579 for (j = 0; j < demo->depth.num_mem; j++)
1580 xglFreeMemory(demo->depth.mem[j]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001581 xglDestroyObject(demo->uniform_data.buf);
1582 for (j = 0; j < demo->uniform_data.num_mem; j++)
1583 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001584
1585 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001586 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001587 xglDestroyObject(demo->buffers[i].view);
1588 xglDestroyObject(demo->buffers[i].image);
1589 }
1590
1591 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001592 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001593
1594 xcb_destroy_window(demo->connection, demo->window);
1595 xcb_disconnect(demo->connection);
1596}
1597
1598int main(void)
1599{
1600 struct demo demo;
1601
1602 demo_init(&demo);
1603
1604 demo_prepare(&demo);
1605 demo_create_window(&demo);
1606 demo_run(&demo);
1607
1608 demo_cleanup(&demo);
1609
1610 return 0;
1611}