blob: 287929086b0ef5d17f5a3576be007fdb29519889 [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);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700361
362 xglDestroyObject(graphics_cmd_buf_info.renderPass);
363 xglDestroyObject(rp_info.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364}
365
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600366
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600367void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600368{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600369 mat4x4 MVP, Model, VP;
370 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600371 uint8_t *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372 XGL_RESULT err;
373
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600374 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600376 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600377 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600378 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600379 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600380
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700381 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600382 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383 assert(!err);
384
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600385 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600386
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700387 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388 assert(!err);
389}
390
391static void demo_draw(struct demo *demo)
392{
393 const XGL_WSI_X11_PRESENT_INFO present = {
394 .destWindow = demo->window,
395 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800396 .async = true,
397 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600398 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800399 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400 XGL_RESULT err;
401
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600402 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wubb57f642014-11-07 14:30:34 +0800403 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
404
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -0600405 uint32_t i, idx = 0;
406 XGL_MEMORY_REF *memRefs;
407 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
408 demo->depth.num_mem +
409 demo->textures[0].num_mem +
410 demo->uniform_data.num_mem));
411 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
412 memRefs[idx].mem = demo->depth.mem[i];
413 memRefs[idx].flags = 0;
414 }
415 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
416 memRefs[idx].mem = demo->textures[0].mem[i];
417 memRefs[idx].flags = 0;
418 }
419 memRefs[idx].mem = demo->buffers[0].mem;
420 memRefs[idx++].flags = 0;
421 memRefs[idx].mem = demo->buffers[1].mem;
422 memRefs[idx++].flags = 0;
423 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
424 memRefs[idx].mem = demo->uniform_data.mem[i];
425 memRefs[idx].flags = 0;
426 }
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700427 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
428 0, NULL, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600429 assert(!err);
430
Chia-I Wubb57f642014-11-07 14:30:34 +0800431 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600432 assert(!err);
433
434 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
435}
436
437static void demo_prepare_buffers(struct demo *demo)
438{
439 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
440 .format = demo->format,
441 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
442 .extent = {
443 .width = demo->width,
444 .height = demo->height,
445 },
446 .flags = 0,
447 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800448 const XGL_FENCE_CREATE_INFO fence = {
449 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
450 .pNext = NULL,
451 .flags = 0,
452 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600453 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600454 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600455
456 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
457 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
458 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
459 .pNext = NULL,
460 .format = demo->format,
461 .mipLevel = 0,
462 .baseArraySlice = 0,
463 .arraySize = 1,
464 };
465
466 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
467 &demo->buffers[i].image, &demo->buffers[i].mem);
468 assert(!err);
469
470 color_attachment_view.image = demo->buffers[i].image;
471
472 err = xglCreateColorAttachmentView(demo->device,
473 &color_attachment_view, &demo->buffers[i].view);
474 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800475
476 err = xglCreateFence(demo->device,
477 &fence, &demo->buffers[i].fence);
478 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600479 }
480}
481
482static void demo_prepare_depth(struct demo *demo)
483{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700484 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600485 const XGL_IMAGE_CREATE_INFO image = {
486 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
487 .pNext = NULL,
488 .imageType = XGL_IMAGE_2D,
489 .format = depth_format,
490 .extent = { demo->width, demo->height, 1 },
491 .mipLevels = 1,
492 .arraySize = 1,
493 .samples = 1,
494 .tiling = XGL_OPTIMAL_TILING,
495 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
496 .flags = 0,
497 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700498 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
499 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
500 .pNext = NULL,
501 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600502 XGL_MEMORY_ALLOC_INFO mem_alloc = {
503 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700504 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600505 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700506 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700507 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600508 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
509 };
510 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
511 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
512 .pNext = NULL,
513 .image = XGL_NULL_HANDLE,
514 .mipLevel = 0,
515 .baseArraySlice = 0,
516 .arraySize = 1,
517 .flags = 0,
518 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700519 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600520 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700521 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600522 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600524 uint32_t num_allocations = 0;
525 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600526
527 demo->depth.format = depth_format;
528
529 /* create image */
530 err = xglCreateImage(demo->device, &image,
531 &demo->depth.image);
532 assert(!err);
533
Jon Ashburnb2a66652015-01-16 09:37:43 -0700534
535 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
536 assert(!err && num_alloc_size == sizeof(num_allocations));
537 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
538 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
539 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600540 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700541 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
542 &mem_reqs_size, mem_reqs);
543 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700544 err = xglGetObjectInfo(demo->depth.image,
545 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
546 &img_reqs_size, &img_reqs);
547 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
548 img_alloc.usage = img_reqs.usage;
549 img_alloc.formatClass = img_reqs.formatClass;
550 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600551 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700552 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553
Jon Ashburnb2a66652015-01-16 09:37:43 -0700554 /* allocate memory */
555 err = xglAllocMemory(demo->device, &mem_alloc,
556 &(demo->depth.mem[i]));
557 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558
Jon Ashburnb2a66652015-01-16 09:37:43 -0700559 /* bind memory */
560 err = xglBindObjectMemory(demo->depth.image, i,
561 demo->depth.mem[i], 0);
562 assert(!err);
563 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564
565 /* create image view */
566 view.image = demo->depth.image;
567 err = xglCreateDepthStencilView(demo->device, &view,
568 &demo->depth.view);
569 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600570}
571
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600572/** loadTexture
573 * loads a png file into an memory object, using cstdio , libpng.
574 *
575 * \param demo : Needed to access XGL calls
576 * \param filename : the png file to be loaded
577 * \param width : width of png, to be updated as a side effect of this function
578 * \param height : height of png, to be updated as a side effect of this function
579 *
580 * \return bool : an opengl texture id. true if successful?,
581 * should be validated by the client of this function.
582 *
583 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
584 * Modified to copy image to memory
585 *
586 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700587bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600588 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600589 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600590{
591 //header for testing if it is a png
592 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700593 int is_png, bit_depth, color_type,rowbytes;
594 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600595 png_structp png_ptr;
596 png_infop info_ptr, end_info;
597 png_byte *image_data;
598 png_bytep *row_pointers;
599
600 //open file as binary
601 FILE *fp = fopen(filename, "rb");
602 if (!fp) {
603 return false;
604 }
605
606 //read the header
607 fread(header, 1, 8, fp);
608
609 //test if png
610 is_png = !png_sig_cmp(header, 0, 8);
611 if (!is_png) {
612 fclose(fp);
613 return false;
614 }
615
616 //create png struct
617 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
618 NULL, NULL);
619 if (!png_ptr) {
620 fclose(fp);
621 return (false);
622 }
623
624 //create png info struct
625 info_ptr = png_create_info_struct(png_ptr);
626 if (!info_ptr) {
627 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
628 fclose(fp);
629 return (false);
630 }
631
632 //create png info struct
633 end_info = png_create_info_struct(png_ptr);
634 if (!end_info) {
635 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
636 fclose(fp);
637 return (false);
638 }
639
640 //png error stuff, not sure libpng man suggests this.
641 if (setjmp(png_jmpbuf(png_ptr))) {
642 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
643 fclose(fp);
644 return (false);
645 }
646
647 //init png reading
648 png_init_io(png_ptr, fp);
649
650 //let libpng know you already read the first 8 bytes
651 png_set_sig_bytes(png_ptr, 8);
652
653 // read all the info up to the image data
654 png_read_info(png_ptr, info_ptr);
655
656 // get info about png
657 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
658 NULL, NULL, NULL);
659
660 //update width and height based on png info
661 *width = twidth;
662 *height = theight;
663
664 // Require that incoming texture be 8bits per color component
665 // and 4 components (RGBA).
666 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
667 png_get_channels(png_ptr, info_ptr) != 4) {
668 return false;
669 }
670
671 if (rgba_data == NULL) {
672 // If data pointer is null, we just want the width & height
673 // clean up memory and close stuff
674 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
675 fclose(fp);
676
677 return true;
678 }
679
680 // Update the png info struct.
681 png_read_update_info(png_ptr, info_ptr);
682
683 // Row size in bytes.
684 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
685
686 // Allocate the image_data as a big block, to be given to opengl
687 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
688 if (!image_data) {
689 //clean up memory and close stuff
690 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
691 fclose(fp);
692 return false;
693 }
694
695 // row_pointers is for pointing to image_data for reading the png with libpng
696 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
697 if (!row_pointers) {
698 //clean up memory and close stuff
699 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
700 // delete[] image_data;
701 fclose(fp);
702 return false;
703 }
704 // set the individual row_pointers to point at the correct offsets of image_data
705 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700706 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600707
708 // read the png into image_data through row_pointers
709 png_read_image(png_ptr, row_pointers);
710
711 // clean up memory and close stuff
712 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
713 free(row_pointers);
714 free(image_data);
715 fclose(fp);
716
717 return true;
718}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600719
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700720static void demo_prepare_texture_image(struct demo *demo,
721 const char *filename,
722 struct texture_objects *tex_objs,
723 XGL_IMAGE_TILING tiling,
724 XGL_FLAGS mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600725{
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700726 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600727 int32_t tex_width;
728 int32_t tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600729 XGL_RESULT err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700730
731 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
732 assert(err);
733
734 tex_objs->tex_width = tex_width;
735 tex_objs->tex_height = tex_height;
736
737 const XGL_IMAGE_CREATE_INFO image_create_info = {
738 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
739 .pNext = NULL,
740 .imageType = XGL_IMAGE_2D,
741 .format = tex_format,
742 .extent = { tex_width, tex_height, 1 },
743 .mipLevels = 1,
744 .arraySize = 1,
745 .samples = 1,
746 .tiling = tiling,
747 .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
748 .flags = 0,
749 };
750 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
751 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
752 .pNext = NULL,
753 };
754 XGL_MEMORY_ALLOC_INFO mem_alloc = {
755 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
756 .pNext = &img_alloc,
757 .allocationSize = 0,
758 .memProps = mem_props,
759 .memType = XGL_MEMORY_TYPE_IMAGE,
760 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
761 };
762
763 XGL_MEMORY_REQUIREMENTS *mem_reqs;
764 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
765 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
766 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
767 uint32_t num_allocations = 0;
768 size_t num_alloc_size = sizeof(num_allocations);
769
770 err = xglCreateImage(demo->device, &image_create_info,
771 &tex_objs->image);
772 assert(!err);
773
774 err = xglGetObjectInfo(tex_objs->image,
775 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
776 &num_alloc_size, &num_allocations);
777 assert(!err && num_alloc_size == sizeof(num_allocations));
778 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
779 tex_objs->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
780 err = xglGetObjectInfo(tex_objs->image,
781 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
782 &mem_reqs_size, mem_reqs);
783 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
784 err = xglGetObjectInfo(tex_objs->image,
785 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
786 &img_reqs_size, &img_reqs);
787 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
788 img_alloc.usage = img_reqs.usage;
789 img_alloc.formatClass = img_reqs.formatClass;
790 img_alloc.samples = img_reqs.samples;
791 mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
792 for (uint32_t j = 0; j < num_allocations; j ++) {
793 mem_alloc.allocationSize = mem_reqs[j].size;
Courtney Goeltzenleuchter02ba99a2015-02-25 11:48:28 -0700794 mem_alloc.memType = mem_reqs[j].memType;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700795
796 /* allocate memory */
797 err = xglAllocMemory(demo->device, &mem_alloc,
798 &(tex_objs->mem[j]));
799 assert(!err);
800
801 /* bind memory */
802 err = xglBindObjectMemory(tex_objs->image, j, tex_objs->mem[j], 0);
803 assert(!err);
804 }
805 free(mem_reqs);
806 mem_reqs = NULL;
807
808 tex_objs->num_mem = num_allocations;
809
810 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
811 const XGL_IMAGE_SUBRESOURCE subres = {
812 .aspect = XGL_IMAGE_ASPECT_COLOR,
813 .mipLevel = 0,
814 .arraySlice = 0,
815 };
816 XGL_SUBRESOURCE_LAYOUT layout;
817 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
818 void *data;
819
820 err = xglGetImageSubresourceInfo(tex_objs->image, &subres,
821 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
822 &layout_size, &layout);
823 assert(!err && layout_size == sizeof(layout));
824 /* Linear texture must be within a single memory object */
825 assert(num_allocations == 1);
826
827 err = xglMapMemory(tex_objs->mem[0], 0, &data);
828 assert(!err);
829
830 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
831 fprintf(stderr, "Error loading texture: %s\n", filename);
832 }
833
834 err = xglUnmapMemory(tex_objs->mem[0]);
835 assert(!err);
836 }
837}
838
839static void demo_destroy_texture_image(struct texture_objects *tex_objs)
840{
841 /* clean up staging resources */
842 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
843 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
844 xglFreeMemory(tex_objs->mem[j]);
845 }
846
847 free(tex_objs->mem);
848 xglDestroyObject(tex_objs->image);
849}
850
851static void demo_prepare_textures(struct demo *demo)
852{
853 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
854 XGL_FORMAT_PROPERTIES props;
855 size_t size = sizeof(props);
856 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600857 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600858
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700859 err = xglGetFormatInfo(demo->device, tex_format,
860 XGL_INFO_TYPE_FORMAT_PROPERTIES,
861 &size, &props);
862 assert(!err);
863
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600864 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700865
866 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
867 /* Device can texture using linear textures */
868 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
869 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
870 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT){
871 /* Must use staging buffer to copy linear texture to optimized */
872 struct texture_objects staging_texture;
873
874 memset(&staging_texture, 0, sizeof(staging_texture));
875 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
876 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
877
878 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
879 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
880
881 XGL_CMD_BUFFER staging_cmd_buf;
882 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {
883 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
884 .pNext = NULL,
885 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
886 .flags = 0
887 };
888
889 err = xglCreateCommandBuffer(demo->device, &cmd_buf_create_info, &staging_cmd_buf);
890 assert(!err);
891
892 /* Copy staging texture to usable texture */
893 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {
894 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
895 .pNext = NULL,
896 .flags = 0
897 };
898
899 err = xglResetCommandBuffer(staging_cmd_buf);
900 assert(!err);
901
902 err = xglBeginCommandBuffer(staging_cmd_buf, &cmd_buf_begin_info);
903 assert(!err);
904
905 XGL_IMAGE_COPY copy_region = {
906 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
907 .srcOffset = { 0, 0, 0 },
908 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
909 .destOffset = { 0, 0, 0 },
910 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
911 };
912 xglCmdCopyImage(staging_cmd_buf, staging_texture.image, demo->textures[i].image, 1, &copy_region);
913
914 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
915 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
916 .pNext = NULL,
917 .outputMask = XGL_MEMORY_OUTPUT_COPY_BIT,
918 .inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT,
919 .oldLayout = XGL_IMAGE_LAYOUT_GENERAL,
920 .newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
921 .image = staging_texture.image,
922 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
923 };
924 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
925
926 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
927 XGL_PIPELINE_BARRIER pipeline_barrier;
928 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
929 pipeline_barrier.pNext = NULL;
930 pipeline_barrier.eventCount = 1;
931 pipeline_barrier.pEvents = set_events;
932 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
933 pipeline_barrier.memBarrierCount = 1;
934 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
935
936 // write barrier to the command buffer
937 xglCmdPipelineBarrier(staging_cmd_buf, &pipeline_barrier);
938
939 err = xglEndCommandBuffer(staging_cmd_buf);
940 assert(!err);
941
942 const XGL_CMD_BUFFER cmd_bufs[] = { staging_cmd_buf };
943 XGL_MEMORY_REF mem_refs[16];
944 uint32_t num_refs = 0;
945
946 for (uint32_t j = 0; j < staging_texture.num_mem; j++) {
947 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
948 mem_refs[num_refs].mem = staging_texture.mem[j];
949 num_refs++;
950 assert(num_refs < 16);
951 }
952
953 for (uint32_t j = 0; j < demo->textures[i].num_mem; j++) {
954 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
955 mem_refs[num_refs].mem = demo->textures[i].mem[j];
956 num_refs++;
957 assert(num_refs < 16);
958 }
959
960 err = xglQueueSubmit(demo->queue, 1, cmd_bufs,
961 num_refs, mem_refs, XGL_NULL_HANDLE);
962 assert(!err);
963
964 err = xglQueueWaitIdle(demo->queue);
965 assert(!err);
966
967 demo_destroy_texture_image(&staging_texture);
968
969 xglDestroyObject(staging_cmd_buf);
970 } else {
971 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
972 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
973 }
974
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600975 const XGL_SAMPLER_CREATE_INFO sampler = {
976 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
977 .pNext = NULL,
978 .magFilter = XGL_TEX_FILTER_NEAREST,
979 .minFilter = XGL_TEX_FILTER_NEAREST,
980 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600981 .addressU = XGL_TEX_ADDRESS_CLAMP,
982 .addressV = XGL_TEX_ADDRESS_CLAMP,
983 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600984 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985 .maxAnisotropy = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600986 .compareFunc = XGL_COMPARE_NEVER,
987 .minLod = 0.0f,
988 .maxLod = 0.0f,
989 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
990 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600991
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600992 XGL_IMAGE_VIEW_CREATE_INFO view = {
993 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
994 .pNext = NULL,
995 .image = XGL_NULL_HANDLE,
996 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700997 .format = tex_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600998 .channels = { XGL_CHANNEL_SWIZZLE_R,
999 XGL_CHANNEL_SWIZZLE_G,
1000 XGL_CHANNEL_SWIZZLE_B,
1001 XGL_CHANNEL_SWIZZLE_A, },
1002 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
1003 .minLod = 0.0f,
1004 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001005
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001006 /* create sampler */
1007 err = xglCreateSampler(demo->device, &sampler,
1008 &demo->textures[i].sampler);
1009 assert(!err);
1010
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001011 /* create image view */
1012 view.image = demo->textures[i].image;
1013 err = xglCreateImageView(demo->device, &view,
1014 &demo->textures[i].view);
1015 assert(!err);
1016 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001017}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001019void demo_prepare_cube_data_buffer(struct demo *demo)
1020{
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001021 XGL_BUFFER_CREATE_INFO buf_info;
1022 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001023 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1024 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1025 .pNext = NULL,
1026 };
1027 XGL_MEMORY_ALLOC_INFO alloc_info = {
1028 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1029 .pNext = &buf_alloc,
1030 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -07001031 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -07001032 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001033 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1034 };
1035 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001036 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001037 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001038 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1039 uint32_t num_allocations = 0;
1040 size_t num_alloc_size = sizeof(num_allocations);
1041 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001042 int i;
1043 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001044 XGL_RESULT err;
1045 struct xgltexcube_vs_uniform data;
1046
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001047 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001048 mat4x4_mul(MVP, VP, demo->model_matrix);
1049 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001050// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001051
1052 for (i=0; i<12*3; i++) {
1053 data.position[i][0] = g_vertex_buffer_data[i*3];
1054 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1055 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1056 data.position[i][3] = 1.0f;
1057 data.attr[i][0] = g_uv_buffer_data[2*i];
1058 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1059 data.attr[i][2] = 0;
1060 data.attr[i][3] = 0;
1061 }
1062
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001063 memset(&buf_info, 0, sizeof(buf_info));
1064 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1065 buf_info.size = sizeof(data);
1066 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1067 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1068 assert(!err);
1069
1070 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001071 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1072 &num_alloc_size, &num_allocations);
1073 assert(!err && num_alloc_size == sizeof(num_allocations));
1074 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1075 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1076 demo->uniform_data.num_mem = num_allocations;
1077 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001078 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001079 &mem_reqs_size, mem_reqs);
1080 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1081 err = xglGetObjectInfo(demo->uniform_data.buf,
1082 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1083 &buf_reqs_size, &buf_reqs);
1084 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1085 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001086 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001087 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001088
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001089 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1090 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001091
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001092 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001093 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001094
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001095 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001096
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001097 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1098 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001099
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001100 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1101 demo->uniform_data.mem[i], 0);
1102 assert(!err);
1103 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001104
1105 memset(&view_info, 0, sizeof(view_info));
1106 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1107 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +08001108 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001109 view_info.offset = 0;
1110 view_info.range = sizeof(data);
1111
1112 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1113 assert(!err);
1114
1115 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1116 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001117}
1118
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001119static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001120{
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001121 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
1122 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1123 .pNext = NULL,
1124 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1125 .count = DEMO_TEXTURE_COUNT,
1126 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
1127 .immutableSampler = XGL_NULL_HANDLE,
1128 };
Chia-I Wu87544e72015-02-23 10:41:08 -07001129 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
1130 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1131 .pNext = &descriptor_layout_fs,
1132 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1133 .count = 1,
1134 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
1135 .immutableSampler = XGL_NULL_HANDLE,
1136 };
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001137 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001138 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001139
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001140 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu87544e72015-02-23 10:41:08 -07001141 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001142 XGL_NULL_HANDLE, &descriptor_layout_vs,
Chia-I Wu87544e72015-02-23 10:41:08 -07001143 &demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001144 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001145}
1146
1147static XGL_SHADER demo_prepare_shader(struct demo *demo,
1148 XGL_PIPELINE_SHADER_STAGE stage,
1149 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001150 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001151{
1152 XGL_SHADER_CREATE_INFO createInfo;
1153 XGL_SHADER shader;
1154 XGL_RESULT err;
1155
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001156
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001157 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1158 createInfo.pNext = NULL;
1159
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001160#ifdef EXTERNAL_BIL
1161 createInfo.codeSize = size;
1162 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001163 createInfo.flags = 0;
1164
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001165 err = xglCreateShader(demo->device, &createInfo, &shader);
1166 if (err) {
1167 free((void *) createInfo.pCode);
1168 }
1169#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001170 // Create fake BIL structure to feed GLSL
1171 // to the driver "under the covers"
1172 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1173 createInfo.pCode = malloc(createInfo.codeSize);
1174 createInfo.flags = 0;
1175
1176 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1177 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1178 ((uint32_t *) createInfo.pCode)[1] = 0;
1179 ((uint32_t *) createInfo.pCode)[2] = stage;
1180 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1181
1182 err = xglCreateShader(demo->device, &createInfo, &shader);
1183 if (err) {
1184 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001185 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001186 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001187#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001188
1189 return shader;
1190}
1191
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001192char *demo_read_bil(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001193{
1194 long int size;
1195 void *shader_code;
1196
1197 FILE *fp = fopen(filename, "rb");
1198 if (!fp) return NULL;
1199
1200 fseek(fp, 0L, SEEK_END);
1201 size = ftell(fp);
1202
1203 fseek(fp, 0L, SEEK_SET);
1204
1205 shader_code = malloc(size);
1206 fread(shader_code, size, 1, fp);
1207
1208 *psize = size;
1209
1210 return shader_code;
1211}
1212
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001213static XGL_SHADER demo_prepare_vs(struct demo *demo)
1214{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001215#ifdef EXTERNAL_BIL
1216 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001217 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001218
1219 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1220
1221 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1222 vertShaderCode, size);
1223#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001224 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001225 "#version 140\n"
1226 "#extension GL_ARB_separate_shader_objects : enable\n"
1227 "#extension GL_ARB_shading_language_420pack : enable\n"
1228 "\n"
1229 "layout(binding = 0) uniform buf {\n"
1230 " mat4 MVP;\n"
1231 " vec4 position[12*3];\n"
1232 " vec4 attr[12*3];\n"
1233 "} ubuf;\n"
1234 "\n"
1235 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001236 "\n"
1237 "void main() \n"
1238 "{\n"
1239 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001240 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1241 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001242
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001243 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1244 (const void *) vertShaderText,
1245 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001246#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247}
1248
1249static XGL_SHADER demo_prepare_fs(struct demo *demo)
1250{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001251#ifdef EXTERNAL_BIL
1252 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001253 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001254
1255 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1256
1257 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1258 fragShaderCode, size);
1259#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001260 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001261 "#version 140\n"
1262 "#extension GL_ARB_separate_shader_objects : enable\n"
1263 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001264 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001265 "\n"
1266 "layout (location = 0) in vec4 texcoord;\n"
1267 "void main() {\n"
1268 " gl_FragColor = texture(tex, texcoord.xy);\n"
1269 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001270
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001271 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1272 (const void *) fragShaderText,
1273 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001274#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001275}
1276
1277static void demo_prepare_pipeline(struct demo *demo)
1278{
1279 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001280 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1281 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001282 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1283 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001284 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1285 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001286 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1287 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001288 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001289
1290 memset(&pipeline, 0, sizeof(pipeline));
1291 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu87544e72015-02-23 10:41:08 -07001292 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001293
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001294 memset(&ia, 0, sizeof(ia));
1295 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1296 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1297
1298 memset(&rs, 0, sizeof(rs));
1299 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001300 rs.fillMode = XGL_FILL_SOLID;
1301 rs.cullMode = XGL_CULL_NONE;
1302 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001303
1304 memset(&cb, 0, sizeof(cb));
1305 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001306 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1307 memset(att_state, 0, sizeof(att_state));
1308 att_state[0].format = demo->format;
1309 att_state[0].channelWriteMask = 0xf;
1310 att_state[0].blendEnable = XGL_FALSE;
1311 cb.attachmentCount = 1;
1312 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001313
Tony Barbour29645d02015-01-16 14:27:35 -07001314 memset(&vp, 0, sizeof(vp));
1315 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001316 vp.numViewports = 1;
1317 vp.clipOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001318
1319 memset(&ds, 0, sizeof(ds));
1320 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1321 ds.format = demo->depth.format;
1322 ds.depthTestEnable = XGL_TRUE;
1323 ds.depthWriteEnable = XGL_TRUE;
1324 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1325 ds.depthBoundsEnable = XGL_FALSE;
1326 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1327 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1328 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1329 ds.stencilTestEnable = XGL_FALSE;
1330 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001331
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001332 memset(&vs, 0, sizeof(vs));
1333 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1334 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001335 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001336 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001337
1338 memset(&fs, 0, sizeof(fs));
1339 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1340 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1341 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001342 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001343
1344 memset(&ms, 0, sizeof(ms));
1345 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1346 ms.sampleMask = 1;
1347 ms.multisampleEnable = XGL_FALSE;
1348 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001349
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001350 pipeline.pNext = (const void *) &ia;
1351 ia.pNext = (const void *) &rs;
1352 rs.pNext = (const void *) &cb;
1353 cb.pNext = (const void *) &ms;
1354 ms.pNext = (const void *) &vp;
1355 vp.pNext = (const void *) &ds;
1356 ds.pNext = (const void *) &vs;
1357 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001358
1359 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1360 assert(!err);
1361
1362 xglDestroyObject(vs.shader.shader);
1363 xglDestroyObject(fs.shader.shader);
1364}
1365
1366static void demo_prepare_dynamic_states(struct demo *demo)
1367{
Tony Barbour29645d02015-01-16 14:27:35 -07001368 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1369 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1370 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1371 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001372 XGL_RESULT err;
1373
Tony Barbour29645d02015-01-16 14:27:35 -07001374 memset(&viewport_create, 0, sizeof(viewport_create));
1375 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001376 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001377 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001378 XGL_RECT scissor;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001379 viewport.height = (float) demo->height;
1380 viewport.width = (float) demo->width;
1381 viewport.minDepth = (float) 0.0f;
1382 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001383 scissor.extent.width = demo->width;
1384 scissor.extent.height = demo->height;
1385 scissor.offset.x = 0;
1386 scissor.offset.y = 0;
Tony Barbour29645d02015-01-16 14:27:35 -07001387 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001388 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389
1390 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001391 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392
1393 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001394 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001395
1396 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001397 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1398 depth_stencil.stencilBackRef = 0;
1399 depth_stencil.stencilFrontRef = 0;
1400 depth_stencil.stencilReadMask = 0xff;
1401 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402
Tony Barbour29645d02015-01-16 14:27:35 -07001403 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404 assert(!err);
1405
Tony Barbour29645d02015-01-16 14:27:35 -07001406 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407 assert(!err);
1408
Tony Barbour29645d02015-01-16 14:27:35 -07001409 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001410 &color_blend, &demo->color_blend);
1411 assert(!err);
1412
Tony Barbour29645d02015-01-16 14:27:35 -07001413 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414 &depth_stencil, &demo->depth_stencil);
1415 assert(!err);
1416}
1417
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001418static void demo_prepare_descriptor_region(struct demo *demo)
1419{
1420 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1421 [0] = {
1422 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1423 .count = 1,
1424 },
1425 [1] = {
1426 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1427 .count = DEMO_TEXTURE_COUNT,
1428 },
1429 };
1430 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1431 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1432 .pNext = NULL,
1433 .count = 2,
1434 .pTypeCount = type_counts,
1435 };
1436 XGL_RESULT err;
1437
1438 err = xglCreateDescriptorRegion(demo->device,
1439 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1440 &descriptor_region, &demo->desc_region);
1441 assert(!err);
1442}
1443
1444static void demo_prepare_descriptor_set(struct demo *demo)
1445{
1446 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1447 &demo->uniform_data.attach;
1448 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1449 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1450 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1451 XGL_UPDATE_BUFFERS update_vs;
1452 XGL_RESULT err;
1453 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001454 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001455
1456 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1457 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1458 view_info[i].pNext = NULL;
1459 view_info[i].view = demo->textures[i].view,
1460 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1461
1462 combined_info[i].pSampler = demo->textures[i].sampler;
1463 combined_info[i].pImageView = &view_info[i];
1464 }
1465
1466 memset(&update_vs, 0, sizeof(update_vs));
1467 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1468 update_vs.pNext = &update_fs;
1469 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1470 update_vs.count = 1;
1471 update_vs.pBufferViews = &view_info_vs;
1472
1473 memset(&update_fs, 0, sizeof(update_fs));
1474 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1475 update_fs.index = 1;
1476 update_fs.count = DEMO_TEXTURE_COUNT;
1477 update_fs.pSamplerImageViews = combined_info;
1478
1479 err = xglAllocDescriptorSets(demo->desc_region,
1480 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001481 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001482 &demo->desc_set, &count);
1483 assert(!err && count == 1);
1484
1485 xglBeginDescriptorRegionUpdate(demo->device,
1486 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1487
1488 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1489 xglUpdateDescriptors(demo->desc_set, &update_vs);
1490
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001491 xglEndDescriptorRegionUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001492}
1493
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001494static void demo_prepare(struct demo *demo)
1495{
1496 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1497 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1498 .pNext = NULL,
1499 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1500 .flags = 0,
1501 };
1502 XGL_RESULT err;
1503
1504 demo_prepare_buffers(demo);
1505 demo_prepare_depth(demo);
1506 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001507 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001508
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001509 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001510 demo_prepare_pipeline(demo);
1511 demo_prepare_dynamic_states(demo);
1512
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001513 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1514 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1515 assert(!err);
1516 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001517
1518 demo_prepare_descriptor_region(demo);
1519 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001520
1521
1522 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1523 demo->current_buffer = i;
1524 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1525 }
1526
1527 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001528}
1529
1530static void demo_handle_event(struct demo *demo,
1531 const xcb_generic_event_t *event)
1532{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001533 u_int8_t event_code = event->response_type & 0x7f;
1534 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001535 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001536 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001538 case XCB_CLIENT_MESSAGE:
1539 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1540 (*demo->atom_wm_delete_window).atom) {
1541 demo->quit = true;
1542 }
1543 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001544 case XCB_KEY_RELEASE:
1545 {
1546 const xcb_key_release_event_t *key =
1547 (const xcb_key_release_event_t *) event;
1548
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001549 switch (key->detail) {
1550 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001551 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001552 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001553 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001554 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001555 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001556 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001557 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001558 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001559 case 0x41:
1560 demo->pause = !demo->pause;
1561 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001562 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001563 }
1564 break;
1565 default:
1566 break;
1567 }
1568}
1569
1570static void demo_run(struct demo *demo)
1571{
1572 xcb_flush(demo->connection);
1573
1574 while (!demo->quit) {
1575 xcb_generic_event_t *event;
1576
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001577 if (demo->pause) {
1578 event = xcb_wait_for_event(demo->connection);
1579 } else {
1580 event = xcb_poll_for_event(demo->connection);
1581 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582 if (event) {
1583 demo_handle_event(demo, event);
1584 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001585 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001586
1587 // Wait for work to finish before updating MVP.
1588 xglDeviceWaitIdle(demo->device);
1589 demo_update_data_buffer(demo);
1590
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001591 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001592
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001593 // Wait for work to finish before updating MVP.
1594 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001595 }
1596}
1597
1598static void demo_create_window(struct demo *demo)
1599{
1600 uint32_t value_mask, value_list[32];
1601
1602 demo->window = xcb_generate_id(demo->connection);
1603
1604 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1605 value_list[0] = demo->screen->black_pixel;
1606 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1607 XCB_EVENT_MASK_EXPOSURE;
1608
1609 xcb_create_window(demo->connection,
1610 XCB_COPY_FROM_PARENT,
1611 demo->window, demo->screen->root,
1612 0, 0, demo->width, demo->height, 0,
1613 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1614 demo->screen->root_visual,
1615 value_mask, value_list);
1616
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001617 /* Magic code that will send notification when window is destroyed */
1618 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1619 "WM_PROTOCOLS");
1620 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1621
1622 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1623 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1624
1625 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1626 demo->window, (*reply).atom, 4, 32, 1,
1627 &(*demo->atom_wm_delete_window).atom);
1628 free(reply);
1629
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001630 xcb_map_window(demo->connection, demo->window);
1631}
1632
1633static void demo_init_xgl(struct demo *demo)
1634{
1635 const XGL_APPLICATION_INFO app = {
1636 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1637 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001638 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001639 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001640 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001641 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001642 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001643 };
1644 const XGL_WSI_X11_CONNECTION_INFO connection = {
1645 .pConnection = demo->connection,
1646 .root = demo->screen->root,
1647 .provider = 0,
1648 };
1649 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1650 .queueNodeIndex = 0,
1651 .queueCount = 1,
1652 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001653 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001654 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001655 };
1656 const XGL_DEVICE_CREATE_INFO device = {
1657 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1658 .pNext = NULL,
1659 .queueRecordCount = 1,
1660 .pRequestedQueues = &queue,
1661 .extensionCount = 1,
1662 .ppEnabledExtensionNames = ext_names,
1663 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1664 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1665 };
1666 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001667 uint32_t gpu_count;
1668 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001669
Jon Ashburn93cfc432015-01-29 15:47:01 -07001670 err = xglCreateInstance(&app, NULL, &demo->inst);
1671 assert(!err);
1672 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001673 assert(!err && gpu_count == 1);
1674
1675 for (i = 0; i < device.extensionCount; i++) {
1676 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1677 assert(!err);
1678 }
1679
1680 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1681 assert(!err);
1682
1683 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1684 assert(!err);
1685
1686 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1687 0, &demo->queue);
1688 assert(!err);
1689}
1690
1691static void demo_init_connection(struct demo *demo)
1692{
1693 const xcb_setup_t *setup;
1694 xcb_screen_iterator_t iter;
1695 int scr;
1696
1697 demo->connection = xcb_connect(NULL, &scr);
1698
1699 setup = xcb_get_setup(demo->connection);
1700 iter = xcb_setup_roots_iterator(setup);
1701 while (scr-- > 0)
1702 xcb_screen_next(&iter);
1703
1704 demo->screen = iter.data;
1705}
1706
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001707static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001708{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001709 vec3 eye = {0.0f, 3.0f, 5.0f};
1710 vec3 origin = {0, 0, 0};
1711 vec3 up = {0.0f, -1.0f, 0.0};
1712
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001713 memset(demo, 0, sizeof(*demo));
1714
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001715 for (int i = 0; i < argc; i++) {
1716 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1717 demo->use_staging_buffer = true;
1718 }
1719
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001720 demo_init_connection(demo);
1721 demo_init_xgl(demo);
1722
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001723 demo->width = 500;
1724 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001725 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001726
1727 demo->spin_angle = 0.01f;
1728 demo->spin_increment = 0.01f;
1729 demo->pause = false;
1730
1731 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1732 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1733 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734}
1735
1736static void demo_cleanup(struct demo *demo)
1737{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001738 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001739
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001740 xglDestroyObject(demo->desc_set);
1741 xglDestroyObject(demo->desc_region);
1742
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001743 xglDestroyObject(demo->viewport);
1744 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745 xglDestroyObject(demo->color_blend);
1746 xglDestroyObject(demo->depth_stencil);
1747
1748 xglDestroyObject(demo->pipeline);
Chia-I Wu87544e72015-02-23 10:41:08 -07001749 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001750
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1752 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001753 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001754 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001755 for (j = 0; j < demo->textures[i].num_mem; j++)
1756 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001757 xglDestroyObject(demo->textures[i].sampler);
1758 }
1759
1760 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001761 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001762 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001763 for (j = 0; j < demo->depth.num_mem; j++)
1764 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001765
1766 xglDestroyObject(demo->uniform_data.view);
1767 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001768 xglDestroyObject(demo->uniform_data.buf);
1769 for (j = 0; j < demo->uniform_data.num_mem; j++)
1770 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001771
1772 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001773 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001774 xglDestroyObject(demo->buffers[i].view);
1775 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001776 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001777 }
1778
1779 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001780 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001781
1782 xcb_destroy_window(demo->connection, demo->window);
1783 xcb_disconnect(demo->connection);
1784}
1785
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001786int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001787{
1788 struct demo demo;
1789
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001790 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001791
1792 demo_prepare(&demo);
1793 demo_create_window(&demo);
1794 demo_run(&demo);
1795
1796 demo_cleanup(&demo);
1797
1798 return 0;
1799}