blob: 0ebbf997c7efe9ce74d5a332fe565a9dab1478aa [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;
Courtney Goeltzenleuchter02ba99a2015-02-25 11:48:28 -0700791 mem_alloc.memType = mem_reqs[j].memType;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700792
793 /* allocate memory */
794 err = xglAllocMemory(demo->device, &mem_alloc,
795 &(tex_objs->mem[j]));
796 assert(!err);
797
798 /* bind memory */
799 err = xglBindObjectMemory(tex_objs->image, j, tex_objs->mem[j], 0);
800 assert(!err);
801 }
802 free(mem_reqs);
803 mem_reqs = NULL;
804
805 tex_objs->num_mem = num_allocations;
806
807 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
808 const XGL_IMAGE_SUBRESOURCE subres = {
809 .aspect = XGL_IMAGE_ASPECT_COLOR,
810 .mipLevel = 0,
811 .arraySlice = 0,
812 };
813 XGL_SUBRESOURCE_LAYOUT layout;
814 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
815 void *data;
816
817 err = xglGetImageSubresourceInfo(tex_objs->image, &subres,
818 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
819 &layout_size, &layout);
820 assert(!err && layout_size == sizeof(layout));
821 /* Linear texture must be within a single memory object */
822 assert(num_allocations == 1);
823
824 err = xglMapMemory(tex_objs->mem[0], 0, &data);
825 assert(!err);
826
827 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
828 fprintf(stderr, "Error loading texture: %s\n", filename);
829 }
830
831 err = xglUnmapMemory(tex_objs->mem[0]);
832 assert(!err);
833 }
834}
835
836static void demo_destroy_texture_image(struct texture_objects *tex_objs)
837{
838 /* clean up staging resources */
839 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
840 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
841 xglFreeMemory(tex_objs->mem[j]);
842 }
843
844 free(tex_objs->mem);
845 xglDestroyObject(tex_objs->image);
846}
847
848static void demo_prepare_textures(struct demo *demo)
849{
850 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
851 XGL_FORMAT_PROPERTIES props;
852 size_t size = sizeof(props);
853 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600854 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600855
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700856 err = xglGetFormatInfo(demo->device, tex_format,
857 XGL_INFO_TYPE_FORMAT_PROPERTIES,
858 &size, &props);
859 assert(!err);
860
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600861 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700862
863 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
864 /* Device can texture using linear textures */
865 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
866 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
867 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT){
868 /* Must use staging buffer to copy linear texture to optimized */
869 struct texture_objects staging_texture;
870
871 memset(&staging_texture, 0, sizeof(staging_texture));
872 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
873 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
874
875 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
876 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
877
878 XGL_CMD_BUFFER staging_cmd_buf;
879 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {
880 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
881 .pNext = NULL,
882 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
883 .flags = 0
884 };
885
886 err = xglCreateCommandBuffer(demo->device, &cmd_buf_create_info, &staging_cmd_buf);
887 assert(!err);
888
889 /* Copy staging texture to usable texture */
890 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {
891 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
892 .pNext = NULL,
893 .flags = 0
894 };
895
896 err = xglResetCommandBuffer(staging_cmd_buf);
897 assert(!err);
898
899 err = xglBeginCommandBuffer(staging_cmd_buf, &cmd_buf_begin_info);
900 assert(!err);
901
902 XGL_IMAGE_COPY copy_region = {
903 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
904 .srcOffset = { 0, 0, 0 },
905 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
906 .destOffset = { 0, 0, 0 },
907 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
908 };
909 xglCmdCopyImage(staging_cmd_buf, staging_texture.image, demo->textures[i].image, 1, &copy_region);
910
911 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
912 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
913 .pNext = NULL,
914 .outputMask = XGL_MEMORY_OUTPUT_COPY_BIT,
915 .inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT,
916 .oldLayout = XGL_IMAGE_LAYOUT_GENERAL,
917 .newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
918 .image = staging_texture.image,
919 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
920 };
921 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
922
923 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
924 XGL_PIPELINE_BARRIER pipeline_barrier;
925 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
926 pipeline_barrier.pNext = NULL;
927 pipeline_barrier.eventCount = 1;
928 pipeline_barrier.pEvents = set_events;
929 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
930 pipeline_barrier.memBarrierCount = 1;
931 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
932
933 // write barrier to the command buffer
934 xglCmdPipelineBarrier(staging_cmd_buf, &pipeline_barrier);
935
936 err = xglEndCommandBuffer(staging_cmd_buf);
937 assert(!err);
938
939 const XGL_CMD_BUFFER cmd_bufs[] = { staging_cmd_buf };
940 XGL_MEMORY_REF mem_refs[16];
941 uint32_t num_refs = 0;
942
943 for (uint32_t j = 0; j < staging_texture.num_mem; j++) {
944 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
945 mem_refs[num_refs].mem = staging_texture.mem[j];
946 num_refs++;
947 assert(num_refs < 16);
948 }
949
950 for (uint32_t j = 0; j < demo->textures[i].num_mem; j++) {
951 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
952 mem_refs[num_refs].mem = demo->textures[i].mem[j];
953 num_refs++;
954 assert(num_refs < 16);
955 }
956
957 err = xglQueueSubmit(demo->queue, 1, cmd_bufs,
958 num_refs, mem_refs, XGL_NULL_HANDLE);
959 assert(!err);
960
961 err = xglQueueWaitIdle(demo->queue);
962 assert(!err);
963
964 demo_destroy_texture_image(&staging_texture);
965
966 xglDestroyObject(staging_cmd_buf);
967 } else {
968 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
969 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
970 }
971
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600972 const XGL_SAMPLER_CREATE_INFO sampler = {
973 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
974 .pNext = NULL,
975 .magFilter = XGL_TEX_FILTER_NEAREST,
976 .minFilter = XGL_TEX_FILTER_NEAREST,
977 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600978 .addressU = XGL_TEX_ADDRESS_CLAMP,
979 .addressV = XGL_TEX_ADDRESS_CLAMP,
980 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600981 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700982 .maxAnisotropy = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600983 .compareFunc = XGL_COMPARE_NEVER,
984 .minLod = 0.0f,
985 .maxLod = 0.0f,
986 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
987 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600988
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600989 XGL_IMAGE_VIEW_CREATE_INFO view = {
990 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
991 .pNext = NULL,
992 .image = XGL_NULL_HANDLE,
993 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700994 .format = tex_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600995 .channels = { XGL_CHANNEL_SWIZZLE_R,
996 XGL_CHANNEL_SWIZZLE_G,
997 XGL_CHANNEL_SWIZZLE_B,
998 XGL_CHANNEL_SWIZZLE_A, },
999 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
1000 .minLod = 0.0f,
1001 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001002
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001003 /* create sampler */
1004 err = xglCreateSampler(demo->device, &sampler,
1005 &demo->textures[i].sampler);
1006 assert(!err);
1007
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001008 /* create image view */
1009 view.image = demo->textures[i].image;
1010 err = xglCreateImageView(demo->device, &view,
1011 &demo->textures[i].view);
1012 assert(!err);
1013 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001014}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001015
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001016void demo_prepare_cube_data_buffer(struct demo *demo)
1017{
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001018 XGL_BUFFER_CREATE_INFO buf_info;
1019 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001020 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1021 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1022 .pNext = NULL,
1023 };
1024 XGL_MEMORY_ALLOC_INFO alloc_info = {
1025 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1026 .pNext = &buf_alloc,
1027 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -07001028 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -07001029 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001030 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1031 };
1032 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001033 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001034 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001035 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1036 uint32_t num_allocations = 0;
1037 size_t num_alloc_size = sizeof(num_allocations);
1038 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001039 int i;
1040 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001041 XGL_RESULT err;
1042 struct xgltexcube_vs_uniform data;
1043
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001044 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001045 mat4x4_mul(MVP, VP, demo->model_matrix);
1046 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001047// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001048
1049 for (i=0; i<12*3; i++) {
1050 data.position[i][0] = g_vertex_buffer_data[i*3];
1051 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1052 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1053 data.position[i][3] = 1.0f;
1054 data.attr[i][0] = g_uv_buffer_data[2*i];
1055 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1056 data.attr[i][2] = 0;
1057 data.attr[i][3] = 0;
1058 }
1059
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001060 memset(&buf_info, 0, sizeof(buf_info));
1061 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1062 buf_info.size = sizeof(data);
1063 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1064 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1065 assert(!err);
1066
1067 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001068 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1069 &num_alloc_size, &num_allocations);
1070 assert(!err && num_alloc_size == sizeof(num_allocations));
1071 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1072 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1073 demo->uniform_data.num_mem = num_allocations;
1074 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001075 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001076 &mem_reqs_size, mem_reqs);
1077 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1078 err = xglGetObjectInfo(demo->uniform_data.buf,
1079 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1080 &buf_reqs_size, &buf_reqs);
1081 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1082 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001083 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001084 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001085
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001086 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1087 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001088
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001089 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001090 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001091
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001092 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001093
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001094 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1095 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001096
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001097 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1098 demo->uniform_data.mem[i], 0);
1099 assert(!err);
1100 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001101
1102 memset(&view_info, 0, sizeof(view_info));
1103 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1104 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +08001105 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001106 view_info.offset = 0;
1107 view_info.range = sizeof(data);
1108
1109 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1110 assert(!err);
1111
1112 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1113 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001114}
1115
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001116static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001117{
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001118 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
1119 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1120 .pNext = NULL,
1121 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1122 .count = DEMO_TEXTURE_COUNT,
1123 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
1124 .immutableSampler = XGL_NULL_HANDLE,
1125 };
Chia-I Wu87544e72015-02-23 10:41:08 -07001126 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
1127 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1128 .pNext = &descriptor_layout_fs,
1129 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1130 .count = 1,
1131 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
1132 .immutableSampler = XGL_NULL_HANDLE,
1133 };
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001134 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001135 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001136
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001137 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu87544e72015-02-23 10:41:08 -07001138 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001139 XGL_NULL_HANDLE, &descriptor_layout_vs,
Chia-I Wu87544e72015-02-23 10:41:08 -07001140 &demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001141 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001142}
1143
1144static XGL_SHADER demo_prepare_shader(struct demo *demo,
1145 XGL_PIPELINE_SHADER_STAGE stage,
1146 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001147 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001148{
1149 XGL_SHADER_CREATE_INFO createInfo;
1150 XGL_SHADER shader;
1151 XGL_RESULT err;
1152
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001153
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001154 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1155 createInfo.pNext = NULL;
1156
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001157#ifdef EXTERNAL_BIL
1158 createInfo.codeSize = size;
1159 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001160 createInfo.flags = 0;
1161
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001162 err = xglCreateShader(demo->device, &createInfo, &shader);
1163 if (err) {
1164 free((void *) createInfo.pCode);
1165 }
1166#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001167 // Create fake BIL structure to feed GLSL
1168 // to the driver "under the covers"
1169 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1170 createInfo.pCode = malloc(createInfo.codeSize);
1171 createInfo.flags = 0;
1172
1173 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1174 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1175 ((uint32_t *) createInfo.pCode)[1] = 0;
1176 ((uint32_t *) createInfo.pCode)[2] = stage;
1177 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1178
1179 err = xglCreateShader(demo->device, &createInfo, &shader);
1180 if (err) {
1181 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001182 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001183 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001184#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001185
1186 return shader;
1187}
1188
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001189char *demo_read_bil(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001190{
1191 long int size;
1192 void *shader_code;
1193
1194 FILE *fp = fopen(filename, "rb");
1195 if (!fp) return NULL;
1196
1197 fseek(fp, 0L, SEEK_END);
1198 size = ftell(fp);
1199
1200 fseek(fp, 0L, SEEK_SET);
1201
1202 shader_code = malloc(size);
1203 fread(shader_code, size, 1, fp);
1204
1205 *psize = size;
1206
1207 return shader_code;
1208}
1209
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210static XGL_SHADER demo_prepare_vs(struct demo *demo)
1211{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001212#ifdef EXTERNAL_BIL
1213 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001214 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001215
1216 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1217
1218 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1219 vertShaderCode, size);
1220#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001221 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001222 "#version 140\n"
1223 "#extension GL_ARB_separate_shader_objects : enable\n"
1224 "#extension GL_ARB_shading_language_420pack : enable\n"
1225 "\n"
1226 "layout(binding = 0) uniform buf {\n"
1227 " mat4 MVP;\n"
1228 " vec4 position[12*3];\n"
1229 " vec4 attr[12*3];\n"
1230 "} ubuf;\n"
1231 "\n"
1232 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001233 "\n"
1234 "void main() \n"
1235 "{\n"
1236 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001237 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1238 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001239
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001240 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1241 (const void *) vertShaderText,
1242 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001243#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001244}
1245
1246static XGL_SHADER demo_prepare_fs(struct demo *demo)
1247{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001248#ifdef EXTERNAL_BIL
1249 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001250 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001251
1252 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1253
1254 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1255 fragShaderCode, size);
1256#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001257 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001258 "#version 140\n"
1259 "#extension GL_ARB_separate_shader_objects : enable\n"
1260 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001261 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001262 "\n"
1263 "layout (location = 0) in vec4 texcoord;\n"
1264 "void main() {\n"
1265 " gl_FragColor = texture(tex, texcoord.xy);\n"
1266 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001267
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001268 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1269 (const void *) fragShaderText,
1270 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001271#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001272}
1273
1274static void demo_prepare_pipeline(struct demo *demo)
1275{
1276 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001277 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1278 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001279 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1280 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001281 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1282 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001283 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1284 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001285 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001286
1287 memset(&pipeline, 0, sizeof(pipeline));
1288 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu87544e72015-02-23 10:41:08 -07001289 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001290
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001291 memset(&ia, 0, sizeof(ia));
1292 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1293 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1294
1295 memset(&rs, 0, sizeof(rs));
1296 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001297 rs.fillMode = XGL_FILL_SOLID;
1298 rs.cullMode = XGL_CULL_NONE;
1299 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001300
1301 memset(&cb, 0, sizeof(cb));
1302 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001303 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1304 memset(att_state, 0, sizeof(att_state));
1305 att_state[0].format = demo->format;
1306 att_state[0].channelWriteMask = 0xf;
1307 att_state[0].blendEnable = XGL_FALSE;
1308 cb.attachmentCount = 1;
1309 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310
Tony Barbour29645d02015-01-16 14:27:35 -07001311 memset(&vp, 0, sizeof(vp));
1312 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001313 vp.numViewports = 1;
1314 vp.clipOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001315
1316 memset(&ds, 0, sizeof(ds));
1317 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1318 ds.format = demo->depth.format;
1319 ds.depthTestEnable = XGL_TRUE;
1320 ds.depthWriteEnable = XGL_TRUE;
1321 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1322 ds.depthBoundsEnable = XGL_FALSE;
1323 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1324 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1325 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1326 ds.stencilTestEnable = XGL_FALSE;
1327 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001328
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001329 memset(&vs, 0, sizeof(vs));
1330 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1331 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001332 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001333 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001334
1335 memset(&fs, 0, sizeof(fs));
1336 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1337 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1338 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001339 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001340
1341 memset(&ms, 0, sizeof(ms));
1342 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1343 ms.sampleMask = 1;
1344 ms.multisampleEnable = XGL_FALSE;
1345 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001347 pipeline.pNext = (const void *) &ia;
1348 ia.pNext = (const void *) &rs;
1349 rs.pNext = (const void *) &cb;
1350 cb.pNext = (const void *) &ms;
1351 ms.pNext = (const void *) &vp;
1352 vp.pNext = (const void *) &ds;
1353 ds.pNext = (const void *) &vs;
1354 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001355
1356 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1357 assert(!err);
1358
1359 xglDestroyObject(vs.shader.shader);
1360 xglDestroyObject(fs.shader.shader);
1361}
1362
1363static void demo_prepare_dynamic_states(struct demo *demo)
1364{
Tony Barbour29645d02015-01-16 14:27:35 -07001365 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1366 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1367 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1368 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001369 XGL_RESULT err;
1370
Tony Barbour29645d02015-01-16 14:27:35 -07001371 memset(&viewport_create, 0, sizeof(viewport_create));
1372 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001373 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001374 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001375 XGL_RECT scissor;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001376 viewport.height = (float) demo->height;
1377 viewport.width = (float) demo->width;
1378 viewport.minDepth = (float) 0.0f;
1379 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001380 scissor.extent.width = demo->width;
1381 scissor.extent.height = demo->height;
1382 scissor.offset.x = 0;
1383 scissor.offset.y = 0;
Tony Barbour29645d02015-01-16 14:27:35 -07001384 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001385 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001386
1387 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001388 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389
1390 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001391 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392
1393 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001394 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1395 depth_stencil.stencilBackRef = 0;
1396 depth_stencil.stencilFrontRef = 0;
1397 depth_stencil.stencilReadMask = 0xff;
1398 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001399
Tony Barbour29645d02015-01-16 14:27:35 -07001400 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001401 assert(!err);
1402
Tony Barbour29645d02015-01-16 14:27:35 -07001403 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404 assert(!err);
1405
Tony Barbour29645d02015-01-16 14:27:35 -07001406 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407 &color_blend, &demo->color_blend);
1408 assert(!err);
1409
Tony Barbour29645d02015-01-16 14:27:35 -07001410 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001411 &depth_stencil, &demo->depth_stencil);
1412 assert(!err);
1413}
1414
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001415static void demo_prepare_descriptor_region(struct demo *demo)
1416{
1417 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1418 [0] = {
1419 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1420 .count = 1,
1421 },
1422 [1] = {
1423 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1424 .count = DEMO_TEXTURE_COUNT,
1425 },
1426 };
1427 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1428 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1429 .pNext = NULL,
1430 .count = 2,
1431 .pTypeCount = type_counts,
1432 };
1433 XGL_RESULT err;
1434
1435 err = xglCreateDescriptorRegion(demo->device,
1436 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1437 &descriptor_region, &demo->desc_region);
1438 assert(!err);
1439}
1440
1441static void demo_prepare_descriptor_set(struct demo *demo)
1442{
1443 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1444 &demo->uniform_data.attach;
1445 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1446 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1447 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1448 XGL_UPDATE_BUFFERS update_vs;
1449 XGL_RESULT err;
1450 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001451 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001452
1453 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1454 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1455 view_info[i].pNext = NULL;
1456 view_info[i].view = demo->textures[i].view,
1457 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1458
1459 combined_info[i].pSampler = demo->textures[i].sampler;
1460 combined_info[i].pImageView = &view_info[i];
1461 }
1462
1463 memset(&update_vs, 0, sizeof(update_vs));
1464 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1465 update_vs.pNext = &update_fs;
1466 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1467 update_vs.count = 1;
1468 update_vs.pBufferViews = &view_info_vs;
1469
1470 memset(&update_fs, 0, sizeof(update_fs));
1471 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1472 update_fs.index = 1;
1473 update_fs.count = DEMO_TEXTURE_COUNT;
1474 update_fs.pSamplerImageViews = combined_info;
1475
1476 err = xglAllocDescriptorSets(demo->desc_region,
1477 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001478 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001479 &demo->desc_set, &count);
1480 assert(!err && count == 1);
1481
1482 xglBeginDescriptorRegionUpdate(demo->device,
1483 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1484
1485 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1486 xglUpdateDescriptors(demo->desc_set, &update_vs);
1487
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001488 xglEndDescriptorRegionUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001489}
1490
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001491static void demo_prepare(struct demo *demo)
1492{
1493 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1494 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1495 .pNext = NULL,
1496 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1497 .flags = 0,
1498 };
1499 XGL_RESULT err;
1500
1501 demo_prepare_buffers(demo);
1502 demo_prepare_depth(demo);
1503 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001504 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001505
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001506 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001507 demo_prepare_pipeline(demo);
1508 demo_prepare_dynamic_states(demo);
1509
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001510 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1511 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1512 assert(!err);
1513 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001514
1515 demo_prepare_descriptor_region(demo);
1516 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001517
1518
1519 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1520 demo->current_buffer = i;
1521 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1522 }
1523
1524 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001525}
1526
1527static void demo_handle_event(struct demo *demo,
1528 const xcb_generic_event_t *event)
1529{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001530 u_int8_t event_code = event->response_type & 0x7f;
1531 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001533 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001534 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001535 case XCB_CLIENT_MESSAGE:
1536 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1537 (*demo->atom_wm_delete_window).atom) {
1538 demo->quit = true;
1539 }
1540 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001541 case XCB_KEY_RELEASE:
1542 {
1543 const xcb_key_release_event_t *key =
1544 (const xcb_key_release_event_t *) event;
1545
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001546 switch (key->detail) {
1547 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001548 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001549 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001550 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001551 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001552 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001553 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001554 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001555 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001556 case 0x41:
1557 demo->pause = !demo->pause;
1558 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001559 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001560 }
1561 break;
1562 default:
1563 break;
1564 }
1565}
1566
1567static void demo_run(struct demo *demo)
1568{
1569 xcb_flush(demo->connection);
1570
1571 while (!demo->quit) {
1572 xcb_generic_event_t *event;
1573
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001574 if (demo->pause) {
1575 event = xcb_wait_for_event(demo->connection);
1576 } else {
1577 event = xcb_poll_for_event(demo->connection);
1578 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001579 if (event) {
1580 demo_handle_event(demo, event);
1581 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001582 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001583
1584 // Wait for work to finish before updating MVP.
1585 xglDeviceWaitIdle(demo->device);
1586 demo_update_data_buffer(demo);
1587
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001588 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001589
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001590 // Wait for work to finish before updating MVP.
1591 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001592 }
1593}
1594
1595static void demo_create_window(struct demo *demo)
1596{
1597 uint32_t value_mask, value_list[32];
1598
1599 demo->window = xcb_generate_id(demo->connection);
1600
1601 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1602 value_list[0] = demo->screen->black_pixel;
1603 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1604 XCB_EVENT_MASK_EXPOSURE;
1605
1606 xcb_create_window(demo->connection,
1607 XCB_COPY_FROM_PARENT,
1608 demo->window, demo->screen->root,
1609 0, 0, demo->width, demo->height, 0,
1610 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1611 demo->screen->root_visual,
1612 value_mask, value_list);
1613
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001614 /* Magic code that will send notification when window is destroyed */
1615 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1616 "WM_PROTOCOLS");
1617 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1618
1619 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1620 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1621
1622 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1623 demo->window, (*reply).atom, 4, 32, 1,
1624 &(*demo->atom_wm_delete_window).atom);
1625 free(reply);
1626
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627 xcb_map_window(demo->connection, demo->window);
1628}
1629
1630static void demo_init_xgl(struct demo *demo)
1631{
1632 const XGL_APPLICATION_INFO app = {
1633 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1634 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001635 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001636 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001637 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001638 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001639 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001640 };
1641 const XGL_WSI_X11_CONNECTION_INFO connection = {
1642 .pConnection = demo->connection,
1643 .root = demo->screen->root,
1644 .provider = 0,
1645 };
1646 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1647 .queueNodeIndex = 0,
1648 .queueCount = 1,
1649 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001650 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001651 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001652 };
1653 const XGL_DEVICE_CREATE_INFO device = {
1654 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1655 .pNext = NULL,
1656 .queueRecordCount = 1,
1657 .pRequestedQueues = &queue,
1658 .extensionCount = 1,
1659 .ppEnabledExtensionNames = ext_names,
1660 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1661 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1662 };
1663 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001664 uint32_t gpu_count;
1665 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001666
Jon Ashburn93cfc432015-01-29 15:47:01 -07001667 err = xglCreateInstance(&app, NULL, &demo->inst);
1668 assert(!err);
1669 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001670 assert(!err && gpu_count == 1);
1671
1672 for (i = 0; i < device.extensionCount; i++) {
1673 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1674 assert(!err);
1675 }
1676
1677 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1678 assert(!err);
1679
1680 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1681 assert(!err);
1682
1683 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1684 0, &demo->queue);
1685 assert(!err);
1686}
1687
1688static void demo_init_connection(struct demo *demo)
1689{
1690 const xcb_setup_t *setup;
1691 xcb_screen_iterator_t iter;
1692 int scr;
1693
1694 demo->connection = xcb_connect(NULL, &scr);
1695
1696 setup = xcb_get_setup(demo->connection);
1697 iter = xcb_setup_roots_iterator(setup);
1698 while (scr-- > 0)
1699 xcb_screen_next(&iter);
1700
1701 demo->screen = iter.data;
1702}
1703
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001704static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001705{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001706 vec3 eye = {0.0f, 3.0f, 5.0f};
1707 vec3 origin = {0, 0, 0};
1708 vec3 up = {0.0f, -1.0f, 0.0};
1709
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001710 memset(demo, 0, sizeof(*demo));
1711
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001712 for (int i = 0; i < argc; i++) {
1713 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1714 demo->use_staging_buffer = true;
1715 }
1716
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001717 demo_init_connection(demo);
1718 demo_init_xgl(demo);
1719
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001720 demo->width = 500;
1721 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001722 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001723
1724 demo->spin_angle = 0.01f;
1725 demo->spin_increment = 0.01f;
1726 demo->pause = false;
1727
1728 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1729 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1730 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001731}
1732
1733static void demo_cleanup(struct demo *demo)
1734{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001735 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001736
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001737 xglDestroyObject(demo->desc_set);
1738 xglDestroyObject(demo->desc_region);
1739
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001740 xglDestroyObject(demo->viewport);
1741 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742 xglDestroyObject(demo->color_blend);
1743 xglDestroyObject(demo->depth_stencil);
1744
1745 xglDestroyObject(demo->pipeline);
Chia-I Wu87544e72015-02-23 10:41:08 -07001746 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001748 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1749 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001750 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001752 for (j = 0; j < demo->textures[i].num_mem; j++)
1753 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001754 xglDestroyObject(demo->textures[i].sampler);
1755 }
1756
1757 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001758 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001759 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001760 for (j = 0; j < demo->depth.num_mem; j++)
1761 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001762
1763 xglDestroyObject(demo->uniform_data.view);
1764 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001765 xglDestroyObject(demo->uniform_data.buf);
1766 for (j = 0; j < demo->uniform_data.num_mem; j++)
1767 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001768
1769 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001770 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001771 xglDestroyObject(demo->buffers[i].view);
1772 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001773 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001774 }
1775
1776 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001777 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001778
1779 xcb_destroy_window(demo->connection, demo->window);
1780 xcb_disconnect(demo->connection);
1781}
1782
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001783int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001784{
1785 struct demo demo;
1786
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001787 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001788
1789 demo_prepare(&demo);
1790 demo_create_window(&demo);
1791 demo_run(&demo);
1792
1793 demo_cleanup(&demo);
1794
1795 return 0;
1796}