blob: 155729fff827ffdc0c45097e8d5b02bfc9a7cd06 [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
34 XGL_FLOAT mvp[4][4];
35 XGL_FLOAT position[12*3][4];
36 XGL_FLOAT color[12*3][4];
37};
38
39struct xgltexcube_vs_uniform {
40 // Must start with MVP
41 XGL_FLOAT mvp[4][4];
42 XGL_FLOAT position[12*3][4];
43 XGL_FLOAT attr[12*3][4];
44};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060045
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060046//--------------------------------------------------------------------------------------
47// Mesh and VertexFormat Data
48//--------------------------------------------------------------------------------------
49struct Vertex
50{
51 XGL_FLOAT posX, posY, posZ, posW; // Position data
52 XGL_FLOAT r, g, b, a; // Color
53};
54
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060055struct VertexPosTex
56{
57 XGL_FLOAT posX, posY, posZ, posW; // Position data
58 XGL_FLOAT u, v, s, t; // Texcoord
59};
60
Courtney 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
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060064static const XGL_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
114static const XGL_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
196 XGL_PHYSICAL_GPU gpu;
197 XGL_DEVICE device;
198 XGL_QUEUE queue;
199
200 int width, height;
201 XGL_FORMAT format;
202
203 struct {
204 XGL_IMAGE image;
205 XGL_GPU_MEMORY mem;
206
207 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wu68040a42014-11-07 14:30:34 +0800208 XGL_FENCE fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600209 } buffers[DEMO_BUFFER_COUNT];
210
211 struct {
212 XGL_FORMAT format;
213
214 XGL_IMAGE image;
215 XGL_GPU_MEMORY mem;
216 XGL_DEPTH_STENCIL_VIEW view;
217 } depth;
218
219 struct {
220 XGL_SAMPLER sampler;
221
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600222 char *filename;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600223 XGL_IMAGE image;
224 XGL_GPU_MEMORY mem;
225 XGL_IMAGE_VIEW view;
226 } textures[DEMO_TEXTURE_COUNT];
227
228 struct {
Chia-I Wu714df452015-01-01 07:55:04 +0800229 XGL_BUFFER buf;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600230 XGL_GPU_MEMORY mem;
Chia-I Wu714df452015-01-01 07:55:04 +0800231 XGL_BUFFER_VIEW view;
232 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600233 } uniform_data;
234
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600235 XGL_DESCRIPTOR_SET dset;
236
237 XGL_PIPELINE pipeline;
238
Tony Barbourfa6cac72015-01-16 14:27:35 -0700239 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
240 XGL_DYNAMIC_RS_STATE_OBJECT raster;
241 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
242 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600243
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600244 mat4x4 projection_matrix;
245 mat4x4 view_matrix;
246 mat4x4 model_matrix;
247
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600248 XGL_FLOAT spin_angle;
249 XGL_FLOAT spin_increment;
250 bool pause;
251
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600252 XGL_CMD_BUFFER cmd;
253
254 xcb_window_t window;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -0700255 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600256
257 bool quit;
258 XGL_UINT current_buffer;
259};
260
261static void demo_draw_build_cmd(struct demo *demo)
262{
263 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
264 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan55658c22014-12-04 11:08:39 +0000265 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600266 };
267 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
268 .view = demo->depth.view,
Mike Stroyan55658c22014-12-04 11:08:39 +0000269 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600270 };
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600271 const XGL_FLOAT clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600272 const XGL_FLOAT clear_depth = 1.0f;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600273 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700274 XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = {
275 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO,
276 .pNext = NULL,
277 .operation = XGL_RENDER_PASS_OPERATION_BEGIN_AND_END,
278 };
Jon Ashburn53d27af2014-12-31 17:08:35 -0700279 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
280 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700281 .pNext = &graphics_cmd_buf_info,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700282 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
283 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
284 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600285 XGL_RESULT err;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700286 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
287 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
288 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
289 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
290 .pNext = NULL,
291 .colorAttachmentCount = 1,
292 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
293 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
294 .sampleCount = 1,
295 };
296 XGL_RENDER_PASS_CREATE_INFO rp_info;
297
298 memset(&rp_info, 0 , sizeof(rp_info));
299 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
300 assert(!err);
301 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
302 rp_info.renderArea.extent.width = demo->width;
303 rp_info.renderArea.extent.height = demo->height;
304 rp_info.pColorLoadOps = &load_op;
305 rp_info.pColorStoreOps = &store_op;
306 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
307 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
308 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
309 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
310 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
311 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600312
Jon Ashburn53d27af2014-12-31 17:08:35 -0700313 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600314 assert(!err);
315
316 xglCmdBindPipeline(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
317 demo->pipeline);
318 xglCmdBindDescriptorSet(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
319 0, demo->dset, 0);
320
Tony Barbourfa6cac72015-01-16 14:27:35 -0700321 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_VIEWPORT, demo->viewport);
322 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_RASTER, demo->raster);
323 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600324 demo->color_blend);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700325 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600326 demo->depth_stencil);
327
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600328 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
329 clear_range.baseMipLevel = 0;
330 clear_range.mipLevels = 1;
331 clear_range.baseArraySlice = 0;
332 clear_range.arraySize = 1;
333 xglCmdClearColorImage(demo->cmd,
334 demo->buffers[demo->current_buffer].image,
335 clear_color, 1, &clear_range);
336
337 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
338 xglCmdClearDepthStencil(demo->cmd, demo->depth.image,
339 clear_depth, 0, 1, &clear_range);
340
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600341 xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600342
343 err = xglEndCommandBuffer(demo->cmd);
344 assert(!err);
345}
346
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600347
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600348void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600349{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600350 mat4x4 MVP, Model, VP;
351 int matrixSize = sizeof(MVP);
352 XGL_UINT8 *pData;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600353 XGL_RESULT err;
354
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600355 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600356
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600357 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600358 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600359 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600360 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600361
362 err = xglMapMemory(demo->uniform_data.mem, 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600363 assert(!err);
364
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600365 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600366
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600367 err = xglUnmapMemory(demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600368 assert(!err);
369}
370
371static void demo_draw(struct demo *demo)
372{
373 const XGL_WSI_X11_PRESENT_INFO present = {
374 .destWindow = demo->window,
375 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wu68040a42014-11-07 14:30:34 +0800376 .async = true,
377 .flip = false,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600378 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800379 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600380 XGL_RESULT err;
381
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600382 demo_draw_build_cmd(demo);
383
Chia-I Wu68040a42014-11-07 14:30:34 +0800384 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((XGL_UINT64) 0));
385 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
386
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600387 err = xglQueueSubmit(demo->queue, 1, &demo->cmd,
388 0, NULL, XGL_NULL_HANDLE);
389 assert(!err);
390
Chia-I Wu68040a42014-11-07 14:30:34 +0800391 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600392 assert(!err);
393
394 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
395}
396
397static void demo_prepare_buffers(struct demo *demo)
398{
399 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
400 .format = demo->format,
401 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
402 .extent = {
403 .width = demo->width,
404 .height = demo->height,
405 },
406 .flags = 0,
407 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800408 const XGL_FENCE_CREATE_INFO fence = {
409 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
410 .pNext = NULL,
411 .flags = 0,
412 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600413 XGL_RESULT err;
414 XGL_UINT i;
415
416 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
417 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
418 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
419 .pNext = NULL,
420 .format = demo->format,
421 .mipLevel = 0,
422 .baseArraySlice = 0,
423 .arraySize = 1,
424 };
425
426 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
427 &demo->buffers[i].image, &demo->buffers[i].mem);
428 assert(!err);
429
430 color_attachment_view.image = demo->buffers[i].image;
431
432 err = xglCreateColorAttachmentView(demo->device,
433 &color_attachment_view, &demo->buffers[i].view);
434 assert(!err);
Chia-I Wu68040a42014-11-07 14:30:34 +0800435
436 err = xglCreateFence(demo->device,
437 &fence, &demo->buffers[i].fence);
438 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600439 }
440}
441
442static void demo_prepare_depth(struct demo *demo)
443{
444 const XGL_FORMAT depth_format = { XGL_CH_FMT_R16, XGL_NUM_FMT_DS };
445 const XGL_IMAGE_CREATE_INFO image = {
446 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
447 .pNext = NULL,
448 .imageType = XGL_IMAGE_2D,
449 .format = depth_format,
450 .extent = { demo->width, demo->height, 1 },
451 .mipLevels = 1,
452 .arraySize = 1,
453 .samples = 1,
454 .tiling = XGL_OPTIMAL_TILING,
455 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
456 .flags = 0,
457 };
458 XGL_MEMORY_ALLOC_INFO mem_alloc = {
459 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
460 .pNext = NULL,
461 .allocationSize = 0,
462 .alignment = 0,
463 .flags = 0,
464 .heapCount = 0,
465 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
466 };
467 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
468 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
469 .pNext = NULL,
470 .image = XGL_NULL_HANDLE,
471 .mipLevel = 0,
472 .baseArraySlice = 0,
473 .arraySize = 1,
474 .flags = 0,
475 };
476 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn71ec15f2014-11-21 13:29:30 -0700477 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600478 XGL_RESULT err;
479
480 demo->depth.format = depth_format;
481
482 /* create image */
483 err = xglCreateImage(demo->device, &image,
484 &demo->depth.image);
485 assert(!err);
486
487 err = xglGetObjectInfo(demo->depth.image,
488 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
489 &mem_reqs_size, &mem_reqs);
490 assert(!err && mem_reqs_size == sizeof(mem_reqs));
491
492 mem_alloc.allocationSize = mem_reqs.size;
493 mem_alloc.alignment = mem_reqs.alignment;
494 mem_alloc.heapCount = mem_reqs.heapCount;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700495 XGL_UINT heapInfo[1];
496 mem_alloc.pHeaps = (const XGL_UINT *)&heapInfo;
497 memcpy(&heapInfo, mem_reqs.pHeaps,
498 sizeof(mem_reqs.pHeaps[0]) * mem_reqs.heapCount);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600499
500 /* allocate memory */
501 err = xglAllocMemory(demo->device, &mem_alloc,
502 &demo->depth.mem);
503 assert(!err);
504
505 /* bind memory */
506 err = xglBindObjectMemory(demo->depth.image,
507 demo->depth.mem, 0);
508 assert(!err);
509
510 /* create image view */
511 view.image = demo->depth.image;
512 err = xglCreateDepthStencilView(demo->device, &view,
513 &demo->depth.view);
514 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600515}
516
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600517/** loadTexture
518 * loads a png file into an memory object, using cstdio , libpng.
519 *
520 * \param demo : Needed to access XGL calls
521 * \param filename : the png file to be loaded
522 * \param width : width of png, to be updated as a side effect of this function
523 * \param height : height of png, to be updated as a side effect of this function
524 *
525 * \return bool : an opengl texture id. true if successful?,
526 * should be validated by the client of this function.
527 *
528 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
529 * Modified to copy image to memory
530 *
531 */
532bool loadTexture(char *filename, XGL_UINT8 *rgba_data,
533 XGL_SUBRESOURCE_LAYOUT *layout,
534 XGL_INT *width, XGL_INT *height)
535{
536 //header for testing if it is a png
537 png_byte header[8];
538 int i, is_png, bit_depth, color_type,rowbytes;
539 png_uint_32 twidth, theight;
540 png_structp png_ptr;
541 png_infop info_ptr, end_info;
542 png_byte *image_data;
543 png_bytep *row_pointers;
544
545 //open file as binary
546 FILE *fp = fopen(filename, "rb");
547 if (!fp) {
548 return false;
549 }
550
551 //read the header
552 fread(header, 1, 8, fp);
553
554 //test if png
555 is_png = !png_sig_cmp(header, 0, 8);
556 if (!is_png) {
557 fclose(fp);
558 return false;
559 }
560
561 //create png struct
562 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
563 NULL, NULL);
564 if (!png_ptr) {
565 fclose(fp);
566 return (false);
567 }
568
569 //create png info struct
570 info_ptr = png_create_info_struct(png_ptr);
571 if (!info_ptr) {
572 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
573 fclose(fp);
574 return (false);
575 }
576
577 //create png info struct
578 end_info = png_create_info_struct(png_ptr);
579 if (!end_info) {
580 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
581 fclose(fp);
582 return (false);
583 }
584
585 //png error stuff, not sure libpng man suggests this.
586 if (setjmp(png_jmpbuf(png_ptr))) {
587 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
588 fclose(fp);
589 return (false);
590 }
591
592 //init png reading
593 png_init_io(png_ptr, fp);
594
595 //let libpng know you already read the first 8 bytes
596 png_set_sig_bytes(png_ptr, 8);
597
598 // read all the info up to the image data
599 png_read_info(png_ptr, info_ptr);
600
601 // get info about png
602 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
603 NULL, NULL, NULL);
604
605 //update width and height based on png info
606 *width = twidth;
607 *height = theight;
608
609 // Require that incoming texture be 8bits per color component
610 // and 4 components (RGBA).
611 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
612 png_get_channels(png_ptr, info_ptr) != 4) {
613 return false;
614 }
615
616 if (rgba_data == NULL) {
617 // If data pointer is null, we just want the width & height
618 // clean up memory and close stuff
619 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
620 fclose(fp);
621
622 return true;
623 }
624
625 // Update the png info struct.
626 png_read_update_info(png_ptr, info_ptr);
627
628 // Row size in bytes.
629 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
630
631 // Allocate the image_data as a big block, to be given to opengl
632 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
633 if (!image_data) {
634 //clean up memory and close stuff
635 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
636 fclose(fp);
637 return false;
638 }
639
640 // row_pointers is for pointing to image_data for reading the png with libpng
641 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
642 if (!row_pointers) {
643 //clean up memory and close stuff
644 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
645 // delete[] image_data;
646 fclose(fp);
647 return false;
648 }
649 // set the individual row_pointers to point at the correct offsets of image_data
650 for (i = 0; i < theight; ++i)
651 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
652
653 // read the png into image_data through row_pointers
654 png_read_image(png_ptr, row_pointers);
655
656 // clean up memory and close stuff
657 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
658 free(row_pointers);
659 free(image_data);
660 fclose(fp);
661
662 return true;
663}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600664
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600665static void demo_prepare_textures(struct demo *demo)
666{
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600667 const XGL_FORMAT tex_format = { XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600668 XGL_INT tex_width;
669 XGL_INT tex_height;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600670 XGL_RESULT err;
671 XGL_UINT i;
672
673 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
674 const XGL_SAMPLER_CREATE_INFO sampler = {
675 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
676 .pNext = NULL,
677 .magFilter = XGL_TEX_FILTER_NEAREST,
678 .minFilter = XGL_TEX_FILTER_NEAREST,
679 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600680 .addressU = XGL_TEX_ADDRESS_CLAMP,
681 .addressV = XGL_TEX_ADDRESS_CLAMP,
682 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600683 .mipLodBias = 0.0f,
684 .maxAnisotropy = 0,
685 .compareFunc = XGL_COMPARE_NEVER,
686 .minLod = 0.0f,
687 .maxLod = 0.0f,
688 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
689 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600690
691 assert(loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height));
692
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600693 const XGL_IMAGE_CREATE_INFO image = {
694 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
695 .pNext = NULL,
696 .imageType = XGL_IMAGE_2D,
697 .format = tex_format,
698 .extent = { tex_width, tex_height, 1 },
699 .mipLevels = 1,
700 .arraySize = 1,
701 .samples = 1,
702 .tiling = XGL_LINEAR_TILING,
703 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
704 .flags = 0,
705 };
706 XGL_MEMORY_ALLOC_INFO mem_alloc = {
707 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
708 .pNext = NULL,
709 .allocationSize = 0,
710 .alignment = 0,
711 .flags = 0,
712 .heapCount = 0,
Tony Barbourfa6cac72015-01-16 14:27:35 -0700713 .pHeaps = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600714 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
715 };
716 XGL_IMAGE_VIEW_CREATE_INFO view = {
717 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
718 .pNext = NULL,
719 .image = XGL_NULL_HANDLE,
720 .viewType = XGL_IMAGE_VIEW_2D,
721 .format = image.format,
722 .channels = { XGL_CHANNEL_SWIZZLE_R,
723 XGL_CHANNEL_SWIZZLE_G,
724 XGL_CHANNEL_SWIZZLE_B,
725 XGL_CHANNEL_SWIZZLE_A, },
726 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
727 .minLod = 0.0f,
728 };
729 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn71ec15f2014-11-21 13:29:30 -0700730 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600731
732 /* create sampler */
733 err = xglCreateSampler(demo->device, &sampler,
734 &demo->textures[i].sampler);
735 assert(!err);
736
737 /* create image */
738 err = xglCreateImage(demo->device, &image,
739 &demo->textures[i].image);
740 assert(!err);
741
742 err = xglGetObjectInfo(demo->textures[i].image,
743 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
744 &mem_reqs_size, &mem_reqs);
745 assert(!err && mem_reqs_size == sizeof(mem_reqs));
746
747 mem_alloc.allocationSize = mem_reqs.size;
748 mem_alloc.alignment = mem_reqs.alignment;
749 mem_alloc.heapCount = mem_reqs.heapCount;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700750 XGL_UINT heapInfo[1];
751 mem_alloc.pHeaps = (const XGL_UINT *)&heapInfo;
752 memcpy(&heapInfo, mem_reqs.pHeaps,
753 sizeof(mem_reqs.pHeaps[0]) * mem_reqs.heapCount);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600754
755 /* allocate memory */
756 err = xglAllocMemory(demo->device, &mem_alloc,
757 &demo->textures[i].mem);
758 assert(!err);
759
760 /* bind memory */
761 err = xglBindObjectMemory(demo->textures[i].image,
762 demo->textures[i].mem, 0);
763 assert(!err);
764
765 /* create image view */
766 view.image = demo->textures[i].image;
767 err = xglCreateImageView(demo->device, &view,
768 &demo->textures[i].view);
769 assert(!err);
770 }
771
772 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
773 const XGL_IMAGE_SUBRESOURCE subres = {
774 .aspect = XGL_IMAGE_ASPECT_COLOR,
775 .mipLevel = 0,
776 .arraySlice = 0,
777 };
778 XGL_SUBRESOURCE_LAYOUT layout;
Chia-I Wu977d2fd2015-01-05 16:27:42 +0800779 XGL_SIZE layout_size = sizeof(layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600780 XGL_VOID *data;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600781
782 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
783 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
784 assert(!err && layout_size == sizeof(layout));
785
786 err = xglMapMemory(demo->textures[i].mem, 0, &data);
787 assert(!err);
788
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600789 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600790
791 err = xglUnmapMemory(demo->textures[i].mem);
792 assert(!err);
793 }
794}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600795
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600796void demo_prepare_cube_data_buffer(struct demo *demo)
797{
Chia-I Wu714df452015-01-01 07:55:04 +0800798 XGL_BUFFER_CREATE_INFO buf_info;
799 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600800 XGL_MEMORY_ALLOC_INFO alloc_info;
Chia-I Wu714df452015-01-01 07:55:04 +0800801 XGL_MEMORY_REQUIREMENTS mem_reqs;
802 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600803 XGL_UINT8 *pData;
804 int i;
805 mat4x4 MVP, VP;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600806 XGL_RESULT err;
807 struct xgltexcube_vs_uniform data;
808
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600809 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600810 mat4x4_mul(MVP, VP, demo->model_matrix);
811 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600812// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600813
814 for (i=0; i<12*3; i++) {
815 data.position[i][0] = g_vertex_buffer_data[i*3];
816 data.position[i][1] = g_vertex_buffer_data[i*3+1];
817 data.position[i][2] = g_vertex_buffer_data[i*3+2];
818 data.position[i][3] = 1.0f;
819 data.attr[i][0] = g_uv_buffer_data[2*i];
820 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
821 data.attr[i][2] = 0;
822 data.attr[i][3] = 0;
823 }
824
Chia-I Wu714df452015-01-01 07:55:04 +0800825 memset(&buf_info, 0, sizeof(buf_info));
826 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
827 buf_info.size = sizeof(data);
828 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
829 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
830 assert(!err);
831
832 err = xglGetObjectInfo(demo->uniform_data.buf,
833 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
834 &mem_reqs_size, &mem_reqs);
835 assert(!err && mem_reqs_size == sizeof(mem_reqs));
836
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600837 memset(&alloc_info, 0, sizeof(alloc_info));
838 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +0800839 alloc_info.allocationSize = mem_reqs.size;
840 alloc_info.alignment = mem_reqs.alignment;
841 alloc_info.heapCount = mem_reqs.heapCount;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700842 XGL_UINT heapInfo[1];
843 alloc_info.pHeaps = (const XGL_UINT *)&heapInfo;
844 memcpy(&heapInfo, mem_reqs.pHeaps,
845 sizeof(mem_reqs.pHeaps[0]) * mem_reqs.heapCount);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600846 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
847
848 err = xglAllocMemory(demo->device, &alloc_info, &demo->uniform_data.mem);
849 assert(!err);
850
851 err = xglMapMemory(demo->uniform_data.mem, 0, (XGL_VOID **) &pData);
852 assert(!err);
853
854 memcpy(pData, &data, alloc_info.allocationSize);
855
856 err = xglUnmapMemory(demo->uniform_data.mem);
857 assert(!err);
858
Chia-I Wu714df452015-01-01 07:55:04 +0800859 err = xglBindObjectMemory(demo->uniform_data.buf,
860 demo->uniform_data.mem, 0);
861 assert(!err);
862
863 memset(&view_info, 0, sizeof(view_info));
864 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
865 view_info.buffer = demo->uniform_data.buf;
866 view_info.viewType = XGL_BUFFER_VIEW_TYPED;
867 view_info.stride = 16;
868 view_info.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
869 view_info.format.numericFormat = XGL_NUM_FMT_FLOAT;
870 view_info.channels.r = XGL_CHANNEL_SWIZZLE_R;
871 view_info.channels.g = XGL_CHANNEL_SWIZZLE_G;
872 view_info.channels.b = XGL_CHANNEL_SWIZZLE_B;
873 view_info.channels.a = XGL_CHANNEL_SWIZZLE_A;
874 view_info.offset = 0;
875 view_info.range = sizeof(data);
876
877 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
878 assert(!err);
879
880 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
881 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600882}
883
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600884static void demo_prepare_descriptor_set(struct demo *demo)
885{
886 const XGL_DESCRIPTOR_SET_CREATE_INFO descriptor_set = {
887 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO,
888 .pNext = NULL,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600889 .slots = DEMO_TEXTURE_COUNT * 2 + 2,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600890 };
891 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600892
893 err = xglCreateDescriptorSet(demo->device, &descriptor_set, &demo->dset);
894 assert(!err);
895
896 xglBeginDescriptorSetUpdate(demo->dset);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600897 xglClearDescriptorSetSlots(demo->dset, 0, DEMO_TEXTURE_COUNT * 2 + 2);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600898
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600899// xglAttachMemoryViewDescriptors(demo->dset, 0, 1, &demo->vertices.view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600900
Chia-I Wu714df452015-01-01 07:55:04 +0800901 xglAttachBufferViewDescriptors(demo->dset, 0, 1, &demo->uniform_data.attach);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600902
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600903 XGL_IMAGE_VIEW_ATTACH_INFO image_view;
904
905 image_view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
906 image_view.pNext = NULL;
907 image_view.view = demo->textures[0].view;
Mike Stroyan55658c22014-12-04 11:08:39 +0000908 image_view.layout = XGL_IMAGE_LAYOUT_GENERAL;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600909
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600910 xglAttachSamplerDescriptors(demo->dset, 1, 1, &demo->textures[0].sampler);
911 xglAttachImageViewDescriptors(demo->dset, 2, 1, &image_view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600912
913 xglEndDescriptorSetUpdate(demo->dset);
914}
915
916static XGL_SHADER demo_prepare_shader(struct demo *demo,
917 XGL_PIPELINE_SHADER_STAGE stage,
918 const void *code,
919 XGL_SIZE size)
920{
921 XGL_SHADER_CREATE_INFO createInfo;
922 XGL_SHADER shader;
923 XGL_RESULT err;
924
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600925
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600926 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
927 createInfo.pNext = NULL;
928
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600929#ifdef EXTERNAL_BIL
930 createInfo.codeSize = size;
931 createInfo.pCode = code;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600932 createInfo.flags = 0;
933
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600934 err = xglCreateShader(demo->device, &createInfo, &shader);
935 if (err) {
936 free((void *) createInfo.pCode);
937 }
938#else
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600939 // Create fake BIL structure to feed GLSL
940 // to the driver "under the covers"
941 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
942 createInfo.pCode = malloc(createInfo.codeSize);
943 createInfo.flags = 0;
944
945 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
946 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
947 ((uint32_t *) createInfo.pCode)[1] = 0;
948 ((uint32_t *) createInfo.pCode)[2] = stage;
949 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
950
951 err = xglCreateShader(demo->device, &createInfo, &shader);
952 if (err) {
953 free((void *) createInfo.pCode);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600954 return NULL;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600955 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600956#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600957
958 return shader;
959}
960
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600961char *demo_read_bil(const char *filename, XGL_SIZE *psize)
962{
963 long int size;
964 void *shader_code;
965
966 FILE *fp = fopen(filename, "rb");
967 if (!fp) return NULL;
968
969 fseek(fp, 0L, SEEK_END);
970 size = ftell(fp);
971
972 fseek(fp, 0L, SEEK_SET);
973
974 shader_code = malloc(size);
975 fread(shader_code, size, 1, fp);
976
977 *psize = size;
978
979 return shader_code;
980}
981
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600982static XGL_SHADER demo_prepare_vs(struct demo *demo)
983{
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600984#ifdef EXTERNAL_BIL
985 void *vertShaderCode;
986 XGL_SIZE size;
987
988 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
989
990 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
991 vertShaderCode, size);
992#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600993 static const char *vertShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600994 "#version 140\n"
995 "#extension GL_ARB_separate_shader_objects : enable\n"
996 "#extension GL_ARB_shading_language_420pack : enable\n"
997 "\n"
998 "layout(binding = 0) uniform buf {\n"
999 " mat4 MVP;\n"
1000 " vec4 position[12*3];\n"
1001 " vec4 attr[12*3];\n"
1002 "} ubuf;\n"
1003 "\n"
1004 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001005 "\n"
1006 "void main() \n"
1007 "{\n"
1008 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001009 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1010 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001011
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001012 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1013 (const void *) vertShaderText,
1014 strlen(vertShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001015#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001016}
1017
1018static XGL_SHADER demo_prepare_fs(struct demo *demo)
1019{
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001020#ifdef EXTERNAL_BIL
1021 void *fragShaderCode;
1022 XGL_SIZE size;
1023
1024 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1025
1026 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1027 fragShaderCode, size);
1028#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001029 static const char *fragShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001030 "#version 140\n"
1031 "#extension GL_ARB_separate_shader_objects : enable\n"
1032 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001033 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001034 "\n"
1035 "layout (location = 0) in vec4 texcoord;\n"
1036 "void main() {\n"
1037 " gl_FragColor = texture(tex, texcoord.xy);\n"
1038 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001039
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001040 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1041 (const void *) fragShaderText,
1042 strlen(fragShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001043#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001044}
1045
1046static void demo_prepare_pipeline(struct demo *demo)
1047{
1048 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001049 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1050 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001051 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1052 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001053 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1054 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001055 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1056 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001057 XGL_DESCRIPTOR_SLOT_INFO vs_slots[3];
1058 XGL_DESCRIPTOR_SLOT_INFO fs_slots[3];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001059 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001060
1061 memset(&pipeline, 0, sizeof(pipeline));
1062 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1063
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001064 memset(&ia, 0, sizeof(ia));
1065 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1066 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1067
1068 memset(&rs, 0, sizeof(rs));
1069 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001070 rs.fillMode = XGL_FILL_SOLID;
1071 rs.cullMode = XGL_CULL_NONE;
1072 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001073
1074 memset(&cb, 0, sizeof(cb));
1075 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001076 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1077 memset(att_state, 0, sizeof(att_state));
1078 att_state[0].format = demo->format;
1079 att_state[0].channelWriteMask = 0xf;
1080 att_state[0].blendEnable = XGL_FALSE;
1081 cb.attachmentCount = 1;
1082 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001083
Tony Barbourfa6cac72015-01-16 14:27:35 -07001084 memset(&vp, 0, sizeof(vp));
1085 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1086 vp.scissorEnable = XGL_FALSE;
1087
1088 memset(&ds, 0, sizeof(ds));
1089 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1090 ds.format = demo->depth.format;
1091 ds.depthTestEnable = XGL_TRUE;
1092 ds.depthWriteEnable = XGL_TRUE;
1093 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1094 ds.depthBoundsEnable = XGL_FALSE;
1095 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1096 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1097 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1098 ds.stencilTestEnable = XGL_FALSE;
1099 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001100
1101 memset(&vs_slots, 0, sizeof(vs_slots));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001102 vs_slots[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1103 vs_slots[0].shaderEntityIndex = 0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001104
1105 memset(&fs_slots, 0, sizeof(fs_slots));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001106 fs_slots[1].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
1107 fs_slots[1].shaderEntityIndex = 0;
Cody Northrop4c7086c2014-12-10 16:59:32 -07001108 fs_slots[2].slotObjectType = XGL_SLOT_SHADER_TEXTURE_RESOURCE;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001109 fs_slots[2].shaderEntityIndex = 0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001110
1111 memset(&vs, 0, sizeof(vs));
1112 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1113 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001114 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001115 assert(vs.shader.shader != NULL);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001116 XGL_DESCRIPTOR_SET_MAPPING ds_mapping1;
1117 ds_mapping1.descriptorCount = 3;
1118 ds_mapping1.pDescriptorInfo = vs_slots;
1119 vs.shader.pDescriptorSetMapping = &ds_mapping1;
1120 vs.shader.linkConstBufferCount = 0;
1121 vs.shader.descriptorSetMappingCount = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001122
1123 memset(&fs, 0, sizeof(fs));
1124 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1125 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1126 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001127 assert(fs.shader.shader != NULL);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001128 XGL_DESCRIPTOR_SET_MAPPING ds_mapping2;
1129 ds_mapping2.descriptorCount = 3;
1130 ds_mapping2.pDescriptorInfo = fs_slots;
1131 fs.shader.pDescriptorSetMapping = &ds_mapping2;
1132 fs.shader.linkConstBufferCount = 0;
1133 fs.shader.descriptorSetMappingCount = 1;
1134
1135 memset(&ms, 0, sizeof(ms));
1136 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1137 ms.sampleMask = 1;
1138 ms.multisampleEnable = XGL_FALSE;
1139 ms.samples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001140
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001141 pipeline.pNext = (const XGL_VOID *) &ia;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001142 ia.pNext = (const XGL_VOID *) &rs;
1143 rs.pNext = (const XGL_VOID *) &cb;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001144 cb.pNext = (const XGL_VOID *) &ms;
1145 ms.pNext = (const XGL_VOID *) &vp;
1146 vp.pNext = (const XGL_VOID *) &ds;
1147 ds.pNext = (const XGL_VOID *) &vs;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001148 vs.pNext = (const XGL_VOID *) &fs;
1149
1150 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1151 assert(!err);
1152
1153 xglDestroyObject(vs.shader.shader);
1154 xglDestroyObject(fs.shader.shader);
1155}
1156
1157static void demo_prepare_dynamic_states(struct demo *demo)
1158{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001159 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1160 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1161 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1162 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001163 XGL_RESULT err;
1164
Tony Barbourfa6cac72015-01-16 14:27:35 -07001165 memset(&viewport_create, 0, sizeof(viewport_create));
1166 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
1167 viewport_create.viewportCount = 1;
1168 XGL_VIEWPORT viewport;
1169 viewport.height = (XGL_FLOAT) demo->height;
1170 viewport.width = (XGL_FLOAT) demo->width;
1171 viewport.minDepth = (XGL_FLOAT) 0.0f;
1172 viewport.maxDepth = (XGL_FLOAT) 1.0f;
1173 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001174
1175 memset(&raster, 0, sizeof(raster));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001176 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001177
1178 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001179 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001180
1181 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001182 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1183 depth_stencil.stencilBackRef = 0;
1184 depth_stencil.stencilFrontRef = 0;
1185 depth_stencil.stencilReadMask = 0xff;
1186 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001187
Tony Barbourfa6cac72015-01-16 14:27:35 -07001188 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001189 assert(!err);
1190
Tony Barbourfa6cac72015-01-16 14:27:35 -07001191 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001192 assert(!err);
1193
Tony Barbourfa6cac72015-01-16 14:27:35 -07001194 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001195 &color_blend, &demo->color_blend);
1196 assert(!err);
1197
Tony Barbourfa6cac72015-01-16 14:27:35 -07001198 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001199 &depth_stencil, &demo->depth_stencil);
1200 assert(!err);
1201}
1202
1203static void demo_prepare(struct demo *demo)
1204{
1205 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1206 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1207 .pNext = NULL,
1208 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1209 .flags = 0,
1210 };
1211 XGL_RESULT err;
1212
1213 demo_prepare_buffers(demo);
1214 demo_prepare_depth(demo);
1215 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001216 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001217 demo_prepare_descriptor_set(demo);
1218
1219 demo_prepare_pipeline(demo);
1220 demo_prepare_dynamic_states(demo);
1221
1222 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
1223 assert(!err);
1224}
1225
1226static void demo_handle_event(struct demo *demo,
1227 const xcb_generic_event_t *event)
1228{
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001229 u_int8_t event_code = event->response_type & 0x7f;
1230 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001231 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001232 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001233 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001234 case XCB_CLIENT_MESSAGE:
1235 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1236 (*demo->atom_wm_delete_window).atom) {
1237 demo->quit = true;
1238 }
1239 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001240 case XCB_KEY_RELEASE:
1241 {
1242 const xcb_key_release_event_t *key =
1243 (const xcb_key_release_event_t *) event;
1244
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001245 switch (key->detail) {
1246 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001247 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001248 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001249 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001250 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001251 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001252 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001253 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001254 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001255 case 0x41:
1256 demo->pause = !demo->pause;
1257 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001258 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001259 }
1260 break;
1261 default:
1262 break;
1263 }
1264}
1265
1266static void demo_run(struct demo *demo)
1267{
1268 xcb_flush(demo->connection);
1269
1270 while (!demo->quit) {
1271 xcb_generic_event_t *event;
1272
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001273 if (demo->pause) {
1274 event = xcb_wait_for_event(demo->connection);
1275 } else {
1276 event = xcb_poll_for_event(demo->connection);
1277 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001278 if (event) {
1279 demo_handle_event(demo, event);
1280 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001281 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001282
1283 // Wait for work to finish before updating MVP.
1284 xglDeviceWaitIdle(demo->device);
1285 demo_update_data_buffer(demo);
1286
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001287 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001288
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001289 // Wait for work to finish before updating MVP.
1290 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001291 }
1292}
1293
1294static void demo_create_window(struct demo *demo)
1295{
1296 uint32_t value_mask, value_list[32];
1297
1298 demo->window = xcb_generate_id(demo->connection);
1299
1300 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1301 value_list[0] = demo->screen->black_pixel;
1302 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1303 XCB_EVENT_MASK_EXPOSURE;
1304
1305 xcb_create_window(demo->connection,
1306 XCB_COPY_FROM_PARENT,
1307 demo->window, demo->screen->root,
1308 0, 0, demo->width, demo->height, 0,
1309 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1310 demo->screen->root_visual,
1311 value_mask, value_list);
1312
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001313 /* Magic code that will send notification when window is destroyed */
1314 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1315 "WM_PROTOCOLS");
1316 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1317
1318 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1319 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1320
1321 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1322 demo->window, (*reply).atom, 4, 32, 1,
1323 &(*demo->atom_wm_delete_window).atom);
1324 free(reply);
1325
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001326 xcb_map_window(demo->connection, demo->window);
1327}
1328
1329static void demo_init_xgl(struct demo *demo)
1330{
1331 const XGL_APPLICATION_INFO app = {
1332 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1333 .pNext = NULL,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001334 .pAppName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001335 .appVersion = 0,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001336 .pEngineName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001337 .engineVersion = 0,
1338 .apiVersion = XGL_MAKE_VERSION(0, 22, 0),
1339 };
1340 const XGL_WSI_X11_CONNECTION_INFO connection = {
1341 .pConnection = demo->connection,
1342 .root = demo->screen->root,
1343 .provider = 0,
1344 };
1345 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1346 .queueNodeIndex = 0,
1347 .queueCount = 1,
1348 };
1349 const XGL_CHAR *ext_names[] = {
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001350 "XGL_WSI_X11",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001351 };
1352 const XGL_DEVICE_CREATE_INFO device = {
1353 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1354 .pNext = NULL,
1355 .queueRecordCount = 1,
1356 .pRequestedQueues = &queue,
1357 .extensionCount = 1,
1358 .ppEnabledExtensionNames = ext_names,
1359 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1360 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1361 };
1362 XGL_RESULT err;
1363 XGL_UINT gpu_count;
1364 XGL_UINT i;
1365
1366 err = xglInitAndEnumerateGpus(&app, NULL, 1, &gpu_count, &demo->gpu);
1367 assert(!err && gpu_count == 1);
1368
1369 for (i = 0; i < device.extensionCount; i++) {
1370 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1371 assert(!err);
1372 }
1373
1374 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1375 assert(!err);
1376
1377 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1378 assert(!err);
1379
1380 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1381 0, &demo->queue);
1382 assert(!err);
1383}
1384
1385static void demo_init_connection(struct demo *demo)
1386{
1387 const xcb_setup_t *setup;
1388 xcb_screen_iterator_t iter;
1389 int scr;
1390
1391 demo->connection = xcb_connect(NULL, &scr);
1392
1393 setup = xcb_get_setup(demo->connection);
1394 iter = xcb_setup_roots_iterator(setup);
1395 while (scr-- > 0)
1396 xcb_screen_next(&iter);
1397
1398 demo->screen = iter.data;
1399}
1400
1401static void demo_init(struct demo *demo)
1402{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001403 vec3 eye = {0.0f, 3.0f, 5.0f};
1404 vec3 origin = {0, 0, 0};
1405 vec3 up = {0.0f, -1.0f, 0.0};
1406
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001407 memset(demo, 0, sizeof(*demo));
1408
1409 demo_init_connection(demo);
1410 demo_init_xgl(demo);
1411
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001412 demo->width = 500;
1413 demo->height = 500;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001414 demo->format.channelFormat = XGL_CH_FMT_B8G8R8A8;
1415 demo->format.numericFormat = XGL_NUM_FMT_UNORM;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001416
1417 demo->spin_angle = 0.01f;
1418 demo->spin_increment = 0.01f;
1419 demo->pause = false;
1420
1421 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1422 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1423 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001424}
1425
1426static void demo_cleanup(struct demo *demo)
1427{
1428 XGL_UINT i;
1429
1430 xglDestroyObject(demo->cmd);
1431
1432 xglDestroyObject(demo->viewport);
1433 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001434 xglDestroyObject(demo->color_blend);
1435 xglDestroyObject(demo->depth_stencil);
1436
1437 xglDestroyObject(demo->pipeline);
1438
1439 xglDestroyObject(demo->dset);
1440
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001441// xglFreeMemory(demo->vertices.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001442
1443 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1444 xglDestroyObject(demo->textures[i].view);
1445 xglDestroyObject(demo->textures[i].image);
1446 xglFreeMemory(demo->textures[i].mem);
1447 xglDestroyObject(demo->textures[i].sampler);
1448 }
1449
1450 xglDestroyObject(demo->depth.view);
1451 xglDestroyObject(demo->depth.image);
1452 xglFreeMemory(demo->depth.mem);
1453
1454 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wu68040a42014-11-07 14:30:34 +08001455 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001456 xglDestroyObject(demo->buffers[i].view);
1457 xglDestroyObject(demo->buffers[i].image);
1458 }
1459
1460 xglDestroyDevice(demo->device);
1461
1462 xcb_destroy_window(demo->connection, demo->window);
1463 xcb_disconnect(demo->connection);
1464}
1465
1466int main(void)
1467{
1468 struct demo demo;
1469
1470 demo_init(&demo);
1471
1472 demo_prepare(&demo);
1473 demo_create_window(&demo);
1474 demo_run(&demo);
1475
1476 demo_cleanup(&demo);
1477
1478 return 0;
1479}