blob: 79aec2e87495d8256bda46c39a879e2307aee9e6 [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;
Courtney Goeltzenleuchterb21fe902015-02-10 14:06:25 -0700314 rp_info.colorAttachmentCount = 1;
Jon Ashburn3325d6b2015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600323
Jon Ashburn53d27af2014-12-31 17:08:35 -0700324 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-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 Wuf8385062015-01-04 16:27:24 +0800330 demo->desc_set, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600331
Tony Barbourfa6cac72015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600335 demo->color_blend);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700336 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600337 demo->depth_stencil);
338
Courtney Goeltzenleuchter6f88d4c2014-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 Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600352 xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600353
354 err = xglEndCommandBuffer(demo->cmd);
355 assert(!err);
356}
357
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600358
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600359void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600360{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600361 mat4x4 MVP, Model, VP;
362 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600363 uint8_t *pData;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600364 XGL_RESULT err;
365
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600366 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600367
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600368 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600369 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600370 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600371 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600372
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700373 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600374 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600375 assert(!err);
376
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600377 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600378
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700379 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter4e8246e2014-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 Wu68040a42014-11-07 14:30:34 +0800388 .async = true,
389 .flip = false,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600390 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800391 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600392 XGL_RESULT err;
393
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600394 demo_draw_build_cmd(demo);
395
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600396 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wu68040a42014-11-07 14:30:34 +0800397 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
398
Mark Lobodzinski15427102015-02-18 16:38:17 -0600399 uint32_t i, idx = 0;
400 XGL_MEMORY_REF *memRefs;
401 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
402 demo->depth.num_mem +
403 demo->textures[0].num_mem +
404 demo->uniform_data.num_mem));
405 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
406 memRefs[idx].mem = demo->depth.mem[i];
407 memRefs[idx].flags = 0;
408 }
409 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
410 memRefs[idx].mem = demo->textures[0].mem[i];
411 memRefs[idx].flags = 0;
412 }
413 memRefs[idx].mem = demo->buffers[0].mem;
414 memRefs[idx++].flags = 0;
415 memRefs[idx].mem = demo->buffers[1].mem;
416 memRefs[idx++].flags = 0;
417 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
418 memRefs[idx].mem = demo->uniform_data.mem[i];
419 memRefs[idx].flags = 0;
420 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600421 err = xglQueueSubmit(demo->queue, 1, &demo->cmd,
Mark Lobodzinski15427102015-02-18 16:38:17 -0600422 idx, memRefs, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600423 assert(!err);
424
Chia-I Wu68040a42014-11-07 14:30:34 +0800425 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600426 assert(!err);
427
428 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
429}
430
431static void demo_prepare_buffers(struct demo *demo)
432{
433 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
434 .format = demo->format,
435 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
436 .extent = {
437 .width = demo->width,
438 .height = demo->height,
439 },
440 .flags = 0,
441 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800442 const XGL_FENCE_CREATE_INFO fence = {
443 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
444 .pNext = NULL,
445 .flags = 0,
446 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600447 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600448 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600449
450 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
451 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
452 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
453 .pNext = NULL,
454 .format = demo->format,
455 .mipLevel = 0,
456 .baseArraySlice = 0,
457 .arraySize = 1,
458 };
459
460 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
461 &demo->buffers[i].image, &demo->buffers[i].mem);
462 assert(!err);
463
464 color_attachment_view.image = demo->buffers[i].image;
465
466 err = xglCreateColorAttachmentView(demo->device,
467 &color_attachment_view, &demo->buffers[i].view);
468 assert(!err);
Chia-I Wu68040a42014-11-07 14:30:34 +0800469
470 err = xglCreateFence(demo->device,
471 &fence, &demo->buffers[i].fence);
472 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600473 }
474}
475
476static void demo_prepare_depth(struct demo *demo)
477{
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700478 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600479 const XGL_IMAGE_CREATE_INFO image = {
480 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
481 .pNext = NULL,
482 .imageType = XGL_IMAGE_2D,
483 .format = depth_format,
484 .extent = { demo->width, demo->height, 1 },
485 .mipLevels = 1,
486 .arraySize = 1,
487 .samples = 1,
488 .tiling = XGL_OPTIMAL_TILING,
489 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
490 .flags = 0,
491 };
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700492 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
493 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
494 .pNext = NULL,
495 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600496 XGL_MEMORY_ALLOC_INFO mem_alloc = {
497 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700498 .pNext = &img_alloc,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600499 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -0700500 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburn32769172015-01-20 15:06:59 -0700501 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600502 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
503 };
504 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
505 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
506 .pNext = NULL,
507 .image = XGL_NULL_HANDLE,
508 .mipLevel = 0,
509 .baseArraySlice = 0,
510 .arraySize = 1,
511 .flags = 0,
512 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700513 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600514 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700515 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600516 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600517 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600518 uint32_t num_allocations = 0;
519 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600520
521 demo->depth.format = depth_format;
522
523 /* create image */
524 err = xglCreateImage(demo->device, &image,
525 &demo->depth.image);
526 assert(!err);
527
Jon Ashburna9ae3832015-01-16 09:37:43 -0700528
529 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
530 assert(!err && num_alloc_size == sizeof(num_allocations));
531 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
532 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
533 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600534 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700535 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
536 &mem_reqs_size, mem_reqs);
537 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700538 err = xglGetObjectInfo(demo->depth.image,
539 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
540 &img_reqs_size, &img_reqs);
541 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
542 img_alloc.usage = img_reqs.usage;
543 img_alloc.formatClass = img_reqs.formatClass;
544 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600545 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -0700546 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600547
Jon Ashburna9ae3832015-01-16 09:37:43 -0700548 /* allocate memory */
549 err = xglAllocMemory(demo->device, &mem_alloc,
550 &(demo->depth.mem[i]));
551 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600552
Jon Ashburna9ae3832015-01-16 09:37:43 -0700553 /* bind memory */
554 err = xglBindObjectMemory(demo->depth.image, i,
555 demo->depth.mem[i], 0);
556 assert(!err);
557 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600558
559 /* create image view */
560 view.image = demo->depth.image;
561 err = xglCreateDepthStencilView(demo->device, &view,
562 &demo->depth.view);
563 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600564}
565
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600566/** loadTexture
567 * loads a png file into an memory object, using cstdio , libpng.
568 *
569 * \param demo : Needed to access XGL calls
570 * \param filename : the png file to be loaded
571 * \param width : width of png, to be updated as a side effect of this function
572 * \param height : height of png, to be updated as a side effect of this function
573 *
574 * \return bool : an opengl texture id. true if successful?,
575 * should be validated by the client of this function.
576 *
577 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
578 * Modified to copy image to memory
579 *
580 */
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600581bool loadTexture(char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600582 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600583 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600584{
585 //header for testing if it is a png
586 png_byte header[8];
Ian Elliott642f8922015-02-13 14:29:21 -0700587 int is_png, bit_depth, color_type,rowbytes;
588 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600589 png_structp png_ptr;
590 png_infop info_ptr, end_info;
591 png_byte *image_data;
592 png_bytep *row_pointers;
593
594 //open file as binary
595 FILE *fp = fopen(filename, "rb");
596 if (!fp) {
597 return false;
598 }
599
600 //read the header
601 fread(header, 1, 8, fp);
602
603 //test if png
604 is_png = !png_sig_cmp(header, 0, 8);
605 if (!is_png) {
606 fclose(fp);
607 return false;
608 }
609
610 //create png struct
611 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
612 NULL, NULL);
613 if (!png_ptr) {
614 fclose(fp);
615 return (false);
616 }
617
618 //create png info struct
619 info_ptr = png_create_info_struct(png_ptr);
620 if (!info_ptr) {
621 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
622 fclose(fp);
623 return (false);
624 }
625
626 //create png info struct
627 end_info = png_create_info_struct(png_ptr);
628 if (!end_info) {
629 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
630 fclose(fp);
631 return (false);
632 }
633
634 //png error stuff, not sure libpng man suggests this.
635 if (setjmp(png_jmpbuf(png_ptr))) {
636 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
637 fclose(fp);
638 return (false);
639 }
640
641 //init png reading
642 png_init_io(png_ptr, fp);
643
644 //let libpng know you already read the first 8 bytes
645 png_set_sig_bytes(png_ptr, 8);
646
647 // read all the info up to the image data
648 png_read_info(png_ptr, info_ptr);
649
650 // get info about png
651 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
652 NULL, NULL, NULL);
653
654 //update width and height based on png info
655 *width = twidth;
656 *height = theight;
657
658 // Require that incoming texture be 8bits per color component
659 // and 4 components (RGBA).
660 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
661 png_get_channels(png_ptr, info_ptr) != 4) {
662 return false;
663 }
664
665 if (rgba_data == NULL) {
666 // If data pointer is null, we just want the width & height
667 // clean up memory and close stuff
668 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
669 fclose(fp);
670
671 return true;
672 }
673
674 // Update the png info struct.
675 png_read_update_info(png_ptr, info_ptr);
676
677 // Row size in bytes.
678 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
679
680 // Allocate the image_data as a big block, to be given to opengl
681 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
682 if (!image_data) {
683 //clean up memory and close stuff
684 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
685 fclose(fp);
686 return false;
687 }
688
689 // row_pointers is for pointing to image_data for reading the png with libpng
690 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
691 if (!row_pointers) {
692 //clean up memory and close stuff
693 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
694 // delete[] image_data;
695 fclose(fp);
696 return false;
697 }
698 // set the individual row_pointers to point at the correct offsets of image_data
699 for (i = 0; i < theight; ++i)
700 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
701
702 // read the png into image_data through row_pointers
703 png_read_image(png_ptr, row_pointers);
704
705 // clean up memory and close stuff
706 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
707 free(row_pointers);
708 free(image_data);
709 fclose(fp);
710
711 return true;
712}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600713
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600714static void demo_prepare_textures(struct demo *demo)
715{
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700716 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600717 int32_t tex_width;
718 int32_t tex_height;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600719 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600720 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600721
722 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
723 const XGL_SAMPLER_CREATE_INFO sampler = {
724 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
725 .pNext = NULL,
726 .magFilter = XGL_TEX_FILTER_NEAREST,
727 .minFilter = XGL_TEX_FILTER_NEAREST,
728 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600729 .addressU = XGL_TEX_ADDRESS_CLAMP,
730 .addressV = XGL_TEX_ADDRESS_CLAMP,
731 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600732 .mipLodBias = 0.0f,
733 .maxAnisotropy = 0,
734 .compareFunc = XGL_COMPARE_NEVER,
735 .minLod = 0.0f,
736 .maxLod = 0.0f,
737 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
738 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600739
Tony Barbour42869bc2015-02-05 14:14:33 -0700740 err = loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height);
741 assert(err);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600742
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600743 const XGL_IMAGE_CREATE_INFO image = {
744 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
745 .pNext = NULL,
746 .imageType = XGL_IMAGE_2D,
747 .format = tex_format,
748 .extent = { tex_width, tex_height, 1 },
749 .mipLevels = 1,
750 .arraySize = 1,
751 .samples = 1,
752 .tiling = XGL_LINEAR_TILING,
753 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
754 .flags = 0,
755 };
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700756 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
757 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
758 .pNext = NULL,
759 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600760 XGL_MEMORY_ALLOC_INFO mem_alloc = {
761 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700762 .pNext = &img_alloc,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600763 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -0700764 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburn32769172015-01-20 15:06:59 -0700765 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600766 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
767 };
768 XGL_IMAGE_VIEW_CREATE_INFO view = {
769 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
770 .pNext = NULL,
771 .image = XGL_NULL_HANDLE,
772 .viewType = XGL_IMAGE_VIEW_2D,
773 .format = image.format,
774 .channels = { XGL_CHANNEL_SWIZZLE_R,
775 XGL_CHANNEL_SWIZZLE_G,
776 XGL_CHANNEL_SWIZZLE_B,
777 XGL_CHANNEL_SWIZZLE_A, },
778 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
779 .minLod = 0.0f,
780 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700781
782 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600783 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700784 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600785 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
786 uint32_t num_allocations = 0;
787 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600788
789 /* create sampler */
790 err = xglCreateSampler(demo->device, &sampler,
791 &demo->textures[i].sampler);
792 assert(!err);
793
794 /* create image */
795 err = xglCreateImage(demo->device, &image,
796 &demo->textures[i].image);
797 assert(!err);
798
799 err = xglGetObjectInfo(demo->textures[i].image,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700800 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
801 &num_alloc_size, &num_allocations);
802 assert(!err && num_alloc_size == sizeof(num_allocations));
803 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
804 demo->textures[i].mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
805 demo->textures[i].num_mem = num_allocations;
806 err = xglGetObjectInfo(demo->textures[i].image,
807 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
808 &mem_reqs_size, mem_reqs);
809 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700810 err = xglGetObjectInfo(demo->textures[i].image,
811 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
812 &img_reqs_size, &img_reqs);
813 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
814 img_alloc.usage = img_reqs.usage;
815 img_alloc.formatClass = img_reqs.formatClass;
816 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600817 for (uint32_t j = 0; j < num_allocations; j ++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -0700818 mem_alloc.allocationSize = mem_reqs[j].size;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600819
Jon Ashburna9ae3832015-01-16 09:37:43 -0700820 /* allocate memory */
821 err = xglAllocMemory(demo->device, &mem_alloc,
822 &(demo->textures[i].mem[j]));
823 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600824
Jon Ashburna9ae3832015-01-16 09:37:43 -0700825 /* bind memory */
826 err = xglBindObjectMemory(demo->textures[i].image, j,
827 demo->textures[i].mem[j], 0);
828 assert(!err);
829 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600830
831 /* create image view */
832 view.image = demo->textures[i].image;
833 err = xglCreateImageView(demo->device, &view,
834 &demo->textures[i].view);
835 assert(!err);
836 }
837
838 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
839 const XGL_IMAGE_SUBRESOURCE subres = {
840 .aspect = XGL_IMAGE_ASPECT_COLOR,
841 .mipLevel = 0,
842 .arraySlice = 0,
843 };
844 XGL_SUBRESOURCE_LAYOUT layout;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600845 size_t layout_size = sizeof(layout);
846 void *data;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600847
848 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
849 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
850 assert(!err && layout_size == sizeof(layout));
Jon Ashburna9ae3832015-01-16 09:37:43 -0700851 assert(demo->textures[i].num_mem == 1);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600852
Jon Ashburna9ae3832015-01-16 09:37:43 -0700853 err = xglMapMemory(demo->textures[i].mem[0], 0, &data);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600854 assert(!err);
855
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600856 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600857
Jon Ashburna9ae3832015-01-16 09:37:43 -0700858 err = xglUnmapMemory(demo->textures[i].mem[0]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600859 assert(!err);
860 }
861}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600862
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600863void demo_prepare_cube_data_buffer(struct demo *demo)
864{
Chia-I Wu714df452015-01-01 07:55:04 +0800865 XGL_BUFFER_CREATE_INFO buf_info;
866 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700867 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
868 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
869 .pNext = NULL,
870 };
871 XGL_MEMORY_ALLOC_INFO alloc_info = {
872 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
873 .pNext = &buf_alloc,
874 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -0700875 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburn32769172015-01-20 15:06:59 -0700876 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700877 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
878 };
879 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600880 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700881 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600882 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
883 uint32_t num_allocations = 0;
884 size_t num_alloc_size = sizeof(num_allocations);
885 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600886 int i;
887 mat4x4 MVP, VP;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600888 XGL_RESULT err;
889 struct xgltexcube_vs_uniform data;
890
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600891 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600892 mat4x4_mul(MVP, VP, demo->model_matrix);
893 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600894// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600895
896 for (i=0; i<12*3; i++) {
897 data.position[i][0] = g_vertex_buffer_data[i*3];
898 data.position[i][1] = g_vertex_buffer_data[i*3+1];
899 data.position[i][2] = g_vertex_buffer_data[i*3+2];
900 data.position[i][3] = 1.0f;
901 data.attr[i][0] = g_uv_buffer_data[2*i];
902 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
903 data.attr[i][2] = 0;
904 data.attr[i][3] = 0;
905 }
906
Chia-I Wu714df452015-01-01 07:55:04 +0800907 memset(&buf_info, 0, sizeof(buf_info));
908 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
909 buf_info.size = sizeof(data);
910 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
911 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
912 assert(!err);
913
914 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700915 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
916 &num_alloc_size, &num_allocations);
917 assert(!err && num_alloc_size == sizeof(num_allocations));
918 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
919 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
920 demo->uniform_data.num_mem = num_allocations;
921 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu714df452015-01-01 07:55:04 +0800922 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700923 &mem_reqs_size, mem_reqs);
924 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
925 err = xglGetObjectInfo(demo->uniform_data.buf,
926 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
927 &buf_reqs_size, &buf_reqs);
928 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
929 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600930 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700931 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu714df452015-01-01 07:55:04 +0800932
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700933 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
934 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600935
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600936 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700937 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600938
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700939 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600940
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700941 err = xglUnmapMemory(demo->uniform_data.mem[i]);
942 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600943
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700944 err = xglBindObjectMemory(demo->uniform_data.buf, i,
945 demo->uniform_data.mem[i], 0);
946 assert(!err);
947 }
Chia-I Wu714df452015-01-01 07:55:04 +0800948
949 memset(&view_info, 0, sizeof(view_info));
950 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
951 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu28fb4ce2015-01-16 22:31:25 +0800952 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +0800953 view_info.offset = 0;
954 view_info.range = sizeof(data);
955
956 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
957 assert(!err);
958
959 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
960 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600961}
962
Chia-I Wuf8385062015-01-04 16:27:24 +0800963static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600964{
Chia-I Wuf8385062015-01-04 16:27:24 +0800965 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
966 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600967 .pNext = NULL,
Chia-I Wuf8385062015-01-04 16:27:24 +0800968 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
969 .count = 1,
970 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
971 .immutableSampler = XGL_NULL_HANDLE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600972 };
Chia-I Wuf8385062015-01-04 16:27:24 +0800973 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
974 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
975 .pNext = NULL,
976 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
977 .count = DEMO_TEXTURE_COUNT,
978 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
979 .immutableSampler = XGL_NULL_HANDLE,
980 };
981 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600982 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600983
Chia-I Wuf8385062015-01-04 16:27:24 +0800984 err = xglCreateDescriptorSetLayout(demo->device,
985 XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, &bind_point,
986 XGL_NULL_HANDLE, &descriptor_layout_vs,
987 &demo->desc_layout_vs);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600988 assert(!err);
989
Chia-I Wuf8385062015-01-04 16:27:24 +0800990 err = xglCreateDescriptorSetLayout(demo->device,
991 XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, &bind_point,
992 demo->desc_layout_vs, &descriptor_layout_fs,
993 &demo->desc_layout_fs);
994 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600995
Chia-I Wuf8385062015-01-04 16:27:24 +0800996 demo->desc_layout_last = &demo->desc_layout_fs;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600997}
998
999static XGL_SHADER demo_prepare_shader(struct demo *demo,
1000 XGL_PIPELINE_SHADER_STAGE stage,
1001 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001002 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001003{
1004 XGL_SHADER_CREATE_INFO createInfo;
1005 XGL_SHADER shader;
1006 XGL_RESULT err;
1007
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001008
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001009 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1010 createInfo.pNext = NULL;
1011
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001012#ifdef EXTERNAL_BIL
1013 createInfo.codeSize = size;
1014 createInfo.pCode = code;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001015 createInfo.flags = 0;
1016
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001017 err = xglCreateShader(demo->device, &createInfo, &shader);
1018 if (err) {
1019 free((void *) createInfo.pCode);
1020 }
1021#else
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001022 // Create fake BIL structure to feed GLSL
1023 // to the driver "under the covers"
1024 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1025 createInfo.pCode = malloc(createInfo.codeSize);
1026 createInfo.flags = 0;
1027
1028 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1029 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1030 ((uint32_t *) createInfo.pCode)[1] = 0;
1031 ((uint32_t *) createInfo.pCode)[2] = stage;
1032 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1033
1034 err = xglCreateShader(demo->device, &createInfo, &shader);
1035 if (err) {
1036 free((void *) createInfo.pCode);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001037 return NULL;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001038 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001039#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001040
1041 return shader;
1042}
1043
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001044char *demo_read_bil(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001045{
1046 long int size;
1047 void *shader_code;
1048
1049 FILE *fp = fopen(filename, "rb");
1050 if (!fp) return NULL;
1051
1052 fseek(fp, 0L, SEEK_END);
1053 size = ftell(fp);
1054
1055 fseek(fp, 0L, SEEK_SET);
1056
1057 shader_code = malloc(size);
1058 fread(shader_code, size, 1, fp);
1059
1060 *psize = size;
1061
1062 return shader_code;
1063}
1064
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001065static XGL_SHADER demo_prepare_vs(struct demo *demo)
1066{
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001067#ifdef EXTERNAL_BIL
1068 void *vertShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001069 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001070
1071 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1072
1073 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1074 vertShaderCode, size);
1075#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001076 static const char *vertShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001077 "#version 140\n"
1078 "#extension GL_ARB_separate_shader_objects : enable\n"
1079 "#extension GL_ARB_shading_language_420pack : enable\n"
1080 "\n"
1081 "layout(binding = 0) uniform buf {\n"
1082 " mat4 MVP;\n"
1083 " vec4 position[12*3];\n"
1084 " vec4 attr[12*3];\n"
1085 "} ubuf;\n"
1086 "\n"
1087 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001088 "\n"
1089 "void main() \n"
1090 "{\n"
1091 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001092 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1093 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001094
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001095 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1096 (const void *) vertShaderText,
1097 strlen(vertShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001098#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001099}
1100
1101static XGL_SHADER demo_prepare_fs(struct demo *demo)
1102{
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001103#ifdef EXTERNAL_BIL
1104 void *fragShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001105 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001106
1107 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1108
1109 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1110 fragShaderCode, size);
1111#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001112 static const char *fragShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001113 "#version 140\n"
1114 "#extension GL_ARB_separate_shader_objects : enable\n"
1115 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001116 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001117 "\n"
1118 "layout (location = 0) in vec4 texcoord;\n"
1119 "void main() {\n"
1120 " gl_FragColor = texture(tex, texcoord.xy);\n"
1121 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001122
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001123 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1124 (const void *) fragShaderText,
1125 strlen(fragShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001126#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001127}
1128
1129static void demo_prepare_pipeline(struct demo *demo)
1130{
1131 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001132 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1133 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001134 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1135 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001136 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1137 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001138 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1139 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001140 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001141
1142 memset(&pipeline, 0, sizeof(pipeline));
1143 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wuf8385062015-01-04 16:27:24 +08001144 pipeline.lastSetLayout = *demo->desc_layout_last;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001145
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001146 memset(&ia, 0, sizeof(ia));
1147 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1148 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1149
1150 memset(&rs, 0, sizeof(rs));
1151 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001152 rs.fillMode = XGL_FILL_SOLID;
1153 rs.cullMode = XGL_CULL_NONE;
1154 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001155
1156 memset(&cb, 0, sizeof(cb));
1157 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001158 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1159 memset(att_state, 0, sizeof(att_state));
1160 att_state[0].format = demo->format;
1161 att_state[0].channelWriteMask = 0xf;
1162 att_state[0].blendEnable = XGL_FALSE;
1163 cb.attachmentCount = 1;
1164 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001165
Tony Barbourfa6cac72015-01-16 14:27:35 -07001166 memset(&vp, 0, sizeof(vp));
1167 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001168
1169 memset(&ds, 0, sizeof(ds));
1170 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1171 ds.format = demo->depth.format;
1172 ds.depthTestEnable = XGL_TRUE;
1173 ds.depthWriteEnable = XGL_TRUE;
1174 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1175 ds.depthBoundsEnable = XGL_FALSE;
1176 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1177 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1178 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1179 ds.stencilTestEnable = XGL_FALSE;
1180 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001181
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001182 memset(&vs, 0, sizeof(vs));
1183 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1184 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001185 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001186 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001187
1188 memset(&fs, 0, sizeof(fs));
1189 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1190 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1191 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001192 assert(fs.shader.shader != NULL);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001193
1194 memset(&ms, 0, sizeof(ms));
1195 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1196 ms.sampleMask = 1;
1197 ms.multisampleEnable = XGL_FALSE;
1198 ms.samples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001199
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001200 pipeline.pNext = (const void *) &ia;
1201 ia.pNext = (const void *) &rs;
1202 rs.pNext = (const void *) &cb;
1203 cb.pNext = (const void *) &ms;
1204 ms.pNext = (const void *) &vp;
1205 vp.pNext = (const void *) &ds;
1206 ds.pNext = (const void *) &vs;
1207 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001208
1209 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1210 assert(!err);
1211
1212 xglDestroyObject(vs.shader.shader);
1213 xglDestroyObject(fs.shader.shader);
1214}
1215
1216static void demo_prepare_dynamic_states(struct demo *demo)
1217{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001218 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1219 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1220 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1221 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001222 XGL_RESULT err;
1223
Tony Barbourfa6cac72015-01-16 14:27:35 -07001224 memset(&viewport_create, 0, sizeof(viewport_create));
1225 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001226 viewport_create.viewportAndScissorCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001227 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001228 XGL_RECT scissor;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001229 viewport.height = (float) demo->height;
1230 viewport.width = (float) demo->width;
1231 viewport.minDepth = (float) 0.0f;
1232 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001233 scissor.extent.width = demo->width;
1234 scissor.extent.height = demo->height;
1235 scissor.offset.x = 0;
1236 scissor.offset.y = 0;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001237 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001238 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001239
1240 memset(&raster, 0, sizeof(raster));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001241 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001242
1243 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001244 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001245
1246 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001247 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1248 depth_stencil.stencilBackRef = 0;
1249 depth_stencil.stencilFrontRef = 0;
1250 depth_stencil.stencilReadMask = 0xff;
1251 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001252
Tony Barbourfa6cac72015-01-16 14:27:35 -07001253 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001254 assert(!err);
1255
Tony Barbourfa6cac72015-01-16 14:27:35 -07001256 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001257 assert(!err);
1258
Tony Barbourfa6cac72015-01-16 14:27:35 -07001259 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001260 &color_blend, &demo->color_blend);
1261 assert(!err);
1262
Tony Barbourfa6cac72015-01-16 14:27:35 -07001263 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001264 &depth_stencil, &demo->depth_stencil);
1265 assert(!err);
1266}
1267
Chia-I Wuf8385062015-01-04 16:27:24 +08001268static void demo_prepare_descriptor_region(struct demo *demo)
1269{
1270 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1271 [0] = {
1272 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1273 .count = 1,
1274 },
1275 [1] = {
1276 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1277 .count = DEMO_TEXTURE_COUNT,
1278 },
1279 };
1280 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1281 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1282 .pNext = NULL,
1283 .count = 2,
1284 .pTypeCount = type_counts,
1285 };
1286 XGL_RESULT err;
1287
1288 err = xglCreateDescriptorRegion(demo->device,
1289 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1290 &descriptor_region, &demo->desc_region);
1291 assert(!err);
1292}
1293
1294static void demo_prepare_descriptor_set(struct demo *demo)
1295{
1296 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1297 &demo->uniform_data.attach;
1298 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1299 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1300 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1301 XGL_UPDATE_BUFFERS update_vs;
1302 XGL_RESULT err;
1303 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001304 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001305
1306 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1307 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1308 view_info[i].pNext = NULL;
1309 view_info[i].view = demo->textures[i].view,
1310 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1311
1312 combined_info[i].pSampler = demo->textures[i].sampler;
1313 combined_info[i].pImageView = &view_info[i];
1314 }
1315
1316 memset(&update_vs, 0, sizeof(update_vs));
1317 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1318 update_vs.pNext = &update_fs;
1319 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1320 update_vs.count = 1;
1321 update_vs.pBufferViews = &view_info_vs;
1322
1323 memset(&update_fs, 0, sizeof(update_fs));
1324 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1325 update_fs.index = 1;
1326 update_fs.count = DEMO_TEXTURE_COUNT;
1327 update_fs.pSamplerImageViews = combined_info;
1328
1329 err = xglAllocDescriptorSets(demo->desc_region,
1330 XGL_DESCRIPTOR_SET_USAGE_STATIC,
1331 1, demo->desc_layout_last,
1332 &demo->desc_set, &count);
1333 assert(!err && count == 1);
1334
1335 xglBeginDescriptorRegionUpdate(demo->device,
1336 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1337
1338 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1339 xglUpdateDescriptors(demo->desc_set, &update_vs);
1340
1341 xglEndDescriptorRegionUpdate(demo->device, demo->cmd);
1342}
1343
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001344static void demo_prepare(struct demo *demo)
1345{
1346 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1347 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1348 .pNext = NULL,
1349 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1350 .flags = 0,
1351 };
1352 XGL_RESULT err;
1353
1354 demo_prepare_buffers(demo);
1355 demo_prepare_depth(demo);
1356 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001357 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001358
Chia-I Wuf8385062015-01-04 16:27:24 +08001359 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001360 demo_prepare_pipeline(demo);
1361 demo_prepare_dynamic_states(demo);
1362
1363 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
1364 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001365
1366 demo_prepare_descriptor_region(demo);
1367 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001368}
1369
1370static void demo_handle_event(struct demo *demo,
1371 const xcb_generic_event_t *event)
1372{
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001373 u_int8_t event_code = event->response_type & 0x7f;
1374 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001375 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001376 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001377 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001378 case XCB_CLIENT_MESSAGE:
1379 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1380 (*demo->atom_wm_delete_window).atom) {
1381 demo->quit = true;
1382 }
1383 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001384 case XCB_KEY_RELEASE:
1385 {
1386 const xcb_key_release_event_t *key =
1387 (const xcb_key_release_event_t *) event;
1388
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001389 switch (key->detail) {
1390 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001391 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001392 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001393 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001394 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001395 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001396 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001397 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001398 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001399 case 0x41:
1400 demo->pause = !demo->pause;
1401 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001402 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001403 }
1404 break;
1405 default:
1406 break;
1407 }
1408}
1409
1410static void demo_run(struct demo *demo)
1411{
1412 xcb_flush(demo->connection);
1413
1414 while (!demo->quit) {
1415 xcb_generic_event_t *event;
1416
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001417 if (demo->pause) {
1418 event = xcb_wait_for_event(demo->connection);
1419 } else {
1420 event = xcb_poll_for_event(demo->connection);
1421 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001422 if (event) {
1423 demo_handle_event(demo, event);
1424 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001425 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001426
1427 // Wait for work to finish before updating MVP.
1428 xglDeviceWaitIdle(demo->device);
1429 demo_update_data_buffer(demo);
1430
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001431 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001432
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001433 // Wait for work to finish before updating MVP.
1434 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001435 }
1436}
1437
1438static void demo_create_window(struct demo *demo)
1439{
1440 uint32_t value_mask, value_list[32];
1441
1442 demo->window = xcb_generate_id(demo->connection);
1443
1444 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1445 value_list[0] = demo->screen->black_pixel;
1446 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1447 XCB_EVENT_MASK_EXPOSURE;
1448
1449 xcb_create_window(demo->connection,
1450 XCB_COPY_FROM_PARENT,
1451 demo->window, demo->screen->root,
1452 0, 0, demo->width, demo->height, 0,
1453 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1454 demo->screen->root_visual,
1455 value_mask, value_list);
1456
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001457 /* Magic code that will send notification when window is destroyed */
1458 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1459 "WM_PROTOCOLS");
1460 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1461
1462 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1463 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1464
1465 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1466 demo->window, (*reply).atom, 4, 32, 1,
1467 &(*demo->atom_wm_delete_window).atom);
1468 free(reply);
1469
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001470 xcb_map_window(demo->connection, demo->window);
1471}
1472
1473static void demo_init_xgl(struct demo *demo)
1474{
1475 const XGL_APPLICATION_INFO app = {
1476 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1477 .pNext = NULL,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001478 .pAppName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001479 .appVersion = 0,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001480 .pEngineName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001481 .engineVersion = 0,
1482 .apiVersion = XGL_MAKE_VERSION(0, 22, 0),
1483 };
1484 const XGL_WSI_X11_CONNECTION_INFO connection = {
1485 .pConnection = demo->connection,
1486 .root = demo->screen->root,
1487 .provider = 0,
1488 };
1489 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1490 .queueNodeIndex = 0,
1491 .queueCount = 1,
1492 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001493 const char *ext_names[] = {
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001494 "XGL_WSI_X11",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001495 };
1496 const XGL_DEVICE_CREATE_INFO device = {
1497 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1498 .pNext = NULL,
1499 .queueRecordCount = 1,
1500 .pRequestedQueues = &queue,
1501 .extensionCount = 1,
1502 .ppEnabledExtensionNames = ext_names,
1503 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1504 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1505 };
1506 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001507 uint32_t gpu_count;
1508 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001509
Jon Ashburn92e80132015-01-29 15:47:01 -07001510 err = xglCreateInstance(&app, NULL, &demo->inst);
1511 assert(!err);
1512 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001513 assert(!err && gpu_count == 1);
1514
1515 for (i = 0; i < device.extensionCount; i++) {
1516 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1517 assert(!err);
1518 }
1519
1520 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1521 assert(!err);
1522
1523 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1524 assert(!err);
1525
1526 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1527 0, &demo->queue);
1528 assert(!err);
1529}
1530
1531static void demo_init_connection(struct demo *demo)
1532{
1533 const xcb_setup_t *setup;
1534 xcb_screen_iterator_t iter;
1535 int scr;
1536
1537 demo->connection = xcb_connect(NULL, &scr);
1538
1539 setup = xcb_get_setup(demo->connection);
1540 iter = xcb_setup_roots_iterator(setup);
1541 while (scr-- > 0)
1542 xcb_screen_next(&iter);
1543
1544 demo->screen = iter.data;
1545}
1546
1547static void demo_init(struct demo *demo)
1548{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001549 vec3 eye = {0.0f, 3.0f, 5.0f};
1550 vec3 origin = {0, 0, 0};
1551 vec3 up = {0.0f, -1.0f, 0.0};
1552
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001553 memset(demo, 0, sizeof(*demo));
1554
1555 demo_init_connection(demo);
1556 demo_init_xgl(demo);
1557
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001558 demo->width = 500;
1559 demo->height = 500;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001560 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001561
1562 demo->spin_angle = 0.01f;
1563 demo->spin_increment = 0.01f;
1564 demo->pause = false;
1565
1566 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1567 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1568 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001569}
1570
1571static void demo_cleanup(struct demo *demo)
1572{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001573 uint32_t i, j;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001574
Chia-I Wuf8385062015-01-04 16:27:24 +08001575 xglDestroyObject(demo->desc_set);
1576 xglDestroyObject(demo->desc_region);
1577
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001578 xglDestroyObject(demo->cmd);
1579
1580 xglDestroyObject(demo->viewport);
1581 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001582 xglDestroyObject(demo->color_blend);
1583 xglDestroyObject(demo->depth_stencil);
1584
1585 xglDestroyObject(demo->pipeline);
Chia-I Wuf8385062015-01-04 16:27:24 +08001586 xglDestroyObject(demo->desc_layout_fs);
1587 xglDestroyObject(demo->desc_layout_vs);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001588
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001589 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1590 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001591 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001592 xglDestroyObject(demo->textures[i].image);
Jon Ashburna9ae3832015-01-16 09:37:43 -07001593 for (j = 0; j < demo->textures[i].num_mem; j++)
1594 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001595 xglDestroyObject(demo->textures[i].sampler);
1596 }
1597
1598 xglDestroyObject(demo->depth.view);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001599 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001600 xglDestroyObject(demo->depth.image);
Jon Ashburna9ae3832015-01-16 09:37:43 -07001601 for (j = 0; j < demo->depth.num_mem; j++)
1602 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001603
1604 xglDestroyObject(demo->uniform_data.view);
1605 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001606 xglDestroyObject(demo->uniform_data.buf);
1607 for (j = 0; j < demo->uniform_data.num_mem; j++)
1608 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001609
1610 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wu68040a42014-11-07 14:30:34 +08001611 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001612 xglDestroyObject(demo->buffers[i].view);
1613 xglDestroyObject(demo->buffers[i].image);
1614 }
1615
1616 xglDestroyDevice(demo->device);
Jon Ashburn92e80132015-01-29 15:47:01 -07001617 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001618
1619 xcb_destroy_window(demo->connection, demo->window);
1620 xcb_disconnect(demo->connection);
1621}
1622
1623int main(void)
1624{
1625 struct demo demo;
1626
1627 demo_init(&demo);
1628
1629 demo_prepare(&demo);
1630 demo_create_window(&demo);
1631 demo_run(&demo);
1632
1633 demo_cleanup(&demo);
1634
1635 return 0;
1636}