blob: 1e961a786631116e478d8b42b5686186d09d84f1 [file] [log] [blame]
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-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 Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -060016#include <unistd.h>
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060017#include <png.h>
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060018
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060019#define DEMO_BUFFER_COUNT 2
20#define DEMO_TEXTURE_COUNT 1
21
Courtney Goeltzenleuchter7e334982014-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 Goeltzenleuchter17223602014-10-29 15:59:49 -060028static char *tex_files[] = {
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060029 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060030};
31
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060032struct xglcube_vs_uniform {
33 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060034 float mvp[4][4];
35 float position[12*3][4];
36 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060037};
38
39struct xgltexcube_vs_uniform {
40 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060041 float mvp[4][4];
42 float position[12*3][4];
43 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060044};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060045
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060046//--------------------------------------------------------------------------------------
47// Mesh and VertexFormat Data
48//--------------------------------------------------------------------------------------
49struct Vertex
50{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060051 float posX, posY, posZ, posW; // Position data
52 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060053};
54
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060055struct VertexPosTex
56{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060057 float posX, posY, posZ, posW; // Position data
58 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060059};
60
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060061#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060062#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060063
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060064static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchtere3342402014-10-28 14:50:30 -0600112};
113
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600114static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600115 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600116 0.0f, 0.0f,
117 0.0f, 1.0f,
118
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600119 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600120 1.0f, 1.0f,
Courtney Goeltzenleuchter3eeff432014-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 Goeltzenleuchter17223602014-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 Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600155 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600156 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600157 1.0f, 0.0f,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600158
159 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600160 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600161 0.0f, 1.0f,
162
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600163 1.0f, 1.0f, // Vertex 10
164 0.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600165 1.0f, 0.0f,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600166
167 1.0f, 0.0f, // Vertex 11
168 0.0f, 0.0f,
169 0.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600192struct demo {
193 xcb_connection_t *connection;
194 xcb_screen_t *screen;
195
Jon Ashburn92e80132015-01-29 15:47:01 -0700196 XGL_INSTANCE inst;
Courtney Goeltzenleuchter4e8246e2014-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 Wu68040a42014-11-07 14:30:34 +0800209 XGL_FENCE fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600210 } buffers[DEMO_BUFFER_COUNT];
211
212 struct {
213 XGL_FORMAT format;
214
215 XGL_IMAGE image;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600216 uint32_t num_mem;
Jon Ashburna9ae3832015-01-16 09:37:43 -0700217 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600218 XGL_DEPTH_STENCIL_VIEW view;
219 } depth;
220
221 struct {
222 XGL_SAMPLER sampler;
223
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600224 char *filename;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600225 XGL_IMAGE image;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600226 uint32_t num_mem;
Jon Ashburna9ae3832015-01-16 09:37:43 -0700227 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600228 XGL_IMAGE_VIEW view;
229 } textures[DEMO_TEXTURE_COUNT];
230
231 struct {
Chia-I Wu714df452015-01-01 07:55:04 +0800232 XGL_BUFFER buf;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600233 uint32_t num_mem;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700234 XGL_GPU_MEMORY *mem;
Chia-I Wu714df452015-01-01 07:55:04 +0800235 XGL_BUFFER_VIEW view;
236 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600237 } uniform_data;
238
Chia-I Wuf8385062015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600242 XGL_PIPELINE pipeline;
243
Tony Barbourfa6cac72015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600248
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600249 mat4x4 projection_matrix;
250 mat4x4 view_matrix;
251 mat4x4 model_matrix;
252
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600253 float spin_angle;
254 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600255 bool pause;
256
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600257 XGL_CMD_BUFFER cmd;
258
Chia-I Wuf8385062015-01-04 16:27:24 +0800259 XGL_DESCRIPTOR_REGION desc_region;
260 XGL_DESCRIPTOR_SET desc_set;
261
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600262 xcb_window_t window;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -0700263 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600264
265 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600266 uint32_t current_buffer;
Courtney Goeltzenleuchter4e8246e2014-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 Stroyan55658c22014-12-04 11:08:39 +0000273 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600274 };
275 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
276 .view = demo->depth.view,
Mike Stroyan55658c22014-12-04 11:08:39 +0000277 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600278 };
Mark Lobodzinskie2d07a52015-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 Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600281 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn3325d6b2015-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 Ashburn3325d6b2015-01-02 18:24:05 -0700285 };
Jon Ashburn53d27af2014-12-31 17:08:35 -0700286 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
287 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700288 .pNext = &graphics_cmd_buf_info,
Jon Ashburn53d27af2014-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600292 XGL_RESULT err;
Jon Ashburn3325d6b2015-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 Lobodzinski71fcc2d2015-01-27 13:24:03 -0600302 .width = demo->width,
303 .height = demo->height,
304 .layers = 1,
Jon Ashburn3325d6b2015-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;
314 rp_info.pColorLoadOps = &load_op;
315 rp_info.pColorStoreOps = &store_op;
316 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
317 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
318 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
319 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
320 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
321 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600322
Jon Ashburn53d27af2014-12-31 17:08:35 -0700323 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600324 assert(!err);
325
326 xglCmdBindPipeline(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
327 demo->pipeline);
328 xglCmdBindDescriptorSet(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuf8385062015-01-04 16:27:24 +0800329 demo->desc_set, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600330
Tony Barbourfa6cac72015-01-16 14:27:35 -0700331 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_VIEWPORT, demo->viewport);
332 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_RASTER, demo->raster);
333 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600334 demo->color_blend);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700335 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600336 demo->depth_stencil);
337
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600338 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
339 clear_range.baseMipLevel = 0;
340 clear_range.mipLevels = 1;
341 clear_range.baseArraySlice = 0;
342 clear_range.arraySize = 1;
343 xglCmdClearColorImage(demo->cmd,
344 demo->buffers[demo->current_buffer].image,
345 clear_color, 1, &clear_range);
346
347 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
348 xglCmdClearDepthStencil(demo->cmd, demo->depth.image,
349 clear_depth, 0, 1, &clear_range);
350
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600351 xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600352
353 err = xglEndCommandBuffer(demo->cmd);
354 assert(!err);
355}
356
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600357
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600358void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600359{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600360 mat4x4 MVP, Model, VP;
361 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600362 uint8_t *pData;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600363 XGL_RESULT err;
364
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600365 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600366
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600367 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600368 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600369 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600370 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600371
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700372 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600373 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600374 assert(!err);
375
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600376 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600377
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700378 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600379 assert(!err);
380}
381
382static void demo_draw(struct demo *demo)
383{
384 const XGL_WSI_X11_PRESENT_INFO present = {
385 .destWindow = demo->window,
386 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wu68040a42014-11-07 14:30:34 +0800387 .async = true,
388 .flip = false,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600389 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800390 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600391 XGL_RESULT err;
392
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600393 demo_draw_build_cmd(demo);
394
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600395 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wu68040a42014-11-07 14:30:34 +0800396 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
397
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600398 err = xglQueueSubmit(demo->queue, 1, &demo->cmd,
399 0, NULL, XGL_NULL_HANDLE);
400 assert(!err);
401
Chia-I Wu68040a42014-11-07 14:30:34 +0800402 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600403 assert(!err);
404
405 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
406}
407
408static void demo_prepare_buffers(struct demo *demo)
409{
410 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
411 .format = demo->format,
412 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
413 .extent = {
414 .width = demo->width,
415 .height = demo->height,
416 },
417 .flags = 0,
418 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800419 const XGL_FENCE_CREATE_INFO fence = {
420 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
421 .pNext = NULL,
422 .flags = 0,
423 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600424 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600425 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600426
427 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
428 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
429 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
430 .pNext = NULL,
431 .format = demo->format,
432 .mipLevel = 0,
433 .baseArraySlice = 0,
434 .arraySize = 1,
435 };
436
437 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
438 &demo->buffers[i].image, &demo->buffers[i].mem);
439 assert(!err);
440
441 color_attachment_view.image = demo->buffers[i].image;
442
443 err = xglCreateColorAttachmentView(demo->device,
444 &color_attachment_view, &demo->buffers[i].view);
445 assert(!err);
Chia-I Wu68040a42014-11-07 14:30:34 +0800446
447 err = xglCreateFence(demo->device,
448 &fence, &demo->buffers[i].fence);
449 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600450 }
451}
452
453static void demo_prepare_depth(struct demo *demo)
454{
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700455 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600456 const XGL_IMAGE_CREATE_INFO image = {
457 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
458 .pNext = NULL,
459 .imageType = XGL_IMAGE_2D,
460 .format = depth_format,
461 .extent = { demo->width, demo->height, 1 },
462 .mipLevels = 1,
463 .arraySize = 1,
464 .samples = 1,
465 .tiling = XGL_OPTIMAL_TILING,
466 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
467 .flags = 0,
468 };
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700469 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
470 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
471 .pNext = NULL,
472 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600473 XGL_MEMORY_ALLOC_INFO mem_alloc = {
474 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700475 .pNext = &img_alloc,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600476 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -0700477 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburn32769172015-01-20 15:06:59 -0700478 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600479 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
480 };
481 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
482 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
483 .pNext = NULL,
484 .image = XGL_NULL_HANDLE,
485 .mipLevel = 0,
486 .baseArraySlice = 0,
487 .arraySize = 1,
488 .flags = 0,
489 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700490 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600491 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700492 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600493 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600494 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600495 uint32_t num_allocations = 0;
496 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600497
498 demo->depth.format = depth_format;
499
500 /* create image */
501 err = xglCreateImage(demo->device, &image,
502 &demo->depth.image);
503 assert(!err);
504
Jon Ashburna9ae3832015-01-16 09:37:43 -0700505
506 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
507 assert(!err && num_alloc_size == sizeof(num_allocations));
508 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
509 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
510 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600511 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700512 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
513 &mem_reqs_size, mem_reqs);
514 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700515 err = xglGetObjectInfo(demo->depth.image,
516 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
517 &img_reqs_size, &img_reqs);
518 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
519 img_alloc.usage = img_reqs.usage;
520 img_alloc.formatClass = img_reqs.formatClass;
521 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600522 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -0700523 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600524
Jon Ashburna9ae3832015-01-16 09:37:43 -0700525 /* allocate memory */
526 err = xglAllocMemory(demo->device, &mem_alloc,
527 &(demo->depth.mem[i]));
528 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600529
Jon Ashburna9ae3832015-01-16 09:37:43 -0700530 /* bind memory */
531 err = xglBindObjectMemory(demo->depth.image, i,
532 demo->depth.mem[i], 0);
533 assert(!err);
534 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600535
536 /* create image view */
537 view.image = demo->depth.image;
538 err = xglCreateDepthStencilView(demo->device, &view,
539 &demo->depth.view);
540 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600541}
542
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600543/** loadTexture
544 * loads a png file into an memory object, using cstdio , libpng.
545 *
546 * \param demo : Needed to access XGL calls
547 * \param filename : the png file to be loaded
548 * \param width : width of png, to be updated as a side effect of this function
549 * \param height : height of png, to be updated as a side effect of this function
550 *
551 * \return bool : an opengl texture id. true if successful?,
552 * should be validated by the client of this function.
553 *
554 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
555 * Modified to copy image to memory
556 *
557 */
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600558bool loadTexture(char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600559 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600560 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600561{
562 //header for testing if it is a png
563 png_byte header[8];
Ian Elliott642f8922015-02-13 14:29:21 -0700564 int is_png, bit_depth, color_type,rowbytes;
565 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600566 png_structp png_ptr;
567 png_infop info_ptr, end_info;
568 png_byte *image_data;
569 png_bytep *row_pointers;
570
571 //open file as binary
572 FILE *fp = fopen(filename, "rb");
573 if (!fp) {
574 return false;
575 }
576
577 //read the header
578 fread(header, 1, 8, fp);
579
580 //test if png
581 is_png = !png_sig_cmp(header, 0, 8);
582 if (!is_png) {
583 fclose(fp);
584 return false;
585 }
586
587 //create png struct
588 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
589 NULL, NULL);
590 if (!png_ptr) {
591 fclose(fp);
592 return (false);
593 }
594
595 //create png info struct
596 info_ptr = png_create_info_struct(png_ptr);
597 if (!info_ptr) {
598 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
599 fclose(fp);
600 return (false);
601 }
602
603 //create png info struct
604 end_info = png_create_info_struct(png_ptr);
605 if (!end_info) {
606 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
607 fclose(fp);
608 return (false);
609 }
610
611 //png error stuff, not sure libpng man suggests this.
612 if (setjmp(png_jmpbuf(png_ptr))) {
613 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
614 fclose(fp);
615 return (false);
616 }
617
618 //init png reading
619 png_init_io(png_ptr, fp);
620
621 //let libpng know you already read the first 8 bytes
622 png_set_sig_bytes(png_ptr, 8);
623
624 // read all the info up to the image data
625 png_read_info(png_ptr, info_ptr);
626
627 // get info about png
628 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
629 NULL, NULL, NULL);
630
631 //update width and height based on png info
632 *width = twidth;
633 *height = theight;
634
635 // Require that incoming texture be 8bits per color component
636 // and 4 components (RGBA).
637 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
638 png_get_channels(png_ptr, info_ptr) != 4) {
639 return false;
640 }
641
642 if (rgba_data == NULL) {
643 // If data pointer is null, we just want the width & height
644 // clean up memory and close stuff
645 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
646 fclose(fp);
647
648 return true;
649 }
650
651 // Update the png info struct.
652 png_read_update_info(png_ptr, info_ptr);
653
654 // Row size in bytes.
655 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
656
657 // Allocate the image_data as a big block, to be given to opengl
658 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
659 if (!image_data) {
660 //clean up memory and close stuff
661 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
662 fclose(fp);
663 return false;
664 }
665
666 // row_pointers is for pointing to image_data for reading the png with libpng
667 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
668 if (!row_pointers) {
669 //clean up memory and close stuff
670 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
671 // delete[] image_data;
672 fclose(fp);
673 return false;
674 }
675 // set the individual row_pointers to point at the correct offsets of image_data
676 for (i = 0; i < theight; ++i)
677 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
678
679 // read the png into image_data through row_pointers
680 png_read_image(png_ptr, row_pointers);
681
682 // clean up memory and close stuff
683 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
684 free(row_pointers);
685 free(image_data);
686 fclose(fp);
687
688 return true;
689}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600690
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600691static void demo_prepare_textures(struct demo *demo)
692{
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700693 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600694 int32_t tex_width;
695 int32_t tex_height;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600696 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600697 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600698
699 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
700 const XGL_SAMPLER_CREATE_INFO sampler = {
701 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
702 .pNext = NULL,
703 .magFilter = XGL_TEX_FILTER_NEAREST,
704 .minFilter = XGL_TEX_FILTER_NEAREST,
705 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600706 .addressU = XGL_TEX_ADDRESS_CLAMP,
707 .addressV = XGL_TEX_ADDRESS_CLAMP,
708 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600709 .mipLodBias = 0.0f,
710 .maxAnisotropy = 0,
711 .compareFunc = XGL_COMPARE_NEVER,
712 .minLod = 0.0f,
713 .maxLod = 0.0f,
714 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
715 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600716
Tony Barbour42869bc2015-02-05 14:14:33 -0700717 err = loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height);
718 assert(err);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600719
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600720 const XGL_IMAGE_CREATE_INFO image = {
721 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
722 .pNext = NULL,
723 .imageType = XGL_IMAGE_2D,
724 .format = tex_format,
725 .extent = { tex_width, tex_height, 1 },
726 .mipLevels = 1,
727 .arraySize = 1,
728 .samples = 1,
729 .tiling = XGL_LINEAR_TILING,
730 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
731 .flags = 0,
732 };
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700733 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
734 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
735 .pNext = NULL,
736 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600737 XGL_MEMORY_ALLOC_INFO mem_alloc = {
738 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700739 .pNext = &img_alloc,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600740 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -0700741 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburn32769172015-01-20 15:06:59 -0700742 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600743 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
744 };
745 XGL_IMAGE_VIEW_CREATE_INFO view = {
746 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
747 .pNext = NULL,
748 .image = XGL_NULL_HANDLE,
749 .viewType = XGL_IMAGE_VIEW_2D,
750 .format = image.format,
751 .channels = { XGL_CHANNEL_SWIZZLE_R,
752 XGL_CHANNEL_SWIZZLE_G,
753 XGL_CHANNEL_SWIZZLE_B,
754 XGL_CHANNEL_SWIZZLE_A, },
755 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
756 .minLod = 0.0f,
757 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700758
759 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600760 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700761 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600762 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
763 uint32_t num_allocations = 0;
764 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600765
766 /* create sampler */
767 err = xglCreateSampler(demo->device, &sampler,
768 &demo->textures[i].sampler);
769 assert(!err);
770
771 /* create image */
772 err = xglCreateImage(demo->device, &image,
773 &demo->textures[i].image);
774 assert(!err);
775
776 err = xglGetObjectInfo(demo->textures[i].image,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700777 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
778 &num_alloc_size, &num_allocations);
779 assert(!err && num_alloc_size == sizeof(num_allocations));
780 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
781 demo->textures[i].mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
782 demo->textures[i].num_mem = num_allocations;
783 err = xglGetObjectInfo(demo->textures[i].image,
784 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
785 &mem_reqs_size, mem_reqs);
786 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700787 err = xglGetObjectInfo(demo->textures[i].image,
788 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
789 &img_reqs_size, &img_reqs);
790 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
791 img_alloc.usage = img_reqs.usage;
792 img_alloc.formatClass = img_reqs.formatClass;
793 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600794 for (uint32_t j = 0; j < num_allocations; j ++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -0700795 mem_alloc.allocationSize = mem_reqs[j].size;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600796
Jon Ashburna9ae3832015-01-16 09:37:43 -0700797 /* allocate memory */
798 err = xglAllocMemory(demo->device, &mem_alloc,
799 &(demo->textures[i].mem[j]));
800 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600801
Jon Ashburna9ae3832015-01-16 09:37:43 -0700802 /* bind memory */
803 err = xglBindObjectMemory(demo->textures[i].image, j,
804 demo->textures[i].mem[j], 0);
805 assert(!err);
806 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600807
808 /* create image view */
809 view.image = demo->textures[i].image;
810 err = xglCreateImageView(demo->device, &view,
811 &demo->textures[i].view);
812 assert(!err);
813 }
814
815 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
816 const XGL_IMAGE_SUBRESOURCE subres = {
817 .aspect = XGL_IMAGE_ASPECT_COLOR,
818 .mipLevel = 0,
819 .arraySlice = 0,
820 };
821 XGL_SUBRESOURCE_LAYOUT layout;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600822 size_t layout_size = sizeof(layout);
823 void *data;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600824
825 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
826 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
827 assert(!err && layout_size == sizeof(layout));
Jon Ashburna9ae3832015-01-16 09:37:43 -0700828 assert(demo->textures[i].num_mem == 1);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600829
Jon Ashburna9ae3832015-01-16 09:37:43 -0700830 err = xglMapMemory(demo->textures[i].mem[0], 0, &data);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600831 assert(!err);
832
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600833 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600834
Jon Ashburna9ae3832015-01-16 09:37:43 -0700835 err = xglUnmapMemory(demo->textures[i].mem[0]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600836 assert(!err);
837 }
838}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600839
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600840void demo_prepare_cube_data_buffer(struct demo *demo)
841{
Chia-I Wu714df452015-01-01 07:55:04 +0800842 XGL_BUFFER_CREATE_INFO buf_info;
843 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700844 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
845 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
846 .pNext = NULL,
847 };
848 XGL_MEMORY_ALLOC_INFO alloc_info = {
849 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
850 .pNext = &buf_alloc,
851 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -0700852 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburn32769172015-01-20 15:06:59 -0700853 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700854 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
855 };
856 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600857 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700858 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600859 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
860 uint32_t num_allocations = 0;
861 size_t num_alloc_size = sizeof(num_allocations);
862 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600863 int i;
864 mat4x4 MVP, VP;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600865 XGL_RESULT err;
866 struct xgltexcube_vs_uniform data;
867
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600868 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600869 mat4x4_mul(MVP, VP, demo->model_matrix);
870 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600871// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600872
873 for (i=0; i<12*3; i++) {
874 data.position[i][0] = g_vertex_buffer_data[i*3];
875 data.position[i][1] = g_vertex_buffer_data[i*3+1];
876 data.position[i][2] = g_vertex_buffer_data[i*3+2];
877 data.position[i][3] = 1.0f;
878 data.attr[i][0] = g_uv_buffer_data[2*i];
879 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
880 data.attr[i][2] = 0;
881 data.attr[i][3] = 0;
882 }
883
Chia-I Wu714df452015-01-01 07:55:04 +0800884 memset(&buf_info, 0, sizeof(buf_info));
885 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
886 buf_info.size = sizeof(data);
887 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
888 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
889 assert(!err);
890
891 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700892 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
893 &num_alloc_size, &num_allocations);
894 assert(!err && num_alloc_size == sizeof(num_allocations));
895 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
896 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
897 demo->uniform_data.num_mem = num_allocations;
898 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu714df452015-01-01 07:55:04 +0800899 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700900 &mem_reqs_size, mem_reqs);
901 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
902 err = xglGetObjectInfo(demo->uniform_data.buf,
903 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
904 &buf_reqs_size, &buf_reqs);
905 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
906 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600907 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700908 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu714df452015-01-01 07:55:04 +0800909
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700910 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
911 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600912
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600913 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700914 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600915
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700916 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600917
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700918 err = xglUnmapMemory(demo->uniform_data.mem[i]);
919 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600920
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700921 err = xglBindObjectMemory(demo->uniform_data.buf, i,
922 demo->uniform_data.mem[i], 0);
923 assert(!err);
924 }
Chia-I Wu714df452015-01-01 07:55:04 +0800925
926 memset(&view_info, 0, sizeof(view_info));
927 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
928 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu28fb4ce2015-01-16 22:31:25 +0800929 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +0800930 view_info.offset = 0;
931 view_info.range = sizeof(data);
932
933 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
934 assert(!err);
935
936 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
937 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600938}
939
Chia-I Wuf8385062015-01-04 16:27:24 +0800940static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600941{
Chia-I Wuf8385062015-01-04 16:27:24 +0800942 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
943 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600944 .pNext = NULL,
Chia-I Wuf8385062015-01-04 16:27:24 +0800945 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
946 .count = 1,
947 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
948 .immutableSampler = XGL_NULL_HANDLE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600949 };
Chia-I Wuf8385062015-01-04 16:27:24 +0800950 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
951 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
952 .pNext = NULL,
953 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
954 .count = DEMO_TEXTURE_COUNT,
955 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
956 .immutableSampler = XGL_NULL_HANDLE,
957 };
958 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600959 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600960
Chia-I Wuf8385062015-01-04 16:27:24 +0800961 err = xglCreateDescriptorSetLayout(demo->device,
962 XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, &bind_point,
963 XGL_NULL_HANDLE, &descriptor_layout_vs,
964 &demo->desc_layout_vs);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600965 assert(!err);
966
Chia-I Wuf8385062015-01-04 16:27:24 +0800967 err = xglCreateDescriptorSetLayout(demo->device,
968 XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, &bind_point,
969 demo->desc_layout_vs, &descriptor_layout_fs,
970 &demo->desc_layout_fs);
971 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600972
Chia-I Wuf8385062015-01-04 16:27:24 +0800973 demo->desc_layout_last = &demo->desc_layout_fs;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600974}
975
976static XGL_SHADER demo_prepare_shader(struct demo *demo,
977 XGL_PIPELINE_SHADER_STAGE stage,
978 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600979 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600980{
981 XGL_SHADER_CREATE_INFO createInfo;
982 XGL_SHADER shader;
983 XGL_RESULT err;
984
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600985
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600986 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
987 createInfo.pNext = NULL;
988
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600989#ifdef EXTERNAL_BIL
990 createInfo.codeSize = size;
991 createInfo.pCode = code;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600992 createInfo.flags = 0;
993
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600994 err = xglCreateShader(demo->device, &createInfo, &shader);
995 if (err) {
996 free((void *) createInfo.pCode);
997 }
998#else
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600999 // Create fake BIL structure to feed GLSL
1000 // to the driver "under the covers"
1001 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1002 createInfo.pCode = malloc(createInfo.codeSize);
1003 createInfo.flags = 0;
1004
1005 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1006 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1007 ((uint32_t *) createInfo.pCode)[1] = 0;
1008 ((uint32_t *) createInfo.pCode)[2] = stage;
1009 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1010
1011 err = xglCreateShader(demo->device, &createInfo, &shader);
1012 if (err) {
1013 free((void *) createInfo.pCode);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001014 return NULL;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001015 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001016#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001017
1018 return shader;
1019}
1020
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001021char *demo_read_bil(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001022{
1023 long int size;
1024 void *shader_code;
1025
1026 FILE *fp = fopen(filename, "rb");
1027 if (!fp) return NULL;
1028
1029 fseek(fp, 0L, SEEK_END);
1030 size = ftell(fp);
1031
1032 fseek(fp, 0L, SEEK_SET);
1033
1034 shader_code = malloc(size);
1035 fread(shader_code, size, 1, fp);
1036
1037 *psize = size;
1038
1039 return shader_code;
1040}
1041
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001042static XGL_SHADER demo_prepare_vs(struct demo *demo)
1043{
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001044#ifdef EXTERNAL_BIL
1045 void *vertShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001046 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001047
1048 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1049
1050 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1051 vertShaderCode, size);
1052#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001053 static const char *vertShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001054 "#version 140\n"
1055 "#extension GL_ARB_separate_shader_objects : enable\n"
1056 "#extension GL_ARB_shading_language_420pack : enable\n"
1057 "\n"
1058 "layout(binding = 0) uniform buf {\n"
1059 " mat4 MVP;\n"
1060 " vec4 position[12*3];\n"
1061 " vec4 attr[12*3];\n"
1062 "} ubuf;\n"
1063 "\n"
1064 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001065 "\n"
1066 "void main() \n"
1067 "{\n"
1068 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001069 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1070 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001071
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001072 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1073 (const void *) vertShaderText,
1074 strlen(vertShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001075#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001076}
1077
1078static XGL_SHADER demo_prepare_fs(struct demo *demo)
1079{
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001080#ifdef EXTERNAL_BIL
1081 void *fragShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001082 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001083
1084 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1085
1086 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1087 fragShaderCode, size);
1088#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001089 static const char *fragShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001090 "#version 140\n"
1091 "#extension GL_ARB_separate_shader_objects : enable\n"
1092 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001093 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001094 "\n"
1095 "layout (location = 0) in vec4 texcoord;\n"
1096 "void main() {\n"
1097 " gl_FragColor = texture(tex, texcoord.xy);\n"
1098 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001099
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001100 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1101 (const void *) fragShaderText,
1102 strlen(fragShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001103#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001104}
1105
1106static void demo_prepare_pipeline(struct demo *demo)
1107{
1108 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001109 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1110 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001111 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1112 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001113 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1114 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001115 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1116 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001117 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001118
1119 memset(&pipeline, 0, sizeof(pipeline));
1120 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wuf8385062015-01-04 16:27:24 +08001121 pipeline.lastSetLayout = *demo->desc_layout_last;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001122
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001123 memset(&ia, 0, sizeof(ia));
1124 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1125 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1126
1127 memset(&rs, 0, sizeof(rs));
1128 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001129 rs.fillMode = XGL_FILL_SOLID;
1130 rs.cullMode = XGL_CULL_NONE;
1131 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001132
1133 memset(&cb, 0, sizeof(cb));
1134 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001135 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1136 memset(att_state, 0, sizeof(att_state));
1137 att_state[0].format = demo->format;
1138 att_state[0].channelWriteMask = 0xf;
1139 att_state[0].blendEnable = XGL_FALSE;
1140 cb.attachmentCount = 1;
1141 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001142
Tony Barbourfa6cac72015-01-16 14:27:35 -07001143 memset(&vp, 0, sizeof(vp));
1144 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001145
1146 memset(&ds, 0, sizeof(ds));
1147 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1148 ds.format = demo->depth.format;
1149 ds.depthTestEnable = XGL_TRUE;
1150 ds.depthWriteEnable = XGL_TRUE;
1151 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1152 ds.depthBoundsEnable = XGL_FALSE;
1153 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1154 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1155 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1156 ds.stencilTestEnable = XGL_FALSE;
1157 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001158
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001159 memset(&vs, 0, sizeof(vs));
1160 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1161 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001162 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001163 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001164
1165 memset(&fs, 0, sizeof(fs));
1166 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1167 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1168 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001169 assert(fs.shader.shader != NULL);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001170
1171 memset(&ms, 0, sizeof(ms));
1172 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1173 ms.sampleMask = 1;
1174 ms.multisampleEnable = XGL_FALSE;
1175 ms.samples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001176
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001177 pipeline.pNext = (const void *) &ia;
1178 ia.pNext = (const void *) &rs;
1179 rs.pNext = (const void *) &cb;
1180 cb.pNext = (const void *) &ms;
1181 ms.pNext = (const void *) &vp;
1182 vp.pNext = (const void *) &ds;
1183 ds.pNext = (const void *) &vs;
1184 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001185
1186 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1187 assert(!err);
1188
1189 xglDestroyObject(vs.shader.shader);
1190 xglDestroyObject(fs.shader.shader);
1191}
1192
1193static void demo_prepare_dynamic_states(struct demo *demo)
1194{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001195 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1196 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1197 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1198 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001199 XGL_RESULT err;
1200
Tony Barbourfa6cac72015-01-16 14:27:35 -07001201 memset(&viewport_create, 0, sizeof(viewport_create));
1202 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001203 viewport_create.viewportAndScissorCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001204 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001205 XGL_RECT scissor;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001206 viewport.height = (float) demo->height;
1207 viewport.width = (float) demo->width;
1208 viewport.minDepth = (float) 0.0f;
1209 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001210 scissor.extent.width = demo->width;
1211 scissor.extent.height = demo->height;
1212 scissor.offset.x = 0;
1213 scissor.offset.y = 0;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001214 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001215 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001216
1217 memset(&raster, 0, sizeof(raster));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001218 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001219
1220 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001221 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001222
1223 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001224 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1225 depth_stencil.stencilBackRef = 0;
1226 depth_stencil.stencilFrontRef = 0;
1227 depth_stencil.stencilReadMask = 0xff;
1228 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001229
Tony Barbourfa6cac72015-01-16 14:27:35 -07001230 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001231 assert(!err);
1232
Tony Barbourfa6cac72015-01-16 14:27:35 -07001233 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001234 assert(!err);
1235
Tony Barbourfa6cac72015-01-16 14:27:35 -07001236 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001237 &color_blend, &demo->color_blend);
1238 assert(!err);
1239
Tony Barbourfa6cac72015-01-16 14:27:35 -07001240 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001241 &depth_stencil, &demo->depth_stencil);
1242 assert(!err);
1243}
1244
Chia-I Wuf8385062015-01-04 16:27:24 +08001245static void demo_prepare_descriptor_region(struct demo *demo)
1246{
1247 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1248 [0] = {
1249 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1250 .count = 1,
1251 },
1252 [1] = {
1253 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1254 .count = DEMO_TEXTURE_COUNT,
1255 },
1256 };
1257 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1258 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1259 .pNext = NULL,
1260 .count = 2,
1261 .pTypeCount = type_counts,
1262 };
1263 XGL_RESULT err;
1264
1265 err = xglCreateDescriptorRegion(demo->device,
1266 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1267 &descriptor_region, &demo->desc_region);
1268 assert(!err);
1269}
1270
1271static void demo_prepare_descriptor_set(struct demo *demo)
1272{
1273 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1274 &demo->uniform_data.attach;
1275 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1276 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1277 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1278 XGL_UPDATE_BUFFERS update_vs;
1279 XGL_RESULT err;
1280 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001281 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001282
1283 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1284 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1285 view_info[i].pNext = NULL;
1286 view_info[i].view = demo->textures[i].view,
1287 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1288
1289 combined_info[i].pSampler = demo->textures[i].sampler;
1290 combined_info[i].pImageView = &view_info[i];
1291 }
1292
1293 memset(&update_vs, 0, sizeof(update_vs));
1294 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1295 update_vs.pNext = &update_fs;
1296 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1297 update_vs.count = 1;
1298 update_vs.pBufferViews = &view_info_vs;
1299
1300 memset(&update_fs, 0, sizeof(update_fs));
1301 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1302 update_fs.index = 1;
1303 update_fs.count = DEMO_TEXTURE_COUNT;
1304 update_fs.pSamplerImageViews = combined_info;
1305
1306 err = xglAllocDescriptorSets(demo->desc_region,
1307 XGL_DESCRIPTOR_SET_USAGE_STATIC,
1308 1, demo->desc_layout_last,
1309 &demo->desc_set, &count);
1310 assert(!err && count == 1);
1311
1312 xglBeginDescriptorRegionUpdate(demo->device,
1313 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1314
1315 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1316 xglUpdateDescriptors(demo->desc_set, &update_vs);
1317
1318 xglEndDescriptorRegionUpdate(demo->device, demo->cmd);
1319}
1320
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001321static void demo_prepare(struct demo *demo)
1322{
1323 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1324 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1325 .pNext = NULL,
1326 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1327 .flags = 0,
1328 };
1329 XGL_RESULT err;
1330
1331 demo_prepare_buffers(demo);
1332 demo_prepare_depth(demo);
1333 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001334 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001335
Chia-I Wuf8385062015-01-04 16:27:24 +08001336 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001337 demo_prepare_pipeline(demo);
1338 demo_prepare_dynamic_states(demo);
1339
1340 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
1341 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001342
1343 demo_prepare_descriptor_region(demo);
1344 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001345}
1346
1347static void demo_handle_event(struct demo *demo,
1348 const xcb_generic_event_t *event)
1349{
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001350 u_int8_t event_code = event->response_type & 0x7f;
1351 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001352 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001353 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001354 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001355 case XCB_CLIENT_MESSAGE:
1356 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1357 (*demo->atom_wm_delete_window).atom) {
1358 demo->quit = true;
1359 }
1360 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001361 case XCB_KEY_RELEASE:
1362 {
1363 const xcb_key_release_event_t *key =
1364 (const xcb_key_release_event_t *) event;
1365
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001366 switch (key->detail) {
1367 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001368 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001369 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001370 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001371 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001372 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001373 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001374 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001375 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001376 case 0x41:
1377 demo->pause = !demo->pause;
1378 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001379 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001380 }
1381 break;
1382 default:
1383 break;
1384 }
1385}
1386
1387static void demo_run(struct demo *demo)
1388{
1389 xcb_flush(demo->connection);
1390
1391 while (!demo->quit) {
1392 xcb_generic_event_t *event;
1393
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001394 if (demo->pause) {
1395 event = xcb_wait_for_event(demo->connection);
1396 } else {
1397 event = xcb_poll_for_event(demo->connection);
1398 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001399 if (event) {
1400 demo_handle_event(demo, event);
1401 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001402 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001403
1404 // Wait for work to finish before updating MVP.
1405 xglDeviceWaitIdle(demo->device);
1406 demo_update_data_buffer(demo);
1407
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001408 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001409
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001410 // Wait for work to finish before updating MVP.
1411 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001412 }
1413}
1414
1415static void demo_create_window(struct demo *demo)
1416{
1417 uint32_t value_mask, value_list[32];
1418
1419 demo->window = xcb_generate_id(demo->connection);
1420
1421 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1422 value_list[0] = demo->screen->black_pixel;
1423 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1424 XCB_EVENT_MASK_EXPOSURE;
1425
1426 xcb_create_window(demo->connection,
1427 XCB_COPY_FROM_PARENT,
1428 demo->window, demo->screen->root,
1429 0, 0, demo->width, demo->height, 0,
1430 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1431 demo->screen->root_visual,
1432 value_mask, value_list);
1433
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001434 /* Magic code that will send notification when window is destroyed */
1435 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1436 "WM_PROTOCOLS");
1437 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1438
1439 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1440 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1441
1442 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1443 demo->window, (*reply).atom, 4, 32, 1,
1444 &(*demo->atom_wm_delete_window).atom);
1445 free(reply);
1446
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001447 xcb_map_window(demo->connection, demo->window);
1448}
1449
1450static void demo_init_xgl(struct demo *demo)
1451{
1452 const XGL_APPLICATION_INFO app = {
1453 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1454 .pNext = NULL,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001455 .pAppName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001456 .appVersion = 0,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001457 .pEngineName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001458 .engineVersion = 0,
1459 .apiVersion = XGL_MAKE_VERSION(0, 22, 0),
1460 };
1461 const XGL_WSI_X11_CONNECTION_INFO connection = {
1462 .pConnection = demo->connection,
1463 .root = demo->screen->root,
1464 .provider = 0,
1465 };
1466 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1467 .queueNodeIndex = 0,
1468 .queueCount = 1,
1469 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001470 const char *ext_names[] = {
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001471 "XGL_WSI_X11",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001472 };
1473 const XGL_DEVICE_CREATE_INFO device = {
1474 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1475 .pNext = NULL,
1476 .queueRecordCount = 1,
1477 .pRequestedQueues = &queue,
1478 .extensionCount = 1,
1479 .ppEnabledExtensionNames = ext_names,
1480 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1481 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1482 };
1483 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001484 uint32_t gpu_count;
1485 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001486
Jon Ashburn92e80132015-01-29 15:47:01 -07001487 err = xglCreateInstance(&app, NULL, &demo->inst);
1488 assert(!err);
1489 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001490 assert(!err && gpu_count == 1);
1491
1492 for (i = 0; i < device.extensionCount; i++) {
1493 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1494 assert(!err);
1495 }
1496
1497 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1498 assert(!err);
1499
1500 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1501 assert(!err);
1502
1503 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1504 0, &demo->queue);
1505 assert(!err);
1506}
1507
1508static void demo_init_connection(struct demo *demo)
1509{
1510 const xcb_setup_t *setup;
1511 xcb_screen_iterator_t iter;
1512 int scr;
1513
1514 demo->connection = xcb_connect(NULL, &scr);
1515
1516 setup = xcb_get_setup(demo->connection);
1517 iter = xcb_setup_roots_iterator(setup);
1518 while (scr-- > 0)
1519 xcb_screen_next(&iter);
1520
1521 demo->screen = iter.data;
1522}
1523
1524static void demo_init(struct demo *demo)
1525{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001526 vec3 eye = {0.0f, 3.0f, 5.0f};
1527 vec3 origin = {0, 0, 0};
1528 vec3 up = {0.0f, -1.0f, 0.0};
1529
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001530 memset(demo, 0, sizeof(*demo));
1531
1532 demo_init_connection(demo);
1533 demo_init_xgl(demo);
1534
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001535 demo->width = 500;
1536 demo->height = 500;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001537 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001538
1539 demo->spin_angle = 0.01f;
1540 demo->spin_increment = 0.01f;
1541 demo->pause = false;
1542
1543 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1544 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1545 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001546}
1547
1548static void demo_cleanup(struct demo *demo)
1549{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001550 uint32_t i, j;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001551
Chia-I Wuf8385062015-01-04 16:27:24 +08001552 xglDestroyObject(demo->desc_set);
1553 xglDestroyObject(demo->desc_region);
1554
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001555 xglDestroyObject(demo->cmd);
1556
1557 xglDestroyObject(demo->viewport);
1558 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001559 xglDestroyObject(demo->color_blend);
1560 xglDestroyObject(demo->depth_stencil);
1561
1562 xglDestroyObject(demo->pipeline);
Chia-I Wuf8385062015-01-04 16:27:24 +08001563 xglDestroyObject(demo->desc_layout_fs);
1564 xglDestroyObject(demo->desc_layout_vs);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001565
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001566// xglFreeMemory(demo->vertices.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001567
1568 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1569 xglDestroyObject(demo->textures[i].view);
1570 xglDestroyObject(demo->textures[i].image);
Jon Ashburna9ae3832015-01-16 09:37:43 -07001571 for (j = 0; j < demo->textures[i].num_mem; j++)
1572 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001573 xglDestroyObject(demo->textures[i].sampler);
1574 }
1575
1576 xglDestroyObject(demo->depth.view);
1577 xglDestroyObject(demo->depth.image);
Jon Ashburna9ae3832015-01-16 09:37:43 -07001578 for (j = 0; j < demo->depth.num_mem; j++)
1579 xglFreeMemory(demo->depth.mem[j]);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001580 xglDestroyObject(demo->uniform_data.buf);
1581 for (j = 0; j < demo->uniform_data.num_mem; j++)
1582 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001583
1584 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wu68040a42014-11-07 14:30:34 +08001585 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001586 xglDestroyObject(demo->buffers[i].view);
1587 xglDestroyObject(demo->buffers[i].image);
1588 }
1589
1590 xglDestroyDevice(demo->device);
Jon Ashburn92e80132015-01-29 15:47:01 -07001591 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001592
1593 xcb_destroy_window(demo->connection, demo->window);
1594 xcb_disconnect(demo->connection);
1595}
1596
1597int main(void)
1598{
1599 struct demo demo;
1600
1601 demo_init(&demo);
1602
1603 demo_prepare(&demo);
1604 demo_create_window(&demo);
1605 demo_run(&demo);
1606
1607 demo_cleanup(&demo);
1608
1609 return 0;
1610}