blob: ea4b12bf7bc36040ce2e0965e759be87865068d7 [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
Cody Northropfe3d8bc2015-03-17 14:54:35 -060013#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060014
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060016#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060017
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060018#define DEMO_BUFFER_COUNT 2
19#define DEMO_TEXTURE_COUNT 1
20
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060021/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070022 * structure to track all objects related to a texture.
23 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060024struct texture_object {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070025 XGL_SAMPLER sampler;
26
27 XGL_IMAGE image;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060028 XGL_IMAGE_LAYOUT imageLayout;
29
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070030 uint32_t num_mem;
31 XGL_GPU_MEMORY *mem;
32 XGL_IMAGE_VIEW view;
33 int32_t tex_width, tex_height;
34};
35
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060036static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060037 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060038};
39
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060040struct xglcube_vs_uniform {
41 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060042 float mvp[4][4];
43 float position[12*3][4];
44 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060045};
46
47struct xgltexcube_vs_uniform {
48 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060049 float mvp[4][4];
50 float position[12*3][4];
51 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060052};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060053
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060054//--------------------------------------------------------------------------------------
55// Mesh and VertexFormat Data
56//--------------------------------------------------------------------------------------
57struct Vertex
58{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060059 float posX, posY, posZ, posW; // Position data
60 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060061};
62
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060063struct VertexPosTex
64{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060065 float posX, posY, posZ, posW; // Position data
66 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060067};
68
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060069#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060070#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060071
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060072static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060073 -1.0f,-1.0f,-1.0f, // Vertex 0
74 -1.0f,-1.0f, 1.0f,
75 -1.0f, 1.0f, 1.0f,
76
77 -1.0f, 1.0f, 1.0f, // Vertex 1
78 -1.0f, 1.0f,-1.0f,
79 -1.0f,-1.0f,-1.0f,
80
81 -1.0f,-1.0f,-1.0f, // Vertex 2
82 1.0f, 1.0f,-1.0f,
83 1.0f,-1.0f,-1.0f,
84
85 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060086 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060087 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060088
89 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060090 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060091 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060092
93 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060094 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060095 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060096
97 -1.0f, 1.0f,-1.0f, // Vertex 6
98 -1.0f, 1.0f, 1.0f,
99 1.0f, 1.0f, 1.0f,
100
101 -1.0f, 1.0f,-1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600102 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600103 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600104
105 1.0f, 1.0f,-1.0f, // Vertex 8
106 1.0f, 1.0f, 1.0f,
107 1.0f,-1.0f, 1.0f,
108
109 1.0f,-1.0f, 1.0f, // Vertex 9
110 1.0f,-1.0f,-1.0f,
111 1.0f, 1.0f,-1.0f,
112
113 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600114 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600115 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600116
117 -1.0f,-1.0f, 1.0f, // Vertex 11
118 1.0f,-1.0f, 1.0f,
119 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600120};
121
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600122static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600123 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600124 0.0f, 0.0f,
125 0.0f, 1.0f,
126
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600127 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600128 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600129 1.0f, 0.0f,
130
131// 0.0f, 1.0f, // Vertex 2
132// 1.0f, 0.0f,
133// 0.0f, 0.0f,
134
135// 0.0f, 1.0f, // Vertex 3
136// 1.0f, 0.0f,
137// 1.0f, 1.0f,
138
139 0.0f, 0.0f, // Vertex 2
140 1.0f, 1.0f,
141 1.0f, 0.0f,
142
143 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600144 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600145 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600146
147 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600148 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600149 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600150
151 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600153 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154
155 0.0f, 1.0f, // Vertex 6
156 1.0f, 1.0f,
157 1.0f, 0.0f,
158
159 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600161 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600163 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600166
167 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 0.0f, 1.0f,
170
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600171 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600173 0.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600174
175 1.0f, 0.0f, // Vertex 11
176 0.0f, 0.0f,
177 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600178};
179
180void dumpMatrix(const char *note, mat4x4 MVP)
181{
182 int i;
183
184 printf("%s: \n", note);
185 for (i=0; i<4; i++) {
186 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
187 }
188 printf("\n");
189 fflush(stdout);
190}
191
192void dumpVec4(const char *note, vec4 vector)
193{
194 printf("%s: \n", note);
195 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
196 printf("\n");
197 fflush(stdout);
198}
199
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600200struct demo {
201 xcb_connection_t *connection;
202 xcb_screen_t *screen;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700203 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600204
Jon Ashburn93cfc432015-01-29 15:47:01 -0700205 XGL_INSTANCE inst;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600206 XGL_PHYSICAL_GPU gpu;
207 XGL_DEVICE device;
208 XGL_QUEUE queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700209 uint32_t graphics_queue_node_index;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600210 XGL_PHYSICAL_GPU_PROPERTIES *gpu_props;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700211 XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600212
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600213 XGL_FRAMEBUFFER framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600214 int width, height;
215 XGL_FORMAT format;
216
217 struct {
218 XGL_IMAGE image;
219 XGL_GPU_MEMORY mem;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700220 XGL_CMD_BUFFER cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600221
222 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800223 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600224 } buffers[DEMO_BUFFER_COUNT];
225
226 struct {
227 XGL_FORMAT format;
228
229 XGL_IMAGE image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600230 uint32_t num_mem;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700231 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600232 XGL_DEPTH_STENCIL_VIEW view;
233 } depth;
234
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600235 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600236
237 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800238 XGL_BUFFER buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600239 uint32_t num_mem;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700240 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800241 XGL_BUFFER_VIEW view;
242 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600243 } uniform_data;
244
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600245 XGL_CMD_BUFFER cmd; // Buffer for initialization commands
246 XGL_MEMORY_REF mem_refs[16];
247 int num_refs;
Chia-I Wu87544e72015-02-23 10:41:08 -0700248 XGL_DESCRIPTOR_SET_LAYOUT desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600249 XGL_PIPELINE pipeline;
250
Tony Barbour29645d02015-01-16 14:27:35 -0700251 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
252 XGL_DYNAMIC_RS_STATE_OBJECT raster;
253 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
254 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600255
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600256 mat4x4 projection_matrix;
257 mat4x4 view_matrix;
258 mat4x4 model_matrix;
259
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600260 float spin_angle;
261 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600262 bool pause;
263
Chia-I Wu63ea9262015-03-26 13:14:16 +0800264 XGL_DESCRIPTOR_POOL desc_pool;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800265 XGL_DESCRIPTOR_SET desc_set;
266
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600267 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700268 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600269
270 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600271 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600272};
273
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600274static void demo_flush_init_cmd(struct demo *demo)
275{
276 XGL_RESULT err;
277
278 if (demo->cmd == XGL_NULL_HANDLE)
279 return;
280
281 err = xglEndCommandBuffer(demo->cmd);
282 assert(!err);
283
284 const XGL_CMD_BUFFER cmd_bufs[] = { demo->cmd };
285
286 err = xglQueueSubmit(demo->queue, 1, cmd_bufs,
287 demo->num_refs, demo->mem_refs, XGL_NULL_HANDLE);
288 assert(!err);
289
290 err = xglQueueWaitIdle(demo->queue);
291 assert(!err);
292
293 xglDestroyObject(demo->cmd);
294 demo->cmd = XGL_NULL_HANDLE;
295 demo->num_refs = 0;
296}
297
298static void demo_add_mem_refs(
299 struct demo *demo,
300 XGL_MEMORY_REF_FLAGS flags,
301 int num_refs, XGL_GPU_MEMORY *mem)
302{
303 for (int i = 0; i < num_refs; i++) {
304 demo->mem_refs[demo->num_refs].flags = flags;
305 demo->mem_refs[demo->num_refs].mem = mem[i];
306 demo->num_refs++;
307 assert(demo->num_refs < 16);
308 }
309}
310
311static void demo_set_image_layout(
312 struct demo *demo,
313 XGL_IMAGE image,
314 XGL_IMAGE_LAYOUT old_image_layout,
315 XGL_IMAGE_LAYOUT new_image_layout)
316{
317 XGL_RESULT err;
318
319 if (demo->cmd == XGL_NULL_HANDLE) {
320 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
321 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
322 .pNext = NULL,
323 .queueNodeIndex = demo->graphics_queue_node_index,
324 .flags = 0,
325 };
326
327 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
328 assert(!err);
329
330 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
331 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
332 .pNext = NULL,
333 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
334 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
335 };
336 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
337 }
338
339 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
340 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
341 .pNext = NULL,
342 .outputMask = 0,
343 .inputMask = 0,
344 .oldLayout = old_image_layout,
345 .newLayout = new_image_layout,
346 .image = image,
347 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
348 };
349
350 if (new_image_layout == XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
351 /* Make sure anything that was copying from this image has completed */
352 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_COPY_BIT;
353 }
354
355 if (new_image_layout == XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
356 /* Make sure any Copy or CPU writes to image are flushed */
357 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT | XGL_MEMORY_OUTPUT_CPU_WRITE_BIT;
358 }
359
360 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
361
362 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_TOP_OF_PIPE };
363
364 XGL_PIPELINE_BARRIER pipeline_barrier;
365 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
366 pipeline_barrier.pNext = NULL;
367 pipeline_barrier.eventCount = 1;
368 pipeline_barrier.pEvents = set_events;
369 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
370 pipeline_barrier.memBarrierCount = 1;
371 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
372
373 xglCmdPipelineBarrier(demo->cmd, &pipeline_barrier);
374}
375
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700376static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600377{
378 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
379 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000380 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600381 };
382 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
383 .view = demo->depth.view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000384 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600385 };
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600386 const XGL_CLEAR_COLOR clear_color = {
387 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
388 .useRawValue = false,
389 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600390 const float clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600391 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700392 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
393 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600394 .pNext = NULL,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700395 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
396 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
397 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600398 XGL_RESULT err;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700399 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
400 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
401 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
402 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
403 .pNext = NULL,
404 .colorAttachmentCount = 1,
405 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
406 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
407 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600408 .width = demo->width,
409 .height = demo->height,
410 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700411 };
412 XGL_RENDER_PASS_CREATE_INFO rp_info;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600413 XGL_RENDER_PASS_BEGIN rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700414
415 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600416 err = xglCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700417 assert(!err);
418 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
419 rp_info.renderArea.extent.width = demo->width;
420 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600421 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
422 rp_info.pColorFormats = &demo->format;
423 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700424 rp_info.pColorLoadOps = &load_op;
425 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600426 rp_info.pColorLoadClearValues = &clear_color;
427 rp_info.depthStencilFormat = XGL_FMT_D16_UNORM;
428 rp_info.depthStencilLayout = depth_stencil.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700429 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600430 rp_info.depthLoadClearValue = clear_depth;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700431 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
432 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600433 rp_info.stencilLoadClearValue = 0;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700434 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600435 err = xglCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700436 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600437
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700438 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600439 assert(!err);
440
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700441 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600442 demo->pipeline);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700443 xglCmdBindDescriptorSet(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800444 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600445
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700446 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
447 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
448 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600449 demo->color_blend);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700450 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600451 demo->depth_stencil);
452
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600453 xglCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600454 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
455 clear_range.baseMipLevel = 0;
456 clear_range.mipLevels = 1;
457 clear_range.baseArraySlice = 0;
458 clear_range.arraySize = 1;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700459 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600460 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -0600461 XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600462 clear_color, 1, &clear_range);
463
464 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700465 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -0600466 XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600467 clear_depth, 0, 1, &clear_range);
468
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700469 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600470 xglCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600471
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700472 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600473 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700474
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600475 xglDestroyObject(rp_begin.renderPass);
476 xglDestroyObject(rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600477}
478
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600479
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600480void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600481{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600482 mat4x4 MVP, Model, VP;
483 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600484 uint8_t *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600485 XGL_RESULT err;
486
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600487 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600488
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600489 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600490 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700491 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600492 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600493
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700494 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600495 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600496 assert(!err);
497
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600498 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600499
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700500 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600501 assert(!err);
502}
503
504static void demo_draw(struct demo *demo)
505{
506 const XGL_WSI_X11_PRESENT_INFO present = {
507 .destWindow = demo->window,
508 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800509 .async = true,
510 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600511 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800512 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600513 XGL_RESULT err;
514
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600515 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wubb57f642014-11-07 14:30:34 +0800516 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
517
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -0600518 uint32_t i, idx = 0;
519 XGL_MEMORY_REF *memRefs;
520 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
521 demo->depth.num_mem +
522 demo->textures[0].num_mem +
523 demo->uniform_data.num_mem));
524 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
525 memRefs[idx].mem = demo->depth.mem[i];
526 memRefs[idx].flags = 0;
527 }
528 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
529 memRefs[idx].mem = demo->textures[0].mem[i];
530 memRefs[idx].flags = 0;
531 }
532 memRefs[idx].mem = demo->buffers[0].mem;
533 memRefs[idx++].flags = 0;
534 memRefs[idx].mem = demo->buffers[1].mem;
535 memRefs[idx++].flags = 0;
536 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
537 memRefs[idx].mem = demo->uniform_data.mem[i];
538 memRefs[idx].flags = 0;
539 }
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700540 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Piers Daniell735ee532015-02-23 16:23:13 -0700541 idx, memRefs, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542 assert(!err);
543
Chia-I Wubb57f642014-11-07 14:30:34 +0800544 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600545 assert(!err);
546
547 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
548}
549
550static void demo_prepare_buffers(struct demo *demo)
551{
552 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
553 .format = demo->format,
554 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
555 .extent = {
556 .width = demo->width,
557 .height = demo->height,
558 },
559 .flags = 0,
560 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800561 const XGL_FENCE_CREATE_INFO fence = {
562 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
563 .pNext = NULL,
564 .flags = 0,
565 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600567 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568
569 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
570 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
571 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
572 .pNext = NULL,
573 .format = demo->format,
574 .mipLevel = 0,
575 .baseArraySlice = 0,
576 .arraySize = 1,
577 };
578
579 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
580 &demo->buffers[i].image, &demo->buffers[i].mem);
581 assert(!err);
582
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600583 demo_set_image_layout(demo, demo->buffers[i].image,
584 XGL_IMAGE_LAYOUT_UNDEFINED,
585 XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
586
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600587 color_attachment_view.image = demo->buffers[i].image;
588
589 err = xglCreateColorAttachmentView(demo->device,
590 &color_attachment_view, &demo->buffers[i].view);
591 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800592
593 err = xglCreateFence(demo->device,
594 &fence, &demo->buffers[i].fence);
595 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596 }
597}
598
599static void demo_prepare_depth(struct demo *demo)
600{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700601 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600602 const XGL_IMAGE_CREATE_INFO image = {
603 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
604 .pNext = NULL,
605 .imageType = XGL_IMAGE_2D,
606 .format = depth_format,
607 .extent = { demo->width, demo->height, 1 },
608 .mipLevels = 1,
609 .arraySize = 1,
610 .samples = 1,
611 .tiling = XGL_OPTIMAL_TILING,
612 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
613 .flags = 0,
614 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700615 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
616 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
617 .pNext = NULL,
618 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600619 XGL_MEMORY_ALLOC_INFO mem_alloc = {
620 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700621 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700623 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700624 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600625 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
626 };
627 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
628 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
629 .pNext = NULL,
630 .image = XGL_NULL_HANDLE,
631 .mipLevel = 0,
632 .baseArraySlice = 0,
633 .arraySize = 1,
634 .flags = 0,
635 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700636 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600637 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700638 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600639 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600640 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600641 uint32_t num_allocations = 0;
642 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643
644 demo->depth.format = depth_format;
645
646 /* create image */
647 err = xglCreateImage(demo->device, &image,
648 &demo->depth.image);
649 assert(!err);
650
Jon Ashburnb2a66652015-01-16 09:37:43 -0700651 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
652 assert(!err && num_alloc_size == sizeof(num_allocations));
653 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
654 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
655 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600656 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700657 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
658 &mem_reqs_size, mem_reqs);
659 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700660 err = xglGetObjectInfo(demo->depth.image,
661 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
662 &img_reqs_size, &img_reqs);
663 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
664 img_alloc.usage = img_reqs.usage;
665 img_alloc.formatClass = img_reqs.formatClass;
666 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600667 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700668 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600669
Jon Ashburnb2a66652015-01-16 09:37:43 -0700670 /* allocate memory */
671 err = xglAllocMemory(demo->device, &mem_alloc,
672 &(demo->depth.mem[i]));
673 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674
Jon Ashburnb2a66652015-01-16 09:37:43 -0700675 /* bind memory */
676 err = xglBindObjectMemory(demo->depth.image, i,
677 demo->depth.mem[i], 0);
678 assert(!err);
679 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600680
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600681 demo_set_image_layout(demo, demo->depth.image,
682 XGL_IMAGE_LAYOUT_UNDEFINED,
683 XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
684
685 demo_add_mem_refs(demo, XGL_MEMORY_REF_READ_ONLY_BIT, num_allocations, demo->depth.mem);
686
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600687 /* create image view */
688 view.image = demo->depth.image;
689 err = xglCreateDepthStencilView(demo->device, &view,
690 &demo->depth.view);
691 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600692}
693
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600694/** loadTexture
695 * loads a png file into an memory object, using cstdio , libpng.
696 *
697 * \param demo : Needed to access XGL calls
698 * \param filename : the png file to be loaded
699 * \param width : width of png, to be updated as a side effect of this function
700 * \param height : height of png, to be updated as a side effect of this function
701 *
702 * \return bool : an opengl texture id. true if successful?,
703 * should be validated by the client of this function.
704 *
705 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
706 * Modified to copy image to memory
707 *
708 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700709bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600710 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600711 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600712{
713 //header for testing if it is a png
714 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700715 int is_png, bit_depth, color_type,rowbytes;
716 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600717 png_structp png_ptr;
718 png_infop info_ptr, end_info;
719 png_byte *image_data;
720 png_bytep *row_pointers;
721
722 //open file as binary
723 FILE *fp = fopen(filename, "rb");
724 if (!fp) {
725 return false;
726 }
727
728 //read the header
729 fread(header, 1, 8, fp);
730
731 //test if png
732 is_png = !png_sig_cmp(header, 0, 8);
733 if (!is_png) {
734 fclose(fp);
735 return false;
736 }
737
738 //create png struct
739 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
740 NULL, NULL);
741 if (!png_ptr) {
742 fclose(fp);
743 return (false);
744 }
745
746 //create png info struct
747 info_ptr = png_create_info_struct(png_ptr);
748 if (!info_ptr) {
749 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
750 fclose(fp);
751 return (false);
752 }
753
754 //create png info struct
755 end_info = png_create_info_struct(png_ptr);
756 if (!end_info) {
757 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
758 fclose(fp);
759 return (false);
760 }
761
762 //png error stuff, not sure libpng man suggests this.
763 if (setjmp(png_jmpbuf(png_ptr))) {
764 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
765 fclose(fp);
766 return (false);
767 }
768
769 //init png reading
770 png_init_io(png_ptr, fp);
771
772 //let libpng know you already read the first 8 bytes
773 png_set_sig_bytes(png_ptr, 8);
774
775 // read all the info up to the image data
776 png_read_info(png_ptr, info_ptr);
777
778 // get info about png
779 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
780 NULL, NULL, NULL);
781
782 //update width and height based on png info
783 *width = twidth;
784 *height = theight;
785
786 // Require that incoming texture be 8bits per color component
787 // and 4 components (RGBA).
788 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
789 png_get_channels(png_ptr, info_ptr) != 4) {
790 return false;
791 }
792
793 if (rgba_data == NULL) {
794 // If data pointer is null, we just want the width & height
795 // clean up memory and close stuff
796 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
797 fclose(fp);
798
799 return true;
800 }
801
802 // Update the png info struct.
803 png_read_update_info(png_ptr, info_ptr);
804
805 // Row size in bytes.
806 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
807
808 // Allocate the image_data as a big block, to be given to opengl
809 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
810 if (!image_data) {
811 //clean up memory and close stuff
812 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
813 fclose(fp);
814 return false;
815 }
816
817 // row_pointers is for pointing to image_data for reading the png with libpng
818 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
819 if (!row_pointers) {
820 //clean up memory and close stuff
821 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
822 // delete[] image_data;
823 fclose(fp);
824 return false;
825 }
826 // set the individual row_pointers to point at the correct offsets of image_data
827 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700828 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600829
830 // read the png into image_data through row_pointers
831 png_read_image(png_ptr, row_pointers);
832
833 // clean up memory and close stuff
834 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
835 free(row_pointers);
836 free(image_data);
837 fclose(fp);
838
839 return true;
840}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600841
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700842static void demo_prepare_texture_image(struct demo *demo,
843 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600844 struct texture_object *tex_obj,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700845 XGL_IMAGE_TILING tiling,
846 XGL_FLAGS mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847{
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700848 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600849 int32_t tex_width;
850 int32_t tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600851 XGL_RESULT err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700852
853 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
854 assert(err);
855
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600856 tex_obj->tex_width = tex_width;
857 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700858
859 const XGL_IMAGE_CREATE_INFO image_create_info = {
860 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
861 .pNext = NULL,
862 .imageType = XGL_IMAGE_2D,
863 .format = tex_format,
864 .extent = { tex_width, tex_height, 1 },
865 .mipLevels = 1,
866 .arraySize = 1,
867 .samples = 1,
868 .tiling = tiling,
869 .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
870 .flags = 0,
871 };
Piers Daniell735ee532015-02-23 16:23:13 -0700872 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
873 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
874 .pNext = NULL,
875 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700876 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
877 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
Piers Daniell735ee532015-02-23 16:23:13 -0700878 .pNext = &buf_alloc,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700879 };
880 XGL_MEMORY_ALLOC_INFO mem_alloc = {
881 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
882 .pNext = &img_alloc,
883 .allocationSize = 0,
884 .memProps = mem_props,
885 .memType = XGL_MEMORY_TYPE_IMAGE,
886 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
887 };
888
889 XGL_MEMORY_REQUIREMENTS *mem_reqs;
890 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Piers Daniell735ee532015-02-23 16:23:13 -0700891 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
892 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700893 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
894 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
895 uint32_t num_allocations = 0;
896 size_t num_alloc_size = sizeof(num_allocations);
897
898 err = xglCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600899 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700900 assert(!err);
901
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600902 err = xglGetObjectInfo(tex_obj->image,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700903 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
904 &num_alloc_size, &num_allocations);
905 assert(!err && num_alloc_size == sizeof(num_allocations));
906 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600907 tex_obj->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
908 err = xglGetObjectInfo(tex_obj->image,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700909 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
910 &mem_reqs_size, mem_reqs);
911 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600912 err = xglGetObjectInfo(tex_obj->image,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700913 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
914 &img_reqs_size, &img_reqs);
915 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
916 img_alloc.usage = img_reqs.usage;
917 img_alloc.formatClass = img_reqs.formatClass;
918 img_alloc.samples = img_reqs.samples;
919 mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
920 for (uint32_t j = 0; j < num_allocations; j ++) {
Courtney Goeltzenleuchter02ba99a2015-02-25 11:48:28 -0700921 mem_alloc.memType = mem_reqs[j].memType;
Piers Daniell735ee532015-02-23 16:23:13 -0700922 mem_alloc.allocationSize = mem_reqs[j].size;
923
924 if (mem_alloc.memType == XGL_MEMORY_TYPE_BUFFER) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600925 err = xglGetObjectInfo(tex_obj->image,
Piers Daniell735ee532015-02-23 16:23:13 -0700926 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
927 &buf_reqs_size, &buf_reqs);
928 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
929 buf_alloc.usage = buf_reqs.usage;
930 img_alloc.pNext = &buf_alloc;
931 } else {
932 img_alloc.pNext = 0;
933 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934
935 /* allocate memory */
936 err = xglAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600937 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700938 assert(!err);
939
940 /* bind memory */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600941 err = xglBindObjectMemory(tex_obj->image, j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700942 assert(!err);
943 }
944 free(mem_reqs);
945 mem_reqs = NULL;
946
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600947 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700948
949 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
950 const XGL_IMAGE_SUBRESOURCE subres = {
951 .aspect = XGL_IMAGE_ASPECT_COLOR,
952 .mipLevel = 0,
953 .arraySlice = 0,
954 };
955 XGL_SUBRESOURCE_LAYOUT layout;
956 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
957 void *data;
958
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600959 err = xglGetImageSubresourceInfo(tex_obj->image, &subres,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700960 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
961 &layout_size, &layout);
962 assert(!err && layout_size == sizeof(layout));
963 /* Linear texture must be within a single memory object */
964 assert(num_allocations == 1);
965
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600966 err = xglMapMemory(tex_obj->mem[0], 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700967 assert(!err);
968
969 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
970 fprintf(stderr, "Error loading texture: %s\n", filename);
971 }
972
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600973 err = xglUnmapMemory(tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700974 assert(!err);
975 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600976
977 tex_obj->imageLayout = XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
978 demo_set_image_layout(demo, tex_obj->image,
979 XGL_IMAGE_LAYOUT_UNDEFINED,
980 tex_obj->imageLayout);
981 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700982}
983
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600984static void demo_destroy_texture_image(struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985{
986 /* clean up staging resources */
987 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
988 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
989 xglFreeMemory(tex_objs->mem[j]);
990 }
991
992 free(tex_objs->mem);
993 xglDestroyObject(tex_objs->image);
994}
995
996static void demo_prepare_textures(struct demo *demo)
997{
998 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
999 XGL_FORMAT_PROPERTIES props;
1000 size_t size = sizeof(props);
1001 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001002 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001003
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001004 err = xglGetFormatInfo(demo->device, tex_format,
1005 XGL_INFO_TYPE_FORMAT_PROPERTIES,
1006 &size, &props);
1007 assert(!err);
1008
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001009 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001010
1011 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
1012 /* Device can texture using linear textures */
1013 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
1014 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001015 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001016 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001017 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018
1019 memset(&staging_texture, 0, sizeof(staging_texture));
1020 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
1021 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
1022
1023 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
1024 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
1025
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001026 demo_set_image_layout(demo, staging_texture.image,
1027 staging_texture.imageLayout,
1028 XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001029
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001030 demo_set_image_layout(demo, demo->textures[i].image,
1031 demo->textures[i].imageLayout,
1032 XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001033
1034 XGL_IMAGE_COPY copy_region = {
1035 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
1036 .srcOffset = { 0, 0, 0 },
1037 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
1038 .destOffset = { 0, 0, 0 },
1039 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1040 };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001041 xglCmdCopyImage(demo->cmd,
1042 staging_texture.image, XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1043 demo->textures[i].image, XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001044 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001045
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001046 demo_add_mem_refs(demo, XGL_MEMORY_REF_READ_ONLY_BIT, staging_texture.num_mem, staging_texture.mem);
1047 demo_add_mem_refs(demo, 0, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001048
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001049 demo_set_image_layout(demo, demo->textures[i].image,
1050 XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
1051 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001053 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001054
1055 demo_destroy_texture_image(&staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001056 } else {
1057 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
1058 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1059 }
1060
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001061 const XGL_SAMPLER_CREATE_INFO sampler = {
1062 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
1063 .pNext = NULL,
1064 .magFilter = XGL_TEX_FILTER_NEAREST,
1065 .minFilter = XGL_TEX_FILTER_NEAREST,
1066 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001067 .addressU = XGL_TEX_ADDRESS_CLAMP,
1068 .addressV = XGL_TEX_ADDRESS_CLAMP,
1069 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001070 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071 .maxAnisotropy = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001072 .compareFunc = XGL_COMPARE_NEVER,
1073 .minLod = 0.0f,
1074 .maxLod = 0.0f,
1075 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
1076 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001077
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001078 XGL_IMAGE_VIEW_CREATE_INFO view = {
1079 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1080 .pNext = NULL,
1081 .image = XGL_NULL_HANDLE,
1082 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001083 .format = tex_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001084 .channels = { XGL_CHANNEL_SWIZZLE_R,
1085 XGL_CHANNEL_SWIZZLE_G,
1086 XGL_CHANNEL_SWIZZLE_B,
1087 XGL_CHANNEL_SWIZZLE_A, },
1088 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
1089 .minLod = 0.0f,
1090 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001091
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001092 /* create sampler */
1093 err = xglCreateSampler(demo->device, &sampler,
1094 &demo->textures[i].sampler);
1095 assert(!err);
1096
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001097 /* create image view */
1098 view.image = demo->textures[i].image;
1099 err = xglCreateImageView(demo->device, &view,
1100 &demo->textures[i].view);
1101 assert(!err);
1102 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001103}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001104
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001105void demo_prepare_cube_data_buffer(struct demo *demo)
1106{
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001107 XGL_BUFFER_CREATE_INFO buf_info;
1108 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001109 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1110 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1111 .pNext = NULL,
1112 };
1113 XGL_MEMORY_ALLOC_INFO alloc_info = {
1114 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1115 .pNext = &buf_alloc,
1116 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -07001117 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -07001118 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001119 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1120 };
1121 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001122 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001123 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001124 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1125 uint32_t num_allocations = 0;
1126 size_t num_alloc_size = sizeof(num_allocations);
1127 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001128 int i;
1129 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001130 XGL_RESULT err;
1131 struct xgltexcube_vs_uniform data;
1132
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001133 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001134 mat4x4_mul(MVP, VP, demo->model_matrix);
1135 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001136// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001137
1138 for (i=0; i<12*3; i++) {
1139 data.position[i][0] = g_vertex_buffer_data[i*3];
1140 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1141 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1142 data.position[i][3] = 1.0f;
1143 data.attr[i][0] = g_uv_buffer_data[2*i];
1144 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1145 data.attr[i][2] = 0;
1146 data.attr[i][3] = 0;
1147 }
1148
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001149 memset(&buf_info, 0, sizeof(buf_info));
1150 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1151 buf_info.size = sizeof(data);
1152 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1153 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1154 assert(!err);
1155
1156 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001157 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1158 &num_alloc_size, &num_allocations);
1159 assert(!err && num_alloc_size == sizeof(num_allocations));
1160 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1161 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1162 demo->uniform_data.num_mem = num_allocations;
1163 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001164 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001165 &mem_reqs_size, mem_reqs);
1166 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1167 err = xglGetObjectInfo(demo->uniform_data.buf,
1168 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1169 &buf_reqs_size, &buf_reqs);
1170 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1171 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001172 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001173 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001174
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001175 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1176 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001177
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001178 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001179 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001180
Piers Daniell735ee532015-02-23 16:23:13 -07001181 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001182
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001183 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1184 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001185
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001186 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1187 demo->uniform_data.mem[i], 0);
1188 assert(!err);
1189 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001190
1191 memset(&view_info, 0, sizeof(view_info));
1192 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1193 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +08001194 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001195 view_info.offset = 0;
1196 view_info.range = sizeof(data);
1197
1198 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1199 assert(!err);
1200
1201 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1202 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001203}
1204
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001205static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206{
Chia-I Wua2aa8632015-03-26 15:04:41 +08001207 const XGL_DESCRIPTOR_SET_LAYOUT_BINDING layout_bindings[2] = {
1208 [0] = {
1209 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1210 .count = 1,
1211 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
1212 .immutableSampler = XGL_NULL_HANDLE,
1213 },
1214 [1] = {
1215 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1216 .count = DEMO_TEXTURE_COUNT,
1217 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
1218 .immutableSampler = XGL_NULL_HANDLE,
1219 },
1220 };
1221 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001222 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1223 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001224 .count = 2,
1225 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001226 };
Chia-I Wu06f82812015-02-23 10:41:08 -07001227 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001228 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001229
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001230 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu06f82812015-02-23 10:41:08 -07001231 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001232 XGL_NULL_HANDLE, &descriptor_layout,
Chia-I Wu87544e72015-02-23 10:41:08 -07001233 &demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001234 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001235}
1236
1237static XGL_SHADER demo_prepare_shader(struct demo *demo,
1238 XGL_PIPELINE_SHADER_STAGE stage,
1239 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001240 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241{
1242 XGL_SHADER_CREATE_INFO createInfo;
1243 XGL_SHADER shader;
1244 XGL_RESULT err;
1245
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001246
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1248 createInfo.pNext = NULL;
1249
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001250#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001251 createInfo.codeSize = size;
1252 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001253 createInfo.flags = 0;
1254
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001255 err = xglCreateShader(demo->device, &createInfo, &shader);
1256 if (err) {
1257 free((void *) createInfo.pCode);
1258 }
1259#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001260 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001261 // to the driver "under the covers"
1262 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1263 createInfo.pCode = malloc(createInfo.codeSize);
1264 createInfo.flags = 0;
1265
1266 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001267 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001268 ((uint32_t *) createInfo.pCode)[1] = 0;
1269 ((uint32_t *) createInfo.pCode)[2] = stage;
1270 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1271
1272 err = xglCreateShader(demo->device, &createInfo, &shader);
1273 if (err) {
1274 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001275 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001276 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001277#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001278
1279 return shader;
1280}
1281
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001282char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001283{
1284 long int size;
1285 void *shader_code;
1286
1287 FILE *fp = fopen(filename, "rb");
1288 if (!fp) return NULL;
1289
1290 fseek(fp, 0L, SEEK_END);
1291 size = ftell(fp);
1292
1293 fseek(fp, 0L, SEEK_SET);
1294
1295 shader_code = malloc(size);
1296 fread(shader_code, size, 1, fp);
1297
1298 *psize = size;
1299
1300 return shader_code;
1301}
1302
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001303static XGL_SHADER demo_prepare_vs(struct demo *demo)
1304{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001305#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001306 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001307 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001308
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001309 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001310
1311 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1312 vertShaderCode, size);
1313#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001314 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001315 "#version 140\n"
1316 "#extension GL_ARB_separate_shader_objects : enable\n"
1317 "#extension GL_ARB_shading_language_420pack : enable\n"
1318 "\n"
1319 "layout(binding = 0) uniform buf {\n"
1320 " mat4 MVP;\n"
1321 " vec4 position[12*3];\n"
1322 " vec4 attr[12*3];\n"
1323 "} ubuf;\n"
1324 "\n"
1325 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001326 "\n"
1327 "void main() \n"
1328 "{\n"
1329 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001330 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1331 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001332
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001333 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1334 (const void *) vertShaderText,
1335 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001336#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001337}
1338
1339static XGL_SHADER demo_prepare_fs(struct demo *demo)
1340{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001341#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001342 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001343 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001344
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001345 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001346
1347 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1348 fragShaderCode, size);
1349#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001350 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001351 "#version 140\n"
1352 "#extension GL_ARB_separate_shader_objects : enable\n"
1353 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001354 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001355 "\n"
1356 "layout (location = 0) in vec4 texcoord;\n"
1357 "void main() {\n"
1358 " gl_FragColor = texture(tex, texcoord.xy);\n"
1359 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001360
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001361 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1362 (const void *) fragShaderText,
1363 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001364#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365}
1366
1367static void demo_prepare_pipeline(struct demo *demo)
1368{
1369 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001370 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1371 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001372 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1373 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001374 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1375 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001376 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1377 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001378 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001379
1380 memset(&pipeline, 0, sizeof(pipeline));
1381 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu87544e72015-02-23 10:41:08 -07001382 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001383
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001384 memset(&ia, 0, sizeof(ia));
1385 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1386 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1387
1388 memset(&rs, 0, sizeof(rs));
1389 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001390 rs.fillMode = XGL_FILL_SOLID;
Mike Stroyan16b3d982015-03-19 14:29:04 -06001391 rs.cullMode = XGL_CULL_BACK;
Tony Barbour29645d02015-01-16 14:27:35 -07001392 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001393
1394 memset(&cb, 0, sizeof(cb));
1395 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001396 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1397 memset(att_state, 0, sizeof(att_state));
1398 att_state[0].format = demo->format;
1399 att_state[0].channelWriteMask = 0xf;
1400 att_state[0].blendEnable = XGL_FALSE;
1401 cb.attachmentCount = 1;
1402 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001403
Tony Barbour29645d02015-01-16 14:27:35 -07001404 memset(&vp, 0, sizeof(vp));
1405 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001406 vp.numViewports = 1;
Mike Stroyan259bc542015-03-24 15:13:34 -06001407 vp.clipOrigin = XGL_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001408
1409 memset(&ds, 0, sizeof(ds));
1410 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1411 ds.format = demo->depth.format;
1412 ds.depthTestEnable = XGL_TRUE;
1413 ds.depthWriteEnable = XGL_TRUE;
1414 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1415 ds.depthBoundsEnable = XGL_FALSE;
1416 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1417 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1418 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1419 ds.stencilTestEnable = XGL_FALSE;
1420 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001422 memset(&vs, 0, sizeof(vs));
1423 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1424 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001425 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001426 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427
1428 memset(&fs, 0, sizeof(fs));
1429 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1430 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1431 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001432 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001433
1434 memset(&ms, 0, sizeof(ms));
1435 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1436 ms.sampleMask = 1;
1437 ms.multisampleEnable = XGL_FALSE;
1438 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001439
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001440 pipeline.pNext = (const void *) &ia;
1441 ia.pNext = (const void *) &rs;
1442 rs.pNext = (const void *) &cb;
1443 cb.pNext = (const void *) &ms;
1444 ms.pNext = (const void *) &vp;
1445 vp.pNext = (const void *) &ds;
1446 ds.pNext = (const void *) &vs;
1447 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001448
1449 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1450 assert(!err);
1451
1452 xglDestroyObject(vs.shader.shader);
1453 xglDestroyObject(fs.shader.shader);
1454}
1455
1456static void demo_prepare_dynamic_states(struct demo *demo)
1457{
Tony Barbour29645d02015-01-16 14:27:35 -07001458 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1459 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1460 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1461 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001462 XGL_RESULT err;
1463
Tony Barbour29645d02015-01-16 14:27:35 -07001464 memset(&viewport_create, 0, sizeof(viewport_create));
1465 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001466 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001467 XGL_VIEWPORT viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001468 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001469 viewport.height = (float) demo->height;
1470 viewport.width = (float) demo->width;
1471 viewport.minDepth = (float) 0.0f;
1472 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001473 viewport_create.pViewports = &viewport;
1474 XGL_RECT scissor;
1475 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001476 scissor.extent.width = demo->width;
1477 scissor.extent.height = demo->height;
1478 scissor.offset.x = 0;
1479 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001480 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001481
1482 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001483 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001484 raster.pointSize = 1.0;
1485 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001486
1487 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001488 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001489 color_blend.blendConst[0] = 1.0f;
1490 color_blend.blendConst[1] = 1.0f;
1491 color_blend.blendConst[2] = 1.0f;
1492 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001493
1494 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001495 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001496 depth_stencil.minDepth = 0.0f;
1497 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001498 depth_stencil.stencilBackRef = 0;
1499 depth_stencil.stencilFrontRef = 0;
1500 depth_stencil.stencilReadMask = 0xff;
1501 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001502
Tony Barbour29645d02015-01-16 14:27:35 -07001503 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001504 assert(!err);
1505
Tony Barbour29645d02015-01-16 14:27:35 -07001506 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001507 assert(!err);
1508
Tony Barbour29645d02015-01-16 14:27:35 -07001509 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001510 &color_blend, &demo->color_blend);
1511 assert(!err);
1512
Tony Barbour29645d02015-01-16 14:27:35 -07001513 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001514 &depth_stencil, &demo->depth_stencil);
1515 assert(!err);
1516}
1517
Chia-I Wu63ea9262015-03-26 13:14:16 +08001518static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001519{
1520 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1521 [0] = {
1522 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1523 .count = 1,
1524 },
1525 [1] = {
1526 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1527 .count = DEMO_TEXTURE_COUNT,
1528 },
1529 };
Chia-I Wu63ea9262015-03-26 13:14:16 +08001530 const XGL_DESCRIPTOR_POOL_CREATE_INFO descriptor_pool = {
1531 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001532 .pNext = NULL,
1533 .count = 2,
1534 .pTypeCount = type_counts,
1535 };
1536 XGL_RESULT err;
1537
Chia-I Wu63ea9262015-03-26 13:14:16 +08001538 err = xglCreateDescriptorPool(demo->device,
1539 XGL_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
1540 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001541 assert(!err);
1542}
1543
1544static void demo_prepare_descriptor_set(struct demo *demo)
1545{
1546 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1547 &demo->uniform_data.attach;
1548 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1549 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1550 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1551 XGL_UPDATE_BUFFERS update_vs;
1552 XGL_RESULT err;
1553 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001554 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001555
1556 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1557 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1558 view_info[i].pNext = NULL;
1559 view_info[i].view = demo->textures[i].view,
1560 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1561
1562 combined_info[i].pSampler = demo->textures[i].sampler;
1563 combined_info[i].pImageView = &view_info[i];
1564 }
1565
1566 memset(&update_vs, 0, sizeof(update_vs));
1567 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1568 update_vs.pNext = &update_fs;
1569 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1570 update_vs.count = 1;
1571 update_vs.pBufferViews = &view_info_vs;
1572
1573 memset(&update_fs, 0, sizeof(update_fs));
1574 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1575 update_fs.index = 1;
1576 update_fs.count = DEMO_TEXTURE_COUNT;
1577 update_fs.pSamplerImageViews = combined_info;
1578
Chia-I Wu63ea9262015-03-26 13:14:16 +08001579 err = xglAllocDescriptorSets(demo->desc_pool,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001580 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001581 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001582 &demo->desc_set, &count);
1583 assert(!err && count == 1);
1584
Chia-I Wu63ea9262015-03-26 13:14:16 +08001585 xglBeginDescriptorPoolUpdate(demo->device,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001586 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1587
Chia-I Wu63ea9262015-03-26 13:14:16 +08001588 xglClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001589 xglUpdateDescriptors(demo->desc_set, &update_vs);
1590
Chia-I Wu63ea9262015-03-26 13:14:16 +08001591 xglEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001592}
1593
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001594static void demo_prepare(struct demo *demo)
1595{
1596 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1597 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1598 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001599 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001600 .flags = 0,
1601 };
1602 XGL_RESULT err;
1603
1604 demo_prepare_buffers(demo);
1605 demo_prepare_depth(demo);
1606 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001607 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001608
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001609 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001610 demo_prepare_pipeline(demo);
1611 demo_prepare_dynamic_states(demo);
1612
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001613 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1614 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1615 assert(!err);
1616 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001617
Chia-I Wu63ea9262015-03-26 13:14:16 +08001618 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001619 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001620
1621
1622 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1623 demo->current_buffer = i;
1624 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1625 }
1626
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001627 /*
1628 * Prepare functions above may generate pipeline commands
1629 * that need to be flushed before beginning the render loop.
1630 */
1631 demo_flush_init_cmd(demo);
1632
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001633 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001634}
1635
1636static void demo_handle_event(struct demo *demo,
1637 const xcb_generic_event_t *event)
1638{
Piers Daniell735ee532015-02-23 16:23:13 -07001639 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001640 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001641 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001642 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001643 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001644 case XCB_CLIENT_MESSAGE:
1645 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1646 (*demo->atom_wm_delete_window).atom) {
1647 demo->quit = true;
1648 }
1649 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001650 case XCB_KEY_RELEASE:
1651 {
1652 const xcb_key_release_event_t *key =
1653 (const xcb_key_release_event_t *) event;
1654
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001655 switch (key->detail) {
1656 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001657 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001658 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001659 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001660 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001661 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001662 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001663 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001664 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001665 case 0x41:
1666 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001667 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001668 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001669 }
1670 break;
1671 default:
1672 break;
1673 }
1674}
1675
1676static void demo_run(struct demo *demo)
1677{
1678 xcb_flush(demo->connection);
1679
1680 while (!demo->quit) {
1681 xcb_generic_event_t *event;
1682
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001683 if (demo->pause) {
1684 event = xcb_wait_for_event(demo->connection);
1685 } else {
1686 event = xcb_poll_for_event(demo->connection);
1687 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001688 if (event) {
1689 demo_handle_event(demo, event);
1690 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001691 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001692
1693 // Wait for work to finish before updating MVP.
1694 xglDeviceWaitIdle(demo->device);
1695 demo_update_data_buffer(demo);
1696
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001697 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001698
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001699 // Wait for work to finish before updating MVP.
1700 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001701 }
1702}
1703
1704static void demo_create_window(struct demo *demo)
1705{
1706 uint32_t value_mask, value_list[32];
1707
1708 demo->window = xcb_generate_id(demo->connection);
1709
1710 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1711 value_list[0] = demo->screen->black_pixel;
1712 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1713 XCB_EVENT_MASK_EXPOSURE;
1714
1715 xcb_create_window(demo->connection,
1716 XCB_COPY_FROM_PARENT,
1717 demo->window, demo->screen->root,
1718 0, 0, demo->width, demo->height, 0,
1719 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1720 demo->screen->root_visual,
1721 value_mask, value_list);
1722
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001723 /* Magic code that will send notification when window is destroyed */
1724 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1725 "WM_PROTOCOLS");
1726 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1727
1728 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1729 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1730
1731 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1732 demo->window, (*reply).atom, 4, 32, 1,
1733 &(*demo->atom_wm_delete_window).atom);
1734 free(reply);
1735
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001736 xcb_map_window(demo->connection, demo->window);
1737}
1738
1739static void demo_init_xgl(struct demo *demo)
1740{
1741 const XGL_APPLICATION_INFO app = {
1742 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1743 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001744 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001746 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001748 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001749 };
1750 const XGL_WSI_X11_CONNECTION_INFO connection = {
1751 .pConnection = demo->connection,
1752 .root = demo->screen->root,
1753 .provider = 0,
1754 };
1755 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1756 .queueNodeIndex = 0,
1757 .queueCount = 1,
1758 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001759 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001760 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001761 };
1762 const XGL_DEVICE_CREATE_INFO device = {
1763 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1764 .pNext = NULL,
1765 .queueRecordCount = 1,
1766 .pRequestedQueues = &queue,
1767 .extensionCount = 1,
1768 .ppEnabledExtensionNames = ext_names,
1769 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1770 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1771 };
1772 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001773 uint32_t gpu_count;
1774 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001775 size_t data_size;
1776 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001777
Jon Ashburn93cfc432015-01-29 15:47:01 -07001778 err = xglCreateInstance(&app, NULL, &demo->inst);
Ian Elliott3979e282015-04-03 15:24:55 -06001779 if (err == XGL_ERROR_INCOMPATIBLE_DRIVER) {
1780 printf("Cannot find a compatible Vulkan installable client driver "
1781 "(ICD).\nExiting ...\n");
1782 fflush(stdout);
1783 exit(1);
1784 } else {
1785 assert(!err);
1786 }
Jon Ashburn93cfc432015-01-29 15:47:01 -07001787 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001788 assert(!err && gpu_count == 1);
1789
1790 for (i = 0; i < device.extensionCount; i++) {
1791 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1792 assert(!err);
1793 }
1794
1795 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1796 assert(!err);
1797
1798 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1799 assert(!err);
1800
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001801 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
1802 &data_size, NULL);
1803 assert(!err);
1804
1805 demo->gpu_props = (XGL_PHYSICAL_GPU_PROPERTIES *) malloc(data_size);
1806 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
1807 &data_size, demo->gpu_props);
1808 assert(!err);
1809
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001810 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1811 &data_size, NULL);
1812 assert(!err);
1813
1814 demo->queue_props = (XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *) malloc(data_size);
1815 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1816 &data_size, demo->queue_props);
1817 assert(!err);
Ian Elliott8e777be2015-04-03 09:54:13 -06001818 queue_count = (uint32_t)(data_size / sizeof(XGL_PHYSICAL_GPU_QUEUE_PROPERTIES));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001819 assert(queue_count >= 1);
1820
1821 for (i = 0; i < queue_count; i++) {
1822 if (demo->queue_props[i].queueFlags & XGL_QUEUE_GRAPHICS_BIT)
1823 break;
1824 }
1825 assert(i < queue_count);
1826 demo->graphics_queue_node_index = i;
1827
1828 err = xglGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001829 0, &demo->queue);
1830 assert(!err);
1831}
1832
1833static void demo_init_connection(struct demo *demo)
1834{
1835 const xcb_setup_t *setup;
1836 xcb_screen_iterator_t iter;
1837 int scr;
1838
1839 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001840 if (demo->connection == NULL) {
1841 printf("Cannot find a compatible Vulkan installable client driver "
1842 "(ICD).\nExiting ...\n");
1843 fflush(stdout);
1844 exit(1);
1845 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001846
1847 setup = xcb_get_setup(demo->connection);
1848 iter = xcb_setup_roots_iterator(setup);
1849 while (scr-- > 0)
1850 xcb_screen_next(&iter);
1851
1852 demo->screen = iter.data;
1853}
1854
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001855static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001856{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001857 vec3 eye = {0.0f, 3.0f, 5.0f};
1858 vec3 origin = {0, 0, 0};
1859 vec3 up = {0.0f, -1.0f, 0.0};
1860
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001861 memset(demo, 0, sizeof(*demo));
1862
Piers Daniell735ee532015-02-23 16:23:13 -07001863 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001864 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1865 demo->use_staging_buffer = true;
1866 }
1867
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001868 demo_init_connection(demo);
1869 demo_init_xgl(demo);
1870
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001871 demo->width = 500;
1872 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001873 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001874
1875 demo->spin_angle = 0.01f;
1876 demo->spin_increment = 0.01f;
1877 demo->pause = false;
1878
Piers Daniell735ee532015-02-23 16:23:13 -07001879 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001880 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1881 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001882}
1883
1884static void demo_cleanup(struct demo *demo)
1885{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001886 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001887
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001888 xglDestroyObject(demo->desc_set);
Chia-I Wu63ea9262015-03-26 13:14:16 +08001889 xglDestroyObject(demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001890
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001891 xglDestroyObject(demo->viewport);
1892 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001893 xglDestroyObject(demo->color_blend);
1894 xglDestroyObject(demo->depth_stencil);
1895
1896 xglDestroyObject(demo->pipeline);
Chia-I Wu87544e72015-02-23 10:41:08 -07001897 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001898
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001899 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1900 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001901 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001902 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001903 for (j = 0; j < demo->textures[i].num_mem; j++)
1904 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001905 xglDestroyObject(demo->textures[i].sampler);
1906 }
1907
1908 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001909 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001910 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001911 for (j = 0; j < demo->depth.num_mem; j++)
1912 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001913
1914 xglDestroyObject(demo->uniform_data.view);
1915 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001916 xglDestroyObject(demo->uniform_data.buf);
1917 for (j = 0; j < demo->uniform_data.num_mem; j++)
1918 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001919
1920 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001921 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001922 xglDestroyObject(demo->buffers[i].view);
1923 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001924 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001925 }
1926
1927 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001928 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001929
1930 xcb_destroy_window(demo->connection, demo->window);
1931 xcb_disconnect(demo->connection);
1932}
1933
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001934int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001935{
1936 struct demo demo;
1937
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001938 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001939
1940 demo_prepare(&demo);
1941 demo_create_window(&demo);
1942 demo_run(&demo);
1943
1944 demo_cleanup(&demo);
1945
1946 return 0;
1947}