blob: 0a21424d0a3cfb7148981473d1282cfb8183f6b3 [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
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060034 float mvp[4][4];
35 float position[12*3][4];
36 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060037};
38
39struct xgltexcube_vs_uniform {
40 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060041 float mvp[4][4];
42 float position[12*3][4];
43 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060044};
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{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060051 float posX, posY, posZ, posW; // Position data
52 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053};
54
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060055struct VertexPosTex
56{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060057 float posX, posY, posZ, posW; // Position data
58 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060059};
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
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060064static const 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
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600114static const 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
Jon Ashburn93cfc432015-01-29 15:47:01 -0700196 XGL_INSTANCE inst;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600197 XGL_PHYSICAL_GPU gpu;
198 XGL_DEVICE device;
199 XGL_QUEUE queue;
200
201 int width, height;
202 XGL_FORMAT format;
203
204 struct {
205 XGL_IMAGE image;
206 XGL_GPU_MEMORY mem;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700207 XGL_CMD_BUFFER cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600208
209 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800210 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600211 } buffers[DEMO_BUFFER_COUNT];
212
213 struct {
214 XGL_FORMAT format;
215
216 XGL_IMAGE image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600217 uint32_t num_mem;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700218 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600219 XGL_DEPTH_STENCIL_VIEW view;
220 } depth;
221
222 struct {
223 XGL_SAMPLER sampler;
224
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 char *filename;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600226 XGL_IMAGE image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600227 uint32_t num_mem;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700228 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600229 XGL_IMAGE_VIEW view;
230 } textures[DEMO_TEXTURE_COUNT];
231
232 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800233 XGL_BUFFER buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600234 uint32_t num_mem;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700235 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800236 XGL_BUFFER_VIEW view;
237 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600238 } uniform_data;
239
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800240 XGL_DESCRIPTOR_SET_LAYOUT desc_layout_vs;
241 XGL_DESCRIPTOR_SET_LAYOUT desc_layout_fs;
242 XGL_DESCRIPTOR_SET_LAYOUT *desc_layout_last;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600243 XGL_PIPELINE pipeline;
244
Tony Barbour29645d02015-01-16 14:27:35 -0700245 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
246 XGL_DYNAMIC_RS_STATE_OBJECT raster;
247 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
248 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600249
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600250 mat4x4 projection_matrix;
251 mat4x4 view_matrix;
252 mat4x4 model_matrix;
253
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600254 float spin_angle;
255 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600256 bool pause;
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;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600265 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600266};
267
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700268static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600269{
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 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600278 const float clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
279 const 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,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700284 };
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700285 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
286 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700287 .pNext = &graphics_cmd_buf_info,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700288 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
289 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
290 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600291 XGL_RESULT err;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700292 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
293 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
294 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
295 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
296 .pNext = NULL,
297 .colorAttachmentCount = 1,
298 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
299 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
300 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600301 .width = demo->width,
302 .height = demo->height,
303 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700304 };
305 XGL_RENDER_PASS_CREATE_INFO rp_info;
306
307 memset(&rp_info, 0 , sizeof(rp_info));
308 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
309 assert(!err);
310 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
311 rp_info.renderArea.extent.width = demo->width;
312 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchterc17d7de2015-02-10 14:06:25 -0700313 rp_info.colorAttachmentCount = 1;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700314 rp_info.pColorLoadOps = &load_op;
315 rp_info.pColorStoreOps = &store_op;
316 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
317 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
318 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
319 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
320 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
321 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700323 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600324 assert(!err);
325
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700326 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327 demo->pipeline);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700328 xglCmdBindDescriptorSet(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800329 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600330
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700331 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
332 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
333 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334 demo->color_blend);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700335 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336 demo->depth_stencil);
337
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600338 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
339 clear_range.baseMipLevel = 0;
340 clear_range.mipLevels = 1;
341 clear_range.baseArraySlice = 0;
342 clear_range.arraySize = 1;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700343 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600344 demo->buffers[demo->current_buffer].image,
345 clear_color, 1, &clear_range);
346
347 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700348 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600349 clear_depth, 0, 1, &clear_range);
350
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700351 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600352
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700353 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354 assert(!err);
355}
356
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600357
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600358void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600359{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600360 mat4x4 MVP, Model, VP;
361 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600362 uint8_t *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363 XGL_RESULT err;
364
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600365 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600367 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600368 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600369 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600370 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600371
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700372 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600373 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374 assert(!err);
375
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600376 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600377
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700378 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379 assert(!err);
380}
381
382static void demo_draw(struct demo *demo)
383{
384 const XGL_WSI_X11_PRESENT_INFO present = {
385 .destWindow = demo->window,
386 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800387 .async = true,
388 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600389 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800390 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600391 XGL_RESULT err;
392
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600393 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wubb57f642014-11-07 14:30:34 +0800394 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
395
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -0600396 uint32_t i, idx = 0;
397 XGL_MEMORY_REF *memRefs;
398 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
399 demo->depth.num_mem +
400 demo->textures[0].num_mem +
401 demo->uniform_data.num_mem));
402 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
403 memRefs[idx].mem = demo->depth.mem[i];
404 memRefs[idx].flags = 0;
405 }
406 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
407 memRefs[idx].mem = demo->textures[0].mem[i];
408 memRefs[idx].flags = 0;
409 }
410 memRefs[idx].mem = demo->buffers[0].mem;
411 memRefs[idx++].flags = 0;
412 memRefs[idx].mem = demo->buffers[1].mem;
413 memRefs[idx++].flags = 0;
414 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
415 memRefs[idx].mem = demo->uniform_data.mem[i];
416 memRefs[idx].flags = 0;
417 }
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700418 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
419 0, NULL, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600420 assert(!err);
421
Chia-I Wubb57f642014-11-07 14:30:34 +0800422 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600423 assert(!err);
424
425 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
426}
427
428static void demo_prepare_buffers(struct demo *demo)
429{
430 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
431 .format = demo->format,
432 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
433 .extent = {
434 .width = demo->width,
435 .height = demo->height,
436 },
437 .flags = 0,
438 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800439 const XGL_FENCE_CREATE_INFO fence = {
440 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
441 .pNext = NULL,
442 .flags = 0,
443 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600444 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600445 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600446
447 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
448 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
449 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
450 .pNext = NULL,
451 .format = demo->format,
452 .mipLevel = 0,
453 .baseArraySlice = 0,
454 .arraySize = 1,
455 };
456
457 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
458 &demo->buffers[i].image, &demo->buffers[i].mem);
459 assert(!err);
460
461 color_attachment_view.image = demo->buffers[i].image;
462
463 err = xglCreateColorAttachmentView(demo->device,
464 &color_attachment_view, &demo->buffers[i].view);
465 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800466
467 err = xglCreateFence(demo->device,
468 &fence, &demo->buffers[i].fence);
469 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600470 }
471}
472
473static void demo_prepare_depth(struct demo *demo)
474{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700475 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600476 const XGL_IMAGE_CREATE_INFO image = {
477 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
478 .pNext = NULL,
479 .imageType = XGL_IMAGE_2D,
480 .format = depth_format,
481 .extent = { demo->width, demo->height, 1 },
482 .mipLevels = 1,
483 .arraySize = 1,
484 .samples = 1,
485 .tiling = XGL_OPTIMAL_TILING,
486 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
487 .flags = 0,
488 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700489 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
490 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
491 .pNext = NULL,
492 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600493 XGL_MEMORY_ALLOC_INFO mem_alloc = {
494 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700495 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600496 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700497 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700498 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600499 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
500 };
501 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
502 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
503 .pNext = NULL,
504 .image = XGL_NULL_HANDLE,
505 .mipLevel = 0,
506 .baseArraySlice = 0,
507 .arraySize = 1,
508 .flags = 0,
509 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700510 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600511 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700512 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600513 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600514 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600515 uint32_t num_allocations = 0;
516 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600517
518 demo->depth.format = depth_format;
519
520 /* create image */
521 err = xglCreateImage(demo->device, &image,
522 &demo->depth.image);
523 assert(!err);
524
Jon Ashburnb2a66652015-01-16 09:37:43 -0700525
526 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
527 assert(!err && num_alloc_size == sizeof(num_allocations));
528 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
529 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
530 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600531 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700532 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
533 &mem_reqs_size, mem_reqs);
534 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700535 err = xglGetObjectInfo(demo->depth.image,
536 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
537 &img_reqs_size, &img_reqs);
538 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
539 img_alloc.usage = img_reqs.usage;
540 img_alloc.formatClass = img_reqs.formatClass;
541 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600542 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700543 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544
Jon Ashburnb2a66652015-01-16 09:37:43 -0700545 /* allocate memory */
546 err = xglAllocMemory(demo->device, &mem_alloc,
547 &(demo->depth.mem[i]));
548 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549
Jon Ashburnb2a66652015-01-16 09:37:43 -0700550 /* bind memory */
551 err = xglBindObjectMemory(demo->depth.image, i,
552 demo->depth.mem[i], 0);
553 assert(!err);
554 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555
556 /* create image view */
557 view.image = demo->depth.image;
558 err = xglCreateDepthStencilView(demo->device, &view,
559 &demo->depth.view);
560 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561}
562
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600563/** loadTexture
564 * loads a png file into an memory object, using cstdio , libpng.
565 *
566 * \param demo : Needed to access XGL calls
567 * \param filename : the png file to be loaded
568 * \param width : width of png, to be updated as a side effect of this function
569 * \param height : height of png, to be updated as a side effect of this function
570 *
571 * \return bool : an opengl texture id. true if successful?,
572 * should be validated by the client of this function.
573 *
574 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
575 * Modified to copy image to memory
576 *
577 */
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600578bool loadTexture(char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600579 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600580 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600581{
582 //header for testing if it is a png
583 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700584 int is_png, bit_depth, color_type,rowbytes;
585 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600586 png_structp png_ptr;
587 png_infop info_ptr, end_info;
588 png_byte *image_data;
589 png_bytep *row_pointers;
590
591 //open file as binary
592 FILE *fp = fopen(filename, "rb");
593 if (!fp) {
594 return false;
595 }
596
597 //read the header
598 fread(header, 1, 8, fp);
599
600 //test if png
601 is_png = !png_sig_cmp(header, 0, 8);
602 if (!is_png) {
603 fclose(fp);
604 return false;
605 }
606
607 //create png struct
608 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
609 NULL, NULL);
610 if (!png_ptr) {
611 fclose(fp);
612 return (false);
613 }
614
615 //create png info struct
616 info_ptr = png_create_info_struct(png_ptr);
617 if (!info_ptr) {
618 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
619 fclose(fp);
620 return (false);
621 }
622
623 //create png info struct
624 end_info = png_create_info_struct(png_ptr);
625 if (!end_info) {
626 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
627 fclose(fp);
628 return (false);
629 }
630
631 //png error stuff, not sure libpng man suggests this.
632 if (setjmp(png_jmpbuf(png_ptr))) {
633 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
634 fclose(fp);
635 return (false);
636 }
637
638 //init png reading
639 png_init_io(png_ptr, fp);
640
641 //let libpng know you already read the first 8 bytes
642 png_set_sig_bytes(png_ptr, 8);
643
644 // read all the info up to the image data
645 png_read_info(png_ptr, info_ptr);
646
647 // get info about png
648 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
649 NULL, NULL, NULL);
650
651 //update width and height based on png info
652 *width = twidth;
653 *height = theight;
654
655 // Require that incoming texture be 8bits per color component
656 // and 4 components (RGBA).
657 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
658 png_get_channels(png_ptr, info_ptr) != 4) {
659 return false;
660 }
661
662 if (rgba_data == NULL) {
663 // If data pointer is null, we just want the width & height
664 // clean up memory and close stuff
665 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
666 fclose(fp);
667
668 return true;
669 }
670
671 // Update the png info struct.
672 png_read_update_info(png_ptr, info_ptr);
673
674 // Row size in bytes.
675 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
676
677 // Allocate the image_data as a big block, to be given to opengl
678 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
679 if (!image_data) {
680 //clean up memory and close stuff
681 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
682 fclose(fp);
683 return false;
684 }
685
686 // row_pointers is for pointing to image_data for reading the png with libpng
687 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
688 if (!row_pointers) {
689 //clean up memory and close stuff
690 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
691 // delete[] image_data;
692 fclose(fp);
693 return false;
694 }
695 // set the individual row_pointers to point at the correct offsets of image_data
696 for (i = 0; i < theight; ++i)
697 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
698
699 // read the png into image_data through row_pointers
700 png_read_image(png_ptr, row_pointers);
701
702 // clean up memory and close stuff
703 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
704 free(row_pointers);
705 free(image_data);
706 fclose(fp);
707
708 return true;
709}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600710
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600711static void demo_prepare_textures(struct demo *demo)
712{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700713 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600714 int32_t tex_width;
715 int32_t tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600716 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600717 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600718
719 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
720 const XGL_SAMPLER_CREATE_INFO sampler = {
721 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
722 .pNext = NULL,
723 .magFilter = XGL_TEX_FILTER_NEAREST,
724 .minFilter = XGL_TEX_FILTER_NEAREST,
725 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600726 .addressU = XGL_TEX_ADDRESS_CLAMP,
727 .addressV = XGL_TEX_ADDRESS_CLAMP,
728 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600729 .mipLodBias = 0.0f,
730 .maxAnisotropy = 0,
731 .compareFunc = XGL_COMPARE_NEVER,
732 .minLod = 0.0f,
733 .maxLod = 0.0f,
734 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
735 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600736
Tony Barbour276d9822015-02-05 14:14:33 -0700737 err = loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height);
738 assert(err);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600739
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600740 const XGL_IMAGE_CREATE_INFO image = {
741 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
742 .pNext = NULL,
743 .imageType = XGL_IMAGE_2D,
744 .format = tex_format,
745 .extent = { tex_width, tex_height, 1 },
746 .mipLevels = 1,
747 .arraySize = 1,
748 .samples = 1,
749 .tiling = XGL_LINEAR_TILING,
750 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
751 .flags = 0,
752 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700753 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
754 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
755 .pNext = NULL,
756 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600757 XGL_MEMORY_ALLOC_INFO mem_alloc = {
758 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700759 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600760 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700761 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700762 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600763 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
764 };
765 XGL_IMAGE_VIEW_CREATE_INFO view = {
766 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
767 .pNext = NULL,
768 .image = XGL_NULL_HANDLE,
769 .viewType = XGL_IMAGE_VIEW_2D,
770 .format = image.format,
771 .channels = { XGL_CHANNEL_SWIZZLE_R,
772 XGL_CHANNEL_SWIZZLE_G,
773 XGL_CHANNEL_SWIZZLE_B,
774 XGL_CHANNEL_SWIZZLE_A, },
775 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
776 .minLod = 0.0f,
777 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700778
779 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600780 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700781 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600782 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
783 uint32_t num_allocations = 0;
784 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600785
786 /* create sampler */
787 err = xglCreateSampler(demo->device, &sampler,
788 &demo->textures[i].sampler);
789 assert(!err);
790
791 /* create image */
792 err = xglCreateImage(demo->device, &image,
793 &demo->textures[i].image);
794 assert(!err);
795
796 err = xglGetObjectInfo(demo->textures[i].image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700797 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
798 &num_alloc_size, &num_allocations);
799 assert(!err && num_alloc_size == sizeof(num_allocations));
800 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
801 demo->textures[i].mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
802 demo->textures[i].num_mem = num_allocations;
803 err = xglGetObjectInfo(demo->textures[i].image,
804 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
805 &mem_reqs_size, mem_reqs);
806 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700807 err = xglGetObjectInfo(demo->textures[i].image,
808 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
809 &img_reqs_size, &img_reqs);
810 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
811 img_alloc.usage = img_reqs.usage;
812 img_alloc.formatClass = img_reqs.formatClass;
813 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600814 for (uint32_t j = 0; j < num_allocations; j ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700815 mem_alloc.allocationSize = mem_reqs[j].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600816
Jon Ashburnb2a66652015-01-16 09:37:43 -0700817 /* allocate memory */
818 err = xglAllocMemory(demo->device, &mem_alloc,
819 &(demo->textures[i].mem[j]));
820 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600821
Jon Ashburnb2a66652015-01-16 09:37:43 -0700822 /* bind memory */
823 err = xglBindObjectMemory(demo->textures[i].image, j,
824 demo->textures[i].mem[j], 0);
825 assert(!err);
826 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600827
828 /* create image view */
829 view.image = demo->textures[i].image;
830 err = xglCreateImageView(demo->device, &view,
831 &demo->textures[i].view);
832 assert(!err);
833 }
834
835 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
836 const XGL_IMAGE_SUBRESOURCE subres = {
837 .aspect = XGL_IMAGE_ASPECT_COLOR,
838 .mipLevel = 0,
839 .arraySlice = 0,
840 };
841 XGL_SUBRESOURCE_LAYOUT layout;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600842 size_t layout_size = sizeof(layout);
843 void *data;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600844
845 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
846 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
847 assert(!err && layout_size == sizeof(layout));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700848 assert(demo->textures[i].num_mem == 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600849
Jon Ashburnb2a66652015-01-16 09:37:43 -0700850 err = xglMapMemory(demo->textures[i].mem[0], 0, &data);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600851 assert(!err);
852
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600853 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600854
Jon Ashburnb2a66652015-01-16 09:37:43 -0700855 err = xglUnmapMemory(demo->textures[i].mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600856 assert(!err);
857 }
858}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600859
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600860void demo_prepare_cube_data_buffer(struct demo *demo)
861{
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800862 XGL_BUFFER_CREATE_INFO buf_info;
863 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700864 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
865 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
866 .pNext = NULL,
867 };
868 XGL_MEMORY_ALLOC_INFO alloc_info = {
869 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
870 .pNext = &buf_alloc,
871 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700872 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700873 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700874 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
875 };
876 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600877 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700878 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600879 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
880 uint32_t num_allocations = 0;
881 size_t num_alloc_size = sizeof(num_allocations);
882 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600883 int i;
884 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600885 XGL_RESULT err;
886 struct xgltexcube_vs_uniform data;
887
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600888 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600889 mat4x4_mul(MVP, VP, demo->model_matrix);
890 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600891// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600892
893 for (i=0; i<12*3; i++) {
894 data.position[i][0] = g_vertex_buffer_data[i*3];
895 data.position[i][1] = g_vertex_buffer_data[i*3+1];
896 data.position[i][2] = g_vertex_buffer_data[i*3+2];
897 data.position[i][3] = 1.0f;
898 data.attr[i][0] = g_uv_buffer_data[2*i];
899 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
900 data.attr[i][2] = 0;
901 data.attr[i][3] = 0;
902 }
903
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800904 memset(&buf_info, 0, sizeof(buf_info));
905 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
906 buf_info.size = sizeof(data);
907 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
908 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
909 assert(!err);
910
911 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700912 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
913 &num_alloc_size, &num_allocations);
914 assert(!err && num_alloc_size == sizeof(num_allocations));
915 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
916 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
917 demo->uniform_data.num_mem = num_allocations;
918 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800919 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700920 &mem_reqs_size, mem_reqs);
921 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
922 err = xglGetObjectInfo(demo->uniform_data.buf,
923 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
924 &buf_reqs_size, &buf_reqs);
925 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
926 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600927 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700928 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800929
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700930 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
931 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600932
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600933 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700934 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600935
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700936 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600937
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700938 err = xglUnmapMemory(demo->uniform_data.mem[i]);
939 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600940
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700941 err = xglBindObjectMemory(demo->uniform_data.buf, i,
942 demo->uniform_data.mem[i], 0);
943 assert(!err);
944 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800945
946 memset(&view_info, 0, sizeof(view_info));
947 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
948 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +0800949 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800950 view_info.offset = 0;
951 view_info.range = sizeof(data);
952
953 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
954 assert(!err);
955
956 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
957 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600958}
959
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800960static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600961{
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800962 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
963 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600964 .pNext = NULL,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800965 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
966 .count = 1,
967 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
968 .immutableSampler = XGL_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600969 };
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800970 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
971 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
972 .pNext = NULL,
973 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
974 .count = DEMO_TEXTURE_COUNT,
975 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
976 .immutableSampler = XGL_NULL_HANDLE,
977 };
978 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600979 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600980
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800981 err = xglCreateDescriptorSetLayout(demo->device,
982 XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, &bind_point,
983 XGL_NULL_HANDLE, &descriptor_layout_vs,
984 &demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600985 assert(!err);
986
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800987 err = xglCreateDescriptorSetLayout(demo->device,
988 XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, &bind_point,
989 demo->desc_layout_vs, &descriptor_layout_fs,
990 &demo->desc_layout_fs);
991 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600992
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800993 demo->desc_layout_last = &demo->desc_layout_fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600994}
995
996static XGL_SHADER demo_prepare_shader(struct demo *demo,
997 XGL_PIPELINE_SHADER_STAGE stage,
998 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600999 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001000{
1001 XGL_SHADER_CREATE_INFO createInfo;
1002 XGL_SHADER shader;
1003 XGL_RESULT err;
1004
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001005
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001006 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1007 createInfo.pNext = NULL;
1008
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001009#ifdef EXTERNAL_BIL
1010 createInfo.codeSize = size;
1011 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001012 createInfo.flags = 0;
1013
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001014 err = xglCreateShader(demo->device, &createInfo, &shader);
1015 if (err) {
1016 free((void *) createInfo.pCode);
1017 }
1018#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001019 // Create fake BIL structure to feed GLSL
1020 // to the driver "under the covers"
1021 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1022 createInfo.pCode = malloc(createInfo.codeSize);
1023 createInfo.flags = 0;
1024
1025 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1026 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1027 ((uint32_t *) createInfo.pCode)[1] = 0;
1028 ((uint32_t *) createInfo.pCode)[2] = stage;
1029 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1030
1031 err = xglCreateShader(demo->device, &createInfo, &shader);
1032 if (err) {
1033 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001034 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001036#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001037
1038 return shader;
1039}
1040
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001041char *demo_read_bil(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001042{
1043 long int size;
1044 void *shader_code;
1045
1046 FILE *fp = fopen(filename, "rb");
1047 if (!fp) return NULL;
1048
1049 fseek(fp, 0L, SEEK_END);
1050 size = ftell(fp);
1051
1052 fseek(fp, 0L, SEEK_SET);
1053
1054 shader_code = malloc(size);
1055 fread(shader_code, size, 1, fp);
1056
1057 *psize = size;
1058
1059 return shader_code;
1060}
1061
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001062static XGL_SHADER demo_prepare_vs(struct demo *demo)
1063{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001064#ifdef EXTERNAL_BIL
1065 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001066 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001067
1068 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1069
1070 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1071 vertShaderCode, size);
1072#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001073 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001074 "#version 140\n"
1075 "#extension GL_ARB_separate_shader_objects : enable\n"
1076 "#extension GL_ARB_shading_language_420pack : enable\n"
1077 "\n"
1078 "layout(binding = 0) uniform buf {\n"
1079 " mat4 MVP;\n"
1080 " vec4 position[12*3];\n"
1081 " vec4 attr[12*3];\n"
1082 "} ubuf;\n"
1083 "\n"
1084 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001085 "\n"
1086 "void main() \n"
1087 "{\n"
1088 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001089 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1090 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001091
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001092 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1093 (const void *) vertShaderText,
1094 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001095#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001096}
1097
1098static XGL_SHADER demo_prepare_fs(struct demo *demo)
1099{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001100#ifdef EXTERNAL_BIL
1101 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001102 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001103
1104 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1105
1106 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1107 fragShaderCode, size);
1108#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001109 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001110 "#version 140\n"
1111 "#extension GL_ARB_separate_shader_objects : enable\n"
1112 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001113 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001114 "\n"
1115 "layout (location = 0) in vec4 texcoord;\n"
1116 "void main() {\n"
1117 " gl_FragColor = texture(tex, texcoord.xy);\n"
1118 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001119
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001120 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1121 (const void *) fragShaderText,
1122 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001123#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001124}
1125
1126static void demo_prepare_pipeline(struct demo *demo)
1127{
1128 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001129 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1130 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001131 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1132 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001133 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1134 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001135 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1136 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001137 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001138
1139 memset(&pipeline, 0, sizeof(pipeline));
1140 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001141 pipeline.lastSetLayout = *demo->desc_layout_last;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001142
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001143 memset(&ia, 0, sizeof(ia));
1144 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1145 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1146
1147 memset(&rs, 0, sizeof(rs));
1148 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001149 rs.fillMode = XGL_FILL_SOLID;
1150 rs.cullMode = XGL_CULL_NONE;
1151 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001152
1153 memset(&cb, 0, sizeof(cb));
1154 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001155 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1156 memset(att_state, 0, sizeof(att_state));
1157 att_state[0].format = demo->format;
1158 att_state[0].channelWriteMask = 0xf;
1159 att_state[0].blendEnable = XGL_FALSE;
1160 cb.attachmentCount = 1;
1161 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001162
Tony Barbour29645d02015-01-16 14:27:35 -07001163 memset(&vp, 0, sizeof(vp));
1164 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001165
1166 memset(&ds, 0, sizeof(ds));
1167 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1168 ds.format = demo->depth.format;
1169 ds.depthTestEnable = XGL_TRUE;
1170 ds.depthWriteEnable = XGL_TRUE;
1171 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1172 ds.depthBoundsEnable = XGL_FALSE;
1173 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1174 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1175 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1176 ds.stencilTestEnable = XGL_FALSE;
1177 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001178
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001179 memset(&vs, 0, sizeof(vs));
1180 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1181 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001182 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001183 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001184
1185 memset(&fs, 0, sizeof(fs));
1186 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1187 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1188 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001189 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001190
1191 memset(&ms, 0, sizeof(ms));
1192 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1193 ms.sampleMask = 1;
1194 ms.multisampleEnable = XGL_FALSE;
1195 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001196
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001197 pipeline.pNext = (const void *) &ia;
1198 ia.pNext = (const void *) &rs;
1199 rs.pNext = (const void *) &cb;
1200 cb.pNext = (const void *) &ms;
1201 ms.pNext = (const void *) &vp;
1202 vp.pNext = (const void *) &ds;
1203 ds.pNext = (const void *) &vs;
1204 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001205
1206 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1207 assert(!err);
1208
1209 xglDestroyObject(vs.shader.shader);
1210 xglDestroyObject(fs.shader.shader);
1211}
1212
1213static void demo_prepare_dynamic_states(struct demo *demo)
1214{
Tony Barbour29645d02015-01-16 14:27:35 -07001215 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1216 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1217 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1218 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001219 XGL_RESULT err;
1220
Tony Barbour29645d02015-01-16 14:27:35 -07001221 memset(&viewport_create, 0, sizeof(viewport_create));
1222 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001223 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001224 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001225 XGL_RECT scissor;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001226 viewport.height = (float) demo->height;
1227 viewport.width = (float) demo->width;
1228 viewport.minDepth = (float) 0.0f;
1229 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001230 scissor.extent.width = demo->width;
1231 scissor.extent.height = demo->height;
1232 scissor.offset.x = 0;
1233 scissor.offset.y = 0;
Tony Barbour29645d02015-01-16 14:27:35 -07001234 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001235 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001236
1237 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001238 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001239
1240 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001241 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001242
1243 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001244 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1245 depth_stencil.stencilBackRef = 0;
1246 depth_stencil.stencilFrontRef = 0;
1247 depth_stencil.stencilReadMask = 0xff;
1248 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001249
Tony Barbour29645d02015-01-16 14:27:35 -07001250 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001251 assert(!err);
1252
Tony Barbour29645d02015-01-16 14:27:35 -07001253 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001254 assert(!err);
1255
Tony Barbour29645d02015-01-16 14:27:35 -07001256 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001257 &color_blend, &demo->color_blend);
1258 assert(!err);
1259
Tony Barbour29645d02015-01-16 14:27:35 -07001260 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001261 &depth_stencil, &demo->depth_stencil);
1262 assert(!err);
1263}
1264
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001265static void demo_prepare_descriptor_region(struct demo *demo)
1266{
1267 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1268 [0] = {
1269 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1270 .count = 1,
1271 },
1272 [1] = {
1273 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1274 .count = DEMO_TEXTURE_COUNT,
1275 },
1276 };
1277 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1278 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1279 .pNext = NULL,
1280 .count = 2,
1281 .pTypeCount = type_counts,
1282 };
1283 XGL_RESULT err;
1284
1285 err = xglCreateDescriptorRegion(demo->device,
1286 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1287 &descriptor_region, &demo->desc_region);
1288 assert(!err);
1289}
1290
1291static void demo_prepare_descriptor_set(struct demo *demo)
1292{
1293 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1294 &demo->uniform_data.attach;
1295 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1296 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1297 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1298 XGL_UPDATE_BUFFERS update_vs;
1299 XGL_RESULT err;
1300 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001301 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001302
1303 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1304 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1305 view_info[i].pNext = NULL;
1306 view_info[i].view = demo->textures[i].view,
1307 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1308
1309 combined_info[i].pSampler = demo->textures[i].sampler;
1310 combined_info[i].pImageView = &view_info[i];
1311 }
1312
1313 memset(&update_vs, 0, sizeof(update_vs));
1314 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1315 update_vs.pNext = &update_fs;
1316 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1317 update_vs.count = 1;
1318 update_vs.pBufferViews = &view_info_vs;
1319
1320 memset(&update_fs, 0, sizeof(update_fs));
1321 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1322 update_fs.index = 1;
1323 update_fs.count = DEMO_TEXTURE_COUNT;
1324 update_fs.pSamplerImageViews = combined_info;
1325
1326 err = xglAllocDescriptorSets(demo->desc_region,
1327 XGL_DESCRIPTOR_SET_USAGE_STATIC,
1328 1, demo->desc_layout_last,
1329 &demo->desc_set, &count);
1330 assert(!err && count == 1);
1331
1332 xglBeginDescriptorRegionUpdate(demo->device,
1333 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1334
1335 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1336 xglUpdateDescriptors(demo->desc_set, &update_vs);
1337
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001338 xglEndDescriptorRegionUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001339}
1340
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001341static void demo_prepare(struct demo *demo)
1342{
1343 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1344 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1345 .pNext = NULL,
1346 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1347 .flags = 0,
1348 };
1349 XGL_RESULT err;
1350
1351 demo_prepare_buffers(demo);
1352 demo_prepare_depth(demo);
1353 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001354 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001355
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001356 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001357 demo_prepare_pipeline(demo);
1358 demo_prepare_dynamic_states(demo);
1359
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001360 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1361 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1362 assert(!err);
1363 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001364
1365 demo_prepare_descriptor_region(demo);
1366 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001367
1368
1369 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1370 demo->current_buffer = i;
1371 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1372 }
1373
1374 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001375}
1376
1377static void demo_handle_event(struct demo *demo,
1378 const xcb_generic_event_t *event)
1379{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001380 u_int8_t event_code = event->response_type & 0x7f;
1381 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001382 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001383 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001384 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001385 case XCB_CLIENT_MESSAGE:
1386 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1387 (*demo->atom_wm_delete_window).atom) {
1388 demo->quit = true;
1389 }
1390 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001391 case XCB_KEY_RELEASE:
1392 {
1393 const xcb_key_release_event_t *key =
1394 (const xcb_key_release_event_t *) event;
1395
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001396 switch (key->detail) {
1397 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001398 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001399 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001400 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001401 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001402 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001403 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001404 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001405 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001406 case 0x41:
1407 demo->pause = !demo->pause;
1408 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001409 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001410 }
1411 break;
1412 default:
1413 break;
1414 }
1415}
1416
1417static void demo_run(struct demo *demo)
1418{
1419 xcb_flush(demo->connection);
1420
1421 while (!demo->quit) {
1422 xcb_generic_event_t *event;
1423
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001424 if (demo->pause) {
1425 event = xcb_wait_for_event(demo->connection);
1426 } else {
1427 event = xcb_poll_for_event(demo->connection);
1428 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429 if (event) {
1430 demo_handle_event(demo, event);
1431 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001432 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001433
1434 // Wait for work to finish before updating MVP.
1435 xglDeviceWaitIdle(demo->device);
1436 demo_update_data_buffer(demo);
1437
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001438 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001439
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001440 // Wait for work to finish before updating MVP.
1441 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001442 }
1443}
1444
1445static void demo_create_window(struct demo *demo)
1446{
1447 uint32_t value_mask, value_list[32];
1448
1449 demo->window = xcb_generate_id(demo->connection);
1450
1451 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1452 value_list[0] = demo->screen->black_pixel;
1453 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1454 XCB_EVENT_MASK_EXPOSURE;
1455
1456 xcb_create_window(demo->connection,
1457 XCB_COPY_FROM_PARENT,
1458 demo->window, demo->screen->root,
1459 0, 0, demo->width, demo->height, 0,
1460 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1461 demo->screen->root_visual,
1462 value_mask, value_list);
1463
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001464 /* Magic code that will send notification when window is destroyed */
1465 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1466 "WM_PROTOCOLS");
1467 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1468
1469 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1470 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1471
1472 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1473 demo->window, (*reply).atom, 4, 32, 1,
1474 &(*demo->atom_wm_delete_window).atom);
1475 free(reply);
1476
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001477 xcb_map_window(demo->connection, demo->window);
1478}
1479
1480static void demo_init_xgl(struct demo *demo)
1481{
1482 const XGL_APPLICATION_INFO app = {
1483 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1484 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001485 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001486 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001487 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001488 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001489 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001490 };
1491 const XGL_WSI_X11_CONNECTION_INFO connection = {
1492 .pConnection = demo->connection,
1493 .root = demo->screen->root,
1494 .provider = 0,
1495 };
1496 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1497 .queueNodeIndex = 0,
1498 .queueCount = 1,
1499 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001500 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001501 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001502 };
1503 const XGL_DEVICE_CREATE_INFO device = {
1504 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1505 .pNext = NULL,
1506 .queueRecordCount = 1,
1507 .pRequestedQueues = &queue,
1508 .extensionCount = 1,
1509 .ppEnabledExtensionNames = ext_names,
1510 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1511 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1512 };
1513 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001514 uint32_t gpu_count;
1515 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001516
Jon Ashburn93cfc432015-01-29 15:47:01 -07001517 err = xglCreateInstance(&app, NULL, &demo->inst);
1518 assert(!err);
1519 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520 assert(!err && gpu_count == 1);
1521
1522 for (i = 0; i < device.extensionCount; i++) {
1523 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1524 assert(!err);
1525 }
1526
1527 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1528 assert(!err);
1529
1530 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1531 assert(!err);
1532
1533 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1534 0, &demo->queue);
1535 assert(!err);
1536}
1537
1538static void demo_init_connection(struct demo *demo)
1539{
1540 const xcb_setup_t *setup;
1541 xcb_screen_iterator_t iter;
1542 int scr;
1543
1544 demo->connection = xcb_connect(NULL, &scr);
1545
1546 setup = xcb_get_setup(demo->connection);
1547 iter = xcb_setup_roots_iterator(setup);
1548 while (scr-- > 0)
1549 xcb_screen_next(&iter);
1550
1551 demo->screen = iter.data;
1552}
1553
1554static void demo_init(struct demo *demo)
1555{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001556 vec3 eye = {0.0f, 3.0f, 5.0f};
1557 vec3 origin = {0, 0, 0};
1558 vec3 up = {0.0f, -1.0f, 0.0};
1559
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001560 memset(demo, 0, sizeof(*demo));
1561
1562 demo_init_connection(demo);
1563 demo_init_xgl(demo);
1564
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001565 demo->width = 500;
1566 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001567 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001568
1569 demo->spin_angle = 0.01f;
1570 demo->spin_increment = 0.01f;
1571 demo->pause = false;
1572
1573 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1574 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1575 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001576}
1577
1578static void demo_cleanup(struct demo *demo)
1579{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001580 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001581
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001582 xglDestroyObject(demo->desc_set);
1583 xglDestroyObject(demo->desc_region);
1584
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001585 xglDestroyObject(demo->viewport);
1586 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001587 xglDestroyObject(demo->color_blend);
1588 xglDestroyObject(demo->depth_stencil);
1589
1590 xglDestroyObject(demo->pipeline);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001591 xglDestroyObject(demo->desc_layout_fs);
1592 xglDestroyObject(demo->desc_layout_vs);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001593
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001594 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1595 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001596 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001597 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001598 for (j = 0; j < demo->textures[i].num_mem; j++)
1599 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001600 xglDestroyObject(demo->textures[i].sampler);
1601 }
1602
1603 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001604 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001605 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001606 for (j = 0; j < demo->depth.num_mem; j++)
1607 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001608
1609 xglDestroyObject(demo->uniform_data.view);
1610 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001611 xglDestroyObject(demo->uniform_data.buf);
1612 for (j = 0; j < demo->uniform_data.num_mem; j++)
1613 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001614
1615 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001616 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001617 xglDestroyObject(demo->buffers[i].view);
1618 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001619 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001620 }
1621
1622 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001623 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001624
1625 xcb_destroy_window(demo->connection, demo->window);
1626 xcb_disconnect(demo->connection);
1627}
1628
1629int main(void)
1630{
1631 struct demo demo;
1632
1633 demo_init(&demo);
1634
1635 demo_prepare(&demo);
1636 demo_create_window(&demo);
1637 demo_run(&demo);
1638
1639 demo_cleanup(&demo);
1640
1641 return 0;
1642}