blob: 5ea5b24886b717c2fbe9cc4ceaba1077feca5791 [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 Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070028/*
29 * structure to track all objects related to a texture.
30 */
31struct texture_objects {
32 XGL_SAMPLER sampler;
33
34 XGL_IMAGE image;
35 uint32_t num_mem;
36 XGL_GPU_MEMORY *mem;
37 XGL_IMAGE_VIEW view;
38 int32_t tex_width, tex_height;
39};
40
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060041static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060042 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060043};
44
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060045struct xglcube_vs_uniform {
46 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060047 float mvp[4][4];
48 float position[12*3][4];
49 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060050};
51
52struct xgltexcube_vs_uniform {
53 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060054 float mvp[4][4];
55 float position[12*3][4];
56 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060057};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060058
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060059//--------------------------------------------------------------------------------------
60// Mesh and VertexFormat Data
61//--------------------------------------------------------------------------------------
62struct Vertex
63{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060064 float posX, posY, posZ, posW; // Position data
65 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060066};
67
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060068struct VertexPosTex
69{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060070 float posX, posY, posZ, posW; // Position data
71 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060072};
73
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060074#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060075#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060076
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060077static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060078 -1.0f,-1.0f,-1.0f, // Vertex 0
79 -1.0f,-1.0f, 1.0f,
80 -1.0f, 1.0f, 1.0f,
81
82 -1.0f, 1.0f, 1.0f, // Vertex 1
83 -1.0f, 1.0f,-1.0f,
84 -1.0f,-1.0f,-1.0f,
85
86 -1.0f,-1.0f,-1.0f, // Vertex 2
87 1.0f, 1.0f,-1.0f,
88 1.0f,-1.0f,-1.0f,
89
90 -1.0f,-1.0f,-1.0f, // Vertex 3
91 1.0f, 1.0f,-1.0f,
92 -1.0f, 1.0f,-1.0f,
93
94 -1.0f,-1.0f,-1.0f, // Vertex 4
95 1.0f,-1.0f, 1.0f,
96 1.0f,-1.0f,-1.0f,
97
98 -1.0f,-1.0f,-1.0f, // Vertex 5
99 -1.0f,-1.0f, 1.0f,
100 1.0f,-1.0f, 1.0f,
101
102 -1.0f, 1.0f,-1.0f, // Vertex 6
103 -1.0f, 1.0f, 1.0f,
104 1.0f, 1.0f, 1.0f,
105
106 -1.0f, 1.0f,-1.0f, // Vertex 7
107 1.0f, 1.0f,-1.0f,
108 1.0f, 1.0f, 1.0f,
109
110 1.0f, 1.0f,-1.0f, // Vertex 8
111 1.0f, 1.0f, 1.0f,
112 1.0f,-1.0f, 1.0f,
113
114 1.0f,-1.0f, 1.0f, // Vertex 9
115 1.0f,-1.0f,-1.0f,
116 1.0f, 1.0f,-1.0f,
117
118 -1.0f, 1.0f, 1.0f, // Vertex 10
119 1.0f, 1.0f, 1.0f,
120 -1.0f,-1.0f, 1.0f,
121
122 -1.0f,-1.0f, 1.0f, // Vertex 11
123 1.0f,-1.0f, 1.0f,
124 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600125};
126
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600127static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600128 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600129 0.0f, 0.0f,
130 0.0f, 1.0f,
131
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600132 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600133 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600134 1.0f, 0.0f,
135
136// 0.0f, 1.0f, // Vertex 2
137// 1.0f, 0.0f,
138// 0.0f, 0.0f,
139
140// 0.0f, 1.0f, // Vertex 3
141// 1.0f, 0.0f,
142// 1.0f, 1.0f,
143
144 0.0f, 0.0f, // Vertex 2
145 1.0f, 1.0f,
146 1.0f, 0.0f,
147
148 0.0f, 0.0f, // Vertex 3
149 1.0f, 1.0f,
150 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600151
152 0.0f, 1.0f, // Vertex 4
153 1.0f, 0.0f,
154 0.0f, 0.0f,
155
156 0.0f, 1.0f, // Vertex 5
157 1.0f, 1.0f,
158 1.0f, 0.0f,
159
160 0.0f, 1.0f, // Vertex 6
161 1.0f, 1.0f,
162 1.0f, 0.0f,
163
164 0.0f, 1.0f, // Vertex 7
165 0.0f, 0.0f,
166 1.0f, 0.0f,
167
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600168 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600171
172 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 0.0f, 1.0f,
175
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600176 1.0f, 1.0f, // Vertex 10
177 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600179
180 1.0f, 0.0f, // Vertex 11
181 0.0f, 0.0f,
182 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600183};
184
185void dumpMatrix(const char *note, mat4x4 MVP)
186{
187 int i;
188
189 printf("%s: \n", note);
190 for (i=0; i<4; i++) {
191 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
192 }
193 printf("\n");
194 fflush(stdout);
195}
196
197void dumpVec4(const char *note, vec4 vector)
198{
199 printf("%s: \n", note);
200 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
201 printf("\n");
202 fflush(stdout);
203}
204
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600205struct demo {
206 xcb_connection_t *connection;
207 xcb_screen_t *screen;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700208 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600209
Jon Ashburn93cfc432015-01-29 15:47:01 -0700210 XGL_INSTANCE inst;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600211 XGL_PHYSICAL_GPU gpu;
212 XGL_DEVICE device;
213 XGL_QUEUE queue;
214
215 int width, height;
216 XGL_FORMAT format;
217
218 struct {
219 XGL_IMAGE image;
220 XGL_GPU_MEMORY mem;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700221 XGL_CMD_BUFFER cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600222
223 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800224 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600225 } buffers[DEMO_BUFFER_COUNT];
226
227 struct {
228 XGL_FORMAT format;
229
230 XGL_IMAGE image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600231 uint32_t num_mem;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700232 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600233 XGL_DEPTH_STENCIL_VIEW view;
234 } depth;
235
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700236 struct texture_objects textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600237
238 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800239 XGL_BUFFER buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600240 uint32_t num_mem;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700241 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800242 XGL_BUFFER_VIEW view;
243 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600244 } uniform_data;
245
Chia-I Wu87544e72015-02-23 10:41:08 -0700246 XGL_DESCRIPTOR_SET_LAYOUT desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600247 XGL_PIPELINE pipeline;
248
Tony Barbour29645d02015-01-16 14:27:35 -0700249 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
250 XGL_DYNAMIC_RS_STATE_OBJECT raster;
251 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
252 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600253
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600254 mat4x4 projection_matrix;
255 mat4x4 view_matrix;
256 mat4x4 model_matrix;
257
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600258 float spin_angle;
259 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600260 bool pause;
261
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800262 XGL_DESCRIPTOR_REGION desc_region;
263 XGL_DESCRIPTOR_SET desc_set;
264
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600265 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700266 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600267
268 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600269 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600270};
271
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700272static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600273{
274 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
275 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000276 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600277 };
278 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
279 .view = demo->depth.view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000280 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600281 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600282 const float clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
283 const float clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600284 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700285 XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = {
286 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO,
287 .pNext = NULL,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700288 };
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700289 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
290 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700291 .pNext = &graphics_cmd_buf_info,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700292 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
293 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
294 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600295 XGL_RESULT err;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700296 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
297 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
298 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
299 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
300 .pNext = NULL,
301 .colorAttachmentCount = 1,
302 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
303 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
304 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600305 .width = demo->width,
306 .height = demo->height,
307 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700308 };
309 XGL_RENDER_PASS_CREATE_INFO rp_info;
310
311 memset(&rp_info, 0 , sizeof(rp_info));
312 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
313 assert(!err);
314 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
315 rp_info.renderArea.extent.width = demo->width;
316 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchterc17d7de2015-02-10 14:06:25 -0700317 rp_info.colorAttachmentCount = 1;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700318 rp_info.pColorLoadOps = &load_op;
319 rp_info.pColorStoreOps = &store_op;
320 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
321 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
322 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
323 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
324 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
325 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700327 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600328 assert(!err);
329
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700330 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331 demo->pipeline);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700332 xglCmdBindDescriptorSet(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800333 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700335 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
336 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
337 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338 demo->color_blend);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700339 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600340 demo->depth_stencil);
341
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700342 xglCmdBeginRenderPass(cmd_buf, graphics_cmd_buf_info.renderPass);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600343 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
344 clear_range.baseMipLevel = 0;
345 clear_range.mipLevels = 1;
346 clear_range.baseArraySlice = 0;
347 clear_range.arraySize = 1;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700348 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600349 demo->buffers[demo->current_buffer].image,
350 clear_color, 1, &clear_range);
351
352 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700353 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600354 clear_depth, 0, 1, &clear_range);
355
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700356 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700357 xglCmdEndRenderPass(cmd_buf, graphics_cmd_buf_info.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600358
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700359 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360 assert(!err);
361}
362
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600363
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600364void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600365{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600366 mat4x4 MVP, Model, VP;
367 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600368 uint8_t *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369 XGL_RESULT err;
370
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600371 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600373 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600374 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600375 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600376 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600377
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700378 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600379 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380 assert(!err);
381
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600382 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700384 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600385 assert(!err);
386}
387
388static void demo_draw(struct demo *demo)
389{
390 const XGL_WSI_X11_PRESENT_INFO present = {
391 .destWindow = demo->window,
392 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800393 .async = true,
394 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800396 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600397 XGL_RESULT err;
398
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600399 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wubb57f642014-11-07 14:30:34 +0800400 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
401
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -0600402 uint32_t i, idx = 0;
403 XGL_MEMORY_REF *memRefs;
404 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
405 demo->depth.num_mem +
406 demo->textures[0].num_mem +
407 demo->uniform_data.num_mem));
408 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
409 memRefs[idx].mem = demo->depth.mem[i];
410 memRefs[idx].flags = 0;
411 }
412 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
413 memRefs[idx].mem = demo->textures[0].mem[i];
414 memRefs[idx].flags = 0;
415 }
416 memRefs[idx].mem = demo->buffers[0].mem;
417 memRefs[idx++].flags = 0;
418 memRefs[idx].mem = demo->buffers[1].mem;
419 memRefs[idx++].flags = 0;
420 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
421 memRefs[idx].mem = demo->uniform_data.mem[i];
422 memRefs[idx].flags = 0;
423 }
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700424 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
425 0, NULL, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600426 assert(!err);
427
Chia-I Wubb57f642014-11-07 14:30:34 +0800428 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600429 assert(!err);
430
431 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
432}
433
434static void demo_prepare_buffers(struct demo *demo)
435{
436 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
437 .format = demo->format,
438 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
439 .extent = {
440 .width = demo->width,
441 .height = demo->height,
442 },
443 .flags = 0,
444 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800445 const XGL_FENCE_CREATE_INFO fence = {
446 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
447 .pNext = NULL,
448 .flags = 0,
449 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600450 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600451 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600452
453 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
454 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
455 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
456 .pNext = NULL,
457 .format = demo->format,
458 .mipLevel = 0,
459 .baseArraySlice = 0,
460 .arraySize = 1,
461 };
462
463 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
464 &demo->buffers[i].image, &demo->buffers[i].mem);
465 assert(!err);
466
467 color_attachment_view.image = demo->buffers[i].image;
468
469 err = xglCreateColorAttachmentView(demo->device,
470 &color_attachment_view, &demo->buffers[i].view);
471 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800472
473 err = xglCreateFence(demo->device,
474 &fence, &demo->buffers[i].fence);
475 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600476 }
477}
478
479static void demo_prepare_depth(struct demo *demo)
480{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700481 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600482 const XGL_IMAGE_CREATE_INFO image = {
483 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
484 .pNext = NULL,
485 .imageType = XGL_IMAGE_2D,
486 .format = depth_format,
487 .extent = { demo->width, demo->height, 1 },
488 .mipLevels = 1,
489 .arraySize = 1,
490 .samples = 1,
491 .tiling = XGL_OPTIMAL_TILING,
492 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
493 .flags = 0,
494 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700495 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
496 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
497 .pNext = NULL,
498 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600499 XGL_MEMORY_ALLOC_INFO mem_alloc = {
500 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700501 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600502 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700503 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700504 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600505 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
506 };
507 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
508 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
509 .pNext = NULL,
510 .image = XGL_NULL_HANDLE,
511 .mipLevel = 0,
512 .baseArraySlice = 0,
513 .arraySize = 1,
514 .flags = 0,
515 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700516 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600517 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700518 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600519 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600520 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600521 uint32_t num_allocations = 0;
522 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523
524 demo->depth.format = depth_format;
525
526 /* create image */
527 err = xglCreateImage(demo->device, &image,
528 &demo->depth.image);
529 assert(!err);
530
Jon Ashburnb2a66652015-01-16 09:37:43 -0700531
532 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
533 assert(!err && num_alloc_size == sizeof(num_allocations));
534 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
535 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
536 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700538 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
539 &mem_reqs_size, mem_reqs);
540 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700541 err = xglGetObjectInfo(demo->depth.image,
542 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
543 &img_reqs_size, &img_reqs);
544 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
545 img_alloc.usage = img_reqs.usage;
546 img_alloc.formatClass = img_reqs.formatClass;
547 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600548 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700549 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600550
Jon Ashburnb2a66652015-01-16 09:37:43 -0700551 /* allocate memory */
552 err = xglAllocMemory(demo->device, &mem_alloc,
553 &(demo->depth.mem[i]));
554 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555
Jon Ashburnb2a66652015-01-16 09:37:43 -0700556 /* bind memory */
557 err = xglBindObjectMemory(demo->depth.image, i,
558 demo->depth.mem[i], 0);
559 assert(!err);
560 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561
562 /* create image view */
563 view.image = demo->depth.image;
564 err = xglCreateDepthStencilView(demo->device, &view,
565 &demo->depth.view);
566 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567}
568
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600569/** loadTexture
570 * loads a png file into an memory object, using cstdio , libpng.
571 *
572 * \param demo : Needed to access XGL calls
573 * \param filename : the png file to be loaded
574 * \param width : width of png, to be updated as a side effect of this function
575 * \param height : height of png, to be updated as a side effect of this function
576 *
577 * \return bool : an opengl texture id. true if successful?,
578 * should be validated by the client of this function.
579 *
580 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
581 * Modified to copy image to memory
582 *
583 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700584bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600585 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600586 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600587{
588 //header for testing if it is a png
589 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700590 int is_png, bit_depth, color_type,rowbytes;
591 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600592 png_structp png_ptr;
593 png_infop info_ptr, end_info;
594 png_byte *image_data;
595 png_bytep *row_pointers;
596
597 //open file as binary
598 FILE *fp = fopen(filename, "rb");
599 if (!fp) {
600 return false;
601 }
602
603 //read the header
604 fread(header, 1, 8, fp);
605
606 //test if png
607 is_png = !png_sig_cmp(header, 0, 8);
608 if (!is_png) {
609 fclose(fp);
610 return false;
611 }
612
613 //create png struct
614 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
615 NULL, NULL);
616 if (!png_ptr) {
617 fclose(fp);
618 return (false);
619 }
620
621 //create png info struct
622 info_ptr = png_create_info_struct(png_ptr);
623 if (!info_ptr) {
624 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
625 fclose(fp);
626 return (false);
627 }
628
629 //create png info struct
630 end_info = png_create_info_struct(png_ptr);
631 if (!end_info) {
632 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
633 fclose(fp);
634 return (false);
635 }
636
637 //png error stuff, not sure libpng man suggests this.
638 if (setjmp(png_jmpbuf(png_ptr))) {
639 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
640 fclose(fp);
641 return (false);
642 }
643
644 //init png reading
645 png_init_io(png_ptr, fp);
646
647 //let libpng know you already read the first 8 bytes
648 png_set_sig_bytes(png_ptr, 8);
649
650 // read all the info up to the image data
651 png_read_info(png_ptr, info_ptr);
652
653 // get info about png
654 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
655 NULL, NULL, NULL);
656
657 //update width and height based on png info
658 *width = twidth;
659 *height = theight;
660
661 // Require that incoming texture be 8bits per color component
662 // and 4 components (RGBA).
663 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
664 png_get_channels(png_ptr, info_ptr) != 4) {
665 return false;
666 }
667
668 if (rgba_data == NULL) {
669 // If data pointer is null, we just want the width & height
670 // clean up memory and close stuff
671 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
672 fclose(fp);
673
674 return true;
675 }
676
677 // Update the png info struct.
678 png_read_update_info(png_ptr, info_ptr);
679
680 // Row size in bytes.
681 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
682
683 // Allocate the image_data as a big block, to be given to opengl
684 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
685 if (!image_data) {
686 //clean up memory and close stuff
687 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
688 fclose(fp);
689 return false;
690 }
691
692 // row_pointers is for pointing to image_data for reading the png with libpng
693 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
694 if (!row_pointers) {
695 //clean up memory and close stuff
696 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
697 // delete[] image_data;
698 fclose(fp);
699 return false;
700 }
701 // set the individual row_pointers to point at the correct offsets of image_data
702 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700703 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600704
705 // read the png into image_data through row_pointers
706 png_read_image(png_ptr, row_pointers);
707
708 // clean up memory and close stuff
709 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
710 free(row_pointers);
711 free(image_data);
712 fclose(fp);
713
714 return true;
715}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600716
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700717static void demo_prepare_texture_image(struct demo *demo,
718 const char *filename,
719 struct texture_objects *tex_objs,
720 XGL_IMAGE_TILING tiling,
721 XGL_FLAGS mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600722{
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700723 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600724 int32_t tex_width;
725 int32_t tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600726 XGL_RESULT err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700727
728 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
729 assert(err);
730
731 tex_objs->tex_width = tex_width;
732 tex_objs->tex_height = tex_height;
733
734 const XGL_IMAGE_CREATE_INFO image_create_info = {
735 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
736 .pNext = NULL,
737 .imageType = XGL_IMAGE_2D,
738 .format = tex_format,
739 .extent = { tex_width, tex_height, 1 },
740 .mipLevels = 1,
741 .arraySize = 1,
742 .samples = 1,
743 .tiling = tiling,
744 .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
745 .flags = 0,
746 };
747 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
748 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
749 .pNext = NULL,
750 };
751 XGL_MEMORY_ALLOC_INFO mem_alloc = {
752 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
753 .pNext = &img_alloc,
754 .allocationSize = 0,
755 .memProps = mem_props,
756 .memType = XGL_MEMORY_TYPE_IMAGE,
757 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
758 };
759
760 XGL_MEMORY_REQUIREMENTS *mem_reqs;
761 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
762 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
763 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
764 uint32_t num_allocations = 0;
765 size_t num_alloc_size = sizeof(num_allocations);
766
767 err = xglCreateImage(demo->device, &image_create_info,
768 &tex_objs->image);
769 assert(!err);
770
771 err = xglGetObjectInfo(tex_objs->image,
772 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
773 &num_alloc_size, &num_allocations);
774 assert(!err && num_alloc_size == sizeof(num_allocations));
775 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
776 tex_objs->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
777 err = xglGetObjectInfo(tex_objs->image,
778 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
779 &mem_reqs_size, mem_reqs);
780 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
781 err = xglGetObjectInfo(tex_objs->image,
782 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
783 &img_reqs_size, &img_reqs);
784 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
785 img_alloc.usage = img_reqs.usage;
786 img_alloc.formatClass = img_reqs.formatClass;
787 img_alloc.samples = img_reqs.samples;
788 mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
789 for (uint32_t j = 0; j < num_allocations; j ++) {
790 mem_alloc.allocationSize = mem_reqs[j].size;
791
792 /* allocate memory */
793 err = xglAllocMemory(demo->device, &mem_alloc,
794 &(tex_objs->mem[j]));
795 assert(!err);
796
797 /* bind memory */
798 err = xglBindObjectMemory(tex_objs->image, j, tex_objs->mem[j], 0);
799 assert(!err);
800 }
801 free(mem_reqs);
802 mem_reqs = NULL;
803
804 tex_objs->num_mem = num_allocations;
805
806 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
807 const XGL_IMAGE_SUBRESOURCE subres = {
808 .aspect = XGL_IMAGE_ASPECT_COLOR,
809 .mipLevel = 0,
810 .arraySlice = 0,
811 };
812 XGL_SUBRESOURCE_LAYOUT layout;
813 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
814 void *data;
815
816 err = xglGetImageSubresourceInfo(tex_objs->image, &subres,
817 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
818 &layout_size, &layout);
819 assert(!err && layout_size == sizeof(layout));
820 /* Linear texture must be within a single memory object */
821 assert(num_allocations == 1);
822
823 err = xglMapMemory(tex_objs->mem[0], 0, &data);
824 assert(!err);
825
826 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
827 fprintf(stderr, "Error loading texture: %s\n", filename);
828 }
829
830 err = xglUnmapMemory(tex_objs->mem[0]);
831 assert(!err);
832 }
833}
834
835static void demo_destroy_texture_image(struct texture_objects *tex_objs)
836{
837 /* clean up staging resources */
838 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
839 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
840 xglFreeMemory(tex_objs->mem[j]);
841 }
842
843 free(tex_objs->mem);
844 xglDestroyObject(tex_objs->image);
845}
846
847static void demo_prepare_textures(struct demo *demo)
848{
849 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
850 XGL_FORMAT_PROPERTIES props;
851 size_t size = sizeof(props);
852 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600853 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600854
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700855 err = xglGetFormatInfo(demo->device, tex_format,
856 XGL_INFO_TYPE_FORMAT_PROPERTIES,
857 &size, &props);
858 assert(!err);
859
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600860 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700861
862 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
863 /* Device can texture using linear textures */
864 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
865 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
866 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT){
867 /* Must use staging buffer to copy linear texture to optimized */
868 struct texture_objects staging_texture;
869
870 memset(&staging_texture, 0, sizeof(staging_texture));
871 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
872 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
873
874 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
875 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
876
877 XGL_CMD_BUFFER staging_cmd_buf;
878 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {
879 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
880 .pNext = NULL,
881 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
882 .flags = 0
883 };
884
885 err = xglCreateCommandBuffer(demo->device, &cmd_buf_create_info, &staging_cmd_buf);
886 assert(!err);
887
888 /* Copy staging texture to usable texture */
889 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {
890 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
891 .pNext = NULL,
892 .flags = 0
893 };
894
895 err = xglResetCommandBuffer(staging_cmd_buf);
896 assert(!err);
897
898 err = xglBeginCommandBuffer(staging_cmd_buf, &cmd_buf_begin_info);
899 assert(!err);
900
901 XGL_IMAGE_COPY copy_region = {
902 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
903 .srcOffset = { 0, 0, 0 },
904 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
905 .destOffset = { 0, 0, 0 },
906 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
907 };
908 xglCmdCopyImage(staging_cmd_buf, staging_texture.image, demo->textures[i].image, 1, &copy_region);
909
910 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
911 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
912 .pNext = NULL,
913 .outputMask = XGL_MEMORY_OUTPUT_COPY_BIT,
914 .inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT,
915 .oldLayout = XGL_IMAGE_LAYOUT_GENERAL,
916 .newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
917 .image = staging_texture.image,
918 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
919 };
920 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
921
922 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
923 XGL_PIPELINE_BARRIER pipeline_barrier;
924 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
925 pipeline_barrier.pNext = NULL;
926 pipeline_barrier.eventCount = 1;
927 pipeline_barrier.pEvents = set_events;
928 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
929 pipeline_barrier.memBarrierCount = 1;
930 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
931
932 // write barrier to the command buffer
933 xglCmdPipelineBarrier(staging_cmd_buf, &pipeline_barrier);
934
935 err = xglEndCommandBuffer(staging_cmd_buf);
936 assert(!err);
937
938 const XGL_CMD_BUFFER cmd_bufs[] = { staging_cmd_buf };
939 XGL_MEMORY_REF mem_refs[16];
940 uint32_t num_refs = 0;
941
942 for (uint32_t j = 0; j < staging_texture.num_mem; j++) {
943 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
944 mem_refs[num_refs].mem = staging_texture.mem[j];
945 num_refs++;
946 assert(num_refs < 16);
947 }
948
949 for (uint32_t j = 0; j < demo->textures[i].num_mem; j++) {
950 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
951 mem_refs[num_refs].mem = demo->textures[i].mem[j];
952 num_refs++;
953 assert(num_refs < 16);
954 }
955
956 err = xglQueueSubmit(demo->queue, 1, cmd_bufs,
957 num_refs, mem_refs, XGL_NULL_HANDLE);
958 assert(!err);
959
960 err = xglQueueWaitIdle(demo->queue);
961 assert(!err);
962
963 demo_destroy_texture_image(&staging_texture);
964
965 xglDestroyObject(staging_cmd_buf);
966 } else {
967 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
968 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
969 }
970
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600971 const XGL_SAMPLER_CREATE_INFO sampler = {
972 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
973 .pNext = NULL,
974 .magFilter = XGL_TEX_FILTER_NEAREST,
975 .minFilter = XGL_TEX_FILTER_NEAREST,
976 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600977 .addressU = XGL_TEX_ADDRESS_CLAMP,
978 .addressV = XGL_TEX_ADDRESS_CLAMP,
979 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600980 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700981 .maxAnisotropy = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600982 .compareFunc = XGL_COMPARE_NEVER,
983 .minLod = 0.0f,
984 .maxLod = 0.0f,
985 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
986 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600987
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600988 XGL_IMAGE_VIEW_CREATE_INFO view = {
989 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
990 .pNext = NULL,
991 .image = XGL_NULL_HANDLE,
992 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700993 .format = tex_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600994 .channels = { XGL_CHANNEL_SWIZZLE_R,
995 XGL_CHANNEL_SWIZZLE_G,
996 XGL_CHANNEL_SWIZZLE_B,
997 XGL_CHANNEL_SWIZZLE_A, },
998 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
999 .minLod = 0.0f,
1000 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001001
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001002 /* create sampler */
1003 err = xglCreateSampler(demo->device, &sampler,
1004 &demo->textures[i].sampler);
1005 assert(!err);
1006
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001007 /* create image view */
1008 view.image = demo->textures[i].image;
1009 err = xglCreateImageView(demo->device, &view,
1010 &demo->textures[i].view);
1011 assert(!err);
1012 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001013}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001014
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001015void demo_prepare_cube_data_buffer(struct demo *demo)
1016{
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001017 XGL_BUFFER_CREATE_INFO buf_info;
1018 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001019 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1020 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1021 .pNext = NULL,
1022 };
1023 XGL_MEMORY_ALLOC_INFO alloc_info = {
1024 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1025 .pNext = &buf_alloc,
1026 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -07001027 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -07001028 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001029 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1030 };
1031 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001032 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001033 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001034 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1035 uint32_t num_allocations = 0;
1036 size_t num_alloc_size = sizeof(num_allocations);
1037 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001038 int i;
1039 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001040 XGL_RESULT err;
1041 struct xgltexcube_vs_uniform data;
1042
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001043 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001044 mat4x4_mul(MVP, VP, demo->model_matrix);
1045 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001046// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001047
1048 for (i=0; i<12*3; i++) {
1049 data.position[i][0] = g_vertex_buffer_data[i*3];
1050 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1051 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1052 data.position[i][3] = 1.0f;
1053 data.attr[i][0] = g_uv_buffer_data[2*i];
1054 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1055 data.attr[i][2] = 0;
1056 data.attr[i][3] = 0;
1057 }
1058
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001059 memset(&buf_info, 0, sizeof(buf_info));
1060 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1061 buf_info.size = sizeof(data);
1062 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1063 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1064 assert(!err);
1065
1066 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001067 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1068 &num_alloc_size, &num_allocations);
1069 assert(!err && num_alloc_size == sizeof(num_allocations));
1070 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1071 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1072 demo->uniform_data.num_mem = num_allocations;
1073 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001074 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001075 &mem_reqs_size, mem_reqs);
1076 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1077 err = xglGetObjectInfo(demo->uniform_data.buf,
1078 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1079 &buf_reqs_size, &buf_reqs);
1080 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1081 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001082 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001083 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001084
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001085 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1086 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001087
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001088 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001089 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001090
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001091 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001092
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001093 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1094 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001095
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001096 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1097 demo->uniform_data.mem[i], 0);
1098 assert(!err);
1099 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001100
1101 memset(&view_info, 0, sizeof(view_info));
1102 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1103 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +08001104 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001105 view_info.offset = 0;
1106 view_info.range = sizeof(data);
1107
1108 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1109 assert(!err);
1110
1111 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1112 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001113}
1114
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001115static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001116{
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001117 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
1118 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1119 .pNext = NULL,
1120 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1121 .count = DEMO_TEXTURE_COUNT,
1122 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
1123 .immutableSampler = XGL_NULL_HANDLE,
1124 };
Chia-I Wu87544e72015-02-23 10:41:08 -07001125 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
1126 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1127 .pNext = &descriptor_layout_fs,
1128 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1129 .count = 1,
1130 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
1131 .immutableSampler = XGL_NULL_HANDLE,
1132 };
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001133 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001134 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001135
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001136 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu87544e72015-02-23 10:41:08 -07001137 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001138 XGL_NULL_HANDLE, &descriptor_layout_vs,
Chia-I Wu87544e72015-02-23 10:41:08 -07001139 &demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001140 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001141}
1142
1143static XGL_SHADER demo_prepare_shader(struct demo *demo,
1144 XGL_PIPELINE_SHADER_STAGE stage,
1145 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001146 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001147{
1148 XGL_SHADER_CREATE_INFO createInfo;
1149 XGL_SHADER shader;
1150 XGL_RESULT err;
1151
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001152
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001153 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1154 createInfo.pNext = NULL;
1155
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001156#ifdef EXTERNAL_BIL
1157 createInfo.codeSize = size;
1158 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001159 createInfo.flags = 0;
1160
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001161 err = xglCreateShader(demo->device, &createInfo, &shader);
1162 if (err) {
1163 free((void *) createInfo.pCode);
1164 }
1165#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001166 // Create fake BIL structure to feed GLSL
1167 // to the driver "under the covers"
1168 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1169 createInfo.pCode = malloc(createInfo.codeSize);
1170 createInfo.flags = 0;
1171
1172 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1173 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1174 ((uint32_t *) createInfo.pCode)[1] = 0;
1175 ((uint32_t *) createInfo.pCode)[2] = stage;
1176 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1177
1178 err = xglCreateShader(demo->device, &createInfo, &shader);
1179 if (err) {
1180 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001181 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001182 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001183#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001184
1185 return shader;
1186}
1187
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001188char *demo_read_bil(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001189{
1190 long int size;
1191 void *shader_code;
1192
1193 FILE *fp = fopen(filename, "rb");
1194 if (!fp) return NULL;
1195
1196 fseek(fp, 0L, SEEK_END);
1197 size = ftell(fp);
1198
1199 fseek(fp, 0L, SEEK_SET);
1200
1201 shader_code = malloc(size);
1202 fread(shader_code, size, 1, fp);
1203
1204 *psize = size;
1205
1206 return shader_code;
1207}
1208
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209static XGL_SHADER demo_prepare_vs(struct demo *demo)
1210{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001211#ifdef EXTERNAL_BIL
1212 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001213 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001214
1215 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1216
1217 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1218 vertShaderCode, size);
1219#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001220 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001221 "#version 140\n"
1222 "#extension GL_ARB_separate_shader_objects : enable\n"
1223 "#extension GL_ARB_shading_language_420pack : enable\n"
1224 "\n"
1225 "layout(binding = 0) uniform buf {\n"
1226 " mat4 MVP;\n"
1227 " vec4 position[12*3];\n"
1228 " vec4 attr[12*3];\n"
1229 "} ubuf;\n"
1230 "\n"
1231 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001232 "\n"
1233 "void main() \n"
1234 "{\n"
1235 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001236 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1237 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001238
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001239 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1240 (const void *) vertShaderText,
1241 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001242#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001243}
1244
1245static XGL_SHADER demo_prepare_fs(struct demo *demo)
1246{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001247#ifdef EXTERNAL_BIL
1248 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001249 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001250
1251 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1252
1253 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1254 fragShaderCode, size);
1255#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001256 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001257 "#version 140\n"
1258 "#extension GL_ARB_separate_shader_objects : enable\n"
1259 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001260 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001261 "\n"
1262 "layout (location = 0) in vec4 texcoord;\n"
1263 "void main() {\n"
1264 " gl_FragColor = texture(tex, texcoord.xy);\n"
1265 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001266
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001267 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1268 (const void *) fragShaderText,
1269 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001270#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001271}
1272
1273static void demo_prepare_pipeline(struct demo *demo)
1274{
1275 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001276 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1277 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001278 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1279 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001280 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1281 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001282 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1283 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001284 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001285
1286 memset(&pipeline, 0, sizeof(pipeline));
1287 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu87544e72015-02-23 10:41:08 -07001288 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001289
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001290 memset(&ia, 0, sizeof(ia));
1291 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1292 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1293
1294 memset(&rs, 0, sizeof(rs));
1295 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001296 rs.fillMode = XGL_FILL_SOLID;
1297 rs.cullMode = XGL_CULL_NONE;
1298 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001299
1300 memset(&cb, 0, sizeof(cb));
1301 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001302 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1303 memset(att_state, 0, sizeof(att_state));
1304 att_state[0].format = demo->format;
1305 att_state[0].channelWriteMask = 0xf;
1306 att_state[0].blendEnable = XGL_FALSE;
1307 cb.attachmentCount = 1;
1308 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001309
Tony Barbour29645d02015-01-16 14:27:35 -07001310 memset(&vp, 0, sizeof(vp));
1311 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001312 vp.numViewports = 1;
1313 vp.clipOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001314
1315 memset(&ds, 0, sizeof(ds));
1316 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1317 ds.format = demo->depth.format;
1318 ds.depthTestEnable = XGL_TRUE;
1319 ds.depthWriteEnable = XGL_TRUE;
1320 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1321 ds.depthBoundsEnable = XGL_FALSE;
1322 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1323 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1324 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1325 ds.stencilTestEnable = XGL_FALSE;
1326 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001327
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001328 memset(&vs, 0, sizeof(vs));
1329 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1330 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001331 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001332 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001333
1334 memset(&fs, 0, sizeof(fs));
1335 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1336 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1337 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001338 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001339
1340 memset(&ms, 0, sizeof(ms));
1341 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1342 ms.sampleMask = 1;
1343 ms.multisampleEnable = XGL_FALSE;
1344 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001345
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001346 pipeline.pNext = (const void *) &ia;
1347 ia.pNext = (const void *) &rs;
1348 rs.pNext = (const void *) &cb;
1349 cb.pNext = (const void *) &ms;
1350 ms.pNext = (const void *) &vp;
1351 vp.pNext = (const void *) &ds;
1352 ds.pNext = (const void *) &vs;
1353 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354
1355 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1356 assert(!err);
1357
1358 xglDestroyObject(vs.shader.shader);
1359 xglDestroyObject(fs.shader.shader);
1360}
1361
1362static void demo_prepare_dynamic_states(struct demo *demo)
1363{
Tony Barbour29645d02015-01-16 14:27:35 -07001364 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1365 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1366 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1367 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001368 XGL_RESULT err;
1369
Tony Barbour29645d02015-01-16 14:27:35 -07001370 memset(&viewport_create, 0, sizeof(viewport_create));
1371 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001372 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001373 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001374 XGL_RECT scissor;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001375 viewport.height = (float) demo->height;
1376 viewport.width = (float) demo->width;
1377 viewport.minDepth = (float) 0.0f;
1378 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001379 scissor.extent.width = demo->width;
1380 scissor.extent.height = demo->height;
1381 scissor.offset.x = 0;
1382 scissor.offset.y = 0;
Tony Barbour29645d02015-01-16 14:27:35 -07001383 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001384 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001385
1386 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001387 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388
1389 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001390 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001391
1392 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001393 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1394 depth_stencil.stencilBackRef = 0;
1395 depth_stencil.stencilFrontRef = 0;
1396 depth_stencil.stencilReadMask = 0xff;
1397 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001398
Tony Barbour29645d02015-01-16 14:27:35 -07001399 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 assert(!err);
1401
Tony Barbour29645d02015-01-16 14:27:35 -07001402 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001403 assert(!err);
1404
Tony Barbour29645d02015-01-16 14:27:35 -07001405 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406 &color_blend, &demo->color_blend);
1407 assert(!err);
1408
Tony Barbour29645d02015-01-16 14:27:35 -07001409 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001410 &depth_stencil, &demo->depth_stencil);
1411 assert(!err);
1412}
1413
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001414static void demo_prepare_descriptor_region(struct demo *demo)
1415{
1416 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1417 [0] = {
1418 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1419 .count = 1,
1420 },
1421 [1] = {
1422 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1423 .count = DEMO_TEXTURE_COUNT,
1424 },
1425 };
1426 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1427 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1428 .pNext = NULL,
1429 .count = 2,
1430 .pTypeCount = type_counts,
1431 };
1432 XGL_RESULT err;
1433
1434 err = xglCreateDescriptorRegion(demo->device,
1435 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1436 &descriptor_region, &demo->desc_region);
1437 assert(!err);
1438}
1439
1440static void demo_prepare_descriptor_set(struct demo *demo)
1441{
1442 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1443 &demo->uniform_data.attach;
1444 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1445 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1446 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1447 XGL_UPDATE_BUFFERS update_vs;
1448 XGL_RESULT err;
1449 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001450 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001451
1452 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1453 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1454 view_info[i].pNext = NULL;
1455 view_info[i].view = demo->textures[i].view,
1456 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1457
1458 combined_info[i].pSampler = demo->textures[i].sampler;
1459 combined_info[i].pImageView = &view_info[i];
1460 }
1461
1462 memset(&update_vs, 0, sizeof(update_vs));
1463 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1464 update_vs.pNext = &update_fs;
1465 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1466 update_vs.count = 1;
1467 update_vs.pBufferViews = &view_info_vs;
1468
1469 memset(&update_fs, 0, sizeof(update_fs));
1470 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1471 update_fs.index = 1;
1472 update_fs.count = DEMO_TEXTURE_COUNT;
1473 update_fs.pSamplerImageViews = combined_info;
1474
1475 err = xglAllocDescriptorSets(demo->desc_region,
1476 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001477 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001478 &demo->desc_set, &count);
1479 assert(!err && count == 1);
1480
1481 xglBeginDescriptorRegionUpdate(demo->device,
1482 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1483
1484 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1485 xglUpdateDescriptors(demo->desc_set, &update_vs);
1486
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001487 xglEndDescriptorRegionUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001488}
1489
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001490static void demo_prepare(struct demo *demo)
1491{
1492 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1493 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1494 .pNext = NULL,
1495 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1496 .flags = 0,
1497 };
1498 XGL_RESULT err;
1499
1500 demo_prepare_buffers(demo);
1501 demo_prepare_depth(demo);
1502 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001503 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001504
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001505 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001506 demo_prepare_pipeline(demo);
1507 demo_prepare_dynamic_states(demo);
1508
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001509 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1510 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1511 assert(!err);
1512 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001513
1514 demo_prepare_descriptor_region(demo);
1515 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001516
1517
1518 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1519 demo->current_buffer = i;
1520 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1521 }
1522
1523 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001524}
1525
1526static void demo_handle_event(struct demo *demo,
1527 const xcb_generic_event_t *event)
1528{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001529 u_int8_t event_code = event->response_type & 0x7f;
1530 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001531 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001532 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001533 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001534 case XCB_CLIENT_MESSAGE:
1535 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1536 (*demo->atom_wm_delete_window).atom) {
1537 demo->quit = true;
1538 }
1539 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001540 case XCB_KEY_RELEASE:
1541 {
1542 const xcb_key_release_event_t *key =
1543 (const xcb_key_release_event_t *) event;
1544
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001545 switch (key->detail) {
1546 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001547 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001548 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001549 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001550 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001551 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001552 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001553 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001554 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001555 case 0x41:
1556 demo->pause = !demo->pause;
1557 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001558 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001559 }
1560 break;
1561 default:
1562 break;
1563 }
1564}
1565
1566static void demo_run(struct demo *demo)
1567{
1568 xcb_flush(demo->connection);
1569
1570 while (!demo->quit) {
1571 xcb_generic_event_t *event;
1572
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001573 if (demo->pause) {
1574 event = xcb_wait_for_event(demo->connection);
1575 } else {
1576 event = xcb_poll_for_event(demo->connection);
1577 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001578 if (event) {
1579 demo_handle_event(demo, event);
1580 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001581 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001582
1583 // Wait for work to finish before updating MVP.
1584 xglDeviceWaitIdle(demo->device);
1585 demo_update_data_buffer(demo);
1586
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001587 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001588
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001589 // Wait for work to finish before updating MVP.
1590 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001591 }
1592}
1593
1594static void demo_create_window(struct demo *demo)
1595{
1596 uint32_t value_mask, value_list[32];
1597
1598 demo->window = xcb_generate_id(demo->connection);
1599
1600 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1601 value_list[0] = demo->screen->black_pixel;
1602 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1603 XCB_EVENT_MASK_EXPOSURE;
1604
1605 xcb_create_window(demo->connection,
1606 XCB_COPY_FROM_PARENT,
1607 demo->window, demo->screen->root,
1608 0, 0, demo->width, demo->height, 0,
1609 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1610 demo->screen->root_visual,
1611 value_mask, value_list);
1612
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001613 /* Magic code that will send notification when window is destroyed */
1614 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1615 "WM_PROTOCOLS");
1616 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1617
1618 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1619 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1620
1621 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1622 demo->window, (*reply).atom, 4, 32, 1,
1623 &(*demo->atom_wm_delete_window).atom);
1624 free(reply);
1625
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001626 xcb_map_window(demo->connection, demo->window);
1627}
1628
1629static void demo_init_xgl(struct demo *demo)
1630{
1631 const XGL_APPLICATION_INFO app = {
1632 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1633 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001634 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001635 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001636 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001637 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001638 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001639 };
1640 const XGL_WSI_X11_CONNECTION_INFO connection = {
1641 .pConnection = demo->connection,
1642 .root = demo->screen->root,
1643 .provider = 0,
1644 };
1645 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1646 .queueNodeIndex = 0,
1647 .queueCount = 1,
1648 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001649 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001650 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001651 };
1652 const XGL_DEVICE_CREATE_INFO device = {
1653 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1654 .pNext = NULL,
1655 .queueRecordCount = 1,
1656 .pRequestedQueues = &queue,
1657 .extensionCount = 1,
1658 .ppEnabledExtensionNames = ext_names,
1659 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1660 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1661 };
1662 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001663 uint32_t gpu_count;
1664 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001665
Jon Ashburn93cfc432015-01-29 15:47:01 -07001666 err = xglCreateInstance(&app, NULL, &demo->inst);
1667 assert(!err);
1668 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001669 assert(!err && gpu_count == 1);
1670
1671 for (i = 0; i < device.extensionCount; i++) {
1672 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1673 assert(!err);
1674 }
1675
1676 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1677 assert(!err);
1678
1679 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1680 assert(!err);
1681
1682 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1683 0, &demo->queue);
1684 assert(!err);
1685}
1686
1687static void demo_init_connection(struct demo *demo)
1688{
1689 const xcb_setup_t *setup;
1690 xcb_screen_iterator_t iter;
1691 int scr;
1692
1693 demo->connection = xcb_connect(NULL, &scr);
1694
1695 setup = xcb_get_setup(demo->connection);
1696 iter = xcb_setup_roots_iterator(setup);
1697 while (scr-- > 0)
1698 xcb_screen_next(&iter);
1699
1700 demo->screen = iter.data;
1701}
1702
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001703static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001704{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001705 vec3 eye = {0.0f, 3.0f, 5.0f};
1706 vec3 origin = {0, 0, 0};
1707 vec3 up = {0.0f, -1.0f, 0.0};
1708
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001709 memset(demo, 0, sizeof(*demo));
1710
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001711 for (int i = 0; i < argc; i++) {
1712 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1713 demo->use_staging_buffer = true;
1714 }
1715
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001716 demo_init_connection(demo);
1717 demo_init_xgl(demo);
1718
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001719 demo->width = 500;
1720 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001721 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001722
1723 demo->spin_angle = 0.01f;
1724 demo->spin_increment = 0.01f;
1725 demo->pause = false;
1726
1727 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1728 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1729 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001730}
1731
1732static void demo_cleanup(struct demo *demo)
1733{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001734 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001735
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001736 xglDestroyObject(demo->desc_set);
1737 xglDestroyObject(demo->desc_region);
1738
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001739 xglDestroyObject(demo->viewport);
1740 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001741 xglDestroyObject(demo->color_blend);
1742 xglDestroyObject(demo->depth_stencil);
1743
1744 xglDestroyObject(demo->pipeline);
Chia-I Wu87544e72015-02-23 10:41:08 -07001745 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001746
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1748 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001749 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001750 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001751 for (j = 0; j < demo->textures[i].num_mem; j++)
1752 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753 xglDestroyObject(demo->textures[i].sampler);
1754 }
1755
1756 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001757 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001759 for (j = 0; j < demo->depth.num_mem; j++)
1760 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001761
1762 xglDestroyObject(demo->uniform_data.view);
1763 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001764 xglDestroyObject(demo->uniform_data.buf);
1765 for (j = 0; j < demo->uniform_data.num_mem; j++)
1766 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001767
1768 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001769 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001770 xglDestroyObject(demo->buffers[i].view);
1771 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001772 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001773 }
1774
1775 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001776 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001777
1778 xcb_destroy_window(demo->connection, demo->window);
1779 xcb_disconnect(demo->connection);
1780}
1781
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001782int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001783{
1784 struct demo demo;
1785
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001786 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001787
1788 demo_prepare(&demo);
1789 demo_create_window(&demo);
1790 demo_run(&demo);
1791
1792 demo_cleanup(&demo);
1793
1794 return 0;
1795}