blob: 9bf56af58064c46133811230d4ce930a1a59454e [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 Wu6a3c8972015-01-04 16:27:24 +08001207 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
1208 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1209 .pNext = NULL,
1210 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1211 .count = DEMO_TEXTURE_COUNT,
1212 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
1213 .immutableSampler = XGL_NULL_HANDLE,
1214 };
Chia-I Wu87544e72015-02-23 10:41:08 -07001215 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
1216 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1217 .pNext = &descriptor_layout_fs,
1218 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1219 .count = 1,
1220 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
1221 .immutableSampler = XGL_NULL_HANDLE,
1222 };
Chia-I Wu06f82812015-02-23 10:41:08 -07001223
1224 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001225 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001226
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001227 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu06f82812015-02-23 10:41:08 -07001228 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001229 XGL_NULL_HANDLE, &descriptor_layout_vs,
Chia-I Wu87544e72015-02-23 10:41:08 -07001230 &demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001231 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232}
1233
1234static XGL_SHADER demo_prepare_shader(struct demo *demo,
1235 XGL_PIPELINE_SHADER_STAGE stage,
1236 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001237 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238{
1239 XGL_SHADER_CREATE_INFO createInfo;
1240 XGL_SHADER shader;
1241 XGL_RESULT err;
1242
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001243
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001244 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1245 createInfo.pNext = NULL;
1246
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001247#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001248 createInfo.codeSize = size;
1249 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001250 createInfo.flags = 0;
1251
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001252 err = xglCreateShader(demo->device, &createInfo, &shader);
1253 if (err) {
1254 free((void *) createInfo.pCode);
1255 }
1256#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001257 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001258 // to the driver "under the covers"
1259 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1260 createInfo.pCode = malloc(createInfo.codeSize);
1261 createInfo.flags = 0;
1262
1263 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001264 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001265 ((uint32_t *) createInfo.pCode)[1] = 0;
1266 ((uint32_t *) createInfo.pCode)[2] = stage;
1267 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1268
1269 err = xglCreateShader(demo->device, &createInfo, &shader);
1270 if (err) {
1271 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001272 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001273 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001274#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001275
1276 return shader;
1277}
1278
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001279char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001280{
1281 long int size;
1282 void *shader_code;
1283
1284 FILE *fp = fopen(filename, "rb");
1285 if (!fp) return NULL;
1286
1287 fseek(fp, 0L, SEEK_END);
1288 size = ftell(fp);
1289
1290 fseek(fp, 0L, SEEK_SET);
1291
1292 shader_code = malloc(size);
1293 fread(shader_code, size, 1, fp);
1294
1295 *psize = size;
1296
1297 return shader_code;
1298}
1299
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001300static XGL_SHADER demo_prepare_vs(struct demo *demo)
1301{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001302#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001303 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001304 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001305
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001306 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001307
1308 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1309 vertShaderCode, size);
1310#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001311 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001312 "#version 140\n"
1313 "#extension GL_ARB_separate_shader_objects : enable\n"
1314 "#extension GL_ARB_shading_language_420pack : enable\n"
1315 "\n"
1316 "layout(binding = 0) uniform buf {\n"
1317 " mat4 MVP;\n"
1318 " vec4 position[12*3];\n"
1319 " vec4 attr[12*3];\n"
1320 "} ubuf;\n"
1321 "\n"
1322 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001323 "\n"
1324 "void main() \n"
1325 "{\n"
1326 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001327 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1328 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001329
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001330 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1331 (const void *) vertShaderText,
1332 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001333#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001334}
1335
1336static XGL_SHADER demo_prepare_fs(struct demo *demo)
1337{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001338#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001339 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001340 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001341
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001342 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001343
1344 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1345 fragShaderCode, size);
1346#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001347 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001348 "#version 140\n"
1349 "#extension GL_ARB_separate_shader_objects : enable\n"
1350 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001351 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001352 "\n"
1353 "layout (location = 0) in vec4 texcoord;\n"
1354 "void main() {\n"
1355 " gl_FragColor = texture(tex, texcoord.xy);\n"
1356 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001357
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001358 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1359 (const void *) fragShaderText,
1360 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001361#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362}
1363
1364static void demo_prepare_pipeline(struct demo *demo)
1365{
1366 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001367 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1368 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001369 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1370 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1372 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001373 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1374 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001375 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001376
1377 memset(&pipeline, 0, sizeof(pipeline));
1378 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu87544e72015-02-23 10:41:08 -07001379 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001381 memset(&ia, 0, sizeof(ia));
1382 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1383 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1384
1385 memset(&rs, 0, sizeof(rs));
1386 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001387 rs.fillMode = XGL_FILL_SOLID;
Mike Stroyan16b3d982015-03-19 14:29:04 -06001388 rs.cullMode = XGL_CULL_BACK;
Tony Barbour29645d02015-01-16 14:27:35 -07001389 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001390
1391 memset(&cb, 0, sizeof(cb));
1392 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001393 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1394 memset(att_state, 0, sizeof(att_state));
1395 att_state[0].format = demo->format;
1396 att_state[0].channelWriteMask = 0xf;
1397 att_state[0].blendEnable = XGL_FALSE;
1398 cb.attachmentCount = 1;
1399 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400
Tony Barbour29645d02015-01-16 14:27:35 -07001401 memset(&vp, 0, sizeof(vp));
1402 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001403 vp.numViewports = 1;
Mike Stroyan259bc542015-03-24 15:13:34 -06001404 vp.clipOrigin = XGL_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001405
1406 memset(&ds, 0, sizeof(ds));
1407 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1408 ds.format = demo->depth.format;
1409 ds.depthTestEnable = XGL_TRUE;
1410 ds.depthWriteEnable = XGL_TRUE;
1411 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1412 ds.depthBoundsEnable = XGL_FALSE;
1413 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1414 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1415 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1416 ds.stencilTestEnable = XGL_FALSE;
1417 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419 memset(&vs, 0, sizeof(vs));
1420 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1421 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001422 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001423 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424
1425 memset(&fs, 0, sizeof(fs));
1426 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1427 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1428 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001429 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001430
1431 memset(&ms, 0, sizeof(ms));
1432 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1433 ms.sampleMask = 1;
1434 ms.multisampleEnable = XGL_FALSE;
1435 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001436
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001437 pipeline.pNext = (const void *) &ia;
1438 ia.pNext = (const void *) &rs;
1439 rs.pNext = (const void *) &cb;
1440 cb.pNext = (const void *) &ms;
1441 ms.pNext = (const void *) &vp;
1442 vp.pNext = (const void *) &ds;
1443 ds.pNext = (const void *) &vs;
1444 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001445
1446 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1447 assert(!err);
1448
1449 xglDestroyObject(vs.shader.shader);
1450 xglDestroyObject(fs.shader.shader);
1451}
1452
1453static void demo_prepare_dynamic_states(struct demo *demo)
1454{
Tony Barbour29645d02015-01-16 14:27:35 -07001455 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1456 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1457 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1458 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001459 XGL_RESULT err;
1460
Tony Barbour29645d02015-01-16 14:27:35 -07001461 memset(&viewport_create, 0, sizeof(viewport_create));
1462 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001463 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001464 XGL_VIEWPORT viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001465 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001466 viewport.height = (float) demo->height;
1467 viewport.width = (float) demo->width;
1468 viewport.minDepth = (float) 0.0f;
1469 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001470 viewport_create.pViewports = &viewport;
1471 XGL_RECT scissor;
1472 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001473 scissor.extent.width = demo->width;
1474 scissor.extent.height = demo->height;
1475 scissor.offset.x = 0;
1476 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001477 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478
1479 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001480 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001481 raster.pointSize = 1.0;
1482 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001483
1484 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001485 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001486 color_blend.blendConst[0] = 1.0f;
1487 color_blend.blendConst[1] = 1.0f;
1488 color_blend.blendConst[2] = 1.0f;
1489 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001490
1491 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001492 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001493 depth_stencil.minDepth = 0.0f;
1494 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001495 depth_stencil.stencilBackRef = 0;
1496 depth_stencil.stencilFrontRef = 0;
1497 depth_stencil.stencilReadMask = 0xff;
1498 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001499
Tony Barbour29645d02015-01-16 14:27:35 -07001500 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001501 assert(!err);
1502
Tony Barbour29645d02015-01-16 14:27:35 -07001503 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001504 assert(!err);
1505
Tony Barbour29645d02015-01-16 14:27:35 -07001506 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001507 &color_blend, &demo->color_blend);
1508 assert(!err);
1509
Tony Barbour29645d02015-01-16 14:27:35 -07001510 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001511 &depth_stencil, &demo->depth_stencil);
1512 assert(!err);
1513}
1514
Chia-I Wu63ea9262015-03-26 13:14:16 +08001515static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001516{
1517 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1518 [0] = {
1519 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1520 .count = 1,
1521 },
1522 [1] = {
1523 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1524 .count = DEMO_TEXTURE_COUNT,
1525 },
1526 };
Chia-I Wu63ea9262015-03-26 13:14:16 +08001527 const XGL_DESCRIPTOR_POOL_CREATE_INFO descriptor_pool = {
1528 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001529 .pNext = NULL,
1530 .count = 2,
1531 .pTypeCount = type_counts,
1532 };
1533 XGL_RESULT err;
1534
Chia-I Wu63ea9262015-03-26 13:14:16 +08001535 err = xglCreateDescriptorPool(demo->device,
1536 XGL_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
1537 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001538 assert(!err);
1539}
1540
1541static void demo_prepare_descriptor_set(struct demo *demo)
1542{
1543 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1544 &demo->uniform_data.attach;
1545 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1546 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1547 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1548 XGL_UPDATE_BUFFERS update_vs;
1549 XGL_RESULT err;
1550 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001551 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001552
1553 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1554 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1555 view_info[i].pNext = NULL;
1556 view_info[i].view = demo->textures[i].view,
1557 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1558
1559 combined_info[i].pSampler = demo->textures[i].sampler;
1560 combined_info[i].pImageView = &view_info[i];
1561 }
1562
1563 memset(&update_vs, 0, sizeof(update_vs));
1564 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1565 update_vs.pNext = &update_fs;
1566 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1567 update_vs.count = 1;
1568 update_vs.pBufferViews = &view_info_vs;
1569
1570 memset(&update_fs, 0, sizeof(update_fs));
1571 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1572 update_fs.index = 1;
1573 update_fs.count = DEMO_TEXTURE_COUNT;
1574 update_fs.pSamplerImageViews = combined_info;
1575
Chia-I Wu63ea9262015-03-26 13:14:16 +08001576 err = xglAllocDescriptorSets(demo->desc_pool,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001577 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001578 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001579 &demo->desc_set, &count);
1580 assert(!err && count == 1);
1581
Chia-I Wu63ea9262015-03-26 13:14:16 +08001582 xglBeginDescriptorPoolUpdate(demo->device,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001583 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1584
Chia-I Wu63ea9262015-03-26 13:14:16 +08001585 xglClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001586 xglUpdateDescriptors(demo->desc_set, &update_vs);
1587
Chia-I Wu63ea9262015-03-26 13:14:16 +08001588 xglEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001589}
1590
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001591static void demo_prepare(struct demo *demo)
1592{
1593 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1594 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1595 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001596 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001597 .flags = 0,
1598 };
1599 XGL_RESULT err;
1600
1601 demo_prepare_buffers(demo);
1602 demo_prepare_depth(demo);
1603 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001604 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001605
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001606 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001607 demo_prepare_pipeline(demo);
1608 demo_prepare_dynamic_states(demo);
1609
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001610 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1611 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1612 assert(!err);
1613 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001614
Chia-I Wu63ea9262015-03-26 13:14:16 +08001615 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001616 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001617
1618
1619 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1620 demo->current_buffer = i;
1621 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1622 }
1623
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001624 /*
1625 * Prepare functions above may generate pipeline commands
1626 * that need to be flushed before beginning the render loop.
1627 */
1628 demo_flush_init_cmd(demo);
1629
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001630 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631}
1632
1633static void demo_handle_event(struct demo *demo,
1634 const xcb_generic_event_t *event)
1635{
Piers Daniell735ee532015-02-23 16:23:13 -07001636 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001637 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001638 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001639 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001640 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001641 case XCB_CLIENT_MESSAGE:
1642 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1643 (*demo->atom_wm_delete_window).atom) {
1644 demo->quit = true;
1645 }
1646 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001647 case XCB_KEY_RELEASE:
1648 {
1649 const xcb_key_release_event_t *key =
1650 (const xcb_key_release_event_t *) event;
1651
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001652 switch (key->detail) {
1653 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001654 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001655 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001656 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001657 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001658 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001659 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001660 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001661 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001662 case 0x41:
1663 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001664 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001665 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001666 }
1667 break;
1668 default:
1669 break;
1670 }
1671}
1672
1673static void demo_run(struct demo *demo)
1674{
1675 xcb_flush(demo->connection);
1676
1677 while (!demo->quit) {
1678 xcb_generic_event_t *event;
1679
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001680 if (demo->pause) {
1681 event = xcb_wait_for_event(demo->connection);
1682 } else {
1683 event = xcb_poll_for_event(demo->connection);
1684 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001685 if (event) {
1686 demo_handle_event(demo, event);
1687 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001688 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001689
1690 // Wait for work to finish before updating MVP.
1691 xglDeviceWaitIdle(demo->device);
1692 demo_update_data_buffer(demo);
1693
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001694 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001695
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001696 // Wait for work to finish before updating MVP.
1697 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001698 }
1699}
1700
1701static void demo_create_window(struct demo *demo)
1702{
1703 uint32_t value_mask, value_list[32];
1704
1705 demo->window = xcb_generate_id(demo->connection);
1706
1707 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1708 value_list[0] = demo->screen->black_pixel;
1709 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1710 XCB_EVENT_MASK_EXPOSURE;
1711
1712 xcb_create_window(demo->connection,
1713 XCB_COPY_FROM_PARENT,
1714 demo->window, demo->screen->root,
1715 0, 0, demo->width, demo->height, 0,
1716 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1717 demo->screen->root_visual,
1718 value_mask, value_list);
1719
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001720 /* Magic code that will send notification when window is destroyed */
1721 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1722 "WM_PROTOCOLS");
1723 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1724
1725 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1726 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1727
1728 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1729 demo->window, (*reply).atom, 4, 32, 1,
1730 &(*demo->atom_wm_delete_window).atom);
1731 free(reply);
1732
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733 xcb_map_window(demo->connection, demo->window);
1734}
1735
1736static void demo_init_xgl(struct demo *demo)
1737{
1738 const XGL_APPLICATION_INFO app = {
1739 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1740 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001741 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001743 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001745 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001746 };
1747 const XGL_WSI_X11_CONNECTION_INFO connection = {
1748 .pConnection = demo->connection,
1749 .root = demo->screen->root,
1750 .provider = 0,
1751 };
1752 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1753 .queueNodeIndex = 0,
1754 .queueCount = 1,
1755 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001756 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001757 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758 };
1759 const XGL_DEVICE_CREATE_INFO device = {
1760 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1761 .pNext = NULL,
1762 .queueRecordCount = 1,
1763 .pRequestedQueues = &queue,
1764 .extensionCount = 1,
1765 .ppEnabledExtensionNames = ext_names,
1766 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1767 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1768 };
1769 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001770 uint32_t gpu_count;
1771 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001772 size_t data_size;
1773 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001774
Jon Ashburn93cfc432015-01-29 15:47:01 -07001775 err = xglCreateInstance(&app, NULL, &demo->inst);
Ian Elliott3979e282015-04-03 15:24:55 -06001776 if (err == XGL_ERROR_INCOMPATIBLE_DRIVER) {
1777 printf("Cannot find a compatible Vulkan installable client driver "
1778 "(ICD).\nExiting ...\n");
1779 fflush(stdout);
1780 exit(1);
1781 } else {
1782 assert(!err);
1783 }
Jon Ashburn93cfc432015-01-29 15:47:01 -07001784 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001785 assert(!err && gpu_count == 1);
1786
1787 for (i = 0; i < device.extensionCount; i++) {
1788 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1789 assert(!err);
1790 }
1791
1792 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1793 assert(!err);
1794
1795 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1796 assert(!err);
1797
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001798 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
1799 &data_size, NULL);
1800 assert(!err);
1801
1802 demo->gpu_props = (XGL_PHYSICAL_GPU_PROPERTIES *) malloc(data_size);
1803 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
1804 &data_size, demo->gpu_props);
1805 assert(!err);
1806
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001807 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1808 &data_size, NULL);
1809 assert(!err);
1810
1811 demo->queue_props = (XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *) malloc(data_size);
1812 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1813 &data_size, demo->queue_props);
1814 assert(!err);
Ian Elliott8e777be2015-04-03 09:54:13 -06001815 queue_count = (uint32_t)(data_size / sizeof(XGL_PHYSICAL_GPU_QUEUE_PROPERTIES));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001816 assert(queue_count >= 1);
1817
1818 for (i = 0; i < queue_count; i++) {
1819 if (demo->queue_props[i].queueFlags & XGL_QUEUE_GRAPHICS_BIT)
1820 break;
1821 }
1822 assert(i < queue_count);
1823 demo->graphics_queue_node_index = i;
1824
1825 err = xglGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001826 0, &demo->queue);
1827 assert(!err);
1828}
1829
1830static void demo_init_connection(struct demo *demo)
1831{
1832 const xcb_setup_t *setup;
1833 xcb_screen_iterator_t iter;
1834 int scr;
1835
1836 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001837 if (demo->connection == NULL) {
1838 printf("Cannot find a compatible Vulkan installable client driver "
1839 "(ICD).\nExiting ...\n");
1840 fflush(stdout);
1841 exit(1);
1842 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001843
1844 setup = xcb_get_setup(demo->connection);
1845 iter = xcb_setup_roots_iterator(setup);
1846 while (scr-- > 0)
1847 xcb_screen_next(&iter);
1848
1849 demo->screen = iter.data;
1850}
1851
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001852static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001853{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001854 vec3 eye = {0.0f, 3.0f, 5.0f};
1855 vec3 origin = {0, 0, 0};
1856 vec3 up = {0.0f, -1.0f, 0.0};
1857
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858 memset(demo, 0, sizeof(*demo));
1859
Piers Daniell735ee532015-02-23 16:23:13 -07001860 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001861 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1862 demo->use_staging_buffer = true;
1863 }
1864
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001865 demo_init_connection(demo);
1866 demo_init_xgl(demo);
1867
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001868 demo->width = 500;
1869 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001870 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001871
1872 demo->spin_angle = 0.01f;
1873 demo->spin_increment = 0.01f;
1874 demo->pause = false;
1875
Piers Daniell735ee532015-02-23 16:23:13 -07001876 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001877 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1878 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001879}
1880
1881static void demo_cleanup(struct demo *demo)
1882{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001883 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001884
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001885 xglDestroyObject(demo->desc_set);
Chia-I Wu63ea9262015-03-26 13:14:16 +08001886 xglDestroyObject(demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001887
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001888 xglDestroyObject(demo->viewport);
1889 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001890 xglDestroyObject(demo->color_blend);
1891 xglDestroyObject(demo->depth_stencil);
1892
1893 xglDestroyObject(demo->pipeline);
Chia-I Wu87544e72015-02-23 10:41:08 -07001894 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001895
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001896 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1897 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001898 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001899 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001900 for (j = 0; j < demo->textures[i].num_mem; j++)
1901 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001902 xglDestroyObject(demo->textures[i].sampler);
1903 }
1904
1905 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001906 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001907 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001908 for (j = 0; j < demo->depth.num_mem; j++)
1909 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001910
1911 xglDestroyObject(demo->uniform_data.view);
1912 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001913 xglDestroyObject(demo->uniform_data.buf);
1914 for (j = 0; j < demo->uniform_data.num_mem; j++)
1915 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001916
1917 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001918 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001919 xglDestroyObject(demo->buffers[i].view);
1920 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001921 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001922 }
1923
1924 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001925 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001926
1927 xcb_destroy_window(demo->connection, demo->window);
1928 xcb_disconnect(demo->connection);
1929}
1930
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001931int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001932{
1933 struct demo demo;
1934
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001935 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001936
1937 demo_prepare(&demo);
1938 demo_create_window(&demo);
1939 demo_run(&demo);
1940
1941 demo_cleanup(&demo);
1942
1943 return 0;
1944}