blob: a3c6659a0a20cd5d47b01fa6ec250ad6fb1485a7 [file] [log] [blame]
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <assert.h>
7
8#include <xcb/xcb.h>
9#include <xgl.h>
10#include <xglDbg.h>
11#include <xglWsiX11Ext.h>
12
13#include "icd-bil.h"
14
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -060016#include <unistd.h>
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060017#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060018
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060019#define DEMO_BUFFER_COUNT 2
20#define DEMO_TEXTURE_COUNT 1
21
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060022/*
23 * When not defined, code will use built-in GLSL compiler
24 * which may not be supported on all drivers
25 */
26#define EXTERNAL_BIL
27
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060029 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060030};
31
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060032struct xglcube_vs_uniform {
33 // Must start with MVP
34 XGL_FLOAT mvp[4][4];
35 XGL_FLOAT position[12*3][4];
36 XGL_FLOAT color[12*3][4];
37};
38
39struct xgltexcube_vs_uniform {
40 // Must start with MVP
41 XGL_FLOAT mvp[4][4];
42 XGL_FLOAT position[12*3][4];
43 XGL_FLOAT attr[12*3][4];
44};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060046//--------------------------------------------------------------------------------------
47// Mesh and VertexFormat Data
48//--------------------------------------------------------------------------------------
49struct Vertex
50{
51 XGL_FLOAT posX, posY, posZ, posW; // Position data
52 XGL_FLOAT r, g, b, a; // Color
53};
54
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060055struct VertexPosTex
56{
57 XGL_FLOAT posX, posY, posZ, posW; // Position data
58 XGL_FLOAT u, v, s, t; // Texcoord
59};
60
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060061#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060062#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060063
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060064static const XGL_FLOAT g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060065 -1.0f,-1.0f,-1.0f, // Vertex 0
66 -1.0f,-1.0f, 1.0f,
67 -1.0f, 1.0f, 1.0f,
68
69 -1.0f, 1.0f, 1.0f, // Vertex 1
70 -1.0f, 1.0f,-1.0f,
71 -1.0f,-1.0f,-1.0f,
72
73 -1.0f,-1.0f,-1.0f, // Vertex 2
74 1.0f, 1.0f,-1.0f,
75 1.0f,-1.0f,-1.0f,
76
77 -1.0f,-1.0f,-1.0f, // Vertex 3
78 1.0f, 1.0f,-1.0f,
79 -1.0f, 1.0f,-1.0f,
80
81 -1.0f,-1.0f,-1.0f, // Vertex 4
82 1.0f,-1.0f, 1.0f,
83 1.0f,-1.0f,-1.0f,
84
85 -1.0f,-1.0f,-1.0f, // Vertex 5
86 -1.0f,-1.0f, 1.0f,
87 1.0f,-1.0f, 1.0f,
88
89 -1.0f, 1.0f,-1.0f, // Vertex 6
90 -1.0f, 1.0f, 1.0f,
91 1.0f, 1.0f, 1.0f,
92
93 -1.0f, 1.0f,-1.0f, // Vertex 7
94 1.0f, 1.0f,-1.0f,
95 1.0f, 1.0f, 1.0f,
96
97 1.0f, 1.0f,-1.0f, // Vertex 8
98 1.0f, 1.0f, 1.0f,
99 1.0f,-1.0f, 1.0f,
100
101 1.0f,-1.0f, 1.0f, // Vertex 9
102 1.0f,-1.0f,-1.0f,
103 1.0f, 1.0f,-1.0f,
104
105 -1.0f, 1.0f, 1.0f, // Vertex 10
106 1.0f, 1.0f, 1.0f,
107 -1.0f,-1.0f, 1.0f,
108
109 -1.0f,-1.0f, 1.0f, // Vertex 11
110 1.0f,-1.0f, 1.0f,
111 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600112};
113
114static const XGL_FLOAT g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600115 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600116 0.0f, 0.0f,
117 0.0f, 1.0f,
118
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600119 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600120 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600121 1.0f, 0.0f,
122
123// 0.0f, 1.0f, // Vertex 2
124// 1.0f, 0.0f,
125// 0.0f, 0.0f,
126
127// 0.0f, 1.0f, // Vertex 3
128// 1.0f, 0.0f,
129// 1.0f, 1.0f,
130
131 0.0f, 0.0f, // Vertex 2
132 1.0f, 1.0f,
133 1.0f, 0.0f,
134
135 0.0f, 0.0f, // Vertex 3
136 1.0f, 1.0f,
137 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600138
139 0.0f, 1.0f, // Vertex 4
140 1.0f, 0.0f,
141 0.0f, 0.0f,
142
143 0.0f, 1.0f, // Vertex 5
144 1.0f, 1.0f,
145 1.0f, 0.0f,
146
147 0.0f, 1.0f, // Vertex 6
148 1.0f, 1.0f,
149 1.0f, 0.0f,
150
151 0.0f, 1.0f, // Vertex 7
152 0.0f, 0.0f,
153 1.0f, 0.0f,
154
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600155 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600158
159 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 0.0f, 1.0f,
162
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600163 1.0f, 1.0f, // Vertex 10
164 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600166
167 1.0f, 0.0f, // Vertex 11
168 0.0f, 0.0f,
169 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600170};
171
172void dumpMatrix(const char *note, mat4x4 MVP)
173{
174 int i;
175
176 printf("%s: \n", note);
177 for (i=0; i<4; i++) {
178 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
179 }
180 printf("\n");
181 fflush(stdout);
182}
183
184void dumpVec4(const char *note, vec4 vector)
185{
186 printf("%s: \n", note);
187 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
188 printf("\n");
189 fflush(stdout);
190}
191
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600192struct demo {
193 xcb_connection_t *connection;
194 xcb_screen_t *screen;
195
196 XGL_PHYSICAL_GPU gpu;
197 XGL_DEVICE device;
198 XGL_QUEUE queue;
199
200 int width, height;
201 XGL_FORMAT format;
202
203 struct {
204 XGL_IMAGE image;
205 XGL_GPU_MEMORY mem;
206
207 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800208 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600209 } buffers[DEMO_BUFFER_COUNT];
210
211 struct {
212 XGL_FORMAT format;
213
214 XGL_IMAGE image;
215 XGL_GPU_MEMORY mem;
216 XGL_DEPTH_STENCIL_VIEW view;
217 } depth;
218
219 struct {
220 XGL_SAMPLER sampler;
221
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222 char *filename;
Courtney Goeltzenleuchter7be88a82014-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 {
229 XGL_GPU_MEMORY mem;
230 XGL_MEMORY_VIEW_ATTACH_INFO view;
231
232 XGL_PIPELINE_VERTEX_INPUT_CREATE_INFO vi;
233 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_bindings[1];
234 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attrs[2];
235 } vertices;
236
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600237 struct {
238 XGL_GPU_MEMORY mem;
239 XGL_MEMORY_VIEW_ATTACH_INFO view;
240 } uniform_data;
241
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600242 XGL_DESCRIPTOR_SET dset;
243
244 XGL_PIPELINE pipeline;
245
246 XGL_VIEWPORT_STATE_OBJECT viewport;
247 XGL_RASTER_STATE_OBJECT raster;
248 XGL_MSAA_STATE_OBJECT msaa;
249 XGL_COLOR_BLEND_STATE_OBJECT color_blend;
250 XGL_DEPTH_STENCIL_STATE_OBJECT depth_stencil;
251
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600252 mat4x4 projection_matrix;
253 mat4x4 view_matrix;
254 mat4x4 model_matrix;
255
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600256 XGL_FLOAT spin_angle;
257 XGL_FLOAT spin_increment;
258 bool pause;
259
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600260 XGL_CMD_BUFFER cmd;
261
262 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700263 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600264
265 bool quit;
266 XGL_UINT current_buffer;
267};
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,
273 .colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL,
274 };
275 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
276 .view = demo->depth.view,
277 .depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL,
278 .stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL,
279 };
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600280 const XGL_FLOAT clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600281 const XGL_FLOAT clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600282 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700283 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
284 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
285 .pNext = NULL,
286 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
287 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
288 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600289 XGL_RESULT err;
290
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700291 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600292 assert(!err);
293
294 xglCmdBindPipeline(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
295 demo->pipeline);
296 xglCmdBindDescriptorSet(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
297 0, demo->dset, 0);
298
299 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_VIEWPORT, demo->viewport);
300 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_RASTER, demo->raster);
301 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_MSAA, demo->msaa);
302 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_COLOR_BLEND,
303 demo->color_blend);
304 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL,
305 demo->depth_stencil);
306
307 xglCmdBindAttachments(demo->cmd, 1, &color_attachment, &depth_stencil);
308
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600309 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
310 clear_range.baseMipLevel = 0;
311 clear_range.mipLevels = 1;
312 clear_range.baseArraySlice = 0;
313 clear_range.arraySize = 1;
314 xglCmdClearColorImage(demo->cmd,
315 demo->buffers[demo->current_buffer].image,
316 clear_color, 1, &clear_range);
317
318 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
319 xglCmdClearDepthStencil(demo->cmd, demo->depth.image,
320 clear_depth, 0, 1, &clear_range);
321
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600322 xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323
324 err = xglEndCommandBuffer(demo->cmd);
325 assert(!err);
326}
327
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600328
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600329void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600330{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600331 mat4x4 MVP, Model, VP;
332 int matrixSize = sizeof(MVP);
333 XGL_UINT8 *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334 XGL_RESULT err;
335
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600336 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600337
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600338 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600339 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600340 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600341 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600342
343 err = xglMapMemory(demo->uniform_data.mem, 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344 assert(!err);
345
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600346 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600347
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600348 err = xglUnmapMemory(demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349 assert(!err);
350}
351
352static void demo_draw(struct demo *demo)
353{
354 const XGL_WSI_X11_PRESENT_INFO present = {
355 .destWindow = demo->window,
356 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800357 .async = true,
358 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600359 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800360 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600361 XGL_RESULT err;
362
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363 demo_draw_build_cmd(demo);
364
Chia-I Wubb57f642014-11-07 14:30:34 +0800365 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((XGL_UINT64) 0));
366 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
367
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600368 err = xglQueueSubmit(demo->queue, 1, &demo->cmd,
369 0, NULL, XGL_NULL_HANDLE);
370 assert(!err);
371
Chia-I Wubb57f642014-11-07 14:30:34 +0800372 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373 assert(!err);
374
375 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
376}
377
378static void demo_prepare_buffers(struct demo *demo)
379{
380 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
381 .format = demo->format,
382 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
383 .extent = {
384 .width = demo->width,
385 .height = demo->height,
386 },
387 .flags = 0,
388 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800389 const XGL_FENCE_CREATE_INFO fence = {
390 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
391 .pNext = NULL,
392 .flags = 0,
393 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600394 XGL_RESULT err;
395 XGL_UINT i;
396
397 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
398 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
399 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
400 .pNext = NULL,
401 .format = demo->format,
402 .mipLevel = 0,
403 .baseArraySlice = 0,
404 .arraySize = 1,
405 };
406
407 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
408 &demo->buffers[i].image, &demo->buffers[i].mem);
409 assert(!err);
410
411 color_attachment_view.image = demo->buffers[i].image;
412
413 err = xglCreateColorAttachmentView(demo->device,
414 &color_attachment_view, &demo->buffers[i].view);
415 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800416
417 err = xglCreateFence(demo->device,
418 &fence, &demo->buffers[i].fence);
419 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600420 }
421}
422
423static void demo_prepare_depth(struct demo *demo)
424{
425 const XGL_FORMAT depth_format = { XGL_CH_FMT_R16, XGL_NUM_FMT_DS };
426 const XGL_IMAGE_CREATE_INFO image = {
427 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
428 .pNext = NULL,
429 .imageType = XGL_IMAGE_2D,
430 .format = depth_format,
431 .extent = { demo->width, demo->height, 1 },
432 .mipLevels = 1,
433 .arraySize = 1,
434 .samples = 1,
435 .tiling = XGL_OPTIMAL_TILING,
436 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
437 .flags = 0,
438 };
439 XGL_MEMORY_ALLOC_INFO mem_alloc = {
440 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
441 .pNext = NULL,
442 .allocationSize = 0,
443 .alignment = 0,
444 .flags = 0,
445 .heapCount = 0,
446 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
447 };
448 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
449 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
450 .pNext = NULL,
451 .image = XGL_NULL_HANDLE,
452 .mipLevel = 0,
453 .baseArraySlice = 0,
454 .arraySize = 1,
455 .flags = 0,
456 };
457 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburnbd75a812014-11-21 13:29:30 -0700458 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600459 XGL_RESULT err;
460
461 demo->depth.format = depth_format;
462
463 /* create image */
464 err = xglCreateImage(demo->device, &image,
465 &demo->depth.image);
466 assert(!err);
467
468 err = xglGetObjectInfo(demo->depth.image,
469 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
470 &mem_reqs_size, &mem_reqs);
471 assert(!err && mem_reqs_size == sizeof(mem_reqs));
472
473 mem_alloc.allocationSize = mem_reqs.size;
474 mem_alloc.alignment = mem_reqs.alignment;
475 mem_alloc.heapCount = mem_reqs.heapCount;
476 memcpy(mem_alloc.heaps, mem_reqs.heaps,
477 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
478
479 /* allocate memory */
480 err = xglAllocMemory(demo->device, &mem_alloc,
481 &demo->depth.mem);
482 assert(!err);
483
484 /* bind memory */
485 err = xglBindObjectMemory(demo->depth.image,
486 demo->depth.mem, 0);
487 assert(!err);
488
489 /* create image view */
490 view.image = demo->depth.image;
491 err = xglCreateDepthStencilView(demo->device, &view,
492 &demo->depth.view);
493 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600494}
495
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600496/** loadTexture
497 * loads a png file into an memory object, using cstdio , libpng.
498 *
499 * \param demo : Needed to access XGL calls
500 * \param filename : the png file to be loaded
501 * \param width : width of png, to be updated as a side effect of this function
502 * \param height : height of png, to be updated as a side effect of this function
503 *
504 * \return bool : an opengl texture id. true if successful?,
505 * should be validated by the client of this function.
506 *
507 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
508 * Modified to copy image to memory
509 *
510 */
511bool loadTexture(char *filename, XGL_UINT8 *rgba_data,
512 XGL_SUBRESOURCE_LAYOUT *layout,
513 XGL_INT *width, XGL_INT *height)
514{
515 //header for testing if it is a png
516 png_byte header[8];
517 int i, is_png, bit_depth, color_type,rowbytes;
518 png_uint_32 twidth, theight;
519 png_structp png_ptr;
520 png_infop info_ptr, end_info;
521 png_byte *image_data;
522 png_bytep *row_pointers;
523
524 //open file as binary
525 FILE *fp = fopen(filename, "rb");
526 if (!fp) {
527 return false;
528 }
529
530 //read the header
531 fread(header, 1, 8, fp);
532
533 //test if png
534 is_png = !png_sig_cmp(header, 0, 8);
535 if (!is_png) {
536 fclose(fp);
537 return false;
538 }
539
540 //create png struct
541 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
542 NULL, NULL);
543 if (!png_ptr) {
544 fclose(fp);
545 return (false);
546 }
547
548 //create png info struct
549 info_ptr = png_create_info_struct(png_ptr);
550 if (!info_ptr) {
551 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
552 fclose(fp);
553 return (false);
554 }
555
556 //create png info struct
557 end_info = png_create_info_struct(png_ptr);
558 if (!end_info) {
559 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
560 fclose(fp);
561 return (false);
562 }
563
564 //png error stuff, not sure libpng man suggests this.
565 if (setjmp(png_jmpbuf(png_ptr))) {
566 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
567 fclose(fp);
568 return (false);
569 }
570
571 //init png reading
572 png_init_io(png_ptr, fp);
573
574 //let libpng know you already read the first 8 bytes
575 png_set_sig_bytes(png_ptr, 8);
576
577 // read all the info up to the image data
578 png_read_info(png_ptr, info_ptr);
579
580 // get info about png
581 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
582 NULL, NULL, NULL);
583
584 //update width and height based on png info
585 *width = twidth;
586 *height = theight;
587
588 // Require that incoming texture be 8bits per color component
589 // and 4 components (RGBA).
590 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
591 png_get_channels(png_ptr, info_ptr) != 4) {
592 return false;
593 }
594
595 if (rgba_data == NULL) {
596 // If data pointer is null, we just want the width & height
597 // clean up memory and close stuff
598 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
599 fclose(fp);
600
601 return true;
602 }
603
604 // Update the png info struct.
605 png_read_update_info(png_ptr, info_ptr);
606
607 // Row size in bytes.
608 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
609
610 // Allocate the image_data as a big block, to be given to opengl
611 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
612 if (!image_data) {
613 //clean up memory and close stuff
614 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
615 fclose(fp);
616 return false;
617 }
618
619 // row_pointers is for pointing to image_data for reading the png with libpng
620 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
621 if (!row_pointers) {
622 //clean up memory and close stuff
623 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
624 // delete[] image_data;
625 fclose(fp);
626 return false;
627 }
628 // set the individual row_pointers to point at the correct offsets of image_data
629 for (i = 0; i < theight; ++i)
630 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
631
632 // read the png into image_data through row_pointers
633 png_read_image(png_ptr, row_pointers);
634
635 // clean up memory and close stuff
636 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
637 free(row_pointers);
638 free(image_data);
639 fclose(fp);
640
641 return true;
642}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600643
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600644static void demo_prepare_textures(struct demo *demo)
645{
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600646 const XGL_FORMAT tex_format = { XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600647 XGL_INT tex_width;
648 XGL_INT tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600649 XGL_RESULT err;
650 XGL_UINT i;
651
652 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
653 const XGL_SAMPLER_CREATE_INFO sampler = {
654 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
655 .pNext = NULL,
656 .magFilter = XGL_TEX_FILTER_NEAREST,
657 .minFilter = XGL_TEX_FILTER_NEAREST,
658 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600659 .addressU = XGL_TEX_ADDRESS_CLAMP,
660 .addressV = XGL_TEX_ADDRESS_CLAMP,
661 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662 .mipLodBias = 0.0f,
663 .maxAnisotropy = 0,
664 .compareFunc = XGL_COMPARE_NEVER,
665 .minLod = 0.0f,
666 .maxLod = 0.0f,
667 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
668 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600669
670 assert(loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height));
671
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600672 const XGL_IMAGE_CREATE_INFO image = {
673 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
674 .pNext = NULL,
675 .imageType = XGL_IMAGE_2D,
676 .format = tex_format,
677 .extent = { tex_width, tex_height, 1 },
678 .mipLevels = 1,
679 .arraySize = 1,
680 .samples = 1,
681 .tiling = XGL_LINEAR_TILING,
682 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
683 .flags = 0,
684 };
685 XGL_MEMORY_ALLOC_INFO mem_alloc = {
686 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
687 .pNext = NULL,
688 .allocationSize = 0,
689 .alignment = 0,
690 .flags = 0,
691 .heapCount = 0,
692 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
693 };
694 XGL_IMAGE_VIEW_CREATE_INFO view = {
695 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
696 .pNext = NULL,
697 .image = XGL_NULL_HANDLE,
698 .viewType = XGL_IMAGE_VIEW_2D,
699 .format = image.format,
700 .channels = { XGL_CHANNEL_SWIZZLE_R,
701 XGL_CHANNEL_SWIZZLE_G,
702 XGL_CHANNEL_SWIZZLE_B,
703 XGL_CHANNEL_SWIZZLE_A, },
704 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
705 .minLod = 0.0f,
706 };
707 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburnbd75a812014-11-21 13:29:30 -0700708 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600709
710 /* create sampler */
711 err = xglCreateSampler(demo->device, &sampler,
712 &demo->textures[i].sampler);
713 assert(!err);
714
715 /* create image */
716 err = xglCreateImage(demo->device, &image,
717 &demo->textures[i].image);
718 assert(!err);
719
720 err = xglGetObjectInfo(demo->textures[i].image,
721 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
722 &mem_reqs_size, &mem_reqs);
723 assert(!err && mem_reqs_size == sizeof(mem_reqs));
724
725 mem_alloc.allocationSize = mem_reqs.size;
726 mem_alloc.alignment = mem_reqs.alignment;
727 mem_alloc.heapCount = mem_reqs.heapCount;
728 memcpy(mem_alloc.heaps, mem_reqs.heaps,
729 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
730
731 /* allocate memory */
732 err = xglAllocMemory(demo->device, &mem_alloc,
733 &demo->textures[i].mem);
734 assert(!err);
735
736 /* bind memory */
737 err = xglBindObjectMemory(demo->textures[i].image,
738 demo->textures[i].mem, 0);
739 assert(!err);
740
741 /* create image view */
742 view.image = demo->textures[i].image;
743 err = xglCreateImageView(demo->device, &view,
744 &demo->textures[i].view);
745 assert(!err);
746 }
747
748 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
749 const XGL_IMAGE_SUBRESOURCE subres = {
750 .aspect = XGL_IMAGE_ASPECT_COLOR,
751 .mipLevel = 0,
752 .arraySlice = 0,
753 };
754 XGL_SUBRESOURCE_LAYOUT layout;
Chia-I Wu170de772015-01-05 16:27:42 +0800755 XGL_SIZE layout_size = sizeof(layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600756 XGL_VOID *data;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600757
758 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
759 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
760 assert(!err && layout_size == sizeof(layout));
761
762 err = xglMapMemory(demo->textures[i].mem, 0, &data);
763 assert(!err);
764
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600765 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600766
767 err = xglUnmapMemory(demo->textures[i].mem);
768 assert(!err);
769 }
770}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600771
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600772void demo_prepare_cube_data_buffer(struct demo *demo)
773{
774 XGL_MEMORY_ALLOC_INFO alloc_info;
775 XGL_UINT8 *pData;
776 int i;
777 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600778 XGL_RESULT err;
779 struct xgltexcube_vs_uniform data;
780
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600781 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600782 mat4x4_mul(MVP, VP, demo->model_matrix);
783 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600784// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600785
786 for (i=0; i<12*3; i++) {
787 data.position[i][0] = g_vertex_buffer_data[i*3];
788 data.position[i][1] = g_vertex_buffer_data[i*3+1];
789 data.position[i][2] = g_vertex_buffer_data[i*3+2];
790 data.position[i][3] = 1.0f;
791 data.attr[i][0] = g_uv_buffer_data[2*i];
792 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
793 data.attr[i][2] = 0;
794 data.attr[i][3] = 0;
795 }
796
797 memset(&alloc_info, 0, sizeof(alloc_info));
798 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
799 alloc_info.allocationSize = sizeof(data);
800 alloc_info.alignment = 0;
801 alloc_info.heapCount = 1;
802 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
803
804 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
805 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
806
807 err = xglAllocMemory(demo->device, &alloc_info, &demo->uniform_data.mem);
808 assert(!err);
809
810 err = xglMapMemory(demo->uniform_data.mem, 0, (XGL_VOID **) &pData);
811 assert(!err);
812
813 memcpy(pData, &data, alloc_info.allocationSize);
814
815 err = xglUnmapMemory(demo->uniform_data.mem);
816 assert(!err);
817
818 // set up the memory view for the constant buffer
819 demo->uniform_data.view.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO;
820 demo->uniform_data.view.stride = 16;
821 demo->uniform_data.view.range = alloc_info.allocationSize;
822 demo->uniform_data.view.offset = 0;
823 demo->uniform_data.view.mem = demo->uniform_data.mem;
824 demo->uniform_data.view.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
825 demo->uniform_data.view.format.numericFormat = XGL_NUM_FMT_FLOAT;
826}
827
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600828static void demo_prepare_descriptor_set(struct demo *demo)
829{
830 const XGL_DESCRIPTOR_SET_CREATE_INFO descriptor_set = {
831 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO,
832 .pNext = NULL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600833 .slots = DEMO_TEXTURE_COUNT * 2 + 2,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600834 };
835 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600836
837 err = xglCreateDescriptorSet(demo->device, &descriptor_set, &demo->dset);
838 assert(!err);
839
840 xglBeginDescriptorSetUpdate(demo->dset);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600841 xglClearDescriptorSetSlots(demo->dset, 0, DEMO_TEXTURE_COUNT * 2 + 2);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600842
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600843// xglAttachMemoryViewDescriptors(demo->dset, 0, 1, &demo->vertices.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600844
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600845 xglAttachMemoryViewDescriptors(demo->dset, 0, 1, &demo->uniform_data.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600846
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600847 XGL_IMAGE_VIEW_ATTACH_INFO image_view;
848
849 image_view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
850 image_view.pNext = NULL;
851 image_view.view = demo->textures[0].view;
852 image_view.state = XGL_IMAGE_STATE_GRAPHICS_SHADER_READ_ONLY;
853
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600854 xglAttachSamplerDescriptors(demo->dset, 1, 1, &demo->textures[0].sampler);
855 xglAttachImageViewDescriptors(demo->dset, 2, 1, &image_view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600856
857 xglEndDescriptorSetUpdate(demo->dset);
858}
859
860static XGL_SHADER demo_prepare_shader(struct demo *demo,
861 XGL_PIPELINE_SHADER_STAGE stage,
862 const void *code,
863 XGL_SIZE size)
864{
865 XGL_SHADER_CREATE_INFO createInfo;
866 XGL_SHADER shader;
867 XGL_RESULT err;
868
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600869
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600870 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
871 createInfo.pNext = NULL;
872
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600873#ifdef EXTERNAL_BIL
874 createInfo.codeSize = size;
875 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600876 createInfo.flags = 0;
877
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600878 err = xglCreateShader(demo->device, &createInfo, &shader);
879 if (err) {
880 free((void *) createInfo.pCode);
881 }
882#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600883 // Create fake BIL structure to feed GLSL
884 // to the driver "under the covers"
885 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
886 createInfo.pCode = malloc(createInfo.codeSize);
887 createInfo.flags = 0;
888
889 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
890 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
891 ((uint32_t *) createInfo.pCode)[1] = 0;
892 ((uint32_t *) createInfo.pCode)[2] = stage;
893 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
894
895 err = xglCreateShader(demo->device, &createInfo, &shader);
896 if (err) {
897 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600898 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600899 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600900#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600901
902 return shader;
903}
904
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600905char *demo_read_bil(const char *filename, XGL_SIZE *psize)
906{
907 long int size;
908 void *shader_code;
909
910 FILE *fp = fopen(filename, "rb");
911 if (!fp) return NULL;
912
913 fseek(fp, 0L, SEEK_END);
914 size = ftell(fp);
915
916 fseek(fp, 0L, SEEK_SET);
917
918 shader_code = malloc(size);
919 fread(shader_code, size, 1, fp);
920
921 *psize = size;
922
923 return shader_code;
924}
925
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600926static XGL_SHADER demo_prepare_vs(struct demo *demo)
927{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600928#ifdef EXTERNAL_BIL
929 void *vertShaderCode;
930 XGL_SIZE size;
931
932 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
933
934 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
935 vertShaderCode, size);
936#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600937 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600938 "#version 140\n"
939 "#extension GL_ARB_separate_shader_objects : enable\n"
940 "#extension GL_ARB_shading_language_420pack : enable\n"
941 "\n"
942 "layout(binding = 0) uniform buf {\n"
943 " mat4 MVP;\n"
944 " vec4 position[12*3];\n"
945 " vec4 attr[12*3];\n"
946 "} ubuf;\n"
947 "\n"
948 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600949 "\n"
950 "void main() \n"
951 "{\n"
952 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600953 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
954 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600955
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600956 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
957 (const void *) vertShaderText,
958 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600959#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600960}
961
962static XGL_SHADER demo_prepare_fs(struct demo *demo)
963{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600964#ifdef EXTERNAL_BIL
965 void *fragShaderCode;
966 XGL_SIZE size;
967
968 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
969
970 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
971 fragShaderCode, size);
972#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600973 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600974 "#version 140\n"
975 "#extension GL_ARB_separate_shader_objects : enable\n"
976 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600977 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600978 "\n"
979 "layout (location = 0) in vec4 texcoord;\n"
980 "void main() {\n"
981 " gl_FragColor = texture(tex, texcoord.xy);\n"
982 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600983
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600984 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
985 (const void *) fragShaderText,
986 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600987#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600988}
989
990static void demo_prepare_pipeline(struct demo *demo)
991{
992 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600993 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
994 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
995 XGL_PIPELINE_CB_STATE cb;
996 XGL_PIPELINE_DB_STATE_CREATE_INFO db;
997 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
998 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600999 XGL_DESCRIPTOR_SLOT_INFO vs_slots[3];
1000 XGL_DESCRIPTOR_SLOT_INFO fs_slots[3];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001001 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001002
1003 memset(&pipeline, 0, sizeof(pipeline));
1004 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1005
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001006 memset(&ia, 0, sizeof(ia));
1007 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1008 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1009
1010 memset(&rs, 0, sizeof(rs));
1011 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
1012
1013 memset(&cb, 0, sizeof(cb));
1014 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
1015 cb.attachment[0].format = demo->format;
1016 cb.attachment[0].channelWriteMask = 0xf;
1017
1018 memset(&db, 0, sizeof(db));
1019 db.sType = XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO;
1020 db.format = demo->depth.format;
1021
1022 memset(&vs_slots, 0, sizeof(vs_slots));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001023 vs_slots[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1024 vs_slots[0].shaderEntityIndex = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001025
1026 memset(&fs_slots, 0, sizeof(fs_slots));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001027 fs_slots[1].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
1028 fs_slots[1].shaderEntityIndex = 0;
Cody Northrop4fd1d6d2014-12-10 16:59:32 -07001029 fs_slots[2].slotObjectType = XGL_SLOT_SHADER_TEXTURE_RESOURCE;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001030 fs_slots[2].shaderEntityIndex = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001031
1032 memset(&vs, 0, sizeof(vs));
1033 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1034 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001035 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001036 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001037 vs.shader.descriptorSetMapping[0].descriptorCount = 3;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038 vs.shader.descriptorSetMapping[0].pDescriptorInfo = vs_slots;
1039
1040 memset(&fs, 0, sizeof(fs));
1041 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1042 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1043 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001044 assert(fs.shader.shader != NULL);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001045 fs.shader.descriptorSetMapping[0].descriptorCount = 3;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001046 fs.shader.descriptorSetMapping[0].pDescriptorInfo = fs_slots;
1047
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001048 pipeline.pNext = (const XGL_VOID *) &ia;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001049 ia.pNext = (const XGL_VOID *) &rs;
1050 rs.pNext = (const XGL_VOID *) &cb;
1051 cb.pNext = (const XGL_VOID *) &db;
1052 db.pNext = (const XGL_VOID *) &vs;
1053 vs.pNext = (const XGL_VOID *) &fs;
1054
1055 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1056 assert(!err);
1057
1058 xglDestroyObject(vs.shader.shader);
1059 xglDestroyObject(fs.shader.shader);
1060}
1061
1062static void demo_prepare_dynamic_states(struct demo *demo)
1063{
1064 XGL_VIEWPORT_STATE_CREATE_INFO viewport;
1065 XGL_RASTER_STATE_CREATE_INFO raster;
1066 XGL_MSAA_STATE_CREATE_INFO msaa;
1067 XGL_COLOR_BLEND_STATE_CREATE_INFO color_blend;
1068 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depth_stencil;
1069 XGL_RESULT err;
1070
1071 memset(&viewport, 0, sizeof(viewport));
1072 viewport.viewportCount = 1;
1073 viewport.scissorEnable = XGL_FALSE;
1074 viewport.viewports[0].width = (XGL_FLOAT) demo->width;
1075 viewport.viewports[0].height = (XGL_FLOAT) demo->height;
1076 viewport.viewports[0].minDepth = (XGL_FLOAT) 0.0f;
1077 viewport.viewports[0].maxDepth = (XGL_FLOAT) 1.0f;
1078
1079 memset(&raster, 0, sizeof(raster));
1080 raster.sType = XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO;
1081 raster.fillMode = XGL_FILL_SOLID;
1082 raster.cullMode = XGL_CULL_NONE;
1083 raster.frontFace = XGL_FRONT_FACE_CCW;
1084
1085 memset(&msaa, 0, sizeof(msaa));
1086 msaa.sType = XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO;
1087 msaa.samples = 1;
1088 msaa.sampleMask = 0x1;
1089
1090 memset(&color_blend, 0, sizeof(color_blend));
1091 color_blend.sType = XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO;
1092
1093 memset(&depth_stencil, 0, sizeof(depth_stencil));
1094 depth_stencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
1095 depth_stencil.depthTestEnable = XGL_TRUE;
1096 depth_stencil.depthWriteEnable = XGL_TRUE;
1097 depth_stencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
1098 depth_stencil.depthBoundsEnable = XGL_FALSE;
1099
1100 err = xglCreateViewportState(demo->device, &viewport, &demo->viewport);
1101 assert(!err);
1102
1103 err = xglCreateRasterState(demo->device, &raster, &demo->raster);
1104 assert(!err);
1105
1106 err = xglCreateMsaaState(demo->device, &msaa, &demo->msaa);
1107 assert(!err);
1108
1109 err = xglCreateColorBlendState(demo->device,
1110 &color_blend, &demo->color_blend);
1111 assert(!err);
1112
1113 err = xglCreateDepthStencilState(demo->device,
1114 &depth_stencil, &demo->depth_stencil);
1115 assert(!err);
1116}
1117
1118static void demo_prepare(struct demo *demo)
1119{
1120 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1121 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1122 .pNext = NULL,
1123 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1124 .flags = 0,
1125 };
1126 XGL_RESULT err;
1127
1128 demo_prepare_buffers(demo);
1129 demo_prepare_depth(demo);
1130 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001131 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001132 demo_prepare_descriptor_set(demo);
1133
1134 demo_prepare_pipeline(demo);
1135 demo_prepare_dynamic_states(demo);
1136
1137 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
1138 assert(!err);
1139}
1140
1141static void demo_handle_event(struct demo *demo,
1142 const xcb_generic_event_t *event)
1143{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001144 u_int8_t event_code = event->response_type & 0x7f;
1145 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001146 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001147 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001148 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001149 case XCB_CLIENT_MESSAGE:
1150 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1151 (*demo->atom_wm_delete_window).atom) {
1152 demo->quit = true;
1153 }
1154 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001155 case XCB_KEY_RELEASE:
1156 {
1157 const xcb_key_release_event_t *key =
1158 (const xcb_key_release_event_t *) event;
1159
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001160 switch (key->detail) {
1161 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001162 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001163 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001164 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001165 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001166 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001167 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001168 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001169 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001170 case 0x41:
1171 demo->pause = !demo->pause;
1172 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001173 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001174 }
1175 break;
1176 default:
1177 break;
1178 }
1179}
1180
1181static void demo_run(struct demo *demo)
1182{
1183 xcb_flush(demo->connection);
1184
1185 while (!demo->quit) {
1186 xcb_generic_event_t *event;
1187
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001188 if (demo->pause) {
1189 event = xcb_wait_for_event(demo->connection);
1190 } else {
1191 event = xcb_poll_for_event(demo->connection);
1192 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001193 if (event) {
1194 demo_handle_event(demo, event);
1195 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001196 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001197
1198 // Wait for work to finish before updating MVP.
1199 xglDeviceWaitIdle(demo->device);
1200 demo_update_data_buffer(demo);
1201
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001202 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001203
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001204 // Wait for work to finish before updating MVP.
1205 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 }
1207}
1208
1209static void demo_create_window(struct demo *demo)
1210{
1211 uint32_t value_mask, value_list[32];
1212
1213 demo->window = xcb_generate_id(demo->connection);
1214
1215 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1216 value_list[0] = demo->screen->black_pixel;
1217 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1218 XCB_EVENT_MASK_EXPOSURE;
1219
1220 xcb_create_window(demo->connection,
1221 XCB_COPY_FROM_PARENT,
1222 demo->window, demo->screen->root,
1223 0, 0, demo->width, demo->height, 0,
1224 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1225 demo->screen->root_visual,
1226 value_mask, value_list);
1227
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001228 /* Magic code that will send notification when window is destroyed */
1229 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1230 "WM_PROTOCOLS");
1231 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1232
1233 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1234 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1235
1236 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1237 demo->window, (*reply).atom, 4, 32, 1,
1238 &(*demo->atom_wm_delete_window).atom);
1239 free(reply);
1240
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241 xcb_map_window(demo->connection, demo->window);
1242}
1243
1244static void demo_init_xgl(struct demo *demo)
1245{
1246 const XGL_APPLICATION_INFO app = {
1247 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1248 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001249 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001250 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001251 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252 .engineVersion = 0,
1253 .apiVersion = XGL_MAKE_VERSION(0, 22, 0),
1254 };
1255 const XGL_WSI_X11_CONNECTION_INFO connection = {
1256 .pConnection = demo->connection,
1257 .root = demo->screen->root,
1258 .provider = 0,
1259 };
1260 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1261 .queueNodeIndex = 0,
1262 .queueCount = 1,
1263 };
1264 const XGL_CHAR *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001265 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001266 };
1267 const XGL_DEVICE_CREATE_INFO device = {
1268 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1269 .pNext = NULL,
1270 .queueRecordCount = 1,
1271 .pRequestedQueues = &queue,
1272 .extensionCount = 1,
1273 .ppEnabledExtensionNames = ext_names,
1274 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1275 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1276 };
1277 XGL_RESULT err;
1278 XGL_UINT gpu_count;
1279 XGL_UINT i;
1280
1281 err = xglInitAndEnumerateGpus(&app, NULL, 1, &gpu_count, &demo->gpu);
1282 assert(!err && gpu_count == 1);
1283
1284 for (i = 0; i < device.extensionCount; i++) {
1285 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1286 assert(!err);
1287 }
1288
1289 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1290 assert(!err);
1291
1292 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1293 assert(!err);
1294
1295 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1296 0, &demo->queue);
1297 assert(!err);
1298}
1299
1300static void demo_init_connection(struct demo *demo)
1301{
1302 const xcb_setup_t *setup;
1303 xcb_screen_iterator_t iter;
1304 int scr;
1305
1306 demo->connection = xcb_connect(NULL, &scr);
1307
1308 setup = xcb_get_setup(demo->connection);
1309 iter = xcb_setup_roots_iterator(setup);
1310 while (scr-- > 0)
1311 xcb_screen_next(&iter);
1312
1313 demo->screen = iter.data;
1314}
1315
1316static void demo_init(struct demo *demo)
1317{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001318 vec3 eye = {0.0f, 3.0f, 5.0f};
1319 vec3 origin = {0, 0, 0};
1320 vec3 up = {0.0f, -1.0f, 0.0};
1321
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001322 memset(demo, 0, sizeof(*demo));
1323
1324 demo_init_connection(demo);
1325 demo_init_xgl(demo);
1326
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001327 demo->width = 500;
1328 demo->height = 500;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001329 demo->format.channelFormat = XGL_CH_FMT_B8G8R8A8;
1330 demo->format.numericFormat = XGL_NUM_FMT_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001331
1332 demo->spin_angle = 0.01f;
1333 demo->spin_increment = 0.01f;
1334 demo->pause = false;
1335
1336 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1337 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1338 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001339}
1340
1341static void demo_cleanup(struct demo *demo)
1342{
1343 XGL_UINT i;
1344
1345 xglDestroyObject(demo->cmd);
1346
1347 xglDestroyObject(demo->viewport);
1348 xglDestroyObject(demo->raster);
1349 xglDestroyObject(demo->msaa);
1350 xglDestroyObject(demo->color_blend);
1351 xglDestroyObject(demo->depth_stencil);
1352
1353 xglDestroyObject(demo->pipeline);
1354
1355 xglDestroyObject(demo->dset);
1356
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001357// xglFreeMemory(demo->vertices.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001358
1359 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1360 xglDestroyObject(demo->textures[i].view);
1361 xglDestroyObject(demo->textures[i].image);
1362 xglFreeMemory(demo->textures[i].mem);
1363 xglDestroyObject(demo->textures[i].sampler);
1364 }
1365
1366 xglDestroyObject(demo->depth.view);
1367 xglDestroyObject(demo->depth.image);
1368 xglFreeMemory(demo->depth.mem);
1369
1370 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001371 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001372 xglDestroyObject(demo->buffers[i].view);
1373 xglDestroyObject(demo->buffers[i].image);
1374 }
1375
1376 xglDestroyDevice(demo->device);
1377
1378 xcb_destroy_window(demo->connection, demo->window);
1379 xcb_disconnect(demo->connection);
1380}
1381
1382int main(void)
1383{
1384 struct demo demo;
1385
1386 demo_init(&demo);
1387
1388 demo_prepare(&demo);
1389 demo_create_window(&demo);
1390 demo_run(&demo);
1391
1392 demo_cleanup(&demo);
1393
1394 return 0;
1395}