blob: d7deecb3395f317b05304537f86e73610d0244d6 [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{
452 const XGL_FORMAT depth_format = { XGL_CH_FMT_R16, XGL_NUM_FMT_DS };
453 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,
474 .alignment = 0,
475 .flags = 0,
476 .heapCount = 0,
477 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
478 };
479 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
480 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
481 .pNext = NULL,
482 .image = XGL_NULL_HANDLE,
483 .mipLevel = 0,
484 .baseArraySlice = 0,
485 .arraySize = 1,
486 .flags = 0,
487 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700488 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Jon Ashburnbd75a812014-11-21 13:29:30 -0700489 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700490 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
491 XGL_SIZE img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600492 XGL_RESULT err;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700493 XGL_UINT num_allocations = 0;
494 XGL_SIZE num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600495
496 demo->depth.format = depth_format;
497
498 /* create image */
499 err = xglCreateImage(demo->device, &image,
500 &demo->depth.image);
501 assert(!err);
502
Jon Ashburnb2a66652015-01-16 09:37:43 -0700503
504 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
505 assert(!err && num_alloc_size == sizeof(num_allocations));
506 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
507 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
508 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600509 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700510 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
511 &mem_reqs_size, mem_reqs);
512 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700513 err = xglGetObjectInfo(demo->depth.image,
514 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
515 &img_reqs_size, &img_reqs);
516 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
517 img_alloc.usage = img_reqs.usage;
518 img_alloc.formatClass = img_reqs.formatClass;
519 img_alloc.samples = img_reqs.samples;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700520 for (XGL_UINT i = 0; i < num_allocations; i ++) {
521 mem_alloc.allocationSize = mem_reqs[i].size;
522 mem_alloc.alignment = mem_reqs[i].alignment;
523 mem_alloc.heapCount = mem_reqs[i].heapCount;
524 XGL_UINT heapInfo[mem_reqs[i].heapCount];
525 mem_alloc.pHeaps = (const XGL_UINT *) heapInfo;
526 memcpy(heapInfo, mem_reqs[i].pHeaps,
527 sizeof(mem_reqs[i].pHeaps[0]) * mem_reqs[i].heapCount);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600528
Jon Ashburnb2a66652015-01-16 09:37:43 -0700529 /* allocate memory */
530 err = xglAllocMemory(demo->device, &mem_alloc,
531 &(demo->depth.mem[i]));
532 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600533
Jon Ashburnb2a66652015-01-16 09:37:43 -0700534 /* bind memory */
535 err = xglBindObjectMemory(demo->depth.image, i,
536 demo->depth.mem[i], 0);
537 assert(!err);
538 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539
540 /* create image view */
541 view.image = demo->depth.image;
542 err = xglCreateDepthStencilView(demo->device, &view,
543 &demo->depth.view);
544 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600545}
546
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600547/** loadTexture
548 * loads a png file into an memory object, using cstdio , libpng.
549 *
550 * \param demo : Needed to access XGL calls
551 * \param filename : the png file to be loaded
552 * \param width : width of png, to be updated as a side effect of this function
553 * \param height : height of png, to be updated as a side effect of this function
554 *
555 * \return bool : an opengl texture id. true if successful?,
556 * should be validated by the client of this function.
557 *
558 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
559 * Modified to copy image to memory
560 *
561 */
562bool loadTexture(char *filename, XGL_UINT8 *rgba_data,
563 XGL_SUBRESOURCE_LAYOUT *layout,
564 XGL_INT *width, XGL_INT *height)
565{
566 //header for testing if it is a png
567 png_byte header[8];
568 int i, is_png, bit_depth, color_type,rowbytes;
569 png_uint_32 twidth, theight;
570 png_structp png_ptr;
571 png_infop info_ptr, end_info;
572 png_byte *image_data;
573 png_bytep *row_pointers;
574
575 //open file as binary
576 FILE *fp = fopen(filename, "rb");
577 if (!fp) {
578 return false;
579 }
580
581 //read the header
582 fread(header, 1, 8, fp);
583
584 //test if png
585 is_png = !png_sig_cmp(header, 0, 8);
586 if (!is_png) {
587 fclose(fp);
588 return false;
589 }
590
591 //create png struct
592 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
593 NULL, NULL);
594 if (!png_ptr) {
595 fclose(fp);
596 return (false);
597 }
598
599 //create png info struct
600 info_ptr = png_create_info_struct(png_ptr);
601 if (!info_ptr) {
602 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
603 fclose(fp);
604 return (false);
605 }
606
607 //create png info struct
608 end_info = png_create_info_struct(png_ptr);
609 if (!end_info) {
610 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
611 fclose(fp);
612 return (false);
613 }
614
615 //png error stuff, not sure libpng man suggests this.
616 if (setjmp(png_jmpbuf(png_ptr))) {
617 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
618 fclose(fp);
619 return (false);
620 }
621
622 //init png reading
623 png_init_io(png_ptr, fp);
624
625 //let libpng know you already read the first 8 bytes
626 png_set_sig_bytes(png_ptr, 8);
627
628 // read all the info up to the image data
629 png_read_info(png_ptr, info_ptr);
630
631 // get info about png
632 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
633 NULL, NULL, NULL);
634
635 //update width and height based on png info
636 *width = twidth;
637 *height = theight;
638
639 // Require that incoming texture be 8bits per color component
640 // and 4 components (RGBA).
641 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
642 png_get_channels(png_ptr, info_ptr) != 4) {
643 return false;
644 }
645
646 if (rgba_data == NULL) {
647 // If data pointer is null, we just want the width & height
648 // clean up memory and close stuff
649 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
650 fclose(fp);
651
652 return true;
653 }
654
655 // Update the png info struct.
656 png_read_update_info(png_ptr, info_ptr);
657
658 // Row size in bytes.
659 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
660
661 // Allocate the image_data as a big block, to be given to opengl
662 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
663 if (!image_data) {
664 //clean up memory and close stuff
665 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
666 fclose(fp);
667 return false;
668 }
669
670 // row_pointers is for pointing to image_data for reading the png with libpng
671 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
672 if (!row_pointers) {
673 //clean up memory and close stuff
674 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
675 // delete[] image_data;
676 fclose(fp);
677 return false;
678 }
679 // set the individual row_pointers to point at the correct offsets of image_data
680 for (i = 0; i < theight; ++i)
681 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
682
683 // read the png into image_data through row_pointers
684 png_read_image(png_ptr, row_pointers);
685
686 // clean up memory and close stuff
687 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
688 free(row_pointers);
689 free(image_data);
690 fclose(fp);
691
692 return true;
693}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600694
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600695static void demo_prepare_textures(struct demo *demo)
696{
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600697 const XGL_FORMAT tex_format = { XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600698 XGL_INT tex_width;
699 XGL_INT tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600700 XGL_RESULT err;
701 XGL_UINT i;
702
703 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
704 const XGL_SAMPLER_CREATE_INFO sampler = {
705 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
706 .pNext = NULL,
707 .magFilter = XGL_TEX_FILTER_NEAREST,
708 .minFilter = XGL_TEX_FILTER_NEAREST,
709 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600710 .addressU = XGL_TEX_ADDRESS_CLAMP,
711 .addressV = XGL_TEX_ADDRESS_CLAMP,
712 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600713 .mipLodBias = 0.0f,
714 .maxAnisotropy = 0,
715 .compareFunc = XGL_COMPARE_NEVER,
716 .minLod = 0.0f,
717 .maxLod = 0.0f,
718 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
719 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600720
721 assert(loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height));
722
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600723 const XGL_IMAGE_CREATE_INFO image = {
724 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
725 .pNext = NULL,
726 .imageType = XGL_IMAGE_2D,
727 .format = tex_format,
728 .extent = { tex_width, tex_height, 1 },
729 .mipLevels = 1,
730 .arraySize = 1,
731 .samples = 1,
732 .tiling = XGL_LINEAR_TILING,
733 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
734 .flags = 0,
735 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700736 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
737 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
738 .pNext = NULL,
739 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600740 XGL_MEMORY_ALLOC_INFO mem_alloc = {
741 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700742 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743 .allocationSize = 0,
744 .alignment = 0,
745 .flags = 0,
746 .heapCount = 0,
Tony Barbour29645d02015-01-16 14:27:35 -0700747 .pHeaps = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600748 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
749 };
750 XGL_IMAGE_VIEW_CREATE_INFO view = {
751 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
752 .pNext = NULL,
753 .image = XGL_NULL_HANDLE,
754 .viewType = XGL_IMAGE_VIEW_2D,
755 .format = image.format,
756 .channels = { XGL_CHANNEL_SWIZZLE_R,
757 XGL_CHANNEL_SWIZZLE_G,
758 XGL_CHANNEL_SWIZZLE_B,
759 XGL_CHANNEL_SWIZZLE_A, },
760 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
761 .minLod = 0.0f,
762 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700763
764 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Jon Ashburnbd75a812014-11-21 13:29:30 -0700765 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700766 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
767 XGL_SIZE img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700768 XGL_UINT num_allocations = 0;
769 XGL_SIZE num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600770
771 /* create sampler */
772 err = xglCreateSampler(demo->device, &sampler,
773 &demo->textures[i].sampler);
774 assert(!err);
775
776 /* create image */
777 err = xglCreateImage(demo->device, &image,
778 &demo->textures[i].image);
779 assert(!err);
780
781 err = xglGetObjectInfo(demo->textures[i].image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700782 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
783 &num_alloc_size, &num_allocations);
784 assert(!err && num_alloc_size == sizeof(num_allocations));
785 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
786 demo->textures[i].mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
787 demo->textures[i].num_mem = num_allocations;
788 err = xglGetObjectInfo(demo->textures[i].image,
789 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
790 &mem_reqs_size, mem_reqs);
791 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700792 err = xglGetObjectInfo(demo->textures[i].image,
793 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
794 &img_reqs_size, &img_reqs);
795 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
796 img_alloc.usage = img_reqs.usage;
797 img_alloc.formatClass = img_reqs.formatClass;
798 img_alloc.samples = img_reqs.samples;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700799 for (XGL_UINT j = 0; j < num_allocations; j ++) {
800 mem_alloc.allocationSize = mem_reqs[j].size;
801 mem_alloc.alignment = mem_reqs[j].alignment;
802 mem_alloc.heapCount = mem_reqs[j].heapCount;
803 XGL_UINT heapInfo[mem_reqs[j].heapCount];
804 mem_alloc.pHeaps = (const XGL_UINT *)heapInfo;
805 memcpy(heapInfo, mem_reqs[j].pHeaps,
806 sizeof(mem_reqs[j].pHeaps[0]) * mem_reqs[j].heapCount);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600807
Jon Ashburnb2a66652015-01-16 09:37:43 -0700808 /* allocate memory */
809 err = xglAllocMemory(demo->device, &mem_alloc,
810 &(demo->textures[i].mem[j]));
811 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600812
Jon Ashburnb2a66652015-01-16 09:37:43 -0700813 /* bind memory */
814 err = xglBindObjectMemory(demo->textures[i].image, j,
815 demo->textures[i].mem[j], 0);
816 assert(!err);
817 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600818
819 /* create image view */
820 view.image = demo->textures[i].image;
821 err = xglCreateImageView(demo->device, &view,
822 &demo->textures[i].view);
823 assert(!err);
824 }
825
826 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
827 const XGL_IMAGE_SUBRESOURCE subres = {
828 .aspect = XGL_IMAGE_ASPECT_COLOR,
829 .mipLevel = 0,
830 .arraySlice = 0,
831 };
832 XGL_SUBRESOURCE_LAYOUT layout;
Chia-I Wu170de772015-01-05 16:27:42 +0800833 XGL_SIZE layout_size = sizeof(layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600834 XGL_VOID *data;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600835
836 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
837 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
838 assert(!err && layout_size == sizeof(layout));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700839 assert(demo->textures[i].num_mem == 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600840
Jon Ashburnb2a66652015-01-16 09:37:43 -0700841 err = xglMapMemory(demo->textures[i].mem[0], 0, &data);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600842 assert(!err);
843
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600844 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600845
Jon Ashburnb2a66652015-01-16 09:37:43 -0700846 err = xglUnmapMemory(demo->textures[i].mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847 assert(!err);
848 }
849}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600850
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600851void demo_prepare_cube_data_buffer(struct demo *demo)
852{
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800853 XGL_BUFFER_CREATE_INFO buf_info;
854 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700855 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
856 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
857 .pNext = NULL,
858 };
859 XGL_MEMORY_ALLOC_INFO alloc_info = {
860 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
861 .pNext = &buf_alloc,
862 .allocationSize = 0,
863 .alignment = 0,
864 .flags = 0,
865 .heapCount = 0,
866 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
867 };
868 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800869 XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700870 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
871 XGL_SIZE buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
872 XGL_UINT num_allocations = 0;
873 XGL_SIZE num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600874 XGL_UINT8 *pData;
875 int i;
876 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600877 XGL_RESULT err;
878 struct xgltexcube_vs_uniform data;
879
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600880 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600881 mat4x4_mul(MVP, VP, demo->model_matrix);
882 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600883// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600884
885 for (i=0; i<12*3; i++) {
886 data.position[i][0] = g_vertex_buffer_data[i*3];
887 data.position[i][1] = g_vertex_buffer_data[i*3+1];
888 data.position[i][2] = g_vertex_buffer_data[i*3+2];
889 data.position[i][3] = 1.0f;
890 data.attr[i][0] = g_uv_buffer_data[2*i];
891 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
892 data.attr[i][2] = 0;
893 data.attr[i][3] = 0;
894 }
895
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800896 memset(&buf_info, 0, sizeof(buf_info));
897 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
898 buf_info.size = sizeof(data);
899 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
900 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
901 assert(!err);
902
903 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700904 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
905 &num_alloc_size, &num_allocations);
906 assert(!err && num_alloc_size == sizeof(num_allocations));
907 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
908 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
909 demo->uniform_data.num_mem = num_allocations;
910 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800911 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700912 &mem_reqs_size, mem_reqs);
913 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
914 err = xglGetObjectInfo(demo->uniform_data.buf,
915 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
916 &buf_reqs_size, &buf_reqs);
917 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
918 buf_alloc.usage = buf_reqs.usage;
919 for (XGL_UINT i = 0; i < num_allocations; i ++) {
920 alloc_info.allocationSize = mem_reqs[i].size;
921 alloc_info.alignment = mem_reqs[i].alignment;
922 alloc_info.heapCount = mem_reqs[i].heapCount;
923 XGL_UINT heapInfo[mem_reqs[i].heapCount];
924 alloc_info.pHeaps = (const XGL_UINT *)heapInfo;
925 memcpy(heapInfo, mem_reqs[i].pHeaps,
926 sizeof(mem_reqs[i].pHeaps[0]) * mem_reqs[i].heapCount);
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800927
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700928 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
929 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600930
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700931 err = xglMapMemory(demo->uniform_data.mem[i], 0, (XGL_VOID **) &pData);
932 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600933
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700934 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600935
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700936 err = xglUnmapMemory(demo->uniform_data.mem[i]);
937 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600938
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700939 err = xglBindObjectMemory(demo->uniform_data.buf, i,
940 demo->uniform_data.mem[i], 0);
941 assert(!err);
942 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800943
944 memset(&view_info, 0, sizeof(view_info));
945 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
946 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +0800947 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800948 view_info.offset = 0;
949 view_info.range = sizeof(data);
950
951 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
952 assert(!err);
953
954 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
955 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600956}
957
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800958static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600959{
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800960 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
961 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600962 .pNext = NULL,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800963 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
964 .count = 1,
965 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
966 .immutableSampler = XGL_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600967 };
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800968 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
969 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
970 .pNext = NULL,
971 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
972 .count = DEMO_TEXTURE_COUNT,
973 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
974 .immutableSampler = XGL_NULL_HANDLE,
975 };
976 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600977 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800979 err = xglCreateDescriptorSetLayout(demo->device,
980 XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, &bind_point,
981 XGL_NULL_HANDLE, &descriptor_layout_vs,
982 &demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600983 assert(!err);
984
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800985 err = xglCreateDescriptorSetLayout(demo->device,
986 XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, &bind_point,
987 demo->desc_layout_vs, &descriptor_layout_fs,
988 &demo->desc_layout_fs);
989 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800991 demo->desc_layout_last = &demo->desc_layout_fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600992}
993
994static XGL_SHADER demo_prepare_shader(struct demo *demo,
995 XGL_PIPELINE_SHADER_STAGE stage,
996 const void *code,
997 XGL_SIZE size)
998{
999 XGL_SHADER_CREATE_INFO createInfo;
1000 XGL_SHADER shader;
1001 XGL_RESULT err;
1002
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001003
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1005 createInfo.pNext = NULL;
1006
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001007#ifdef EXTERNAL_BIL
1008 createInfo.codeSize = size;
1009 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001010 createInfo.flags = 0;
1011
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001012 err = xglCreateShader(demo->device, &createInfo, &shader);
1013 if (err) {
1014 free((void *) createInfo.pCode);
1015 }
1016#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001017 // Create fake BIL structure to feed GLSL
1018 // to the driver "under the covers"
1019 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1020 createInfo.pCode = malloc(createInfo.codeSize);
1021 createInfo.flags = 0;
1022
1023 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1024 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1025 ((uint32_t *) createInfo.pCode)[1] = 0;
1026 ((uint32_t *) createInfo.pCode)[2] = stage;
1027 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1028
1029 err = xglCreateShader(demo->device, &createInfo, &shader);
1030 if (err) {
1031 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001032 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001033 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001034#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035
1036 return shader;
1037}
1038
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001039char *demo_read_bil(const char *filename, XGL_SIZE *psize)
1040{
1041 long int size;
1042 void *shader_code;
1043
1044 FILE *fp = fopen(filename, "rb");
1045 if (!fp) return NULL;
1046
1047 fseek(fp, 0L, SEEK_END);
1048 size = ftell(fp);
1049
1050 fseek(fp, 0L, SEEK_SET);
1051
1052 shader_code = malloc(size);
1053 fread(shader_code, size, 1, fp);
1054
1055 *psize = size;
1056
1057 return shader_code;
1058}
1059
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001060static XGL_SHADER demo_prepare_vs(struct demo *demo)
1061{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001062#ifdef EXTERNAL_BIL
1063 void *vertShaderCode;
1064 XGL_SIZE size;
1065
1066 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1067
1068 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1069 vertShaderCode, size);
1070#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001071 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001072 "#version 140\n"
1073 "#extension GL_ARB_separate_shader_objects : enable\n"
1074 "#extension GL_ARB_shading_language_420pack : enable\n"
1075 "\n"
1076 "layout(binding = 0) uniform buf {\n"
1077 " mat4 MVP;\n"
1078 " vec4 position[12*3];\n"
1079 " vec4 attr[12*3];\n"
1080 "} ubuf;\n"
1081 "\n"
1082 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001083 "\n"
1084 "void main() \n"
1085 "{\n"
1086 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001087 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1088 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001089
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001090 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1091 (const void *) vertShaderText,
1092 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001093#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001094}
1095
1096static XGL_SHADER demo_prepare_fs(struct demo *demo)
1097{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001098#ifdef EXTERNAL_BIL
1099 void *fragShaderCode;
1100 XGL_SIZE size;
1101
1102 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1103
1104 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1105 fragShaderCode, size);
1106#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001107 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001108 "#version 140\n"
1109 "#extension GL_ARB_separate_shader_objects : enable\n"
1110 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001111 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001112 "\n"
1113 "layout (location = 0) in vec4 texcoord;\n"
1114 "void main() {\n"
1115 " gl_FragColor = texture(tex, texcoord.xy);\n"
1116 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001117
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001118 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1119 (const void *) fragShaderText,
1120 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001121#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001122}
1123
1124static void demo_prepare_pipeline(struct demo *demo)
1125{
1126 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001127 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1128 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001129 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1130 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001131 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1132 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001133 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1134 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001135 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001136
1137 memset(&pipeline, 0, sizeof(pipeline));
1138 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001139 pipeline.lastSetLayout = *demo->desc_layout_last;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001140
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001141 memset(&ia, 0, sizeof(ia));
1142 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1143 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1144
1145 memset(&rs, 0, sizeof(rs));
1146 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001147 rs.fillMode = XGL_FILL_SOLID;
1148 rs.cullMode = XGL_CULL_NONE;
1149 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001150
1151 memset(&cb, 0, sizeof(cb));
1152 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001153 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1154 memset(att_state, 0, sizeof(att_state));
1155 att_state[0].format = demo->format;
1156 att_state[0].channelWriteMask = 0xf;
1157 att_state[0].blendEnable = XGL_FALSE;
1158 cb.attachmentCount = 1;
1159 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001160
Tony Barbour29645d02015-01-16 14:27:35 -07001161 memset(&vp, 0, sizeof(vp));
1162 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
1163 vp.scissorEnable = XGL_FALSE;
1164
1165 memset(&ds, 0, sizeof(ds));
1166 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1167 ds.format = demo->depth.format;
1168 ds.depthTestEnable = XGL_TRUE;
1169 ds.depthWriteEnable = XGL_TRUE;
1170 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1171 ds.depthBoundsEnable = XGL_FALSE;
1172 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1173 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1174 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1175 ds.stencilTestEnable = XGL_FALSE;
1176 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001177
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001178 memset(&vs, 0, sizeof(vs));
1179 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1180 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001181 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001182 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001183
1184 memset(&fs, 0, sizeof(fs));
1185 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1186 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1187 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001188 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001189
1190 memset(&ms, 0, sizeof(ms));
1191 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1192 ms.sampleMask = 1;
1193 ms.multisampleEnable = XGL_FALSE;
1194 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001195
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001196 pipeline.pNext = (const XGL_VOID *) &ia;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001197 ia.pNext = (const XGL_VOID *) &rs;
1198 rs.pNext = (const XGL_VOID *) &cb;
Tony Barbour29645d02015-01-16 14:27:35 -07001199 cb.pNext = (const XGL_VOID *) &ms;
1200 ms.pNext = (const XGL_VOID *) &vp;
1201 vp.pNext = (const XGL_VOID *) &ds;
1202 ds.pNext = (const XGL_VOID *) &vs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203 vs.pNext = (const XGL_VOID *) &fs;
1204
1205 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1206 assert(!err);
1207
1208 xglDestroyObject(vs.shader.shader);
1209 xglDestroyObject(fs.shader.shader);
1210}
1211
1212static void demo_prepare_dynamic_states(struct demo *demo)
1213{
Tony Barbour29645d02015-01-16 14:27:35 -07001214 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1215 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1216 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1217 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001218 XGL_RESULT err;
1219
Tony Barbour29645d02015-01-16 14:27:35 -07001220 memset(&viewport_create, 0, sizeof(viewport_create));
1221 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
1222 viewport_create.viewportCount = 1;
1223 XGL_VIEWPORT viewport;
1224 viewport.height = (XGL_FLOAT) demo->height;
1225 viewport.width = (XGL_FLOAT) demo->width;
1226 viewport.minDepth = (XGL_FLOAT) 0.0f;
1227 viewport.maxDepth = (XGL_FLOAT) 1.0f;
1228 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001229
1230 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001231 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232
1233 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001234 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001235
1236 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001237 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1238 depth_stencil.stencilBackRef = 0;
1239 depth_stencil.stencilFrontRef = 0;
1240 depth_stencil.stencilReadMask = 0xff;
1241 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001242
Tony Barbour29645d02015-01-16 14:27:35 -07001243 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001244 assert(!err);
1245
Tony Barbour29645d02015-01-16 14:27:35 -07001246 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247 assert(!err);
1248
Tony Barbour29645d02015-01-16 14:27:35 -07001249 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001250 &color_blend, &demo->color_blend);
1251 assert(!err);
1252
Tony Barbour29645d02015-01-16 14:27:35 -07001253 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001254 &depth_stencil, &demo->depth_stencil);
1255 assert(!err);
1256}
1257
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001258static void demo_prepare_descriptor_region(struct demo *demo)
1259{
1260 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1261 [0] = {
1262 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1263 .count = 1,
1264 },
1265 [1] = {
1266 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1267 .count = DEMO_TEXTURE_COUNT,
1268 },
1269 };
1270 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1271 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1272 .pNext = NULL,
1273 .count = 2,
1274 .pTypeCount = type_counts,
1275 };
1276 XGL_RESULT err;
1277
1278 err = xglCreateDescriptorRegion(demo->device,
1279 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1280 &descriptor_region, &demo->desc_region);
1281 assert(!err);
1282}
1283
1284static void demo_prepare_descriptor_set(struct demo *demo)
1285{
1286 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1287 &demo->uniform_data.attach;
1288 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1289 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1290 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1291 XGL_UPDATE_BUFFERS update_vs;
1292 XGL_RESULT err;
1293 uint32_t count;
1294 XGL_UINT i;
1295
1296 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1297 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1298 view_info[i].pNext = NULL;
1299 view_info[i].view = demo->textures[i].view,
1300 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1301
1302 combined_info[i].pSampler = demo->textures[i].sampler;
1303 combined_info[i].pImageView = &view_info[i];
1304 }
1305
1306 memset(&update_vs, 0, sizeof(update_vs));
1307 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1308 update_vs.pNext = &update_fs;
1309 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1310 update_vs.count = 1;
1311 update_vs.pBufferViews = &view_info_vs;
1312
1313 memset(&update_fs, 0, sizeof(update_fs));
1314 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1315 update_fs.index = 1;
1316 update_fs.count = DEMO_TEXTURE_COUNT;
1317 update_fs.pSamplerImageViews = combined_info;
1318
1319 err = xglAllocDescriptorSets(demo->desc_region,
1320 XGL_DESCRIPTOR_SET_USAGE_STATIC,
1321 1, demo->desc_layout_last,
1322 &demo->desc_set, &count);
1323 assert(!err && count == 1);
1324
1325 xglBeginDescriptorRegionUpdate(demo->device,
1326 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1327
1328 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1329 xglUpdateDescriptors(demo->desc_set, &update_vs);
1330
1331 xglEndDescriptorRegionUpdate(demo->device, demo->cmd);
1332}
1333
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001334static void demo_prepare(struct demo *demo)
1335{
1336 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1337 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1338 .pNext = NULL,
1339 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1340 .flags = 0,
1341 };
1342 XGL_RESULT err;
1343
1344 demo_prepare_buffers(demo);
1345 demo_prepare_depth(demo);
1346 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001347 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001348
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001349 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001350 demo_prepare_pipeline(demo);
1351 demo_prepare_dynamic_states(demo);
1352
1353 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
1354 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001355
1356 demo_prepare_descriptor_region(demo);
1357 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001358}
1359
1360static void demo_handle_event(struct demo *demo,
1361 const xcb_generic_event_t *event)
1362{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001363 u_int8_t event_code = event->response_type & 0x7f;
1364 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001366 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001367 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001368 case XCB_CLIENT_MESSAGE:
1369 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1370 (*demo->atom_wm_delete_window).atom) {
1371 demo->quit = true;
1372 }
1373 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001374 case XCB_KEY_RELEASE:
1375 {
1376 const xcb_key_release_event_t *key =
1377 (const xcb_key_release_event_t *) event;
1378
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001379 switch (key->detail) {
1380 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001381 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001382 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001383 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001384 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001385 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001386 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001387 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001388 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001389 case 0x41:
1390 demo->pause = !demo->pause;
1391 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001392 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001393 }
1394 break;
1395 default:
1396 break;
1397 }
1398}
1399
1400static void demo_run(struct demo *demo)
1401{
1402 xcb_flush(demo->connection);
1403
1404 while (!demo->quit) {
1405 xcb_generic_event_t *event;
1406
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001407 if (demo->pause) {
1408 event = xcb_wait_for_event(demo->connection);
1409 } else {
1410 event = xcb_poll_for_event(demo->connection);
1411 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412 if (event) {
1413 demo_handle_event(demo, event);
1414 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001415 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001416
1417 // Wait for work to finish before updating MVP.
1418 xglDeviceWaitIdle(demo->device);
1419 demo_update_data_buffer(demo);
1420
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001421 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001422
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001423 // Wait for work to finish before updating MVP.
1424 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425 }
1426}
1427
1428static void demo_create_window(struct demo *demo)
1429{
1430 uint32_t value_mask, value_list[32];
1431
1432 demo->window = xcb_generate_id(demo->connection);
1433
1434 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1435 value_list[0] = demo->screen->black_pixel;
1436 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1437 XCB_EVENT_MASK_EXPOSURE;
1438
1439 xcb_create_window(demo->connection,
1440 XCB_COPY_FROM_PARENT,
1441 demo->window, demo->screen->root,
1442 0, 0, demo->width, demo->height, 0,
1443 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1444 demo->screen->root_visual,
1445 value_mask, value_list);
1446
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001447 /* Magic code that will send notification when window is destroyed */
1448 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1449 "WM_PROTOCOLS");
1450 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1451
1452 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1453 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1454
1455 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1456 demo->window, (*reply).atom, 4, 32, 1,
1457 &(*demo->atom_wm_delete_window).atom);
1458 free(reply);
1459
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001460 xcb_map_window(demo->connection, demo->window);
1461}
1462
1463static void demo_init_xgl(struct demo *demo)
1464{
1465 const XGL_APPLICATION_INFO app = {
1466 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1467 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001468 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001469 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001470 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001471 .engineVersion = 0,
1472 .apiVersion = XGL_MAKE_VERSION(0, 22, 0),
1473 };
1474 const XGL_WSI_X11_CONNECTION_INFO connection = {
1475 .pConnection = demo->connection,
1476 .root = demo->screen->root,
1477 .provider = 0,
1478 };
1479 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1480 .queueNodeIndex = 0,
1481 .queueCount = 1,
1482 };
1483 const XGL_CHAR *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001484 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001485 };
1486 const XGL_DEVICE_CREATE_INFO device = {
1487 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1488 .pNext = NULL,
1489 .queueRecordCount = 1,
1490 .pRequestedQueues = &queue,
1491 .extensionCount = 1,
1492 .ppEnabledExtensionNames = ext_names,
1493 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1494 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1495 };
1496 XGL_RESULT err;
1497 XGL_UINT gpu_count;
1498 XGL_UINT i;
1499
1500 err = xglInitAndEnumerateGpus(&app, NULL, 1, &gpu_count, &demo->gpu);
1501 assert(!err && gpu_count == 1);
1502
1503 for (i = 0; i < device.extensionCount; i++) {
1504 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1505 assert(!err);
1506 }
1507
1508 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1509 assert(!err);
1510
1511 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1512 assert(!err);
1513
1514 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1515 0, &demo->queue);
1516 assert(!err);
1517}
1518
1519static void demo_init_connection(struct demo *demo)
1520{
1521 const xcb_setup_t *setup;
1522 xcb_screen_iterator_t iter;
1523 int scr;
1524
1525 demo->connection = xcb_connect(NULL, &scr);
1526
1527 setup = xcb_get_setup(demo->connection);
1528 iter = xcb_setup_roots_iterator(setup);
1529 while (scr-- > 0)
1530 xcb_screen_next(&iter);
1531
1532 demo->screen = iter.data;
1533}
1534
1535static void demo_init(struct demo *demo)
1536{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001537 vec3 eye = {0.0f, 3.0f, 5.0f};
1538 vec3 origin = {0, 0, 0};
1539 vec3 up = {0.0f, -1.0f, 0.0};
1540
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001541 memset(demo, 0, sizeof(*demo));
1542
1543 demo_init_connection(demo);
1544 demo_init_xgl(demo);
1545
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001546 demo->width = 500;
1547 demo->height = 500;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001548 demo->format.channelFormat = XGL_CH_FMT_B8G8R8A8;
1549 demo->format.numericFormat = XGL_NUM_FMT_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001550
1551 demo->spin_angle = 0.01f;
1552 demo->spin_increment = 0.01f;
1553 demo->pause = false;
1554
1555 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1556 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1557 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001558}
1559
1560static void demo_cleanup(struct demo *demo)
1561{
Jon Ashburnb2a66652015-01-16 09:37:43 -07001562 XGL_UINT i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001563
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001564 xglDestroyObject(demo->desc_set);
1565 xglDestroyObject(demo->desc_region);
1566
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567 xglDestroyObject(demo->cmd);
1568
1569 xglDestroyObject(demo->viewport);
1570 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001571 xglDestroyObject(demo->color_blend);
1572 xglDestroyObject(demo->depth_stencil);
1573
1574 xglDestroyObject(demo->pipeline);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001575 xglDestroyObject(demo->desc_layout_fs);
1576 xglDestroyObject(demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001577
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001578// xglFreeMemory(demo->vertices.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001579
1580 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1581 xglDestroyObject(demo->textures[i].view);
1582 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001583 for (j = 0; j < demo->textures[i].num_mem; j++)
1584 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001585 xglDestroyObject(demo->textures[i].sampler);
1586 }
1587
1588 xglDestroyObject(demo->depth.view);
1589 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001590 for (j = 0; j < demo->depth.num_mem; j++)
1591 xglFreeMemory(demo->depth.mem[j]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001592 xglDestroyObject(demo->uniform_data.buf);
1593 for (j = 0; j < demo->uniform_data.num_mem; j++)
1594 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001595
1596 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001597 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001598 xglDestroyObject(demo->buffers[i].view);
1599 xglDestroyObject(demo->buffers[i].image);
1600 }
1601
1602 xglDestroyDevice(demo->device);
1603
1604 xcb_destroy_window(demo->connection, demo->window);
1605 xcb_disconnect(demo->connection);
1606}
1607
1608int main(void)
1609{
1610 struct demo demo;
1611
1612 demo_init(&demo);
1613
1614 demo_prepare(&demo);
1615 demo_create_window(&demo);
1616 demo_run(&demo);
1617
1618 demo_cleanup(&demo);
1619
1620 return 0;
1621}