blob: b02e443bc477067599783c9e2e12f5dccd313954 [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;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700215 XGL_UINT num_mem;
216 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600217 XGL_DEPTH_STENCIL_VIEW view;
218 } depth;
219
220 struct {
221 XGL_SAMPLER sampler;
222
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223 char *filename;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600224 XGL_IMAGE image;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700225 XGL_UINT num_mem;
226 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600227 XGL_IMAGE_VIEW view;
228 } textures[DEMO_TEXTURE_COUNT];
229
230 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800231 XGL_BUFFER buf;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700232 XGL_UINT num_mem;
233 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800234 XGL_BUFFER_VIEW view;
235 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600236 } uniform_data;
237
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800238 XGL_DESCRIPTOR_SET_LAYOUT desc_layout_vs;
239 XGL_DESCRIPTOR_SET_LAYOUT desc_layout_fs;
240 XGL_DESCRIPTOR_SET_LAYOUT *desc_layout_last;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600241 XGL_PIPELINE pipeline;
242
Tony Barbour29645d02015-01-16 14:27:35 -0700243 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
244 XGL_DYNAMIC_RS_STATE_OBJECT raster;
245 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
246 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600247
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600248 mat4x4 projection_matrix;
249 mat4x4 view_matrix;
250 mat4x4 model_matrix;
251
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600252 XGL_FLOAT spin_angle;
253 XGL_FLOAT spin_increment;
254 bool pause;
255
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600256 XGL_CMD_BUFFER cmd;
257
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800258 XGL_DESCRIPTOR_REGION desc_region;
259 XGL_DESCRIPTOR_SET desc_set;
260
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600261 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700262 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600263
264 bool quit;
265 XGL_UINT current_buffer;
266};
267
268static void demo_draw_build_cmd(struct demo *demo)
269{
270 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
271 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000272 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600273 };
274 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
275 .view = demo->depth.view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000276 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600277 };
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600278 const XGL_FLOAT clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600279 const XGL_FLOAT clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600280 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700281 XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = {
282 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO,
283 .pNext = NULL,
284 .operation = XGL_RENDER_PASS_OPERATION_BEGIN_AND_END,
285 };
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700286 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
287 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700288 .pNext = &graphics_cmd_buf_info,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700289 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
290 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
291 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600292 XGL_RESULT err;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700293 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
294 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
295 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
296 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
297 .pNext = NULL,
298 .colorAttachmentCount = 1,
299 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
300 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
301 .sampleCount = 1,
302 };
303 XGL_RENDER_PASS_CREATE_INFO rp_info;
304
305 memset(&rp_info, 0 , sizeof(rp_info));
306 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
307 assert(!err);
308 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
309 rp_info.renderArea.extent.width = demo->width;
310 rp_info.renderArea.extent.height = demo->height;
311 rp_info.pColorLoadOps = &load_op;
312 rp_info.pColorStoreOps = &store_op;
313 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
314 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
315 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
316 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
317 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
318 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700320 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321 assert(!err);
322
323 xglCmdBindPipeline(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
324 demo->pipeline);
325 xglCmdBindDescriptorSet(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800326 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327
Tony Barbour29645d02015-01-16 14:27:35 -0700328 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_VIEWPORT, demo->viewport);
329 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_RASTER, demo->raster);
330 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331 demo->color_blend);
Tony Barbour29645d02015-01-16 14:27:35 -0700332 xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333 demo->depth_stencil);
334
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600335 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
336 clear_range.baseMipLevel = 0;
337 clear_range.mipLevels = 1;
338 clear_range.baseArraySlice = 0;
339 clear_range.arraySize = 1;
340 xglCmdClearColorImage(demo->cmd,
341 demo->buffers[demo->current_buffer].image,
342 clear_color, 1, &clear_range);
343
344 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
345 xglCmdClearDepthStencil(demo->cmd, demo->depth.image,
346 clear_depth, 0, 1, &clear_range);
347
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600348 xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349
350 err = xglEndCommandBuffer(demo->cmd);
351 assert(!err);
352}
353
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600354
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600355void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600356{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600357 mat4x4 MVP, Model, VP;
358 int matrixSize = sizeof(MVP);
359 XGL_UINT8 *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360 XGL_RESULT err;
361
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600362 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600364 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600365 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600366 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600367 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600368
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700369 assert(demo->uniform_data.num_mem == 1);
370 err = xglMapMemory(demo->uniform_data.mem[0], 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371 assert(!err);
372
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600373 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700375 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376 assert(!err);
377}
378
379static void demo_draw(struct demo *demo)
380{
381 const XGL_WSI_X11_PRESENT_INFO present = {
382 .destWindow = demo->window,
383 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800384 .async = true,
385 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600386 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800387 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388 XGL_RESULT err;
389
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390 demo_draw_build_cmd(demo);
391
Chia-I Wubb57f642014-11-07 14:30:34 +0800392 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((XGL_UINT64) 0));
393 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
394
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395 err = xglQueueSubmit(demo->queue, 1, &demo->cmd,
396 0, NULL, XGL_NULL_HANDLE);
397 assert(!err);
398
Chia-I Wubb57f642014-11-07 14:30:34 +0800399 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400 assert(!err);
401
402 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
403}
404
405static void demo_prepare_buffers(struct demo *demo)
406{
407 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
408 .format = demo->format,
409 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
410 .extent = {
411 .width = demo->width,
412 .height = demo->height,
413 },
414 .flags = 0,
415 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800416 const XGL_FENCE_CREATE_INFO fence = {
417 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
418 .pNext = NULL,
419 .flags = 0,
420 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600421 XGL_RESULT err;
422 XGL_UINT i;
423
424 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
425 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
426 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
427 .pNext = NULL,
428 .format = demo->format,
429 .mipLevel = 0,
430 .baseArraySlice = 0,
431 .arraySize = 1,
432 };
433
434 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
435 &demo->buffers[i].image, &demo->buffers[i].mem);
436 assert(!err);
437
438 color_attachment_view.image = demo->buffers[i].image;
439
440 err = xglCreateColorAttachmentView(demo->device,
441 &color_attachment_view, &demo->buffers[i].view);
442 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800443
444 err = xglCreateFence(demo->device,
445 &fence, &demo->buffers[i].fence);
446 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600447 }
448}
449
450static void demo_prepare_depth(struct demo *demo)
451{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700452 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600453 const XGL_IMAGE_CREATE_INFO image = {
454 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
455 .pNext = NULL,
456 .imageType = XGL_IMAGE_2D,
457 .format = depth_format,
458 .extent = { demo->width, demo->height, 1 },
459 .mipLevels = 1,
460 .arraySize = 1,
461 .samples = 1,
462 .tiling = XGL_OPTIMAL_TILING,
463 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
464 .flags = 0,
465 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700466 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
467 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
468 .pNext = NULL,
469 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600470 XGL_MEMORY_ALLOC_INFO mem_alloc = {
471 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700472 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600473 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700474 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700475 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600476 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
477 };
478 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
479 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
480 .pNext = NULL,
481 .image = XGL_NULL_HANDLE,
482 .mipLevel = 0,
483 .baseArraySlice = 0,
484 .arraySize = 1,
485 .flags = 0,
486 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700487 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Jon Ashburnbd75a812014-11-21 13:29:30 -0700488 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700489 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
490 XGL_SIZE img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600491 XGL_RESULT err;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700492 XGL_UINT num_allocations = 0;
493 XGL_SIZE num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600494
495 demo->depth.format = depth_format;
496
497 /* create image */
498 err = xglCreateImage(demo->device, &image,
499 &demo->depth.image);
500 assert(!err);
501
Jon Ashburnb2a66652015-01-16 09:37:43 -0700502
503 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
504 assert(!err && num_alloc_size == sizeof(num_allocations));
505 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
506 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
507 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600508 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700509 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
510 &mem_reqs_size, mem_reqs);
511 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700512 err = xglGetObjectInfo(demo->depth.image,
513 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
514 &img_reqs_size, &img_reqs);
515 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
516 img_alloc.usage = img_reqs.usage;
517 img_alloc.formatClass = img_reqs.formatClass;
518 img_alloc.samples = img_reqs.samples;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700519 for (XGL_UINT i = 0; i < num_allocations; i ++) {
520 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600521
Jon Ashburnb2a66652015-01-16 09:37:43 -0700522 /* allocate memory */
523 err = xglAllocMemory(demo->device, &mem_alloc,
524 &(demo->depth.mem[i]));
525 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600526
Jon Ashburnb2a66652015-01-16 09:37:43 -0700527 /* bind memory */
528 err = xglBindObjectMemory(demo->depth.image, i,
529 demo->depth.mem[i], 0);
530 assert(!err);
531 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600532
533 /* create image view */
534 view.image = demo->depth.image;
535 err = xglCreateDepthStencilView(demo->device, &view,
536 &demo->depth.view);
537 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600538}
539
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600540/** loadTexture
541 * loads a png file into an memory object, using cstdio , libpng.
542 *
543 * \param demo : Needed to access XGL calls
544 * \param filename : the png file to be loaded
545 * \param width : width of png, to be updated as a side effect of this function
546 * \param height : height of png, to be updated as a side effect of this function
547 *
548 * \return bool : an opengl texture id. true if successful?,
549 * should be validated by the client of this function.
550 *
551 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
552 * Modified to copy image to memory
553 *
554 */
555bool loadTexture(char *filename, XGL_UINT8 *rgba_data,
556 XGL_SUBRESOURCE_LAYOUT *layout,
557 XGL_INT *width, XGL_INT *height)
558{
559 //header for testing if it is a png
560 png_byte header[8];
561 int i, is_png, bit_depth, color_type,rowbytes;
562 png_uint_32 twidth, theight;
563 png_structp png_ptr;
564 png_infop info_ptr, end_info;
565 png_byte *image_data;
566 png_bytep *row_pointers;
567
568 //open file as binary
569 FILE *fp = fopen(filename, "rb");
570 if (!fp) {
571 return false;
572 }
573
574 //read the header
575 fread(header, 1, 8, fp);
576
577 //test if png
578 is_png = !png_sig_cmp(header, 0, 8);
579 if (!is_png) {
580 fclose(fp);
581 return false;
582 }
583
584 //create png struct
585 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
586 NULL, NULL);
587 if (!png_ptr) {
588 fclose(fp);
589 return (false);
590 }
591
592 //create png info struct
593 info_ptr = png_create_info_struct(png_ptr);
594 if (!info_ptr) {
595 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
596 fclose(fp);
597 return (false);
598 }
599
600 //create png info struct
601 end_info = png_create_info_struct(png_ptr);
602 if (!end_info) {
603 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
604 fclose(fp);
605 return (false);
606 }
607
608 //png error stuff, not sure libpng man suggests this.
609 if (setjmp(png_jmpbuf(png_ptr))) {
610 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
611 fclose(fp);
612 return (false);
613 }
614
615 //init png reading
616 png_init_io(png_ptr, fp);
617
618 //let libpng know you already read the first 8 bytes
619 png_set_sig_bytes(png_ptr, 8);
620
621 // read all the info up to the image data
622 png_read_info(png_ptr, info_ptr);
623
624 // get info about png
625 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
626 NULL, NULL, NULL);
627
628 //update width and height based on png info
629 *width = twidth;
630 *height = theight;
631
632 // Require that incoming texture be 8bits per color component
633 // and 4 components (RGBA).
634 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
635 png_get_channels(png_ptr, info_ptr) != 4) {
636 return false;
637 }
638
639 if (rgba_data == NULL) {
640 // If data pointer is null, we just want the width & height
641 // clean up memory and close stuff
642 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
643 fclose(fp);
644
645 return true;
646 }
647
648 // Update the png info struct.
649 png_read_update_info(png_ptr, info_ptr);
650
651 // Row size in bytes.
652 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
653
654 // Allocate the image_data as a big block, to be given to opengl
655 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
656 if (!image_data) {
657 //clean up memory and close stuff
658 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
659 fclose(fp);
660 return false;
661 }
662
663 // row_pointers is for pointing to image_data for reading the png with libpng
664 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
665 if (!row_pointers) {
666 //clean up memory and close stuff
667 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
668 // delete[] image_data;
669 fclose(fp);
670 return false;
671 }
672 // set the individual row_pointers to point at the correct offsets of image_data
673 for (i = 0; i < theight; ++i)
674 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
675
676 // read the png into image_data through row_pointers
677 png_read_image(png_ptr, row_pointers);
678
679 // clean up memory and close stuff
680 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
681 free(row_pointers);
682 free(image_data);
683 fclose(fp);
684
685 return true;
686}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600687
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600688static void demo_prepare_textures(struct demo *demo)
689{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700690 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600691 XGL_INT tex_width;
692 XGL_INT tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600693 XGL_RESULT err;
694 XGL_UINT i;
695
696 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
697 const XGL_SAMPLER_CREATE_INFO sampler = {
698 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
699 .pNext = NULL,
700 .magFilter = XGL_TEX_FILTER_NEAREST,
701 .minFilter = XGL_TEX_FILTER_NEAREST,
702 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600703 .addressU = XGL_TEX_ADDRESS_CLAMP,
704 .addressV = XGL_TEX_ADDRESS_CLAMP,
705 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600706 .mipLodBias = 0.0f,
707 .maxAnisotropy = 0,
708 .compareFunc = XGL_COMPARE_NEVER,
709 .minLod = 0.0f,
710 .maxLod = 0.0f,
711 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
712 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600713
714 assert(loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height));
715
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600716 const XGL_IMAGE_CREATE_INFO image = {
717 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
718 .pNext = NULL,
719 .imageType = XGL_IMAGE_2D,
720 .format = tex_format,
721 .extent = { tex_width, tex_height, 1 },
722 .mipLevels = 1,
723 .arraySize = 1,
724 .samples = 1,
725 .tiling = XGL_LINEAR_TILING,
726 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
727 .flags = 0,
728 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700729 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
730 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
731 .pNext = NULL,
732 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733 XGL_MEMORY_ALLOC_INFO mem_alloc = {
734 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700735 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600736 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700737 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700738 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600739 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
740 };
741 XGL_IMAGE_VIEW_CREATE_INFO view = {
742 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
743 .pNext = NULL,
744 .image = XGL_NULL_HANDLE,
745 .viewType = XGL_IMAGE_VIEW_2D,
746 .format = image.format,
747 .channels = { XGL_CHANNEL_SWIZZLE_R,
748 XGL_CHANNEL_SWIZZLE_G,
749 XGL_CHANNEL_SWIZZLE_B,
750 XGL_CHANNEL_SWIZZLE_A, },
751 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
752 .minLod = 0.0f,
753 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700754
755 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Jon Ashburnbd75a812014-11-21 13:29:30 -0700756 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700757 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
758 XGL_SIZE img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700759 XGL_UINT num_allocations = 0;
760 XGL_SIZE num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600761
762 /* create sampler */
763 err = xglCreateSampler(demo->device, &sampler,
764 &demo->textures[i].sampler);
765 assert(!err);
766
767 /* create image */
768 err = xglCreateImage(demo->device, &image,
769 &demo->textures[i].image);
770 assert(!err);
771
772 err = xglGetObjectInfo(demo->textures[i].image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700773 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
774 &num_alloc_size, &num_allocations);
775 assert(!err && num_alloc_size == sizeof(num_allocations));
776 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
777 demo->textures[i].mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
778 demo->textures[i].num_mem = num_allocations;
779 err = xglGetObjectInfo(demo->textures[i].image,
780 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
781 &mem_reqs_size, mem_reqs);
782 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700783 err = xglGetObjectInfo(demo->textures[i].image,
784 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
785 &img_reqs_size, &img_reqs);
786 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
787 img_alloc.usage = img_reqs.usage;
788 img_alloc.formatClass = img_reqs.formatClass;
789 img_alloc.samples = img_reqs.samples;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700790 for (XGL_UINT j = 0; j < num_allocations; j ++) {
791 mem_alloc.allocationSize = mem_reqs[j].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600792
Jon Ashburnb2a66652015-01-16 09:37:43 -0700793 /* allocate memory */
794 err = xglAllocMemory(demo->device, &mem_alloc,
795 &(demo->textures[i].mem[j]));
796 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600797
Jon Ashburnb2a66652015-01-16 09:37:43 -0700798 /* bind memory */
799 err = xglBindObjectMemory(demo->textures[i].image, j,
800 demo->textures[i].mem[j], 0);
801 assert(!err);
802 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600803
804 /* create image view */
805 view.image = demo->textures[i].image;
806 err = xglCreateImageView(demo->device, &view,
807 &demo->textures[i].view);
808 assert(!err);
809 }
810
811 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
812 const XGL_IMAGE_SUBRESOURCE subres = {
813 .aspect = XGL_IMAGE_ASPECT_COLOR,
814 .mipLevel = 0,
815 .arraySlice = 0,
816 };
817 XGL_SUBRESOURCE_LAYOUT layout;
Chia-I Wu170de772015-01-05 16:27:42 +0800818 XGL_SIZE layout_size = sizeof(layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819 XGL_VOID *data;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600820
821 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
822 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
823 assert(!err && layout_size == sizeof(layout));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700824 assert(demo->textures[i].num_mem == 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600825
Jon Ashburnb2a66652015-01-16 09:37:43 -0700826 err = xglMapMemory(demo->textures[i].mem[0], 0, &data);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600827 assert(!err);
828
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600829 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830
Jon Ashburnb2a66652015-01-16 09:37:43 -0700831 err = xglUnmapMemory(demo->textures[i].mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600832 assert(!err);
833 }
834}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600835
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600836void demo_prepare_cube_data_buffer(struct demo *demo)
837{
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800838 XGL_BUFFER_CREATE_INFO buf_info;
839 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700840 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
841 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
842 .pNext = NULL,
843 };
844 XGL_MEMORY_ALLOC_INFO alloc_info = {
845 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
846 .pNext = &buf_alloc,
847 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700848 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700849 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700850 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
851 };
852 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800853 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700854 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
855 XGL_SIZE buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
856 XGL_UINT num_allocations = 0;
857 XGL_SIZE num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600858 XGL_UINT8 *pData;
859 int i;
860 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600861 XGL_RESULT err;
862 struct xgltexcube_vs_uniform data;
863
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600864 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600865 mat4x4_mul(MVP, VP, demo->model_matrix);
866 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600867// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600868
869 for (i=0; i<12*3; i++) {
870 data.position[i][0] = g_vertex_buffer_data[i*3];
871 data.position[i][1] = g_vertex_buffer_data[i*3+1];
872 data.position[i][2] = g_vertex_buffer_data[i*3+2];
873 data.position[i][3] = 1.0f;
874 data.attr[i][0] = g_uv_buffer_data[2*i];
875 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
876 data.attr[i][2] = 0;
877 data.attr[i][3] = 0;
878 }
879
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800880 memset(&buf_info, 0, sizeof(buf_info));
881 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
882 buf_info.size = sizeof(data);
883 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
884 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
885 assert(!err);
886
887 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700888 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
889 &num_alloc_size, &num_allocations);
890 assert(!err && num_alloc_size == sizeof(num_allocations));
891 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
892 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
893 demo->uniform_data.num_mem = num_allocations;
894 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800895 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700896 &mem_reqs_size, mem_reqs);
897 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
898 err = xglGetObjectInfo(demo->uniform_data.buf,
899 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
900 &buf_reqs_size, &buf_reqs);
901 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
902 buf_alloc.usage = buf_reqs.usage;
903 for (XGL_UINT i = 0; i < num_allocations; i ++) {
904 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800905
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700906 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
907 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600908
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700909 err = xglMapMemory(demo->uniform_data.mem[i], 0, (XGL_VOID **) &pData);
910 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600911
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700912 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600913
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700914 err = xglUnmapMemory(demo->uniform_data.mem[i]);
915 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600916
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700917 err = xglBindObjectMemory(demo->uniform_data.buf, i,
918 demo->uniform_data.mem[i], 0);
919 assert(!err);
920 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800921
922 memset(&view_info, 0, sizeof(view_info));
923 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
924 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +0800925 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800926 view_info.offset = 0;
927 view_info.range = sizeof(data);
928
929 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
930 assert(!err);
931
932 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
933 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600934}
935
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800936static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600937{
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800938 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
939 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600940 .pNext = NULL,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800941 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
942 .count = 1,
943 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
944 .immutableSampler = XGL_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600945 };
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800946 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
947 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
948 .pNext = NULL,
949 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
950 .count = DEMO_TEXTURE_COUNT,
951 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
952 .immutableSampler = XGL_NULL_HANDLE,
953 };
954 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600955 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600956
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800957 err = xglCreateDescriptorSetLayout(demo->device,
958 XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, &bind_point,
959 XGL_NULL_HANDLE, &descriptor_layout_vs,
960 &demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600961 assert(!err);
962
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800963 err = xglCreateDescriptorSetLayout(demo->device,
964 XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, &bind_point,
965 demo->desc_layout_vs, &descriptor_layout_fs,
966 &demo->desc_layout_fs);
967 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600968
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800969 demo->desc_layout_last = &demo->desc_layout_fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600970}
971
972static XGL_SHADER demo_prepare_shader(struct demo *demo,
973 XGL_PIPELINE_SHADER_STAGE stage,
974 const void *code,
975 XGL_SIZE size)
976{
977 XGL_SHADER_CREATE_INFO createInfo;
978 XGL_SHADER shader;
979 XGL_RESULT err;
980
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600981
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600982 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
983 createInfo.pNext = NULL;
984
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600985#ifdef EXTERNAL_BIL
986 createInfo.codeSize = size;
987 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600988 createInfo.flags = 0;
989
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600990 err = xglCreateShader(demo->device, &createInfo, &shader);
991 if (err) {
992 free((void *) createInfo.pCode);
993 }
994#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600995 // Create fake BIL structure to feed GLSL
996 // to the driver "under the covers"
997 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
998 createInfo.pCode = malloc(createInfo.codeSize);
999 createInfo.flags = 0;
1000
1001 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1002 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1003 ((uint32_t *) createInfo.pCode)[1] = 0;
1004 ((uint32_t *) createInfo.pCode)[2] = stage;
1005 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1006
1007 err = xglCreateShader(demo->device, &createInfo, &shader);
1008 if (err) {
1009 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001010 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001011 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001012#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001013
1014 return shader;
1015}
1016
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001017char *demo_read_bil(const char *filename, XGL_SIZE *psize)
1018{
1019 long int size;
1020 void *shader_code;
1021
1022 FILE *fp = fopen(filename, "rb");
1023 if (!fp) return NULL;
1024
1025 fseek(fp, 0L, SEEK_END);
1026 size = ftell(fp);
1027
1028 fseek(fp, 0L, SEEK_SET);
1029
1030 shader_code = malloc(size);
1031 fread(shader_code, size, 1, fp);
1032
1033 *psize = size;
1034
1035 return shader_code;
1036}
1037
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038static XGL_SHADER demo_prepare_vs(struct demo *demo)
1039{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001040#ifdef EXTERNAL_BIL
1041 void *vertShaderCode;
1042 XGL_SIZE size;
1043
1044 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1045
1046 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1047 vertShaderCode, size);
1048#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001049 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001050 "#version 140\n"
1051 "#extension GL_ARB_separate_shader_objects : enable\n"
1052 "#extension GL_ARB_shading_language_420pack : enable\n"
1053 "\n"
1054 "layout(binding = 0) uniform buf {\n"
1055 " mat4 MVP;\n"
1056 " vec4 position[12*3];\n"
1057 " vec4 attr[12*3];\n"
1058 "} ubuf;\n"
1059 "\n"
1060 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001061 "\n"
1062 "void main() \n"
1063 "{\n"
1064 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001065 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1066 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001067
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001068 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1069 (const void *) vertShaderText,
1070 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001071#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001072}
1073
1074static XGL_SHADER demo_prepare_fs(struct demo *demo)
1075{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001076#ifdef EXTERNAL_BIL
1077 void *fragShaderCode;
1078 XGL_SIZE size;
1079
1080 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1081
1082 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1083 fragShaderCode, size);
1084#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001085 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001086 "#version 140\n"
1087 "#extension GL_ARB_separate_shader_objects : enable\n"
1088 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001089 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001090 "\n"
1091 "layout (location = 0) in vec4 texcoord;\n"
1092 "void main() {\n"
1093 " gl_FragColor = texture(tex, texcoord.xy);\n"
1094 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001095
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001096 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1097 (const void *) fragShaderText,
1098 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001099#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001100}
1101
1102static void demo_prepare_pipeline(struct demo *demo)
1103{
1104 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001105 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1106 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001107 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1108 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001109 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1110 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001111 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1112 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001113 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114
1115 memset(&pipeline, 0, sizeof(pipeline));
1116 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001117 pipeline.lastSetLayout = *demo->desc_layout_last;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001118
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001119 memset(&ia, 0, sizeof(ia));
1120 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1121 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1122
1123 memset(&rs, 0, sizeof(rs));
1124 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001125 rs.fillMode = XGL_FILL_SOLID;
1126 rs.cullMode = XGL_CULL_NONE;
1127 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001128
1129 memset(&cb, 0, sizeof(cb));
1130 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001131 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1132 memset(att_state, 0, sizeof(att_state));
1133 att_state[0].format = demo->format;
1134 att_state[0].channelWriteMask = 0xf;
1135 att_state[0].blendEnable = XGL_FALSE;
1136 cb.attachmentCount = 1;
1137 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001138
Tony Barbour29645d02015-01-16 14:27:35 -07001139 memset(&vp, 0, sizeof(vp));
1140 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1141 vp.scissorEnable = XGL_FALSE;
1142
1143 memset(&ds, 0, sizeof(ds));
1144 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1145 ds.format = demo->depth.format;
1146 ds.depthTestEnable = XGL_TRUE;
1147 ds.depthWriteEnable = XGL_TRUE;
1148 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1149 ds.depthBoundsEnable = XGL_FALSE;
1150 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1151 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1152 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1153 ds.stencilTestEnable = XGL_FALSE;
1154 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001155
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001156 memset(&vs, 0, sizeof(vs));
1157 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1158 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001159 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001160 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001161
1162 memset(&fs, 0, sizeof(fs));
1163 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1164 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1165 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001166 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001167
1168 memset(&ms, 0, sizeof(ms));
1169 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1170 ms.sampleMask = 1;
1171 ms.multisampleEnable = XGL_FALSE;
1172 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001173
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001174 pipeline.pNext = (const XGL_VOID *) &ia;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001175 ia.pNext = (const XGL_VOID *) &rs;
1176 rs.pNext = (const XGL_VOID *) &cb;
Tony Barbour29645d02015-01-16 14:27:35 -07001177 cb.pNext = (const XGL_VOID *) &ms;
1178 ms.pNext = (const XGL_VOID *) &vp;
1179 vp.pNext = (const XGL_VOID *) &ds;
1180 ds.pNext = (const XGL_VOID *) &vs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001181 vs.pNext = (const XGL_VOID *) &fs;
1182
1183 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1184 assert(!err);
1185
1186 xglDestroyObject(vs.shader.shader);
1187 xglDestroyObject(fs.shader.shader);
1188}
1189
1190static void demo_prepare_dynamic_states(struct demo *demo)
1191{
Tony Barbour29645d02015-01-16 14:27:35 -07001192 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1193 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1194 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1195 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001196 XGL_RESULT err;
1197
Tony Barbour29645d02015-01-16 14:27:35 -07001198 memset(&viewport_create, 0, sizeof(viewport_create));
1199 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
1200 viewport_create.viewportCount = 1;
1201 XGL_VIEWPORT viewport;
1202 viewport.height = (XGL_FLOAT) demo->height;
1203 viewport.width = (XGL_FLOAT) demo->width;
1204 viewport.minDepth = (XGL_FLOAT) 0.0f;
1205 viewport.maxDepth = (XGL_FLOAT) 1.0f;
1206 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001207
1208 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001209 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210
1211 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001212 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001213
1214 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001215 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1216 depth_stencil.stencilBackRef = 0;
1217 depth_stencil.stencilFrontRef = 0;
1218 depth_stencil.stencilReadMask = 0xff;
1219 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001220
Tony Barbour29645d02015-01-16 14:27:35 -07001221 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001222 assert(!err);
1223
Tony Barbour29645d02015-01-16 14:27:35 -07001224 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001225 assert(!err);
1226
Tony Barbour29645d02015-01-16 14:27:35 -07001227 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001228 &color_blend, &demo->color_blend);
1229 assert(!err);
1230
Tony Barbour29645d02015-01-16 14:27:35 -07001231 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232 &depth_stencil, &demo->depth_stencil);
1233 assert(!err);
1234}
1235
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001236static void demo_prepare_descriptor_region(struct demo *demo)
1237{
1238 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1239 [0] = {
1240 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1241 .count = 1,
1242 },
1243 [1] = {
1244 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1245 .count = DEMO_TEXTURE_COUNT,
1246 },
1247 };
1248 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1249 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1250 .pNext = NULL,
1251 .count = 2,
1252 .pTypeCount = type_counts,
1253 };
1254 XGL_RESULT err;
1255
1256 err = xglCreateDescriptorRegion(demo->device,
1257 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1258 &descriptor_region, &demo->desc_region);
1259 assert(!err);
1260}
1261
1262static void demo_prepare_descriptor_set(struct demo *demo)
1263{
1264 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1265 &demo->uniform_data.attach;
1266 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1267 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1268 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1269 XGL_UPDATE_BUFFERS update_vs;
1270 XGL_RESULT err;
1271 uint32_t count;
1272 XGL_UINT i;
1273
1274 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1275 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1276 view_info[i].pNext = NULL;
1277 view_info[i].view = demo->textures[i].view,
1278 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1279
1280 combined_info[i].pSampler = demo->textures[i].sampler;
1281 combined_info[i].pImageView = &view_info[i];
1282 }
1283
1284 memset(&update_vs, 0, sizeof(update_vs));
1285 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1286 update_vs.pNext = &update_fs;
1287 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1288 update_vs.count = 1;
1289 update_vs.pBufferViews = &view_info_vs;
1290
1291 memset(&update_fs, 0, sizeof(update_fs));
1292 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1293 update_fs.index = 1;
1294 update_fs.count = DEMO_TEXTURE_COUNT;
1295 update_fs.pSamplerImageViews = combined_info;
1296
1297 err = xglAllocDescriptorSets(demo->desc_region,
1298 XGL_DESCRIPTOR_SET_USAGE_STATIC,
1299 1, demo->desc_layout_last,
1300 &demo->desc_set, &count);
1301 assert(!err && count == 1);
1302
1303 xglBeginDescriptorRegionUpdate(demo->device,
1304 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1305
1306 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1307 xglUpdateDescriptors(demo->desc_set, &update_vs);
1308
1309 xglEndDescriptorRegionUpdate(demo->device, demo->cmd);
1310}
1311
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001312static void demo_prepare(struct demo *demo)
1313{
1314 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1315 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1316 .pNext = NULL,
1317 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1318 .flags = 0,
1319 };
1320 XGL_RESULT err;
1321
1322 demo_prepare_buffers(demo);
1323 demo_prepare_depth(demo);
1324 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001325 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001326
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001327 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001328 demo_prepare_pipeline(demo);
1329 demo_prepare_dynamic_states(demo);
1330
1331 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
1332 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001333
1334 demo_prepare_descriptor_region(demo);
1335 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001336}
1337
1338static void demo_handle_event(struct demo *demo,
1339 const xcb_generic_event_t *event)
1340{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001341 u_int8_t event_code = event->response_type & 0x7f;
1342 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001343 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001344 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001345 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001346 case XCB_CLIENT_MESSAGE:
1347 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1348 (*demo->atom_wm_delete_window).atom) {
1349 demo->quit = true;
1350 }
1351 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001352 case XCB_KEY_RELEASE:
1353 {
1354 const xcb_key_release_event_t *key =
1355 (const xcb_key_release_event_t *) event;
1356
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001357 switch (key->detail) {
1358 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001359 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001360 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001361 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001362 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001363 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001364 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001365 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001366 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001367 case 0x41:
1368 demo->pause = !demo->pause;
1369 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001370 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371 }
1372 break;
1373 default:
1374 break;
1375 }
1376}
1377
1378static void demo_run(struct demo *demo)
1379{
1380 xcb_flush(demo->connection);
1381
1382 while (!demo->quit) {
1383 xcb_generic_event_t *event;
1384
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001385 if (demo->pause) {
1386 event = xcb_wait_for_event(demo->connection);
1387 } else {
1388 event = xcb_poll_for_event(demo->connection);
1389 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001390 if (event) {
1391 demo_handle_event(demo, event);
1392 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001393 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001394
1395 // Wait for work to finish before updating MVP.
1396 xglDeviceWaitIdle(demo->device);
1397 demo_update_data_buffer(demo);
1398
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001399 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001400
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001401 // Wait for work to finish before updating MVP.
1402 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001403 }
1404}
1405
1406static void demo_create_window(struct demo *demo)
1407{
1408 uint32_t value_mask, value_list[32];
1409
1410 demo->window = xcb_generate_id(demo->connection);
1411
1412 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1413 value_list[0] = demo->screen->black_pixel;
1414 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1415 XCB_EVENT_MASK_EXPOSURE;
1416
1417 xcb_create_window(demo->connection,
1418 XCB_COPY_FROM_PARENT,
1419 demo->window, demo->screen->root,
1420 0, 0, demo->width, demo->height, 0,
1421 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1422 demo->screen->root_visual,
1423 value_mask, value_list);
1424
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001425 /* Magic code that will send notification when window is destroyed */
1426 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1427 "WM_PROTOCOLS");
1428 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1429
1430 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1431 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1432
1433 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1434 demo->window, (*reply).atom, 4, 32, 1,
1435 &(*demo->atom_wm_delete_window).atom);
1436 free(reply);
1437
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438 xcb_map_window(demo->connection, demo->window);
1439}
1440
1441static void demo_init_xgl(struct demo *demo)
1442{
1443 const XGL_APPLICATION_INFO app = {
1444 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1445 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001446 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001447 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001448 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001449 .engineVersion = 0,
1450 .apiVersion = XGL_MAKE_VERSION(0, 22, 0),
1451 };
1452 const XGL_WSI_X11_CONNECTION_INFO connection = {
1453 .pConnection = demo->connection,
1454 .root = demo->screen->root,
1455 .provider = 0,
1456 };
1457 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1458 .queueNodeIndex = 0,
1459 .queueCount = 1,
1460 };
1461 const XGL_CHAR *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001462 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001463 };
1464 const XGL_DEVICE_CREATE_INFO device = {
1465 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1466 .pNext = NULL,
1467 .queueRecordCount = 1,
1468 .pRequestedQueues = &queue,
1469 .extensionCount = 1,
1470 .ppEnabledExtensionNames = ext_names,
1471 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1472 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1473 };
1474 XGL_RESULT err;
1475 XGL_UINT gpu_count;
1476 XGL_UINT i;
1477
1478 err = xglInitAndEnumerateGpus(&app, NULL, 1, &gpu_count, &demo->gpu);
1479 assert(!err && gpu_count == 1);
1480
1481 for (i = 0; i < device.extensionCount; i++) {
1482 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1483 assert(!err);
1484 }
1485
1486 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1487 assert(!err);
1488
1489 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1490 assert(!err);
1491
1492 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1493 0, &demo->queue);
1494 assert(!err);
1495}
1496
1497static void demo_init_connection(struct demo *demo)
1498{
1499 const xcb_setup_t *setup;
1500 xcb_screen_iterator_t iter;
1501 int scr;
1502
1503 demo->connection = xcb_connect(NULL, &scr);
1504
1505 setup = xcb_get_setup(demo->connection);
1506 iter = xcb_setup_roots_iterator(setup);
1507 while (scr-- > 0)
1508 xcb_screen_next(&iter);
1509
1510 demo->screen = iter.data;
1511}
1512
1513static void demo_init(struct demo *demo)
1514{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001515 vec3 eye = {0.0f, 3.0f, 5.0f};
1516 vec3 origin = {0, 0, 0};
1517 vec3 up = {0.0f, -1.0f, 0.0};
1518
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001519 memset(demo, 0, sizeof(*demo));
1520
1521 demo_init_connection(demo);
1522 demo_init_xgl(demo);
1523
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001524 demo->width = 500;
1525 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001526 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001527
1528 demo->spin_angle = 0.01f;
1529 demo->spin_increment = 0.01f;
1530 demo->pause = false;
1531
1532 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1533 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1534 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001535}
1536
1537static void demo_cleanup(struct demo *demo)
1538{
Jon Ashburnb2a66652015-01-16 09:37:43 -07001539 XGL_UINT i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001540
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001541 xglDestroyObject(demo->desc_set);
1542 xglDestroyObject(demo->desc_region);
1543
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001544 xglDestroyObject(demo->cmd);
1545
1546 xglDestroyObject(demo->viewport);
1547 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001548 xglDestroyObject(demo->color_blend);
1549 xglDestroyObject(demo->depth_stencil);
1550
1551 xglDestroyObject(demo->pipeline);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001552 xglDestroyObject(demo->desc_layout_fs);
1553 xglDestroyObject(demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001554
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001555// xglFreeMemory(demo->vertices.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001556
1557 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1558 xglDestroyObject(demo->textures[i].view);
1559 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001560 for (j = 0; j < demo->textures[i].num_mem; j++)
1561 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001562 xglDestroyObject(demo->textures[i].sampler);
1563 }
1564
1565 xglDestroyObject(demo->depth.view);
1566 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001567 for (j = 0; j < demo->depth.num_mem; j++)
1568 xglFreeMemory(demo->depth.mem[j]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001569 xglDestroyObject(demo->uniform_data.buf);
1570 for (j = 0; j < demo->uniform_data.num_mem; j++)
1571 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001572
1573 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001574 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001575 xglDestroyObject(demo->buffers[i].view);
1576 xglDestroyObject(demo->buffers[i].image);
1577 }
1578
1579 xglDestroyDevice(demo->device);
1580
1581 xcb_destroy_window(demo->connection, demo->window);
1582 xcb_disconnect(demo->connection);
1583}
1584
1585int main(void)
1586{
1587 struct demo demo;
1588
1589 demo_init(&demo);
1590
1591 demo_prepare(&demo);
1592 demo_create_window(&demo);
1593 demo_run(&demo);
1594
1595 demo_cleanup(&demo);
1596
1597 return 0;
1598}