blob: 1da70b3142b228923e2b0918efd8b3ace1ccd942 [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"
Piers Daniell735ee532015-02-23 16:23:13 -070016#if defined(__linux__)
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -060017#include <unistd.h>
Piers Daniell735ee532015-02-23 16:23:13 -070018#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060019#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060020
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060021#define DEMO_BUFFER_COUNT 2
22#define DEMO_TEXTURE_COUNT 1
23
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060024/*
25 * When not defined, code will use built-in GLSL compiler
26 * which may not be supported on all drivers
27 */
Piers Daniell735ee532015-02-23 16:23:13 -070028#if !defined(XCB_NVIDIA)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060029#define EXTERNAL_BIL
Piers Daniell735ee532015-02-23 16:23:13 -070030#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060031
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070032/*
33 * structure to track all objects related to a texture.
34 */
35struct texture_objects {
36 XGL_SAMPLER sampler;
37
38 XGL_IMAGE image;
39 uint32_t num_mem;
40 XGL_GPU_MEMORY *mem;
41 XGL_IMAGE_VIEW view;
42 int32_t tex_width, tex_height;
43};
44
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060045static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060046 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060047};
48
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060049struct xglcube_vs_uniform {
50 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060051 float mvp[4][4];
52 float position[12*3][4];
53 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060054};
55
56struct xgltexcube_vs_uniform {
57 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060058 float mvp[4][4];
59 float position[12*3][4];
60 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060061};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060062
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060063//--------------------------------------------------------------------------------------
64// Mesh and VertexFormat Data
65//--------------------------------------------------------------------------------------
66struct Vertex
67{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060068 float posX, posY, posZ, posW; // Position data
69 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060070};
71
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060072struct VertexPosTex
73{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060074 float posX, posY, posZ, posW; // Position data
75 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060076};
77
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060078#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060079#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060080
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060081static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060082 -1.0f,-1.0f,-1.0f, // Vertex 0
83 -1.0f,-1.0f, 1.0f,
84 -1.0f, 1.0f, 1.0f,
85
86 -1.0f, 1.0f, 1.0f, // Vertex 1
87 -1.0f, 1.0f,-1.0f,
88 -1.0f,-1.0f,-1.0f,
89
90 -1.0f,-1.0f,-1.0f, // Vertex 2
91 1.0f, 1.0f,-1.0f,
92 1.0f,-1.0f,-1.0f,
93
94 -1.0f,-1.0f,-1.0f, // Vertex 3
95 1.0f, 1.0f,-1.0f,
96 -1.0f, 1.0f,-1.0f,
97
98 -1.0f,-1.0f,-1.0f, // Vertex 4
99 1.0f,-1.0f, 1.0f,
100 1.0f,-1.0f,-1.0f,
101
102 -1.0f,-1.0f,-1.0f, // Vertex 5
103 -1.0f,-1.0f, 1.0f,
104 1.0f,-1.0f, 1.0f,
105
106 -1.0f, 1.0f,-1.0f, // Vertex 6
107 -1.0f, 1.0f, 1.0f,
108 1.0f, 1.0f, 1.0f,
109
110 -1.0f, 1.0f,-1.0f, // Vertex 7
111 1.0f, 1.0f,-1.0f,
112 1.0f, 1.0f, 1.0f,
113
114 1.0f, 1.0f,-1.0f, // Vertex 8
115 1.0f, 1.0f, 1.0f,
116 1.0f,-1.0f, 1.0f,
117
118 1.0f,-1.0f, 1.0f, // Vertex 9
119 1.0f,-1.0f,-1.0f,
120 1.0f, 1.0f,-1.0f,
121
122 -1.0f, 1.0f, 1.0f, // Vertex 10
123 1.0f, 1.0f, 1.0f,
124 -1.0f,-1.0f, 1.0f,
125
126 -1.0f,-1.0f, 1.0f, // Vertex 11
127 1.0f,-1.0f, 1.0f,
128 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600129};
130
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600131static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600132 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600133 0.0f, 0.0f,
134 0.0f, 1.0f,
135
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600136 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600137 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600138 1.0f, 0.0f,
139
140// 0.0f, 1.0f, // Vertex 2
141// 1.0f, 0.0f,
142// 0.0f, 0.0f,
143
144// 0.0f, 1.0f, // Vertex 3
145// 1.0f, 0.0f,
146// 1.0f, 1.0f,
147
148 0.0f, 0.0f, // Vertex 2
149 1.0f, 1.0f,
150 1.0f, 0.0f,
151
152 0.0f, 0.0f, // Vertex 3
153 1.0f, 1.0f,
154 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600155
156 0.0f, 1.0f, // Vertex 4
157 1.0f, 0.0f,
158 0.0f, 0.0f,
159
160 0.0f, 1.0f, // Vertex 5
161 1.0f, 1.0f,
162 1.0f, 0.0f,
163
164 0.0f, 1.0f, // Vertex 6
165 1.0f, 1.0f,
166 1.0f, 0.0f,
167
168 0.0f, 1.0f, // Vertex 7
169 0.0f, 0.0f,
170 1.0f, 0.0f,
171
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600172 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600175
176 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 0.0f, 1.0f,
179
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600180 1.0f, 1.0f, // Vertex 10
181 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600183
184 1.0f, 0.0f, // Vertex 11
185 0.0f, 0.0f,
186 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600187};
188
189void dumpMatrix(const char *note, mat4x4 MVP)
190{
191 int i;
192
193 printf("%s: \n", note);
194 for (i=0; i<4; i++) {
195 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
196 }
197 printf("\n");
198 fflush(stdout);
199}
200
201void dumpVec4(const char *note, vec4 vector)
202{
203 printf("%s: \n", note);
204 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
205 printf("\n");
206 fflush(stdout);
207}
208
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600209struct demo {
210 xcb_connection_t *connection;
211 xcb_screen_t *screen;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700212 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600213
Jon Ashburn93cfc432015-01-29 15:47:01 -0700214 XGL_INSTANCE inst;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600215 XGL_PHYSICAL_GPU gpu;
216 XGL_DEVICE device;
217 XGL_QUEUE queue;
218
219 int width, height;
220 XGL_FORMAT format;
221
222 struct {
223 XGL_IMAGE image;
224 XGL_GPU_MEMORY mem;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700225 XGL_CMD_BUFFER cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600226
227 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800228 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600229 } buffers[DEMO_BUFFER_COUNT];
230
231 struct {
232 XGL_FORMAT format;
233
234 XGL_IMAGE image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600235 uint32_t num_mem;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700236 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600237 XGL_DEPTH_STENCIL_VIEW view;
238 } depth;
239
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700240 struct texture_objects textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600241
242 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800243 XGL_BUFFER buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600244 uint32_t num_mem;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700245 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800246 XGL_BUFFER_VIEW view;
247 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600248 } uniform_data;
249
Chia-I Wu87544e72015-02-23 10:41:08 -0700250 XGL_DESCRIPTOR_SET_LAYOUT desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600251 XGL_PIPELINE pipeline;
252
Tony Barbour29645d02015-01-16 14:27:35 -0700253 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
254 XGL_DYNAMIC_RS_STATE_OBJECT raster;
255 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
256 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600257
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600258 mat4x4 projection_matrix;
259 mat4x4 view_matrix;
260 mat4x4 model_matrix;
261
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600262 float spin_angle;
263 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600264 bool pause;
265
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800266 XGL_DESCRIPTOR_REGION desc_region;
267 XGL_DESCRIPTOR_SET desc_set;
268
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600269 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700270 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600271
272 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600273 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600274};
275
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700276static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600277{
278 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
279 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000280 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600281 };
282 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
283 .view = demo->depth.view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000284 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600285 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600286 const float clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
287 const float clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600288 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700289 XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = {
290 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO,
291 .pNext = NULL,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700292 };
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700293 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
294 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700295 .pNext = &graphics_cmd_buf_info,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700296 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
297 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
298 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600299 XGL_RESULT err;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700300 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
301 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
302 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
303 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
304 .pNext = NULL,
305 .colorAttachmentCount = 1,
306 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
307 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
308 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600309 .width = demo->width,
310 .height = demo->height,
311 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700312 };
313 XGL_RENDER_PASS_CREATE_INFO rp_info;
314
315 memset(&rp_info, 0 , sizeof(rp_info));
316 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
317 assert(!err);
318 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
319 rp_info.renderArea.extent.width = demo->width;
320 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchterc17d7de2015-02-10 14:06:25 -0700321 rp_info.colorAttachmentCount = 1;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700322 rp_info.pColorLoadOps = &load_op;
323 rp_info.pColorStoreOps = &store_op;
324 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
325 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
326 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
327 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
328 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
329 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600330
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700331 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600332 assert(!err);
333
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700334 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335 demo->pipeline);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700336 xglCmdBindDescriptorSet(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800337 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700339 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
340 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
341 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600342 demo->color_blend);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700343 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344 demo->depth_stencil);
345
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700346 xglCmdBeginRenderPass(cmd_buf, graphics_cmd_buf_info.renderPass);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600347 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
348 clear_range.baseMipLevel = 0;
349 clear_range.mipLevels = 1;
350 clear_range.baseArraySlice = 0;
351 clear_range.arraySize = 1;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700352 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600353 demo->buffers[demo->current_buffer].image,
354 clear_color, 1, &clear_range);
355
356 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700357 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600358 clear_depth, 0, 1, &clear_range);
359
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700360 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700361 xglCmdEndRenderPass(cmd_buf, graphics_cmd_buf_info.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600362
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700363 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700365
366 xglDestroyObject(graphics_cmd_buf_info.renderPass);
367 xglDestroyObject(rp_info.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600368}
369
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600370
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600371void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600373 mat4x4 MVP, Model, VP;
374 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600375 uint8_t *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376 XGL_RESULT err;
377
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600378 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600380 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600381 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700382 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600383 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600384
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700385 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600386 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600387 assert(!err);
388
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600389 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700391 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600392 assert(!err);
393}
394
395static void demo_draw(struct demo *demo)
396{
397 const XGL_WSI_X11_PRESENT_INFO present = {
398 .destWindow = demo->window,
399 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800400 .async = true,
401 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600402 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800403 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404 XGL_RESULT err;
405
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600406 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wubb57f642014-11-07 14:30:34 +0800407 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
408
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -0600409 uint32_t i, idx = 0;
410 XGL_MEMORY_REF *memRefs;
411 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
412 demo->depth.num_mem +
413 demo->textures[0].num_mem +
414 demo->uniform_data.num_mem));
415 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
416 memRefs[idx].mem = demo->depth.mem[i];
417 memRefs[idx].flags = 0;
418 }
419 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
420 memRefs[idx].mem = demo->textures[0].mem[i];
421 memRefs[idx].flags = 0;
422 }
423 memRefs[idx].mem = demo->buffers[0].mem;
424 memRefs[idx++].flags = 0;
425 memRefs[idx].mem = demo->buffers[1].mem;
426 memRefs[idx++].flags = 0;
427 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
428 memRefs[idx].mem = demo->uniform_data.mem[i];
429 memRefs[idx].flags = 0;
430 }
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700431 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Piers Daniell735ee532015-02-23 16:23:13 -0700432 idx, memRefs, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600433 assert(!err);
434
Chia-I Wubb57f642014-11-07 14:30:34 +0800435 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600436 assert(!err);
437
438 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
439}
440
441static void demo_prepare_buffers(struct demo *demo)
442{
443 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
444 .format = demo->format,
445 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
446 .extent = {
447 .width = demo->width,
448 .height = demo->height,
449 },
450 .flags = 0,
451 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800452 const XGL_FENCE_CREATE_INFO fence = {
453 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
454 .pNext = NULL,
455 .flags = 0,
456 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600457 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600458 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600459
460 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
461 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
462 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
463 .pNext = NULL,
464 .format = demo->format,
465 .mipLevel = 0,
466 .baseArraySlice = 0,
467 .arraySize = 1,
468 };
469
470 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
471 &demo->buffers[i].image, &demo->buffers[i].mem);
472 assert(!err);
473
474 color_attachment_view.image = demo->buffers[i].image;
475
476 err = xglCreateColorAttachmentView(demo->device,
477 &color_attachment_view, &demo->buffers[i].view);
478 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800479
480 err = xglCreateFence(demo->device,
481 &fence, &demo->buffers[i].fence);
482 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600483 }
484}
485
486static void demo_prepare_depth(struct demo *demo)
487{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700488 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600489 const XGL_IMAGE_CREATE_INFO image = {
490 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
491 .pNext = NULL,
492 .imageType = XGL_IMAGE_2D,
493 .format = depth_format,
494 .extent = { demo->width, demo->height, 1 },
495 .mipLevels = 1,
496 .arraySize = 1,
497 .samples = 1,
498 .tiling = XGL_OPTIMAL_TILING,
499 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
500 .flags = 0,
501 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700502 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
503 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
504 .pNext = NULL,
505 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600506 XGL_MEMORY_ALLOC_INFO mem_alloc = {
507 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700508 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600509 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700510 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700511 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600512 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
513 };
514 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
515 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
516 .pNext = NULL,
517 .image = XGL_NULL_HANDLE,
518 .mipLevel = 0,
519 .baseArraySlice = 0,
520 .arraySize = 1,
521 .flags = 0,
522 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700523 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600524 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700525 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600526 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600527 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600528 uint32_t num_allocations = 0;
529 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600530
531 demo->depth.format = depth_format;
532
533 /* create image */
534 err = xglCreateImage(demo->device, &image,
535 &demo->depth.image);
536 assert(!err);
537
Jon Ashburnb2a66652015-01-16 09:37:43 -0700538
539 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
540 assert(!err && num_alloc_size == sizeof(num_allocations));
541 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
542 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
543 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700545 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
546 &mem_reqs_size, mem_reqs);
547 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700548 err = xglGetObjectInfo(demo->depth.image,
549 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
550 &img_reqs_size, &img_reqs);
551 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
552 img_alloc.usage = img_reqs.usage;
553 img_alloc.formatClass = img_reqs.formatClass;
554 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600555 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700556 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600557
Jon Ashburnb2a66652015-01-16 09:37:43 -0700558 /* allocate memory */
559 err = xglAllocMemory(demo->device, &mem_alloc,
560 &(demo->depth.mem[i]));
561 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600562
Jon Ashburnb2a66652015-01-16 09:37:43 -0700563 /* bind memory */
564 err = xglBindObjectMemory(demo->depth.image, i,
565 demo->depth.mem[i], 0);
566 assert(!err);
567 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568
569 /* create image view */
570 view.image = demo->depth.image;
571 err = xglCreateDepthStencilView(demo->device, &view,
572 &demo->depth.view);
573 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600574}
575
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600576/** loadTexture
577 * loads a png file into an memory object, using cstdio , libpng.
578 *
579 * \param demo : Needed to access XGL calls
580 * \param filename : the png file to be loaded
581 * \param width : width of png, to be updated as a side effect of this function
582 * \param height : height of png, to be updated as a side effect of this function
583 *
584 * \return bool : an opengl texture id. true if successful?,
585 * should be validated by the client of this function.
586 *
587 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
588 * Modified to copy image to memory
589 *
590 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700591bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600592 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600593 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600594{
595 //header for testing if it is a png
596 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700597 int is_png, bit_depth, color_type,rowbytes;
598 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600599 png_structp png_ptr;
600 png_infop info_ptr, end_info;
601 png_byte *image_data;
602 png_bytep *row_pointers;
603
604 //open file as binary
605 FILE *fp = fopen(filename, "rb");
606 if (!fp) {
607 return false;
608 }
609
610 //read the header
611 fread(header, 1, 8, fp);
612
613 //test if png
614 is_png = !png_sig_cmp(header, 0, 8);
615 if (!is_png) {
616 fclose(fp);
617 return false;
618 }
619
620 //create png struct
621 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
622 NULL, NULL);
623 if (!png_ptr) {
624 fclose(fp);
625 return (false);
626 }
627
628 //create png info struct
629 info_ptr = png_create_info_struct(png_ptr);
630 if (!info_ptr) {
631 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
632 fclose(fp);
633 return (false);
634 }
635
636 //create png info struct
637 end_info = png_create_info_struct(png_ptr);
638 if (!end_info) {
639 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
640 fclose(fp);
641 return (false);
642 }
643
644 //png error stuff, not sure libpng man suggests this.
645 if (setjmp(png_jmpbuf(png_ptr))) {
646 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
647 fclose(fp);
648 return (false);
649 }
650
651 //init png reading
652 png_init_io(png_ptr, fp);
653
654 //let libpng know you already read the first 8 bytes
655 png_set_sig_bytes(png_ptr, 8);
656
657 // read all the info up to the image data
658 png_read_info(png_ptr, info_ptr);
659
660 // get info about png
661 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
662 NULL, NULL, NULL);
663
664 //update width and height based on png info
665 *width = twidth;
666 *height = theight;
667
668 // Require that incoming texture be 8bits per color component
669 // and 4 components (RGBA).
670 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
671 png_get_channels(png_ptr, info_ptr) != 4) {
672 return false;
673 }
674
675 if (rgba_data == NULL) {
676 // If data pointer is null, we just want the width & height
677 // clean up memory and close stuff
678 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
679 fclose(fp);
680
681 return true;
682 }
683
684 // Update the png info struct.
685 png_read_update_info(png_ptr, info_ptr);
686
687 // Row size in bytes.
688 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
689
690 // Allocate the image_data as a big block, to be given to opengl
691 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
692 if (!image_data) {
693 //clean up memory and close stuff
694 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
695 fclose(fp);
696 return false;
697 }
698
699 // row_pointers is for pointing to image_data for reading the png with libpng
700 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
701 if (!row_pointers) {
702 //clean up memory and close stuff
703 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
704 // delete[] image_data;
705 fclose(fp);
706 return false;
707 }
708 // set the individual row_pointers to point at the correct offsets of image_data
709 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700710 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600711
712 // read the png into image_data through row_pointers
713 png_read_image(png_ptr, row_pointers);
714
715 // clean up memory and close stuff
716 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
717 free(row_pointers);
718 free(image_data);
719 fclose(fp);
720
721 return true;
722}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600723
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700724static void demo_prepare_texture_image(struct demo *demo,
725 const char *filename,
726 struct texture_objects *tex_objs,
727 XGL_IMAGE_TILING tiling,
728 XGL_FLAGS mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600729{
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700730 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600731 int32_t tex_width;
732 int32_t tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733 XGL_RESULT err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700734
735 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
736 assert(err);
737
738 tex_objs->tex_width = tex_width;
739 tex_objs->tex_height = tex_height;
740
741 const XGL_IMAGE_CREATE_INFO image_create_info = {
742 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
743 .pNext = NULL,
744 .imageType = XGL_IMAGE_2D,
745 .format = tex_format,
746 .extent = { tex_width, tex_height, 1 },
747 .mipLevels = 1,
748 .arraySize = 1,
749 .samples = 1,
750 .tiling = tiling,
751 .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
752 .flags = 0,
753 };
Piers Daniell735ee532015-02-23 16:23:13 -0700754 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
755 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
756 .pNext = NULL,
757 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700758 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
759 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
Piers Daniell735ee532015-02-23 16:23:13 -0700760 .pNext = &buf_alloc,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700761 };
762 XGL_MEMORY_ALLOC_INFO mem_alloc = {
763 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
764 .pNext = &img_alloc,
765 .allocationSize = 0,
766 .memProps = mem_props,
767 .memType = XGL_MEMORY_TYPE_IMAGE,
768 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
769 };
770
771 XGL_MEMORY_REQUIREMENTS *mem_reqs;
772 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Piers Daniell735ee532015-02-23 16:23:13 -0700773 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
774 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700775 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
776 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
777 uint32_t num_allocations = 0;
778 size_t num_alloc_size = sizeof(num_allocations);
779
780 err = xglCreateImage(demo->device, &image_create_info,
781 &tex_objs->image);
782 assert(!err);
783
784 err = xglGetObjectInfo(tex_objs->image,
785 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
786 &num_alloc_size, &num_allocations);
787 assert(!err && num_alloc_size == sizeof(num_allocations));
788 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
789 tex_objs->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
790 err = xglGetObjectInfo(tex_objs->image,
791 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
792 &mem_reqs_size, mem_reqs);
793 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
794 err = xglGetObjectInfo(tex_objs->image,
795 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
796 &img_reqs_size, &img_reqs);
797 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
798 img_alloc.usage = img_reqs.usage;
799 img_alloc.formatClass = img_reqs.formatClass;
800 img_alloc.samples = img_reqs.samples;
801 mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
802 for (uint32_t j = 0; j < num_allocations; j ++) {
Courtney Goeltzenleuchter02ba99a2015-02-25 11:48:28 -0700803 mem_alloc.memType = mem_reqs[j].memType;
Piers Daniell735ee532015-02-23 16:23:13 -0700804 mem_alloc.allocationSize = mem_reqs[j].size;
805
806 if (mem_alloc.memType == XGL_MEMORY_TYPE_BUFFER) {
807 err = xglGetObjectInfo(tex_objs->image,
808 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
809 &buf_reqs_size, &buf_reqs);
810 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
811 buf_alloc.usage = buf_reqs.usage;
812 img_alloc.pNext = &buf_alloc;
813 } else {
814 img_alloc.pNext = 0;
815 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700816
817 /* allocate memory */
818 err = xglAllocMemory(demo->device, &mem_alloc,
819 &(tex_objs->mem[j]));
820 assert(!err);
821
822 /* bind memory */
823 err = xglBindObjectMemory(tex_objs->image, j, tex_objs->mem[j], 0);
824 assert(!err);
825 }
826 free(mem_reqs);
827 mem_reqs = NULL;
828
829 tex_objs->num_mem = num_allocations;
830
831 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
832 const XGL_IMAGE_SUBRESOURCE subres = {
833 .aspect = XGL_IMAGE_ASPECT_COLOR,
834 .mipLevel = 0,
835 .arraySlice = 0,
836 };
837 XGL_SUBRESOURCE_LAYOUT layout;
838 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
839 void *data;
840
841 err = xglGetImageSubresourceInfo(tex_objs->image, &subres,
842 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
843 &layout_size, &layout);
844 assert(!err && layout_size == sizeof(layout));
845 /* Linear texture must be within a single memory object */
846 assert(num_allocations == 1);
847
848 err = xglMapMemory(tex_objs->mem[0], 0, &data);
849 assert(!err);
850
851 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
852 fprintf(stderr, "Error loading texture: %s\n", filename);
853 }
854
855 err = xglUnmapMemory(tex_objs->mem[0]);
856 assert(!err);
857 }
858}
859
860static void demo_destroy_texture_image(struct texture_objects *tex_objs)
861{
862 /* clean up staging resources */
863 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
864 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
865 xglFreeMemory(tex_objs->mem[j]);
866 }
867
868 free(tex_objs->mem);
869 xglDestroyObject(tex_objs->image);
870}
871
872static void demo_prepare_textures(struct demo *demo)
873{
874 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
875 XGL_FORMAT_PROPERTIES props;
876 size_t size = sizeof(props);
877 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600878 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600879
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700880 err = xglGetFormatInfo(demo->device, tex_format,
881 XGL_INFO_TYPE_FORMAT_PROPERTIES,
882 &size, &props);
883 assert(!err);
884
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600885 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700886
887 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
888 /* Device can texture using linear textures */
889 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
890 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
891 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT){
892 /* Must use staging buffer to copy linear texture to optimized */
893 struct texture_objects staging_texture;
894
895 memset(&staging_texture, 0, sizeof(staging_texture));
896 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
897 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
898
899 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
900 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
901
902 XGL_CMD_BUFFER staging_cmd_buf;
903 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {
904 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
905 .pNext = NULL,
906 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
907 .flags = 0
908 };
909
910 err = xglCreateCommandBuffer(demo->device, &cmd_buf_create_info, &staging_cmd_buf);
911 assert(!err);
912
913 /* Copy staging texture to usable texture */
914 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {
915 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
916 .pNext = NULL,
917 .flags = 0
918 };
919
920 err = xglResetCommandBuffer(staging_cmd_buf);
921 assert(!err);
922
923 err = xglBeginCommandBuffer(staging_cmd_buf, &cmd_buf_begin_info);
924 assert(!err);
925
926 XGL_IMAGE_COPY copy_region = {
927 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
928 .srcOffset = { 0, 0, 0 },
929 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
930 .destOffset = { 0, 0, 0 },
931 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
932 };
933 xglCmdCopyImage(staging_cmd_buf, staging_texture.image, demo->textures[i].image, 1, &copy_region);
934
935 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
936 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
937 .pNext = NULL,
938 .outputMask = XGL_MEMORY_OUTPUT_COPY_BIT,
939 .inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT,
940 .oldLayout = XGL_IMAGE_LAYOUT_GENERAL,
941 .newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
942 .image = staging_texture.image,
943 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
944 };
945 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
946
947 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
948 XGL_PIPELINE_BARRIER pipeline_barrier;
949 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
950 pipeline_barrier.pNext = NULL;
951 pipeline_barrier.eventCount = 1;
952 pipeline_barrier.pEvents = set_events;
953 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
954 pipeline_barrier.memBarrierCount = 1;
955 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
956
957 // write barrier to the command buffer
958 xglCmdPipelineBarrier(staging_cmd_buf, &pipeline_barrier);
959
960 err = xglEndCommandBuffer(staging_cmd_buf);
961 assert(!err);
962
963 const XGL_CMD_BUFFER cmd_bufs[] = { staging_cmd_buf };
964 XGL_MEMORY_REF mem_refs[16];
965 uint32_t num_refs = 0;
966
967 for (uint32_t j = 0; j < staging_texture.num_mem; j++) {
968 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
969 mem_refs[num_refs].mem = staging_texture.mem[j];
970 num_refs++;
971 assert(num_refs < 16);
972 }
973
974 for (uint32_t j = 0; j < demo->textures[i].num_mem; j++) {
975 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
976 mem_refs[num_refs].mem = demo->textures[i].mem[j];
977 num_refs++;
978 assert(num_refs < 16);
979 }
980
981 err = xglQueueSubmit(demo->queue, 1, cmd_bufs,
982 num_refs, mem_refs, XGL_NULL_HANDLE);
983 assert(!err);
984
985 err = xglQueueWaitIdle(demo->queue);
986 assert(!err);
987
988 demo_destroy_texture_image(&staging_texture);
989
990 xglDestroyObject(staging_cmd_buf);
991 } else {
992 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
993 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
994 }
995
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600996 const XGL_SAMPLER_CREATE_INFO sampler = {
997 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
998 .pNext = NULL,
999 .magFilter = XGL_TEX_FILTER_NEAREST,
1000 .minFilter = XGL_TEX_FILTER_NEAREST,
1001 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001002 .addressU = XGL_TEX_ADDRESS_CLAMP,
1003 .addressV = XGL_TEX_ADDRESS_CLAMP,
1004 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001005 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001006 .maxAnisotropy = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001007 .compareFunc = XGL_COMPARE_NEVER,
1008 .minLod = 0.0f,
1009 .maxLod = 0.0f,
1010 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
1011 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001012
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001013 XGL_IMAGE_VIEW_CREATE_INFO view = {
1014 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1015 .pNext = NULL,
1016 .image = XGL_NULL_HANDLE,
1017 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018 .format = tex_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001019 .channels = { XGL_CHANNEL_SWIZZLE_R,
1020 XGL_CHANNEL_SWIZZLE_G,
1021 XGL_CHANNEL_SWIZZLE_B,
1022 XGL_CHANNEL_SWIZZLE_A, },
1023 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
1024 .minLod = 0.0f,
1025 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001026
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001027 /* create sampler */
1028 err = xglCreateSampler(demo->device, &sampler,
1029 &demo->textures[i].sampler);
1030 assert(!err);
1031
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001032 /* create image view */
1033 view.image = demo->textures[i].image;
1034 err = xglCreateImageView(demo->device, &view,
1035 &demo->textures[i].view);
1036 assert(!err);
1037 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001039
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001040void demo_prepare_cube_data_buffer(struct demo *demo)
1041{
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001042 XGL_BUFFER_CREATE_INFO buf_info;
1043 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001044 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1045 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1046 .pNext = NULL,
1047 };
1048 XGL_MEMORY_ALLOC_INFO alloc_info = {
1049 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1050 .pNext = &buf_alloc,
1051 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -07001052 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -07001053 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001054 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1055 };
1056 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001057 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001058 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001059 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1060 uint32_t num_allocations = 0;
1061 size_t num_alloc_size = sizeof(num_allocations);
1062 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001063 int i;
1064 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001065 XGL_RESULT err;
1066 struct xgltexcube_vs_uniform data;
1067
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001068 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001069 mat4x4_mul(MVP, VP, demo->model_matrix);
1070 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001071// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001072
1073 for (i=0; i<12*3; i++) {
1074 data.position[i][0] = g_vertex_buffer_data[i*3];
1075 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1076 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1077 data.position[i][3] = 1.0f;
1078 data.attr[i][0] = g_uv_buffer_data[2*i];
1079 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1080 data.attr[i][2] = 0;
1081 data.attr[i][3] = 0;
1082 }
1083
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001084 memset(&buf_info, 0, sizeof(buf_info));
1085 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1086 buf_info.size = sizeof(data);
1087 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1088 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1089 assert(!err);
1090
1091 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001092 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1093 &num_alloc_size, &num_allocations);
1094 assert(!err && num_alloc_size == sizeof(num_allocations));
1095 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1096 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1097 demo->uniform_data.num_mem = num_allocations;
1098 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001099 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001100 &mem_reqs_size, mem_reqs);
1101 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1102 err = xglGetObjectInfo(demo->uniform_data.buf,
1103 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1104 &buf_reqs_size, &buf_reqs);
1105 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1106 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001107 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001108 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001109
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001110 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1111 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001112
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001113 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001114 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001115
Piers Daniell735ee532015-02-23 16:23:13 -07001116 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001117
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001118 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1119 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001120
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001121 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1122 demo->uniform_data.mem[i], 0);
1123 assert(!err);
1124 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001125
1126 memset(&view_info, 0, sizeof(view_info));
1127 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1128 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +08001129 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001130 view_info.offset = 0;
1131 view_info.range = sizeof(data);
1132
1133 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1134 assert(!err);
1135
1136 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1137 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001138}
1139
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001140static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001141{
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001142 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
1143 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1144 .pNext = NULL,
1145 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1146 .count = DEMO_TEXTURE_COUNT,
1147 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
1148 .immutableSampler = XGL_NULL_HANDLE,
1149 };
Chia-I Wu87544e72015-02-23 10:41:08 -07001150 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
1151 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1152 .pNext = &descriptor_layout_fs,
1153 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1154 .count = 1,
1155 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
1156 .immutableSampler = XGL_NULL_HANDLE,
1157 };
Chia-I Wu06f82812015-02-23 10:41:08 -07001158
1159 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001160 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001161
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001162 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu06f82812015-02-23 10:41:08 -07001163 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001164 XGL_NULL_HANDLE, &descriptor_layout_vs,
Chia-I Wu87544e72015-02-23 10:41:08 -07001165 &demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001166 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001167}
1168
1169static XGL_SHADER demo_prepare_shader(struct demo *demo,
1170 XGL_PIPELINE_SHADER_STAGE stage,
1171 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001172 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001173{
1174 XGL_SHADER_CREATE_INFO createInfo;
1175 XGL_SHADER shader;
1176 XGL_RESULT err;
1177
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001178
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001179 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1180 createInfo.pNext = NULL;
1181
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001182#ifdef EXTERNAL_BIL
1183 createInfo.codeSize = size;
1184 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001185 createInfo.flags = 0;
1186
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001187 err = xglCreateShader(demo->device, &createInfo, &shader);
1188 if (err) {
1189 free((void *) createInfo.pCode);
1190 }
1191#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001192 // Create fake BIL structure to feed GLSL
1193 // to the driver "under the covers"
1194 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1195 createInfo.pCode = malloc(createInfo.codeSize);
1196 createInfo.flags = 0;
1197
1198 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1199 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1200 ((uint32_t *) createInfo.pCode)[1] = 0;
1201 ((uint32_t *) createInfo.pCode)[2] = stage;
1202 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1203
1204 err = xglCreateShader(demo->device, &createInfo, &shader);
1205 if (err) {
1206 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001207 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001208 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001209#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210
1211 return shader;
1212}
1213
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001214char *demo_read_bil(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001215{
1216 long int size;
1217 void *shader_code;
1218
1219 FILE *fp = fopen(filename, "rb");
1220 if (!fp) return NULL;
1221
1222 fseek(fp, 0L, SEEK_END);
1223 size = ftell(fp);
1224
1225 fseek(fp, 0L, SEEK_SET);
1226
1227 shader_code = malloc(size);
1228 fread(shader_code, size, 1, fp);
1229
1230 *psize = size;
1231
1232 return shader_code;
1233}
1234
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001235static XGL_SHADER demo_prepare_vs(struct demo *demo)
1236{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001237#ifdef EXTERNAL_BIL
1238 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001239 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001240
Steve Kc8ea79c2015-03-17 10:18:08 -06001241 vertShaderCode = demo_read_bil("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001242
1243 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1244 vertShaderCode, size);
1245#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001246 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001247 "#version 140\n"
1248 "#extension GL_ARB_separate_shader_objects : enable\n"
1249 "#extension GL_ARB_shading_language_420pack : enable\n"
1250 "\n"
1251 "layout(binding = 0) uniform buf {\n"
1252 " mat4 MVP;\n"
1253 " vec4 position[12*3];\n"
1254 " vec4 attr[12*3];\n"
1255 "} ubuf;\n"
1256 "\n"
1257 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001258 "\n"
1259 "void main() \n"
1260 "{\n"
1261 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001262 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1263 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001264
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001265 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1266 (const void *) vertShaderText,
1267 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001268#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001269}
1270
1271static XGL_SHADER demo_prepare_fs(struct demo *demo)
1272{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001273#ifdef EXTERNAL_BIL
1274 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001275 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001276
Steve Kc8ea79c2015-03-17 10:18:08 -06001277 fragShaderCode = demo_read_bil("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001278
1279 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1280 fragShaderCode, size);
1281#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001282 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001283 "#version 140\n"
1284 "#extension GL_ARB_separate_shader_objects : enable\n"
1285 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001286 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001287 "\n"
1288 "layout (location = 0) in vec4 texcoord;\n"
1289 "void main() {\n"
1290 " gl_FragColor = texture(tex, texcoord.xy);\n"
1291 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001292
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001293 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1294 (const void *) fragShaderText,
1295 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001296#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001297}
1298
1299static void demo_prepare_pipeline(struct demo *demo)
1300{
1301 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001302 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1303 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001304 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1305 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1307 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001308 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1309 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001311
1312 memset(&pipeline, 0, sizeof(pipeline));
1313 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu87544e72015-02-23 10:41:08 -07001314 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001316 memset(&ia, 0, sizeof(ia));
1317 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1318 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1319
1320 memset(&rs, 0, sizeof(rs));
1321 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001322 rs.fillMode = XGL_FILL_SOLID;
1323 rs.cullMode = XGL_CULL_NONE;
1324 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001325
1326 memset(&cb, 0, sizeof(cb));
1327 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001328 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1329 memset(att_state, 0, sizeof(att_state));
1330 att_state[0].format = demo->format;
1331 att_state[0].channelWriteMask = 0xf;
1332 att_state[0].blendEnable = XGL_FALSE;
1333 cb.attachmentCount = 1;
1334 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335
Tony Barbour29645d02015-01-16 14:27:35 -07001336 memset(&vp, 0, sizeof(vp));
1337 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001338 vp.numViewports = 1;
1339 vp.clipOrigin = XGL_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001340
1341 memset(&ds, 0, sizeof(ds));
1342 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1343 ds.format = demo->depth.format;
1344 ds.depthTestEnable = XGL_TRUE;
1345 ds.depthWriteEnable = XGL_TRUE;
1346 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1347 ds.depthBoundsEnable = XGL_FALSE;
1348 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1349 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1350 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1351 ds.stencilTestEnable = XGL_FALSE;
1352 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354 memset(&vs, 0, sizeof(vs));
1355 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1356 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001357 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001358 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001359
1360 memset(&fs, 0, sizeof(fs));
1361 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1362 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1363 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001364 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001365
1366 memset(&ms, 0, sizeof(ms));
1367 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1368 ms.sampleMask = 1;
1369 ms.multisampleEnable = XGL_FALSE;
1370 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001372 pipeline.pNext = (const void *) &ia;
1373 ia.pNext = (const void *) &rs;
1374 rs.pNext = (const void *) &cb;
1375 cb.pNext = (const void *) &ms;
1376 ms.pNext = (const void *) &vp;
1377 vp.pNext = (const void *) &ds;
1378 ds.pNext = (const void *) &vs;
1379 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380
1381 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1382 assert(!err);
1383
1384 xglDestroyObject(vs.shader.shader);
1385 xglDestroyObject(fs.shader.shader);
1386}
1387
1388static void demo_prepare_dynamic_states(struct demo *demo)
1389{
Tony Barbour29645d02015-01-16 14:27:35 -07001390 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1391 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1392 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1393 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001394 XGL_RESULT err;
1395
Tony Barbour29645d02015-01-16 14:27:35 -07001396 memset(&viewport_create, 0, sizeof(viewport_create));
1397 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001398 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001399 XGL_VIEWPORT viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001400 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001401 viewport.height = (float) demo->height;
1402 viewport.width = (float) demo->width;
1403 viewport.minDepth = (float) 0.0f;
1404 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001405 viewport_create.pViewports = &viewport;
1406 XGL_RECT scissor;
1407 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001408 scissor.extent.width = demo->width;
1409 scissor.extent.height = demo->height;
1410 scissor.offset.x = 0;
1411 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001412 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413
1414 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001415 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001416 raster.pointSize = 1.0;
1417 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418
1419 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001420 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001421 color_blend.blendConst[0] = 1.0f;
1422 color_blend.blendConst[1] = 1.0f;
1423 color_blend.blendConst[2] = 1.0f;
1424 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425
1426 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001427 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001428 depth_stencil.minDepth = 0.0f;
1429 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001430 depth_stencil.stencilBackRef = 0;
1431 depth_stencil.stencilFrontRef = 0;
1432 depth_stencil.stencilReadMask = 0xff;
1433 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001434
Tony Barbour29645d02015-01-16 14:27:35 -07001435 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001436 assert(!err);
1437
Tony Barbour29645d02015-01-16 14:27:35 -07001438 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001439 assert(!err);
1440
Tony Barbour29645d02015-01-16 14:27:35 -07001441 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001442 &color_blend, &demo->color_blend);
1443 assert(!err);
1444
Tony Barbour29645d02015-01-16 14:27:35 -07001445 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001446 &depth_stencil, &demo->depth_stencil);
1447 assert(!err);
1448}
1449
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001450static void demo_prepare_descriptor_region(struct demo *demo)
1451{
1452 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1453 [0] = {
1454 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1455 .count = 1,
1456 },
1457 [1] = {
1458 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1459 .count = DEMO_TEXTURE_COUNT,
1460 },
1461 };
1462 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1463 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1464 .pNext = NULL,
1465 .count = 2,
1466 .pTypeCount = type_counts,
1467 };
1468 XGL_RESULT err;
1469
1470 err = xglCreateDescriptorRegion(demo->device,
1471 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1472 &descriptor_region, &demo->desc_region);
1473 assert(!err);
1474}
1475
1476static void demo_prepare_descriptor_set(struct demo *demo)
1477{
1478 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1479 &demo->uniform_data.attach;
1480 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1481 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1482 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1483 XGL_UPDATE_BUFFERS update_vs;
1484 XGL_RESULT err;
1485 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001486 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001487
1488 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1489 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1490 view_info[i].pNext = NULL;
1491 view_info[i].view = demo->textures[i].view,
1492 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1493
1494 combined_info[i].pSampler = demo->textures[i].sampler;
1495 combined_info[i].pImageView = &view_info[i];
1496 }
1497
1498 memset(&update_vs, 0, sizeof(update_vs));
1499 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1500 update_vs.pNext = &update_fs;
1501 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1502 update_vs.count = 1;
1503 update_vs.pBufferViews = &view_info_vs;
1504
1505 memset(&update_fs, 0, sizeof(update_fs));
1506 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1507 update_fs.index = 1;
1508 update_fs.count = DEMO_TEXTURE_COUNT;
1509 update_fs.pSamplerImageViews = combined_info;
1510
1511 err = xglAllocDescriptorSets(demo->desc_region,
1512 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001513 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001514 &demo->desc_set, &count);
1515 assert(!err && count == 1);
1516
1517 xglBeginDescriptorRegionUpdate(demo->device,
1518 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1519
1520 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1521 xglUpdateDescriptors(demo->desc_set, &update_vs);
1522
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001523 xglEndDescriptorRegionUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001524}
1525
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001526static void demo_prepare(struct demo *demo)
1527{
1528 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1529 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1530 .pNext = NULL,
1531 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1532 .flags = 0,
1533 };
1534 XGL_RESULT err;
1535
1536 demo_prepare_buffers(demo);
1537 demo_prepare_depth(demo);
1538 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001539 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001540
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001541 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001542 demo_prepare_pipeline(demo);
1543 demo_prepare_dynamic_states(demo);
1544
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001545 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1546 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1547 assert(!err);
1548 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001549
1550 demo_prepare_descriptor_region(demo);
1551 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001552
1553
1554 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1555 demo->current_buffer = i;
1556 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1557 }
1558
1559 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001560}
1561
1562static void demo_handle_event(struct demo *demo,
1563 const xcb_generic_event_t *event)
1564{
Piers Daniell735ee532015-02-23 16:23:13 -07001565 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001566 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001568 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001569 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001570 case XCB_CLIENT_MESSAGE:
1571 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1572 (*demo->atom_wm_delete_window).atom) {
1573 demo->quit = true;
1574 }
1575 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001576 case XCB_KEY_RELEASE:
1577 {
1578 const xcb_key_release_event_t *key =
1579 (const xcb_key_release_event_t *) event;
1580
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001581 switch (key->detail) {
1582 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001583 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001584 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001585 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001586 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001587 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001588 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001589 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001590 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001591 case 0x41:
1592 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001593 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001594 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001595 }
1596 break;
1597 default:
1598 break;
1599 }
1600}
1601
1602static void demo_run(struct demo *demo)
1603{
1604 xcb_flush(demo->connection);
1605
1606 while (!demo->quit) {
1607 xcb_generic_event_t *event;
1608
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001609 if (demo->pause) {
1610 event = xcb_wait_for_event(demo->connection);
1611 } else {
1612 event = xcb_poll_for_event(demo->connection);
1613 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001614 if (event) {
1615 demo_handle_event(demo, event);
1616 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001617 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001618
1619 // Wait for work to finish before updating MVP.
1620 xglDeviceWaitIdle(demo->device);
1621 demo_update_data_buffer(demo);
1622
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001623 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001624
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001625 // Wait for work to finish before updating MVP.
1626 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627 }
1628}
1629
1630static void demo_create_window(struct demo *demo)
1631{
1632 uint32_t value_mask, value_list[32];
1633
1634 demo->window = xcb_generate_id(demo->connection);
1635
1636 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1637 value_list[0] = demo->screen->black_pixel;
1638 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1639 XCB_EVENT_MASK_EXPOSURE;
1640
1641 xcb_create_window(demo->connection,
1642 XCB_COPY_FROM_PARENT,
1643 demo->window, demo->screen->root,
1644 0, 0, demo->width, demo->height, 0,
1645 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1646 demo->screen->root_visual,
1647 value_mask, value_list);
1648
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001649 /* Magic code that will send notification when window is destroyed */
1650 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1651 "WM_PROTOCOLS");
1652 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1653
1654 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1655 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1656
1657 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1658 demo->window, (*reply).atom, 4, 32, 1,
1659 &(*demo->atom_wm_delete_window).atom);
1660 free(reply);
1661
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001662 xcb_map_window(demo->connection, demo->window);
1663}
1664
1665static void demo_init_xgl(struct demo *demo)
1666{
1667 const XGL_APPLICATION_INFO app = {
1668 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1669 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001670 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001671 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001672 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001673 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001674 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001675 };
1676 const XGL_WSI_X11_CONNECTION_INFO connection = {
1677 .pConnection = demo->connection,
1678 .root = demo->screen->root,
1679 .provider = 0,
1680 };
1681 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1682 .queueNodeIndex = 0,
1683 .queueCount = 1,
1684 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001685 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001686 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001687 };
1688 const XGL_DEVICE_CREATE_INFO device = {
1689 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1690 .pNext = NULL,
1691 .queueRecordCount = 1,
1692 .pRequestedQueues = &queue,
1693 .extensionCount = 1,
1694 .ppEnabledExtensionNames = ext_names,
1695 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1696 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1697 };
1698 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001699 uint32_t gpu_count;
1700 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001701
Jon Ashburn93cfc432015-01-29 15:47:01 -07001702 err = xglCreateInstance(&app, NULL, &demo->inst);
1703 assert(!err);
1704 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001705 assert(!err && gpu_count == 1);
1706
1707 for (i = 0; i < device.extensionCount; i++) {
1708 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1709 assert(!err);
1710 }
1711
1712 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1713 assert(!err);
1714
1715 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1716 assert(!err);
1717
1718 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1719 0, &demo->queue);
1720 assert(!err);
1721}
1722
1723static void demo_init_connection(struct demo *demo)
1724{
1725 const xcb_setup_t *setup;
1726 xcb_screen_iterator_t iter;
1727 int scr;
1728
1729 demo->connection = xcb_connect(NULL, &scr);
1730
1731 setup = xcb_get_setup(demo->connection);
1732 iter = xcb_setup_roots_iterator(setup);
1733 while (scr-- > 0)
1734 xcb_screen_next(&iter);
1735
1736 demo->screen = iter.data;
1737}
1738
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001739static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001740{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001741 vec3 eye = {0.0f, 3.0f, 5.0f};
1742 vec3 origin = {0, 0, 0};
1743 vec3 up = {0.0f, -1.0f, 0.0};
1744
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745 memset(demo, 0, sizeof(*demo));
1746
Piers Daniell735ee532015-02-23 16:23:13 -07001747 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001748 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1749 demo->use_staging_buffer = true;
1750 }
1751
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752 demo_init_connection(demo);
1753 demo_init_xgl(demo);
1754
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001755 demo->width = 500;
1756 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001757 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001758
1759 demo->spin_angle = 0.01f;
1760 demo->spin_increment = 0.01f;
1761 demo->pause = false;
1762
Piers Daniell735ee532015-02-23 16:23:13 -07001763 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001764 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1765 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001766}
1767
1768static void demo_cleanup(struct demo *demo)
1769{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001770 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001771
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001772 xglDestroyObject(demo->desc_set);
1773 xglDestroyObject(demo->desc_region);
1774
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001775 xglDestroyObject(demo->viewport);
1776 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001777 xglDestroyObject(demo->color_blend);
1778 xglDestroyObject(demo->depth_stencil);
1779
1780 xglDestroyObject(demo->pipeline);
Chia-I Wu87544e72015-02-23 10:41:08 -07001781 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001782
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001783 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1784 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001785 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001786 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001787 for (j = 0; j < demo->textures[i].num_mem; j++)
1788 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001789 xglDestroyObject(demo->textures[i].sampler);
1790 }
1791
1792 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001793 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001794 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001795 for (j = 0; j < demo->depth.num_mem; j++)
1796 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001797
1798 xglDestroyObject(demo->uniform_data.view);
1799 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001800 xglDestroyObject(demo->uniform_data.buf);
1801 for (j = 0; j < demo->uniform_data.num_mem; j++)
1802 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001803
1804 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001805 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001806 xglDestroyObject(demo->buffers[i].view);
1807 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001808 xglDestroyObject(demo->buffers[i].cmd);
Piers Daniell735ee532015-02-23 16:23:13 -07001809#if defined(XCB_NVIDIA)
1810 xglFreeMemory(demo->buffers[i].mem);
1811#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001812 }
1813
1814 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001815 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001816
1817 xcb_destroy_window(demo->connection, demo->window);
1818 xcb_disconnect(demo->connection);
1819}
1820
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001821int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001822{
1823 struct demo demo;
1824
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001825 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001826
1827 demo_prepare(&demo);
1828 demo_create_window(&demo);
1829 demo_run(&demo);
1830
1831 demo_cleanup(&demo);
1832
1833 return 0;
1834}