blob: 319ef05eb99333750b335bcddf59a0f61144feb7 [file] [log] [blame]
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-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 Northropd4e020a2015-03-17 14:54:35 -060013#include "icd-spv.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060014
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060016#include <png.h>
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060017
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060018#define DEMO_BUFFER_COUNT 2
19#define DEMO_TEXTURE_COUNT 1
20
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060021/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070022 * structure to track all objects related to a texture.
23 */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060024struct texture_object {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070025 XGL_SAMPLER sampler;
26
27 XGL_IMAGE image;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060028 XGL_IMAGE_LAYOUT imageLayout;
29
Courtney Goeltzenleuchter40a8b0a2015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -060036static char *tex_files[] = {
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060037 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060038};
39
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060040struct xglcube_vs_uniform {
41 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060042 float mvp[4][4];
43 float position[12*3][4];
44 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060045};
46
47struct xgltexcube_vs_uniform {
48 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060049 float mvp[4][4];
50 float position[12*3][4];
51 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060052};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060053
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060054//--------------------------------------------------------------------------------------
55// Mesh and VertexFormat Data
56//--------------------------------------------------------------------------------------
57struct Vertex
58{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060059 float posX, posY, posZ, posW; // Position data
60 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060061};
62
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060063struct VertexPosTex
64{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060065 float posX, posY, posZ, posW; // Position data
66 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060067};
68
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060069#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060070#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060071
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060072static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchter17223602014-10-29 15:59:49 -060086 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -060087 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060088
89 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060090 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -060091 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060092
93 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060094 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -060095 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600102 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600103 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600114 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600115 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchtere3342402014-10-28 14:50:30 -0600120};
121
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600122static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600123 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600124 0.0f, 0.0f,
125 0.0f, 1.0f,
126
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600127 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600128 1.0f, 1.0f,
Courtney Goeltzenleuchter3eeff432014-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 Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600144 0.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600145 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600146
147 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600148 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600149 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600150
151 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600152 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600153 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600160 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600161 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600162
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600163 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600164 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600165 1.0f, 0.0f,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600166
167 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600168 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600169 0.0f, 1.0f,
170
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600171 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600172 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600173 0.0f, 1.0f,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600174
175 1.0f, 0.0f, // Vertex 11
176 0.0f, 0.0f,
177 0.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600200struct demo {
201 xcb_connection_t *connection;
202 xcb_screen_t *screen;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700203 bool use_staging_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600204
Jon Ashburn92e80132015-01-29 15:47:01 -0700205 XGL_INSTANCE inst;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600206 XGL_PHYSICAL_GPU gpu;
207 XGL_DEVICE device;
208 XGL_QUEUE queue;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700209 uint32_t graphics_queue_node_index;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600210 XGL_PHYSICAL_GPU_PROPERTIES *gpu_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700211 XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *queue_props;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600212
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600213 XGL_FRAMEBUFFER framebuffer;
Courtney Goeltzenleuchter4e8246e2014-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 Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700220 XGL_CMD_BUFFER cmd;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600221
222 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wu68040a42014-11-07 14:30:34 +0800223 XGL_FENCE fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600224 } buffers[DEMO_BUFFER_COUNT];
225
226 struct {
227 XGL_FORMAT format;
228
229 XGL_IMAGE image;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600230 uint32_t num_mem;
Jon Ashburna9ae3832015-01-16 09:37:43 -0700231 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600232 XGL_DEPTH_STENCIL_VIEW view;
233 } depth;
234
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600235 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600236
237 struct {
Chia-I Wu714df452015-01-01 07:55:04 +0800238 XGL_BUFFER buf;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600239 uint32_t num_mem;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700240 XGL_GPU_MEMORY *mem;
Chia-I Wu714df452015-01-01 07:55:04 +0800241 XGL_BUFFER_VIEW view;
242 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600243 } uniform_data;
244
Courtney Goeltzenleuchterbfccf412015-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 Wu7732cb22015-03-26 15:27:55 +0800248 XGL_DESCRIPTOR_SET_LAYOUT_CHAIN desc_layout_chain;
Chia-I Wu6e68a892015-02-23 10:41:08 -0700249 XGL_DESCRIPTOR_SET_LAYOUT desc_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600250 XGL_PIPELINE pipeline;
251
Tony Barbourfa6cac72015-01-16 14:27:35 -0700252 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
253 XGL_DYNAMIC_RS_STATE_OBJECT raster;
254 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
255 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600256
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600257 mat4x4 projection_matrix;
258 mat4x4 view_matrix;
259 mat4x4 model_matrix;
260
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600261 float spin_angle;
262 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600263 bool pause;
264
Chia-I Wu8d24b3b2015-03-26 13:14:16 +0800265 XGL_DESCRIPTOR_POOL desc_pool;
Chia-I Wuf8385062015-01-04 16:27:24 +0800266 XGL_DESCRIPTOR_SET desc_set;
267
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600268 xcb_window_t window;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -0700269 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600270
271 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600272 uint32_t current_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600273};
274
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600275static void demo_flush_init_cmd(struct demo *demo)
276{
277 XGL_RESULT err;
278
279 if (demo->cmd == XGL_NULL_HANDLE)
280 return;
281
282 err = xglEndCommandBuffer(demo->cmd);
283 assert(!err);
284
285 const XGL_CMD_BUFFER cmd_bufs[] = { demo->cmd };
286
287 err = xglQueueSubmit(demo->queue, 1, cmd_bufs,
288 demo->num_refs, demo->mem_refs, XGL_NULL_HANDLE);
289 assert(!err);
290
291 err = xglQueueWaitIdle(demo->queue);
292 assert(!err);
293
294 xglDestroyObject(demo->cmd);
295 demo->cmd = XGL_NULL_HANDLE;
296 demo->num_refs = 0;
297}
298
299static void demo_add_mem_refs(
300 struct demo *demo,
301 XGL_MEMORY_REF_FLAGS flags,
302 int num_refs, XGL_GPU_MEMORY *mem)
303{
304 for (int i = 0; i < num_refs; i++) {
305 demo->mem_refs[demo->num_refs].flags = flags;
306 demo->mem_refs[demo->num_refs].mem = mem[i];
307 demo->num_refs++;
308 assert(demo->num_refs < 16);
309 }
310}
311
312static void demo_set_image_layout(
313 struct demo *demo,
314 XGL_IMAGE image,
315 XGL_IMAGE_LAYOUT old_image_layout,
316 XGL_IMAGE_LAYOUT new_image_layout)
317{
318 XGL_RESULT err;
319
320 if (demo->cmd == XGL_NULL_HANDLE) {
321 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
322 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
323 .pNext = NULL,
324 .queueNodeIndex = demo->graphics_queue_node_index,
325 .flags = 0,
326 };
327
328 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
329 assert(!err);
330
331 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
332 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
333 .pNext = NULL,
334 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
335 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
336 };
337 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
338 }
339
340 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
341 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
342 .pNext = NULL,
343 .outputMask = 0,
344 .inputMask = 0,
345 .oldLayout = old_image_layout,
346 .newLayout = new_image_layout,
347 .image = image,
348 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
349 };
350
351 if (new_image_layout == XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
352 /* Make sure anything that was copying from this image has completed */
353 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_COPY_BIT;
354 }
355
356 if (new_image_layout == XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
357 /* Make sure any Copy or CPU writes to image are flushed */
358 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT | XGL_MEMORY_OUTPUT_CPU_WRITE_BIT;
359 }
360
361 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
362
363 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_TOP_OF_PIPE };
364
365 XGL_PIPELINE_BARRIER pipeline_barrier;
366 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
367 pipeline_barrier.pNext = NULL;
368 pipeline_barrier.eventCount = 1;
369 pipeline_barrier.pEvents = set_events;
370 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
371 pipeline_barrier.memBarrierCount = 1;
372 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
373
374 xglCmdPipelineBarrier(demo->cmd, &pipeline_barrier);
375}
376
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700377static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600378{
379 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
380 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan55658c22014-12-04 11:08:39 +0000381 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600382 };
383 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
384 .view = demo->depth.view,
Mike Stroyan55658c22014-12-04 11:08:39 +0000385 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600386 };
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600387 const XGL_CLEAR_COLOR clear_color = {
388 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
389 .useRawValue = false,
390 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600391 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600392 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn53d27af2014-12-31 17:08:35 -0700393 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
394 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600395 .pNext = NULL,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700396 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
397 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
398 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600399 XGL_RESULT err;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700400 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
401 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
402 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
403 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
404 .pNext = NULL,
405 .colorAttachmentCount = 1,
406 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
407 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
408 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600409 .width = demo->width,
410 .height = demo->height,
411 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700412 };
413 XGL_RENDER_PASS_CREATE_INFO rp_info;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600414 XGL_RENDER_PASS_BEGIN rp_begin;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700415
416 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600417 err = xglCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700418 assert(!err);
419 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
420 rp_info.renderArea.extent.width = demo->width;
421 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600422 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
423 rp_info.pColorFormats = &demo->format;
424 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700425 rp_info.pColorLoadOps = &load_op;
426 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600427 rp_info.pColorLoadClearValues = &clear_color;
428 rp_info.depthStencilFormat = XGL_FMT_D16_UNORM;
429 rp_info.depthStencilLayout = depth_stencil.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700430 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600431 rp_info.depthLoadClearValue = clear_depth;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700432 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
433 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600434 rp_info.stencilLoadClearValue = 0;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700435 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600436 err = xglCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700437 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600438
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700439 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600440 assert(!err);
441
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700442 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600443 demo->pipeline);
Chia-I Wu862c5572015-03-28 15:23:55 +0800444 xglCmdBindDescriptorSets(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
445 demo->desc_layout_chain, 0, 1, &demo->desc_set, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600446
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700447 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
448 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
449 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600450 demo->color_blend);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700451 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600452 demo->depth_stencil);
453
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600454 xglCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600455 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
456 clear_range.baseMipLevel = 0;
457 clear_range.mipLevels = 1;
458 clear_range.baseArraySlice = 0;
459 clear_range.arraySize = 1;
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700460 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600461 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600462 XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600463 clear_color, 1, &clear_range);
464
465 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700466 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600467 XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600468 clear_depth, 0, 1, &clear_range);
469
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700470 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600471 xglCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600472
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700473 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600474 assert(!err);
Courtney Goeltzenleuchter04fc2fb2015-02-25 17:53:18 -0700475
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600476 xglDestroyObject(rp_begin.renderPass);
477 xglDestroyObject(rp_begin.framebuffer);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600478}
479
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600480
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600481void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600482{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600483 mat4x4 MVP, Model, VP;
484 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600485 uint8_t *pData;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600486 XGL_RESULT err;
487
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600488 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600489
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600490 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600491 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700492 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600493 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600494
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700495 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600496 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600497 assert(!err);
498
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600499 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600500
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700501 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600502 assert(!err);
503}
504
505static void demo_draw(struct demo *demo)
506{
507 const XGL_WSI_X11_PRESENT_INFO present = {
508 .destWindow = demo->window,
509 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wu68040a42014-11-07 14:30:34 +0800510 .async = true,
511 .flip = false,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600512 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800513 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600514 XGL_RESULT err;
515
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600516 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wu68040a42014-11-07 14:30:34 +0800517 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
518
Mark Lobodzinski15427102015-02-18 16:38:17 -0600519 uint32_t i, idx = 0;
520 XGL_MEMORY_REF *memRefs;
521 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
522 demo->depth.num_mem +
523 demo->textures[0].num_mem +
524 demo->uniform_data.num_mem));
525 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
526 memRefs[idx].mem = demo->depth.mem[i];
527 memRefs[idx].flags = 0;
528 }
529 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
530 memRefs[idx].mem = demo->textures[0].mem[i];
531 memRefs[idx].flags = 0;
532 }
533 memRefs[idx].mem = demo->buffers[0].mem;
534 memRefs[idx++].flags = 0;
535 memRefs[idx].mem = demo->buffers[1].mem;
536 memRefs[idx++].flags = 0;
537 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
538 memRefs[idx].mem = demo->uniform_data.mem[i];
539 memRefs[idx].flags = 0;
540 }
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700541 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Piers Daniell886be472015-02-23 16:23:13 -0700542 idx, memRefs, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600543 assert(!err);
544
Chia-I Wu68040a42014-11-07 14:30:34 +0800545 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600546 assert(!err);
547
548 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
549}
550
551static void demo_prepare_buffers(struct demo *demo)
552{
553 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
554 .format = demo->format,
555 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
556 .extent = {
557 .width = demo->width,
558 .height = demo->height,
559 },
560 .flags = 0,
561 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800562 const XGL_FENCE_CREATE_INFO fence = {
563 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
564 .pNext = NULL,
565 .flags = 0,
566 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600567 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600568 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600569
570 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
571 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
572 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
573 .pNext = NULL,
574 .format = demo->format,
575 .mipLevel = 0,
576 .baseArraySlice = 0,
577 .arraySize = 1,
578 };
579
580 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
581 &demo->buffers[i].image, &demo->buffers[i].mem);
582 assert(!err);
583
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600584 demo_set_image_layout(demo, demo->buffers[i].image,
585 XGL_IMAGE_LAYOUT_UNDEFINED,
586 XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
587
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600588 color_attachment_view.image = demo->buffers[i].image;
589
590 err = xglCreateColorAttachmentView(demo->device,
591 &color_attachment_view, &demo->buffers[i].view);
592 assert(!err);
Chia-I Wu68040a42014-11-07 14:30:34 +0800593
594 err = xglCreateFence(demo->device,
595 &fence, &demo->buffers[i].fence);
596 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600597 }
598}
599
600static void demo_prepare_depth(struct demo *demo)
601{
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700602 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600603 const XGL_IMAGE_CREATE_INFO image = {
604 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
605 .pNext = NULL,
606 .imageType = XGL_IMAGE_2D,
607 .format = depth_format,
608 .extent = { demo->width, demo->height, 1 },
609 .mipLevels = 1,
610 .arraySize = 1,
611 .samples = 1,
612 .tiling = XGL_OPTIMAL_TILING,
613 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
614 .flags = 0,
615 };
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700616 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
617 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
618 .pNext = NULL,
619 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600620 XGL_MEMORY_ALLOC_INFO mem_alloc = {
621 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700622 .pNext = &img_alloc,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600623 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -0700624 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburn32769172015-01-20 15:06:59 -0700625 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600626 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
627 };
628 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
629 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
630 .pNext = NULL,
631 .image = XGL_NULL_HANDLE,
632 .mipLevel = 0,
633 .baseArraySlice = 0,
634 .arraySize = 1,
635 .flags = 0,
636 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700637 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600638 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700639 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600640 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600641 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600642 uint32_t num_allocations = 0;
643 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600644
645 demo->depth.format = depth_format;
646
647 /* create image */
648 err = xglCreateImage(demo->device, &image,
649 &demo->depth.image);
650 assert(!err);
651
Jon Ashburna9ae3832015-01-16 09:37:43 -0700652 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
653 assert(!err && num_alloc_size == sizeof(num_allocations));
654 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
655 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
656 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600657 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700658 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
659 &mem_reqs_size, mem_reqs);
660 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700661 err = xglGetObjectInfo(demo->depth.image,
662 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
663 &img_reqs_size, &img_reqs);
664 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
665 img_alloc.usage = img_reqs.usage;
666 img_alloc.formatClass = img_reqs.formatClass;
667 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600668 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -0700669 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600670
Jon Ashburna9ae3832015-01-16 09:37:43 -0700671 /* allocate memory */
672 err = xglAllocMemory(demo->device, &mem_alloc,
673 &(demo->depth.mem[i]));
674 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600675
Jon Ashburna9ae3832015-01-16 09:37:43 -0700676 /* bind memory */
677 err = xglBindObjectMemory(demo->depth.image, i,
678 demo->depth.mem[i], 0);
679 assert(!err);
680 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600681
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600682 demo_set_image_layout(demo, demo->depth.image,
683 XGL_IMAGE_LAYOUT_UNDEFINED,
684 XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
685
686 demo_add_mem_refs(demo, XGL_MEMORY_REF_READ_ONLY_BIT, num_allocations, demo->depth.mem);
687
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600688 /* create image view */
689 view.image = demo->depth.image;
690 err = xglCreateDepthStencilView(demo->device, &view,
691 &demo->depth.view);
692 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600693}
694
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600695/** loadTexture
696 * loads a png file into an memory object, using cstdio , libpng.
697 *
698 * \param demo : Needed to access XGL calls
699 * \param filename : the png file to be loaded
700 * \param width : width of png, to be updated as a side effect of this function
701 * \param height : height of png, to be updated as a side effect of this function
702 *
703 * \return bool : an opengl texture id. true if successful?,
704 * should be validated by the client of this function.
705 *
706 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
707 * Modified to copy image to memory
708 *
709 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700710bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600711 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600712 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600713{
714 //header for testing if it is a png
715 png_byte header[8];
Ian Elliott642f8922015-02-13 14:29:21 -0700716 int is_png, bit_depth, color_type,rowbytes;
717 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600718 png_structp png_ptr;
719 png_infop info_ptr, end_info;
720 png_byte *image_data;
721 png_bytep *row_pointers;
722
723 //open file as binary
724 FILE *fp = fopen(filename, "rb");
725 if (!fp) {
726 return false;
727 }
728
729 //read the header
730 fread(header, 1, 8, fp);
731
732 //test if png
733 is_png = !png_sig_cmp(header, 0, 8);
734 if (!is_png) {
735 fclose(fp);
736 return false;
737 }
738
739 //create png struct
740 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
741 NULL, NULL);
742 if (!png_ptr) {
743 fclose(fp);
744 return (false);
745 }
746
747 //create png info struct
748 info_ptr = png_create_info_struct(png_ptr);
749 if (!info_ptr) {
750 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
751 fclose(fp);
752 return (false);
753 }
754
755 //create png info struct
756 end_info = png_create_info_struct(png_ptr);
757 if (!end_info) {
758 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
759 fclose(fp);
760 return (false);
761 }
762
763 //png error stuff, not sure libpng man suggests this.
764 if (setjmp(png_jmpbuf(png_ptr))) {
765 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
766 fclose(fp);
767 return (false);
768 }
769
770 //init png reading
771 png_init_io(png_ptr, fp);
772
773 //let libpng know you already read the first 8 bytes
774 png_set_sig_bytes(png_ptr, 8);
775
776 // read all the info up to the image data
777 png_read_info(png_ptr, info_ptr);
778
779 // get info about png
780 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
781 NULL, NULL, NULL);
782
783 //update width and height based on png info
784 *width = twidth;
785 *height = theight;
786
787 // Require that incoming texture be 8bits per color component
788 // and 4 components (RGBA).
789 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
790 png_get_channels(png_ptr, info_ptr) != 4) {
791 return false;
792 }
793
794 if (rgba_data == NULL) {
795 // If data pointer is null, we just want the width & height
796 // clean up memory and close stuff
797 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
798 fclose(fp);
799
800 return true;
801 }
802
803 // Update the png info struct.
804 png_read_update_info(png_ptr, info_ptr);
805
806 // Row size in bytes.
807 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
808
809 // Allocate the image_data as a big block, to be given to opengl
810 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
811 if (!image_data) {
812 //clean up memory and close stuff
813 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
814 fclose(fp);
815 return false;
816 }
817
818 // row_pointers is for pointing to image_data for reading the png with libpng
819 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
820 if (!row_pointers) {
821 //clean up memory and close stuff
822 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
823 // delete[] image_data;
824 fclose(fp);
825 return false;
826 }
827 // set the individual row_pointers to point at the correct offsets of image_data
828 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700829 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600830
831 // read the png into image_data through row_pointers
832 png_read_image(png_ptr, row_pointers);
833
834 // clean up memory and close stuff
835 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
836 free(row_pointers);
837 free(image_data);
838 fclose(fp);
839
840 return true;
841}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600842
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700843static void demo_prepare_texture_image(struct demo *demo,
844 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600845 struct texture_object *tex_obj,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700846 XGL_IMAGE_TILING tiling,
847 XGL_FLAGS mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600848{
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700849 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600850 int32_t tex_width;
851 int32_t tex_height;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600852 XGL_RESULT err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700853
854 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
855 assert(err);
856
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600857 tex_obj->tex_width = tex_width;
858 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700859
860 const XGL_IMAGE_CREATE_INFO image_create_info = {
861 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
862 .pNext = NULL,
863 .imageType = XGL_IMAGE_2D,
864 .format = tex_format,
865 .extent = { tex_width, tex_height, 1 },
866 .mipLevels = 1,
867 .arraySize = 1,
868 .samples = 1,
869 .tiling = tiling,
870 .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
871 .flags = 0,
872 };
Piers Daniell886be472015-02-23 16:23:13 -0700873 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
874 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
875 .pNext = NULL,
876 };
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700877 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
878 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
Piers Daniell886be472015-02-23 16:23:13 -0700879 .pNext = &buf_alloc,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700880 };
881 XGL_MEMORY_ALLOC_INFO mem_alloc = {
882 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
883 .pNext = &img_alloc,
884 .allocationSize = 0,
885 .memProps = mem_props,
886 .memType = XGL_MEMORY_TYPE_IMAGE,
887 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
888 };
889
890 XGL_MEMORY_REQUIREMENTS *mem_reqs;
891 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Piers Daniell886be472015-02-23 16:23:13 -0700892 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
893 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700894 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
895 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
896 uint32_t num_allocations = 0;
897 size_t num_alloc_size = sizeof(num_allocations);
898
899 err = xglCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600900 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700901 assert(!err);
902
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600903 err = xglGetObjectInfo(tex_obj->image,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700904 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
905 &num_alloc_size, &num_allocations);
906 assert(!err && num_alloc_size == sizeof(num_allocations));
907 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600908 tex_obj->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
909 err = xglGetObjectInfo(tex_obj->image,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700910 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
911 &mem_reqs_size, mem_reqs);
912 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600913 err = xglGetObjectInfo(tex_obj->image,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700914 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
915 &img_reqs_size, &img_reqs);
916 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
917 img_alloc.usage = img_reqs.usage;
918 img_alloc.formatClass = img_reqs.formatClass;
919 img_alloc.samples = img_reqs.samples;
920 mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
921 for (uint32_t j = 0; j < num_allocations; j ++) {
Courtney Goeltzenleuchter6e5e81c2015-02-25 11:48:28 -0700922 mem_alloc.memType = mem_reqs[j].memType;
Piers Daniell886be472015-02-23 16:23:13 -0700923 mem_alloc.allocationSize = mem_reqs[j].size;
924
925 if (mem_alloc.memType == XGL_MEMORY_TYPE_BUFFER) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600926 err = xglGetObjectInfo(tex_obj->image,
Piers Daniell886be472015-02-23 16:23:13 -0700927 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
928 &buf_reqs_size, &buf_reqs);
929 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
930 buf_alloc.usage = buf_reqs.usage;
931 img_alloc.pNext = &buf_alloc;
932 } else {
933 img_alloc.pNext = 0;
934 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700935
936 /* allocate memory */
937 err = xglAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600938 &(tex_obj->mem[j]));
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700939 assert(!err);
940
941 /* bind memory */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600942 err = xglBindObjectMemory(tex_obj->image, j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700943 assert(!err);
944 }
945 free(mem_reqs);
946 mem_reqs = NULL;
947
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600948 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700949
950 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
951 const XGL_IMAGE_SUBRESOURCE subres = {
952 .aspect = XGL_IMAGE_ASPECT_COLOR,
953 .mipLevel = 0,
954 .arraySlice = 0,
955 };
956 XGL_SUBRESOURCE_LAYOUT layout;
957 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
958 void *data;
959
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600960 err = xglGetImageSubresourceInfo(tex_obj->image, &subres,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700961 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
962 &layout_size, &layout);
963 assert(!err && layout_size == sizeof(layout));
964 /* Linear texture must be within a single memory object */
965 assert(num_allocations == 1);
966
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600967 err = xglMapMemory(tex_obj->mem[0], 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700968 assert(!err);
969
970 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
971 fprintf(stderr, "Error loading texture: %s\n", filename);
972 }
973
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600974 err = xglUnmapMemory(tex_obj->mem[0]);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700975 assert(!err);
976 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600977
978 tex_obj->imageLayout = XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
979 demo_set_image_layout(demo, tex_obj->image,
980 XGL_IMAGE_LAYOUT_UNDEFINED,
981 tex_obj->imageLayout);
982 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700983}
984
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600985static void demo_destroy_texture_image(struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700986{
987 /* clean up staging resources */
988 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
989 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
990 xglFreeMemory(tex_objs->mem[j]);
991 }
992
993 free(tex_objs->mem);
994 xglDestroyObject(tex_objs->image);
995}
996
997static void demo_prepare_textures(struct demo *demo)
998{
999 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
1000 XGL_FORMAT_PROPERTIES props;
1001 size_t size = sizeof(props);
1002 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001003 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001004
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001005 err = xglGetFormatInfo(demo->device, tex_format,
1006 XGL_INFO_TYPE_FORMAT_PROPERTIES,
1007 &size, &props);
1008 assert(!err);
1009
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001010 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001011
1012 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
1013 /* Device can texture using linear textures */
1014 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
1015 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001016 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001017 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001018 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001019
1020 memset(&staging_texture, 0, sizeof(staging_texture));
1021 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
1022 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
1023
1024 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
1025 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
1026
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001027 demo_set_image_layout(demo, staging_texture.image,
1028 staging_texture.imageLayout,
1029 XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001030
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001031 demo_set_image_layout(demo, demo->textures[i].image,
1032 demo->textures[i].imageLayout,
1033 XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001034
1035 XGL_IMAGE_COPY copy_region = {
1036 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
1037 .srcOffset = { 0, 0, 0 },
1038 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
1039 .destOffset = { 0, 0, 0 },
1040 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1041 };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001042 xglCmdCopyImage(demo->cmd,
1043 staging_texture.image, XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1044 demo->textures[i].image, XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001045 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001046
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001047 demo_add_mem_refs(demo, XGL_MEMORY_REF_READ_ONLY_BIT, staging_texture.num_mem, staging_texture.mem);
1048 demo_add_mem_refs(demo, 0, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001049
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001050 demo_set_image_layout(demo, demo->textures[i].image,
1051 XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
1052 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001053
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001054 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001055
1056 demo_destroy_texture_image(&staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001057 } else {
1058 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
1059 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1060 }
1061
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001062 const XGL_SAMPLER_CREATE_INFO sampler = {
1063 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
1064 .pNext = NULL,
1065 .magFilter = XGL_TEX_FILTER_NEAREST,
1066 .minFilter = XGL_TEX_FILTER_NEAREST,
1067 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001068 .addressU = XGL_TEX_ADDRESS_CLAMP,
1069 .addressV = XGL_TEX_ADDRESS_CLAMP,
1070 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001071 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001072 .maxAnisotropy = 1,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001073 .compareFunc = XGL_COMPARE_NEVER,
1074 .minLod = 0.0f,
1075 .maxLod = 0.0f,
1076 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
1077 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001078
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001079 XGL_IMAGE_VIEW_CREATE_INFO view = {
1080 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1081 .pNext = NULL,
1082 .image = XGL_NULL_HANDLE,
1083 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001084 .format = tex_format,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001085 .channels = { XGL_CHANNEL_SWIZZLE_R,
1086 XGL_CHANNEL_SWIZZLE_G,
1087 XGL_CHANNEL_SWIZZLE_B,
1088 XGL_CHANNEL_SWIZZLE_A, },
1089 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
1090 .minLod = 0.0f,
1091 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001092
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001093 /* create sampler */
1094 err = xglCreateSampler(demo->device, &sampler,
1095 &demo->textures[i].sampler);
1096 assert(!err);
1097
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001098 /* create image view */
1099 view.image = demo->textures[i].image;
1100 err = xglCreateImageView(demo->device, &view,
1101 &demo->textures[i].view);
1102 assert(!err);
1103 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001104}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001105
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001106void demo_prepare_cube_data_buffer(struct demo *demo)
1107{
Chia-I Wu714df452015-01-01 07:55:04 +08001108 XGL_BUFFER_CREATE_INFO buf_info;
1109 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001110 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1111 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1112 .pNext = NULL,
1113 };
1114 XGL_MEMORY_ALLOC_INFO alloc_info = {
1115 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1116 .pNext = &buf_alloc,
1117 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -07001118 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburn32769172015-01-20 15:06:59 -07001119 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001120 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1121 };
1122 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001123 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001124 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001125 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1126 uint32_t num_allocations = 0;
1127 size_t num_alloc_size = sizeof(num_allocations);
1128 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001129 int i;
1130 mat4x4 MVP, VP;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001131 XGL_RESULT err;
1132 struct xgltexcube_vs_uniform data;
1133
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001134 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001135 mat4x4_mul(MVP, VP, demo->model_matrix);
1136 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001137// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001138
1139 for (i=0; i<12*3; i++) {
1140 data.position[i][0] = g_vertex_buffer_data[i*3];
1141 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1142 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1143 data.position[i][3] = 1.0f;
1144 data.attr[i][0] = g_uv_buffer_data[2*i];
1145 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1146 data.attr[i][2] = 0;
1147 data.attr[i][3] = 0;
1148 }
1149
Chia-I Wu714df452015-01-01 07:55:04 +08001150 memset(&buf_info, 0, sizeof(buf_info));
1151 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1152 buf_info.size = sizeof(data);
1153 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1154 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1155 assert(!err);
1156
1157 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001158 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1159 &num_alloc_size, &num_allocations);
1160 assert(!err && num_alloc_size == sizeof(num_allocations));
1161 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1162 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1163 demo->uniform_data.num_mem = num_allocations;
1164 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu714df452015-01-01 07:55:04 +08001165 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001166 &mem_reqs_size, mem_reqs);
1167 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1168 err = xglGetObjectInfo(demo->uniform_data.buf,
1169 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1170 &buf_reqs_size, &buf_reqs);
1171 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1172 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001173 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001174 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu714df452015-01-01 07:55:04 +08001175
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001176 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1177 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001178
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001179 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001180 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001181
Piers Daniell886be472015-02-23 16:23:13 -07001182 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001183
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001184 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1185 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001186
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001187 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1188 demo->uniform_data.mem[i], 0);
1189 assert(!err);
1190 }
Chia-I Wu714df452015-01-01 07:55:04 +08001191
1192 memset(&view_info, 0, sizeof(view_info));
1193 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1194 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu28fb4ce2015-01-16 22:31:25 +08001195 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001196 view_info.offset = 0;
1197 view_info.range = sizeof(data);
1198
1199 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1200 assert(!err);
1201
1202 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1203 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001204}
1205
Chia-I Wuf8385062015-01-04 16:27:24 +08001206static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001207{
Chia-I Wufc9d9132015-03-26 15:04:41 +08001208 const XGL_DESCRIPTOR_SET_LAYOUT_BINDING layout_bindings[2] = {
1209 [0] = {
1210 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1211 .count = 1,
1212 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001213 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001214 },
1215 [1] = {
1216 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1217 .count = DEMO_TEXTURE_COUNT,
1218 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001219 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001220 },
1221 };
1222 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001223 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1224 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001225 .count = 2,
1226 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001227 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001228 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001229
Chia-I Wuf8385062015-01-04 16:27:24 +08001230 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001231 &descriptor_layout, &demo->desc_layout);
1232 assert(!err);
1233
1234 err = xglCreateDescriptorSetLayoutChain(demo->device,
1235 1, &demo->desc_layout, &demo->desc_layout_chain);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001236 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001237}
1238
1239static XGL_SHADER demo_prepare_shader(struct demo *demo,
1240 XGL_PIPELINE_SHADER_STAGE stage,
1241 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001242 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001243{
1244 XGL_SHADER_CREATE_INFO createInfo;
1245 XGL_SHADER shader;
1246 XGL_RESULT err;
1247
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001248
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001249 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1250 createInfo.pNext = NULL;
1251
Cody Northropacfb0492015-03-17 15:55:58 -06001252#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001253 createInfo.codeSize = size;
1254 createInfo.pCode = code;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001255 createInfo.flags = 0;
1256
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001257 err = xglCreateShader(demo->device, &createInfo, &shader);
1258 if (err) {
1259 free((void *) createInfo.pCode);
1260 }
1261#else
Cody Northropacfb0492015-03-17 15:55:58 -06001262 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001263 // to the driver "under the covers"
1264 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1265 createInfo.pCode = malloc(createInfo.codeSize);
1266 createInfo.flags = 0;
1267
1268 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northropacfb0492015-03-17 15:55:58 -06001269 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001270 ((uint32_t *) createInfo.pCode)[1] = 0;
1271 ((uint32_t *) createInfo.pCode)[2] = stage;
1272 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1273
1274 err = xglCreateShader(demo->device, &createInfo, &shader);
1275 if (err) {
1276 free((void *) createInfo.pCode);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001277 return NULL;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001278 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001279#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001280
1281 return shader;
1282}
1283
Cody Northropacfb0492015-03-17 15:55:58 -06001284char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001285{
1286 long int size;
1287 void *shader_code;
1288
1289 FILE *fp = fopen(filename, "rb");
1290 if (!fp) return NULL;
1291
1292 fseek(fp, 0L, SEEK_END);
1293 size = ftell(fp);
1294
1295 fseek(fp, 0L, SEEK_SET);
1296
1297 shader_code = malloc(size);
1298 fread(shader_code, size, 1, fp);
1299
1300 *psize = size;
1301
1302 return shader_code;
1303}
1304
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001305static XGL_SHADER demo_prepare_vs(struct demo *demo)
1306{
Cody Northropacfb0492015-03-17 15:55:58 -06001307#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001308 void *vertShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001309 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001310
Cody Northropacfb0492015-03-17 15:55:58 -06001311 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001312
1313 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1314 vertShaderCode, size);
1315#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001316 static const char *vertShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001317 "#version 140\n"
1318 "#extension GL_ARB_separate_shader_objects : enable\n"
1319 "#extension GL_ARB_shading_language_420pack : enable\n"
1320 "\n"
1321 "layout(binding = 0) uniform buf {\n"
1322 " mat4 MVP;\n"
1323 " vec4 position[12*3];\n"
1324 " vec4 attr[12*3];\n"
1325 "} ubuf;\n"
1326 "\n"
1327 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001328 "\n"
1329 "void main() \n"
1330 "{\n"
1331 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001332 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1333 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001334
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001335 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1336 (const void *) vertShaderText,
1337 strlen(vertShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001338#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001339}
1340
1341static XGL_SHADER demo_prepare_fs(struct demo *demo)
1342{
Cody Northropacfb0492015-03-17 15:55:58 -06001343#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001344 void *fragShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001345 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001346
Cody Northropacfb0492015-03-17 15:55:58 -06001347 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001348
1349 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1350 fragShaderCode, size);
1351#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001352 static const char *fragShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001353 "#version 140\n"
1354 "#extension GL_ARB_separate_shader_objects : enable\n"
1355 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter988cc782015-02-25 15:13:35 -07001356 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001357 "\n"
1358 "layout (location = 0) in vec4 texcoord;\n"
1359 "void main() {\n"
1360 " gl_FragColor = texture(tex, texcoord.xy);\n"
1361 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001362
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001363 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1364 (const void *) fragShaderText,
1365 strlen(fragShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001366#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001367}
1368
1369static void demo_prepare_pipeline(struct demo *demo)
1370{
1371 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001372 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1373 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001374 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1375 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001376 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1377 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001378 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1379 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001380 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001381
1382 memset(&pipeline, 0, sizeof(pipeline));
1383 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001384 pipeline.pSetLayoutChain = demo->desc_layout_chain;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001385
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001386 memset(&ia, 0, sizeof(ia));
1387 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1388 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1389
1390 memset(&rs, 0, sizeof(rs));
1391 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001392 rs.fillMode = XGL_FILL_SOLID;
Mike Stroyanea3945c2015-03-19 14:29:04 -06001393 rs.cullMode = XGL_CULL_BACK;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001394 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001395
1396 memset(&cb, 0, sizeof(cb));
1397 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001398 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1399 memset(att_state, 0, sizeof(att_state));
1400 att_state[0].format = demo->format;
1401 att_state[0].channelWriteMask = 0xf;
1402 att_state[0].blendEnable = XGL_FALSE;
1403 cb.attachmentCount = 1;
1404 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001405
Tony Barbourfa6cac72015-01-16 14:27:35 -07001406 memset(&vp, 0, sizeof(vp));
1407 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001408 vp.numViewports = 1;
Mike Stroyan97e79b32015-03-24 15:13:34 -06001409 vp.clipOrigin = XGL_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001410
1411 memset(&ds, 0, sizeof(ds));
1412 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1413 ds.format = demo->depth.format;
1414 ds.depthTestEnable = XGL_TRUE;
1415 ds.depthWriteEnable = XGL_TRUE;
1416 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1417 ds.depthBoundsEnable = XGL_FALSE;
1418 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1419 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1420 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1421 ds.stencilTestEnable = XGL_FALSE;
1422 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001423
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001424 memset(&vs, 0, sizeof(vs));
1425 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1426 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001427 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001428 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001429
1430 memset(&fs, 0, sizeof(fs));
1431 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1432 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1433 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001434 assert(fs.shader.shader != NULL);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001435
1436 memset(&ms, 0, sizeof(ms));
1437 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1438 ms.sampleMask = 1;
1439 ms.multisampleEnable = XGL_FALSE;
1440 ms.samples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001441
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001442 pipeline.pNext = (const void *) &ia;
1443 ia.pNext = (const void *) &rs;
1444 rs.pNext = (const void *) &cb;
1445 cb.pNext = (const void *) &ms;
1446 ms.pNext = (const void *) &vp;
1447 vp.pNext = (const void *) &ds;
1448 ds.pNext = (const void *) &vs;
1449 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001450
1451 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1452 assert(!err);
1453
1454 xglDestroyObject(vs.shader.shader);
1455 xglDestroyObject(fs.shader.shader);
1456}
1457
1458static void demo_prepare_dynamic_states(struct demo *demo)
1459{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001460 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1461 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1462 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1463 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001464 XGL_RESULT err;
1465
Tony Barbourfa6cac72015-01-16 14:27:35 -07001466 memset(&viewport_create, 0, sizeof(viewport_create));
1467 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001468 viewport_create.viewportAndScissorCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001469 XGL_VIEWPORT viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001470 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001471 viewport.height = (float) demo->height;
1472 viewport.width = (float) demo->width;
1473 viewport.minDepth = (float) 0.0f;
1474 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001475 viewport_create.pViewports = &viewport;
1476 XGL_RECT scissor;
1477 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001478 scissor.extent.width = demo->width;
1479 scissor.extent.height = demo->height;
1480 scissor.offset.x = 0;
1481 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001482 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001483
1484 memset(&raster, 0, sizeof(raster));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001485 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001486 raster.pointSize = 1.0;
1487 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001488
1489 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001490 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001491 color_blend.blendConst[0] = 1.0f;
1492 color_blend.blendConst[1] = 1.0f;
1493 color_blend.blendConst[2] = 1.0f;
1494 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001495
1496 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001497 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001498 depth_stencil.minDepth = 0.0f;
1499 depth_stencil.maxDepth = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001500 depth_stencil.stencilBackRef = 0;
1501 depth_stencil.stencilFrontRef = 0;
1502 depth_stencil.stencilReadMask = 0xff;
1503 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001504
Tony Barbourfa6cac72015-01-16 14:27:35 -07001505 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001506 assert(!err);
1507
Tony Barbourfa6cac72015-01-16 14:27:35 -07001508 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001509 assert(!err);
1510
Tony Barbourfa6cac72015-01-16 14:27:35 -07001511 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001512 &color_blend, &demo->color_blend);
1513 assert(!err);
1514
Tony Barbourfa6cac72015-01-16 14:27:35 -07001515 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001516 &depth_stencil, &demo->depth_stencil);
1517 assert(!err);
1518}
1519
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001520static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001521{
1522 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1523 [0] = {
1524 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1525 .count = 1,
1526 },
1527 [1] = {
1528 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1529 .count = DEMO_TEXTURE_COUNT,
1530 },
1531 };
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001532 const XGL_DESCRIPTOR_POOL_CREATE_INFO descriptor_pool = {
1533 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001534 .pNext = NULL,
1535 .count = 2,
1536 .pTypeCount = type_counts,
1537 };
1538 XGL_RESULT err;
1539
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001540 err = xglCreateDescriptorPool(demo->device,
1541 XGL_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
1542 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001543 assert(!err);
1544}
1545
1546static void demo_prepare_descriptor_set(struct demo *demo)
1547{
Chia-I Wuf8385062015-01-04 16:27:24 +08001548 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1549 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1550 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1551 XGL_UPDATE_BUFFERS update_vs;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001552 const void *update_array[2] = { &update_vs, &update_fs };
Chia-I Wuf8385062015-01-04 16:27:24 +08001553 XGL_RESULT err;
1554 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001555 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001556
1557 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1558 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1559 view_info[i].pNext = NULL;
1560 view_info[i].view = demo->textures[i].view,
1561 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1562
Chia-I Wu7732cb22015-03-26 15:27:55 +08001563 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wuf8385062015-01-04 16:27:24 +08001564 combined_info[i].pImageView = &view_info[i];
1565 }
1566
1567 memset(&update_vs, 0, sizeof(update_vs));
1568 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1569 update_vs.pNext = &update_fs;
1570 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1571 update_vs.count = 1;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001572 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wuf8385062015-01-04 16:27:24 +08001573
1574 memset(&update_fs, 0, sizeof(update_fs));
1575 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001576 update_fs.binding = 1;
Chia-I Wuf8385062015-01-04 16:27:24 +08001577 update_fs.count = DEMO_TEXTURE_COUNT;
1578 update_fs.pSamplerImageViews = combined_info;
1579
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001580 err = xglAllocDescriptorSets(demo->desc_pool,
Chia-I Wuf8385062015-01-04 16:27:24 +08001581 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001582 1, &demo->desc_layout,
Chia-I Wuf8385062015-01-04 16:27:24 +08001583 &demo->desc_set, &count);
1584 assert(!err && count == 1);
1585
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001586 xglBeginDescriptorPoolUpdate(demo->device,
Chia-I Wuf8385062015-01-04 16:27:24 +08001587 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1588
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001589 xglClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set);
Chia-I Wu7732cb22015-03-26 15:27:55 +08001590 xglUpdateDescriptors(demo->desc_set, 2, update_array);
Chia-I Wuf8385062015-01-04 16:27:24 +08001591
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001592 xglEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wuf8385062015-01-04 16:27:24 +08001593}
1594
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001595static void demo_prepare(struct demo *demo)
1596{
1597 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1598 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1599 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001600 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001601 .flags = 0,
1602 };
1603 XGL_RESULT err;
1604
1605 demo_prepare_buffers(demo);
1606 demo_prepare_depth(demo);
1607 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001608 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001609
Chia-I Wuf8385062015-01-04 16:27:24 +08001610 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001611 demo_prepare_pipeline(demo);
1612 demo_prepare_dynamic_states(demo);
1613
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001614 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1615 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1616 assert(!err);
1617 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001618
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001619 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001620 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001621
1622
1623 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1624 demo->current_buffer = i;
1625 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1626 }
1627
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001628 /*
1629 * Prepare functions above may generate pipeline commands
1630 * that need to be flushed before beginning the render loop.
1631 */
1632 demo_flush_init_cmd(demo);
1633
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001634 demo->current_buffer = 0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001635}
1636
1637static void demo_handle_event(struct demo *demo,
1638 const xcb_generic_event_t *event)
1639{
Piers Daniell886be472015-02-23 16:23:13 -07001640 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001641 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001642 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001643 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001644 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001645 case XCB_CLIENT_MESSAGE:
1646 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1647 (*demo->atom_wm_delete_window).atom) {
1648 demo->quit = true;
1649 }
1650 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001651 case XCB_KEY_RELEASE:
1652 {
1653 const xcb_key_release_event_t *key =
1654 (const xcb_key_release_event_t *) event;
1655
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001656 switch (key->detail) {
1657 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001658 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001659 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001660 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001661 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001662 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001663 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001664 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001665 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001666 case 0x41:
1667 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001668 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001669 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001670 }
1671 break;
1672 default:
1673 break;
1674 }
1675}
1676
1677static void demo_run(struct demo *demo)
1678{
1679 xcb_flush(demo->connection);
1680
1681 while (!demo->quit) {
1682 xcb_generic_event_t *event;
1683
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001684 if (demo->pause) {
1685 event = xcb_wait_for_event(demo->connection);
1686 } else {
1687 event = xcb_poll_for_event(demo->connection);
1688 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001689 if (event) {
1690 demo_handle_event(demo, event);
1691 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001692 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001693
1694 // Wait for work to finish before updating MVP.
1695 xglDeviceWaitIdle(demo->device);
1696 demo_update_data_buffer(demo);
1697
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001698 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001699
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001700 // Wait for work to finish before updating MVP.
1701 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001702 }
1703}
1704
1705static void demo_create_window(struct demo *demo)
1706{
1707 uint32_t value_mask, value_list[32];
1708
1709 demo->window = xcb_generate_id(demo->connection);
1710
1711 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1712 value_list[0] = demo->screen->black_pixel;
1713 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1714 XCB_EVENT_MASK_EXPOSURE;
1715
1716 xcb_create_window(demo->connection,
1717 XCB_COPY_FROM_PARENT,
1718 demo->window, demo->screen->root,
1719 0, 0, demo->width, demo->height, 0,
1720 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1721 demo->screen->root_visual,
1722 value_mask, value_list);
1723
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001724 /* Magic code that will send notification when window is destroyed */
1725 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1726 "WM_PROTOCOLS");
1727 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1728
1729 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1730 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1731
1732 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1733 demo->window, (*reply).atom, 4, 32, 1,
1734 &(*demo->atom_wm_delete_window).atom);
1735 free(reply);
1736
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001737 xcb_map_window(demo->connection, demo->window);
1738}
1739
1740static void demo_init_xgl(struct demo *demo)
1741{
1742 const XGL_APPLICATION_INFO app = {
1743 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1744 .pNext = NULL,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001745 .pAppName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001746 .appVersion = 0,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001747 .pEngineName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001748 .engineVersion = 0,
Courtney Goeltzenleuchter211cc542015-02-23 17:40:15 -07001749 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001750 };
1751 const XGL_WSI_X11_CONNECTION_INFO connection = {
1752 .pConnection = demo->connection,
1753 .root = demo->screen->root,
1754 .provider = 0,
1755 };
1756 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1757 .queueNodeIndex = 0,
1758 .queueCount = 1,
1759 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001760 const char *ext_names[] = {
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001761 "XGL_WSI_X11",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001762 };
1763 const XGL_DEVICE_CREATE_INFO device = {
1764 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1765 .pNext = NULL,
1766 .queueRecordCount = 1,
1767 .pRequestedQueues = &queue,
1768 .extensionCount = 1,
1769 .ppEnabledExtensionNames = ext_names,
1770 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1771 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1772 };
1773 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001774 uint32_t gpu_count;
1775 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001776 size_t data_size;
1777 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001778
Jon Ashburn92e80132015-01-29 15:47:01 -07001779 err = xglCreateInstance(&app, NULL, &demo->inst);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001780 if (err == XGL_ERROR_INCOMPATIBLE_DRIVER) {
1781 printf("Cannot find a compatible Vulkan installable client driver "
1782 "(ICD).\nExiting ...\n");
1783 fflush(stdout);
1784 exit(1);
1785 } else {
1786 assert(!err);
1787 }
Jon Ashburn92e80132015-01-29 15:47:01 -07001788 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001789 assert(!err && gpu_count == 1);
1790
1791 for (i = 0; i < device.extensionCount; i++) {
1792 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1793 assert(!err);
1794 }
1795
1796 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1797 assert(!err);
1798
1799 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1800 assert(!err);
1801
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001802 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
1803 &data_size, NULL);
1804 assert(!err);
1805
1806 demo->gpu_props = (XGL_PHYSICAL_GPU_PROPERTIES *) malloc(data_size);
1807 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
1808 &data_size, demo->gpu_props);
1809 assert(!err);
1810
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001811 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1812 &data_size, NULL);
1813 assert(!err);
1814
1815 demo->queue_props = (XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *) malloc(data_size);
1816 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1817 &data_size, demo->queue_props);
1818 assert(!err);
Ian Elliott0fd7b6e2015-04-03 09:54:13 -06001819 queue_count = (uint32_t)(data_size / sizeof(XGL_PHYSICAL_GPU_QUEUE_PROPERTIES));
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001820 assert(queue_count >= 1);
1821
1822 for (i = 0; i < queue_count; i++) {
1823 if (demo->queue_props[i].queueFlags & XGL_QUEUE_GRAPHICS_BIT)
1824 break;
1825 }
1826 assert(i < queue_count);
1827 demo->graphics_queue_node_index = i;
1828
1829 err = xglGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001830 0, &demo->queue);
1831 assert(!err);
1832}
1833
1834static void demo_init_connection(struct demo *demo)
1835{
1836 const xcb_setup_t *setup;
1837 xcb_screen_iterator_t iter;
1838 int scr;
1839
1840 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001841 if (demo->connection == NULL) {
1842 printf("Cannot find a compatible Vulkan installable client driver "
1843 "(ICD).\nExiting ...\n");
1844 fflush(stdout);
1845 exit(1);
1846 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001847
1848 setup = xcb_get_setup(demo->connection);
1849 iter = xcb_setup_roots_iterator(setup);
1850 while (scr-- > 0)
1851 xcb_screen_next(&iter);
1852
1853 demo->screen = iter.data;
1854}
1855
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001856static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001857{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001858 vec3 eye = {0.0f, 3.0f, 5.0f};
1859 vec3 origin = {0, 0, 0};
1860 vec3 up = {0.0f, -1.0f, 0.0};
1861
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001862 memset(demo, 0, sizeof(*demo));
1863
Piers Daniell886be472015-02-23 16:23:13 -07001864 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001865 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1866 demo->use_staging_buffer = true;
1867 }
1868
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001869 demo_init_connection(demo);
1870 demo_init_xgl(demo);
1871
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001872 demo->width = 500;
1873 demo->height = 500;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001874 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001875
1876 demo->spin_angle = 0.01f;
1877 demo->spin_increment = 0.01f;
1878 demo->pause = false;
1879
Piers Daniell886be472015-02-23 16:23:13 -07001880 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001881 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1882 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001883}
1884
1885static void demo_cleanup(struct demo *demo)
1886{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001887 uint32_t i, j;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001888
Chia-I Wuf8385062015-01-04 16:27:24 +08001889 xglDestroyObject(demo->desc_set);
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001890 xglDestroyObject(demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001891
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001892 xglDestroyObject(demo->viewport);
1893 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001894 xglDestroyObject(demo->color_blend);
1895 xglDestroyObject(demo->depth_stencil);
1896
1897 xglDestroyObject(demo->pipeline);
Chia-I Wu7732cb22015-03-26 15:27:55 +08001898 xglDestroyObject(demo->desc_layout_chain);
Chia-I Wu6e68a892015-02-23 10:41:08 -07001899 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001900
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001901 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1902 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001903 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001904 xglDestroyObject(demo->textures[i].image);
Jon Ashburna9ae3832015-01-16 09:37:43 -07001905 for (j = 0; j < demo->textures[i].num_mem; j++)
1906 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001907 xglDestroyObject(demo->textures[i].sampler);
1908 }
1909
1910 xglDestroyObject(demo->depth.view);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001911 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001912 xglDestroyObject(demo->depth.image);
Jon Ashburna9ae3832015-01-16 09:37:43 -07001913 for (j = 0; j < demo->depth.num_mem; j++)
1914 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001915
1916 xglDestroyObject(demo->uniform_data.view);
1917 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001918 xglDestroyObject(demo->uniform_data.buf);
1919 for (j = 0; j < demo->uniform_data.num_mem; j++)
1920 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001921
1922 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wu68040a42014-11-07 14:30:34 +08001923 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001924 xglDestroyObject(demo->buffers[i].view);
1925 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001926 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001927 }
1928
1929 xglDestroyDevice(demo->device);
Jon Ashburn92e80132015-01-29 15:47:01 -07001930 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001931
1932 xcb_destroy_window(demo->connection, demo->window);
1933 xcb_disconnect(demo->connection);
1934}
1935
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001936int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001937{
1938 struct demo demo;
1939
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001940 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001941
1942 demo_prepare(&demo);
1943 demo_create_window(&demo);
1944 demo_run(&demo);
1945
1946 demo_cleanup(&demo);
1947
1948 return 0;
1949}