blob: 91751c39574c7433d75c47e29a22fdc7cbabbb5d [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);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -0600309 xglQueueAddMemReference(demo->queue, mem[i]);
310 }
311}
312
313static void demo_remove_mem_refs(
314 struct demo *demo,
315 int num_refs, XGL_GPU_MEMORY *mem)
316{
317 for (int i = 0; i < num_refs; i++) {
318 xglQueueRemoveMemReference(demo->queue, mem[i]);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600319 }
320}
321
322static void demo_set_image_layout(
323 struct demo *demo,
324 XGL_IMAGE image,
325 XGL_IMAGE_LAYOUT old_image_layout,
326 XGL_IMAGE_LAYOUT new_image_layout)
327{
328 XGL_RESULT err;
329
330 if (demo->cmd == XGL_NULL_HANDLE) {
331 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
332 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
333 .pNext = NULL,
334 .queueNodeIndex = demo->graphics_queue_node_index,
335 .flags = 0,
336 };
337
338 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
339 assert(!err);
340
341 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
342 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
343 .pNext = NULL,
344 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
345 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
346 };
347 err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info);
348 }
349
350 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
351 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
352 .pNext = NULL,
353 .outputMask = 0,
354 .inputMask = 0,
355 .oldLayout = old_image_layout,
356 .newLayout = new_image_layout,
357 .image = image,
358 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
359 };
360
361 if (new_image_layout == XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
362 /* Make sure anything that was copying from this image has completed */
363 image_memory_barrier.inputMask = XGL_MEMORY_INPUT_COPY_BIT;
364 }
365
366 if (new_image_layout == XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
367 /* Make sure any Copy or CPU writes to image are flushed */
368 image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT | XGL_MEMORY_OUTPUT_CPU_WRITE_BIT;
369 }
370
371 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
372
373 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_TOP_OF_PIPE };
374
375 XGL_PIPELINE_BARRIER pipeline_barrier;
376 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
377 pipeline_barrier.pNext = NULL;
378 pipeline_barrier.eventCount = 1;
379 pipeline_barrier.pEvents = set_events;
380 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
381 pipeline_barrier.memBarrierCount = 1;
382 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
383
384 xglCmdPipelineBarrier(demo->cmd, &pipeline_barrier);
385}
386
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700387static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600388{
389 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
390 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan55658c22014-12-04 11:08:39 +0000391 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600392 };
393 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
394 .view = demo->depth.view,
Mike Stroyan55658c22014-12-04 11:08:39 +0000395 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600396 };
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600397 const XGL_CLEAR_COLOR clear_color = {
398 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
399 .useRawValue = false,
400 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600401 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600402 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn53d27af2014-12-31 17:08:35 -0700403 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
404 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600405 .pNext = NULL,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700406 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
407 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
408 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600409 XGL_RESULT err;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700410 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
411 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
412 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
413 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
414 .pNext = NULL,
415 .colorAttachmentCount = 1,
416 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
417 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
418 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600419 .width = demo->width,
420 .height = demo->height,
421 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700422 };
423 XGL_RENDER_PASS_CREATE_INFO rp_info;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600424 XGL_RENDER_PASS_BEGIN rp_begin;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700425
426 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600427 err = xglCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700428 assert(!err);
429 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
430 rp_info.renderArea.extent.width = demo->width;
431 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600432 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
433 rp_info.pColorFormats = &demo->format;
434 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700435 rp_info.pColorLoadOps = &load_op;
436 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600437 rp_info.pColorLoadClearValues = &clear_color;
438 rp_info.depthStencilFormat = XGL_FMT_D16_UNORM;
439 rp_info.depthStencilLayout = depth_stencil.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700440 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600441 rp_info.depthLoadClearValue = clear_depth;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700442 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
443 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600444 rp_info.stencilLoadClearValue = 0;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700445 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600446 err = xglCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700447 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600448
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700449 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600450 assert(!err);
451
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700452 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600453 demo->pipeline);
Chia-I Wu862c5572015-03-28 15:23:55 +0800454 xglCmdBindDescriptorSets(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
455 demo->desc_layout_chain, 0, 1, &demo->desc_set, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600456
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700457 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
458 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
459 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600460 demo->color_blend);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700461 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600462 demo->depth_stencil);
463
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600464 xglCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600465 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
466 clear_range.baseMipLevel = 0;
467 clear_range.mipLevels = 1;
468 clear_range.baseArraySlice = 0;
469 clear_range.arraySize = 1;
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700470 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600471 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600472 XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600473 clear_color, 1, &clear_range);
474
475 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700476 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600477 XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600478 clear_depth, 0, 1, &clear_range);
479
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700480 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600481 xglCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600482
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700483 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600484 assert(!err);
Courtney Goeltzenleuchter04fc2fb2015-02-25 17:53:18 -0700485
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600486 xglDestroyObject(rp_begin.renderPass);
487 xglDestroyObject(rp_begin.framebuffer);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600488}
489
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600490
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600491void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600492{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600493 mat4x4 MVP, Model, VP;
494 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600495 uint8_t *pData;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600496 XGL_RESULT err;
497
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600498 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600499
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600500 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600501 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700502 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600503 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600504
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700505 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600506 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600507 assert(!err);
508
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600509 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600510
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700511 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600512 assert(!err);
513}
514
515static void demo_draw(struct demo *demo)
516{
517 const XGL_WSI_X11_PRESENT_INFO present = {
518 .destWindow = demo->window,
519 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wu68040a42014-11-07 14:30:34 +0800520 .async = true,
521 .flip = false,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600522 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800523 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600524 XGL_RESULT err;
525
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600526 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wu68040a42014-11-07 14:30:34 +0800527 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
528
Mark Lobodzinski15427102015-02-18 16:38:17 -0600529 uint32_t i, idx = 0;
530 XGL_MEMORY_REF *memRefs;
531 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
532 demo->depth.num_mem +
533 demo->textures[0].num_mem +
534 demo->uniform_data.num_mem));
535 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
536 memRefs[idx].mem = demo->depth.mem[i];
537 memRefs[idx].flags = 0;
538 }
539 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
540 memRefs[idx].mem = demo->textures[0].mem[i];
541 memRefs[idx].flags = 0;
542 }
543 memRefs[idx].mem = demo->buffers[0].mem;
544 memRefs[idx++].flags = 0;
545 memRefs[idx].mem = demo->buffers[1].mem;
546 memRefs[idx++].flags = 0;
547 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
548 memRefs[idx].mem = demo->uniform_data.mem[i];
549 memRefs[idx].flags = 0;
550 }
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700551 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Piers Daniell886be472015-02-23 16:23:13 -0700552 idx, memRefs, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600553 assert(!err);
554
Chia-I Wu68040a42014-11-07 14:30:34 +0800555 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600556 assert(!err);
557
558 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
559}
560
561static void demo_prepare_buffers(struct demo *demo)
562{
563 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
564 .format = demo->format,
565 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
566 .extent = {
567 .width = demo->width,
568 .height = demo->height,
569 },
570 .flags = 0,
571 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800572 const XGL_FENCE_CREATE_INFO fence = {
573 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
574 .pNext = NULL,
575 .flags = 0,
576 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600577 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600578 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600579
580 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
581 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
582 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
583 .pNext = NULL,
584 .format = demo->format,
585 .mipLevel = 0,
586 .baseArraySlice = 0,
587 .arraySize = 1,
588 };
589
590 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
591 &demo->buffers[i].image, &demo->buffers[i].mem);
592 assert(!err);
593
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -0600594 demo_add_mem_refs(demo, XGL_MEMORY_REF_READ_ONLY_BIT, 1, &demo->buffers[i].mem);
595
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600596 demo_set_image_layout(demo, demo->buffers[i].image,
597 XGL_IMAGE_LAYOUT_UNDEFINED,
598 XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
599
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600600 color_attachment_view.image = demo->buffers[i].image;
601
602 err = xglCreateColorAttachmentView(demo->device,
603 &color_attachment_view, &demo->buffers[i].view);
604 assert(!err);
Chia-I Wu68040a42014-11-07 14:30:34 +0800605
606 err = xglCreateFence(demo->device,
607 &fence, &demo->buffers[i].fence);
608 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600609 }
610}
611
612static void demo_prepare_depth(struct demo *demo)
613{
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700614 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600615 const XGL_IMAGE_CREATE_INFO image = {
616 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
617 .pNext = NULL,
618 .imageType = XGL_IMAGE_2D,
619 .format = depth_format,
620 .extent = { demo->width, demo->height, 1 },
621 .mipLevels = 1,
622 .arraySize = 1,
623 .samples = 1,
624 .tiling = XGL_OPTIMAL_TILING,
625 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
626 .flags = 0,
627 };
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700628 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
629 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
630 .pNext = NULL,
631 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600632 XGL_MEMORY_ALLOC_INFO mem_alloc = {
633 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700634 .pNext = &img_alloc,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600635 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -0700636 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburn32769172015-01-20 15:06:59 -0700637 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600638 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
639 };
640 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
641 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
642 .pNext = NULL,
643 .image = XGL_NULL_HANDLE,
644 .mipLevel = 0,
645 .baseArraySlice = 0,
646 .arraySize = 1,
647 .flags = 0,
648 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700649 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600650 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700651 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600652 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600653 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600654 uint32_t num_allocations = 0;
655 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600656
657 demo->depth.format = depth_format;
658
659 /* create image */
660 err = xglCreateImage(demo->device, &image,
661 &demo->depth.image);
662 assert(!err);
663
Jon Ashburna9ae3832015-01-16 09:37:43 -0700664 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
665 assert(!err && num_alloc_size == sizeof(num_allocations));
666 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
667 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
668 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600669 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700670 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
671 &mem_reqs_size, mem_reqs);
672 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700673 err = xglGetObjectInfo(demo->depth.image,
674 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
675 &img_reqs_size, &img_reqs);
676 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
677 img_alloc.usage = img_reqs.usage;
678 img_alloc.formatClass = img_reqs.formatClass;
679 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600680 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -0700681 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600682
Jon Ashburna9ae3832015-01-16 09:37:43 -0700683 /* allocate memory */
684 err = xglAllocMemory(demo->device, &mem_alloc,
685 &(demo->depth.mem[i]));
686 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600687
Jon Ashburna9ae3832015-01-16 09:37:43 -0700688 /* bind memory */
689 err = xglBindObjectMemory(demo->depth.image, i,
690 demo->depth.mem[i], 0);
691 assert(!err);
692 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600693
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600694 demo_set_image_layout(demo, demo->depth.image,
695 XGL_IMAGE_LAYOUT_UNDEFINED,
696 XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
697
698 demo_add_mem_refs(demo, XGL_MEMORY_REF_READ_ONLY_BIT, num_allocations, demo->depth.mem);
699
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600700 /* create image view */
701 view.image = demo->depth.image;
702 err = xglCreateDepthStencilView(demo->device, &view,
703 &demo->depth.view);
704 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600705}
706
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600707/** loadTexture
708 * loads a png file into an memory object, using cstdio , libpng.
709 *
710 * \param demo : Needed to access XGL calls
711 * \param filename : the png file to be loaded
712 * \param width : width of png, to be updated as a side effect of this function
713 * \param height : height of png, to be updated as a side effect of this function
714 *
715 * \return bool : an opengl texture id. true if successful?,
716 * should be validated by the client of this function.
717 *
718 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
719 * Modified to copy image to memory
720 *
721 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700722bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600723 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600724 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600725{
726 //header for testing if it is a png
727 png_byte header[8];
Ian Elliott642f8922015-02-13 14:29:21 -0700728 int is_png, bit_depth, color_type,rowbytes;
729 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600730 png_structp png_ptr;
731 png_infop info_ptr, end_info;
732 png_byte *image_data;
733 png_bytep *row_pointers;
734
735 //open file as binary
736 FILE *fp = fopen(filename, "rb");
737 if (!fp) {
738 return false;
739 }
740
741 //read the header
742 fread(header, 1, 8, fp);
743
744 //test if png
745 is_png = !png_sig_cmp(header, 0, 8);
746 if (!is_png) {
747 fclose(fp);
748 return false;
749 }
750
751 //create png struct
752 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
753 NULL, NULL);
754 if (!png_ptr) {
755 fclose(fp);
756 return (false);
757 }
758
759 //create png info struct
760 info_ptr = png_create_info_struct(png_ptr);
761 if (!info_ptr) {
762 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
763 fclose(fp);
764 return (false);
765 }
766
767 //create png info struct
768 end_info = png_create_info_struct(png_ptr);
769 if (!end_info) {
770 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
771 fclose(fp);
772 return (false);
773 }
774
775 //png error stuff, not sure libpng man suggests this.
776 if (setjmp(png_jmpbuf(png_ptr))) {
777 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
778 fclose(fp);
779 return (false);
780 }
781
782 //init png reading
783 png_init_io(png_ptr, fp);
784
785 //let libpng know you already read the first 8 bytes
786 png_set_sig_bytes(png_ptr, 8);
787
788 // read all the info up to the image data
789 png_read_info(png_ptr, info_ptr);
790
791 // get info about png
792 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
793 NULL, NULL, NULL);
794
795 //update width and height based on png info
796 *width = twidth;
797 *height = theight;
798
799 // Require that incoming texture be 8bits per color component
800 // and 4 components (RGBA).
801 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
802 png_get_channels(png_ptr, info_ptr) != 4) {
803 return false;
804 }
805
806 if (rgba_data == NULL) {
807 // If data pointer is null, we just want the width & height
808 // clean up memory and close stuff
809 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
810 fclose(fp);
811
812 return true;
813 }
814
815 // Update the png info struct.
816 png_read_update_info(png_ptr, info_ptr);
817
818 // Row size in bytes.
819 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
820
821 // Allocate the image_data as a big block, to be given to opengl
822 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
823 if (!image_data) {
824 //clean up memory and close stuff
825 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
826 fclose(fp);
827 return false;
828 }
829
830 // row_pointers is for pointing to image_data for reading the png with libpng
831 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
832 if (!row_pointers) {
833 //clean up memory and close stuff
834 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
835 // delete[] image_data;
836 fclose(fp);
837 return false;
838 }
839 // set the individual row_pointers to point at the correct offsets of image_data
840 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700841 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600842
843 // read the png into image_data through row_pointers
844 png_read_image(png_ptr, row_pointers);
845
846 // clean up memory and close stuff
847 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
848 free(row_pointers);
849 free(image_data);
850 fclose(fp);
851
852 return true;
853}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600854
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700855static void demo_prepare_texture_image(struct demo *demo,
856 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600857 struct texture_object *tex_obj,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700858 XGL_IMAGE_TILING tiling,
859 XGL_FLAGS mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600860{
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700861 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600862 int32_t tex_width;
863 int32_t tex_height;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600864 XGL_RESULT err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700865
866 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
867 assert(err);
868
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600869 tex_obj->tex_width = tex_width;
870 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700871
872 const XGL_IMAGE_CREATE_INFO image_create_info = {
873 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
874 .pNext = NULL,
875 .imageType = XGL_IMAGE_2D,
876 .format = tex_format,
877 .extent = { tex_width, tex_height, 1 },
878 .mipLevels = 1,
879 .arraySize = 1,
880 .samples = 1,
881 .tiling = tiling,
882 .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
883 .flags = 0,
884 };
Piers Daniell886be472015-02-23 16:23:13 -0700885 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
886 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
887 .pNext = NULL,
888 };
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700889 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
890 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
Piers Daniell886be472015-02-23 16:23:13 -0700891 .pNext = &buf_alloc,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700892 };
893 XGL_MEMORY_ALLOC_INFO mem_alloc = {
894 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
895 .pNext = &img_alloc,
896 .allocationSize = 0,
897 .memProps = mem_props,
898 .memType = XGL_MEMORY_TYPE_IMAGE,
899 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
900 };
901
902 XGL_MEMORY_REQUIREMENTS *mem_reqs;
903 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Piers Daniell886be472015-02-23 16:23:13 -0700904 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
905 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700906 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
907 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
908 uint32_t num_allocations = 0;
909 size_t num_alloc_size = sizeof(num_allocations);
910
911 err = xglCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600912 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700913 assert(!err);
914
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600915 err = xglGetObjectInfo(tex_obj->image,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700916 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
917 &num_alloc_size, &num_allocations);
918 assert(!err && num_alloc_size == sizeof(num_allocations));
919 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600920 tex_obj->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
921 err = xglGetObjectInfo(tex_obj->image,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700922 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
923 &mem_reqs_size, mem_reqs);
924 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600925 err = xglGetObjectInfo(tex_obj->image,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700926 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
927 &img_reqs_size, &img_reqs);
928 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
929 img_alloc.usage = img_reqs.usage;
930 img_alloc.formatClass = img_reqs.formatClass;
931 img_alloc.samples = img_reqs.samples;
932 mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
933 for (uint32_t j = 0; j < num_allocations; j ++) {
Courtney Goeltzenleuchter6e5e81c2015-02-25 11:48:28 -0700934 mem_alloc.memType = mem_reqs[j].memType;
Piers Daniell886be472015-02-23 16:23:13 -0700935 mem_alloc.allocationSize = mem_reqs[j].size;
936
937 if (mem_alloc.memType == XGL_MEMORY_TYPE_BUFFER) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600938 err = xglGetObjectInfo(tex_obj->image,
Piers Daniell886be472015-02-23 16:23:13 -0700939 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
940 &buf_reqs_size, &buf_reqs);
941 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
942 buf_alloc.usage = buf_reqs.usage;
943 img_alloc.pNext = &buf_alloc;
944 } else {
945 img_alloc.pNext = 0;
946 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700947
948 /* allocate memory */
949 err = xglAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600950 &(tex_obj->mem[j]));
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700951 assert(!err);
952
953 /* bind memory */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600954 err = xglBindObjectMemory(tex_obj->image, j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700955 assert(!err);
956 }
957 free(mem_reqs);
958 mem_reqs = NULL;
959
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600960 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700961
962 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
963 const XGL_IMAGE_SUBRESOURCE subres = {
964 .aspect = XGL_IMAGE_ASPECT_COLOR,
965 .mipLevel = 0,
966 .arraySlice = 0,
967 };
968 XGL_SUBRESOURCE_LAYOUT layout;
969 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
970 void *data;
971
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600972 err = xglGetImageSubresourceInfo(tex_obj->image, &subres,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700973 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
974 &layout_size, &layout);
975 assert(!err && layout_size == sizeof(layout));
976 /* Linear texture must be within a single memory object */
977 assert(num_allocations == 1);
978
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600979 err = xglMapMemory(tex_obj->mem[0], 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700980 assert(!err);
981
982 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
983 fprintf(stderr, "Error loading texture: %s\n", filename);
984 }
985
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600986 err = xglUnmapMemory(tex_obj->mem[0]);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700987 assert(!err);
988 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600989
990 tex_obj->imageLayout = XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
991 demo_set_image_layout(demo, tex_obj->image,
992 XGL_IMAGE_LAYOUT_UNDEFINED,
993 tex_obj->imageLayout);
994 /* 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 -0700995}
996
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600997static void demo_destroy_texture_image(struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700998{
999 /* clean up staging resources */
1000 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
1001 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
1002 xglFreeMemory(tex_objs->mem[j]);
1003 }
1004
1005 free(tex_objs->mem);
1006 xglDestroyObject(tex_objs->image);
1007}
1008
1009static void demo_prepare_textures(struct demo *demo)
1010{
1011 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
1012 XGL_FORMAT_PROPERTIES props;
1013 size_t size = sizeof(props);
1014 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001015 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001016
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001017 err = xglGetFormatInfo(demo->device, tex_format,
1018 XGL_INFO_TYPE_FORMAT_PROPERTIES,
1019 &size, &props);
1020 assert(!err);
1021
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001022 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001023
1024 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
1025 /* Device can texture using linear textures */
1026 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
1027 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001028 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001029 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001030 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001031
1032 memset(&staging_texture, 0, sizeof(staging_texture));
1033 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
1034 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
1035
1036 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
1037 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
1038
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001039 demo_set_image_layout(demo, staging_texture.image,
1040 staging_texture.imageLayout,
1041 XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001042
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001043 demo_set_image_layout(demo, demo->textures[i].image,
1044 demo->textures[i].imageLayout,
1045 XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001046
1047 XGL_IMAGE_COPY copy_region = {
1048 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
1049 .srcOffset = { 0, 0, 0 },
1050 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
1051 .destOffset = { 0, 0, 0 },
1052 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1053 };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001054 xglCmdCopyImage(demo->cmd,
1055 staging_texture.image, XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1056 demo->textures[i].image, XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001057 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001058
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001059 demo_add_mem_refs(demo, XGL_MEMORY_REF_READ_ONLY_BIT, staging_texture.num_mem, staging_texture.mem);
1060 demo_add_mem_refs(demo, 0, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001061
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001062 demo_set_image_layout(demo, demo->textures[i].image,
1063 XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
1064 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001065
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001066 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001067
1068 demo_destroy_texture_image(&staging_texture);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06001069 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001070 } else {
1071 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
1072 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1073 }
1074
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001075 const XGL_SAMPLER_CREATE_INFO sampler = {
1076 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
1077 .pNext = NULL,
1078 .magFilter = XGL_TEX_FILTER_NEAREST,
1079 .minFilter = XGL_TEX_FILTER_NEAREST,
1080 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001081 .addressU = XGL_TEX_ADDRESS_CLAMP,
1082 .addressV = XGL_TEX_ADDRESS_CLAMP,
1083 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001084 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001085 .maxAnisotropy = 1,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001086 .compareFunc = XGL_COMPARE_NEVER,
1087 .minLod = 0.0f,
1088 .maxLod = 0.0f,
1089 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
1090 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001091
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001092 XGL_IMAGE_VIEW_CREATE_INFO view = {
1093 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1094 .pNext = NULL,
1095 .image = XGL_NULL_HANDLE,
1096 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001097 .format = tex_format,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001098 .channels = { XGL_CHANNEL_SWIZZLE_R,
1099 XGL_CHANNEL_SWIZZLE_G,
1100 XGL_CHANNEL_SWIZZLE_B,
1101 XGL_CHANNEL_SWIZZLE_A, },
1102 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
1103 .minLod = 0.0f,
1104 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001105
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001106 /* create sampler */
1107 err = xglCreateSampler(demo->device, &sampler,
1108 &demo->textures[i].sampler);
1109 assert(!err);
1110
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001111 /* create image view */
1112 view.image = demo->textures[i].image;
1113 err = xglCreateImageView(demo->device, &view,
1114 &demo->textures[i].view);
1115 assert(!err);
1116 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001117}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001118
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001119void demo_prepare_cube_data_buffer(struct demo *demo)
1120{
Chia-I Wu714df452015-01-01 07:55:04 +08001121 XGL_BUFFER_CREATE_INFO buf_info;
1122 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001123 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1124 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1125 .pNext = NULL,
1126 };
1127 XGL_MEMORY_ALLOC_INFO alloc_info = {
1128 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1129 .pNext = &buf_alloc,
1130 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -07001131 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburn32769172015-01-20 15:06:59 -07001132 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001133 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1134 };
1135 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001136 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001137 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001138 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1139 uint32_t num_allocations = 0;
1140 size_t num_alloc_size = sizeof(num_allocations);
1141 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001142 int i;
1143 mat4x4 MVP, VP;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001144 XGL_RESULT err;
1145 struct xgltexcube_vs_uniform data;
1146
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001147 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001148 mat4x4_mul(MVP, VP, demo->model_matrix);
1149 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001150// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001151
1152 for (i=0; i<12*3; i++) {
1153 data.position[i][0] = g_vertex_buffer_data[i*3];
1154 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1155 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1156 data.position[i][3] = 1.0f;
1157 data.attr[i][0] = g_uv_buffer_data[2*i];
1158 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1159 data.attr[i][2] = 0;
1160 data.attr[i][3] = 0;
1161 }
1162
Chia-I Wu714df452015-01-01 07:55:04 +08001163 memset(&buf_info, 0, sizeof(buf_info));
1164 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1165 buf_info.size = sizeof(data);
1166 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1167 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1168 assert(!err);
1169
1170 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001171 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1172 &num_alloc_size, &num_allocations);
1173 assert(!err && num_alloc_size == sizeof(num_allocations));
1174 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1175 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1176 demo->uniform_data.num_mem = num_allocations;
1177 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu714df452015-01-01 07:55:04 +08001178 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001179 &mem_reqs_size, mem_reqs);
1180 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1181 err = xglGetObjectInfo(demo->uniform_data.buf,
1182 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1183 &buf_reqs_size, &buf_reqs);
1184 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1185 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001186 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001187 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu714df452015-01-01 07:55:04 +08001188
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001189 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1190 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001191
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001192 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001193 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001194
Piers Daniell886be472015-02-23 16:23:13 -07001195 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001196
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001197 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1198 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001199
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001200 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1201 demo->uniform_data.mem[i], 0);
1202 assert(!err);
1203 }
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06001204 demo_add_mem_refs(demo, XGL_MEMORY_REF_READ_ONLY_BIT, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu714df452015-01-01 07:55:04 +08001205
1206 memset(&view_info, 0, sizeof(view_info));
1207 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1208 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu28fb4ce2015-01-16 22:31:25 +08001209 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001210 view_info.offset = 0;
1211 view_info.range = sizeof(data);
1212
1213 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1214 assert(!err);
1215
1216 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1217 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001218}
1219
Chia-I Wuf8385062015-01-04 16:27:24 +08001220static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001221{
Chia-I Wufc9d9132015-03-26 15:04:41 +08001222 const XGL_DESCRIPTOR_SET_LAYOUT_BINDING layout_bindings[2] = {
1223 [0] = {
1224 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1225 .count = 1,
1226 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001227 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001228 },
1229 [1] = {
1230 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1231 .count = DEMO_TEXTURE_COUNT,
1232 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001233 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001234 },
1235 };
1236 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001237 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1238 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001239 .count = 2,
1240 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001241 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001242 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001243
Chia-I Wuf8385062015-01-04 16:27:24 +08001244 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001245 &descriptor_layout, &demo->desc_layout);
1246 assert(!err);
1247
1248 err = xglCreateDescriptorSetLayoutChain(demo->device,
1249 1, &demo->desc_layout, &demo->desc_layout_chain);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001250 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001251}
1252
1253static XGL_SHADER demo_prepare_shader(struct demo *demo,
1254 XGL_PIPELINE_SHADER_STAGE stage,
1255 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001256 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001257{
1258 XGL_SHADER_CREATE_INFO createInfo;
1259 XGL_SHADER shader;
1260 XGL_RESULT err;
1261
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001262
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001263 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1264 createInfo.pNext = NULL;
1265
Cody Northropacfb0492015-03-17 15:55:58 -06001266#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001267 createInfo.codeSize = size;
1268 createInfo.pCode = code;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001269 createInfo.flags = 0;
1270
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001271 err = xglCreateShader(demo->device, &createInfo, &shader);
1272 if (err) {
1273 free((void *) createInfo.pCode);
1274 }
1275#else
Cody Northropacfb0492015-03-17 15:55:58 -06001276 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001277 // to the driver "under the covers"
1278 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1279 createInfo.pCode = malloc(createInfo.codeSize);
1280 createInfo.flags = 0;
1281
1282 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northropacfb0492015-03-17 15:55:58 -06001283 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001284 ((uint32_t *) createInfo.pCode)[1] = 0;
1285 ((uint32_t *) createInfo.pCode)[2] = stage;
1286 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1287
1288 err = xglCreateShader(demo->device, &createInfo, &shader);
1289 if (err) {
1290 free((void *) createInfo.pCode);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001291 return NULL;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001292 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001293#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001294
1295 return shader;
1296}
1297
Cody Northropacfb0492015-03-17 15:55:58 -06001298char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001299{
1300 long int size;
1301 void *shader_code;
1302
1303 FILE *fp = fopen(filename, "rb");
1304 if (!fp) return NULL;
1305
1306 fseek(fp, 0L, SEEK_END);
1307 size = ftell(fp);
1308
1309 fseek(fp, 0L, SEEK_SET);
1310
1311 shader_code = malloc(size);
1312 fread(shader_code, size, 1, fp);
1313
1314 *psize = size;
1315
1316 return shader_code;
1317}
1318
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001319static XGL_SHADER demo_prepare_vs(struct demo *demo)
1320{
Cody Northropacfb0492015-03-17 15:55:58 -06001321#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001322 void *vertShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001323 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001324
Cody Northropacfb0492015-03-17 15:55:58 -06001325 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001326
1327 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1328 vertShaderCode, size);
1329#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001330 static const char *vertShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001331 "#version 140\n"
1332 "#extension GL_ARB_separate_shader_objects : enable\n"
1333 "#extension GL_ARB_shading_language_420pack : enable\n"
1334 "\n"
1335 "layout(binding = 0) uniform buf {\n"
1336 " mat4 MVP;\n"
1337 " vec4 position[12*3];\n"
1338 " vec4 attr[12*3];\n"
1339 "} ubuf;\n"
1340 "\n"
1341 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001342 "\n"
1343 "void main() \n"
1344 "{\n"
1345 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001346 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1347 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001348
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001349 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1350 (const void *) vertShaderText,
1351 strlen(vertShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001352#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001353}
1354
1355static XGL_SHADER demo_prepare_fs(struct demo *demo)
1356{
Cody Northropacfb0492015-03-17 15:55:58 -06001357#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001358 void *fragShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001359 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001360
Cody Northropacfb0492015-03-17 15:55:58 -06001361 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001362
1363 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1364 fragShaderCode, size);
1365#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001366 static const char *fragShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001367 "#version 140\n"
1368 "#extension GL_ARB_separate_shader_objects : enable\n"
1369 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter988cc782015-02-25 15:13:35 -07001370 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001371 "\n"
1372 "layout (location = 0) in vec4 texcoord;\n"
1373 "void main() {\n"
1374 " gl_FragColor = texture(tex, texcoord.xy);\n"
1375 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001376
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001377 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1378 (const void *) fragShaderText,
1379 strlen(fragShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001380#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001381}
1382
1383static void demo_prepare_pipeline(struct demo *demo)
1384{
1385 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001386 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1387 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001388 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1389 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001390 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1391 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001392 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1393 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001394 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001395
1396 memset(&pipeline, 0, sizeof(pipeline));
1397 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001398 pipeline.pSetLayoutChain = demo->desc_layout_chain;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001399
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001400 memset(&ia, 0, sizeof(ia));
1401 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1402 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1403
1404 memset(&rs, 0, sizeof(rs));
1405 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001406 rs.fillMode = XGL_FILL_SOLID;
Mike Stroyanea3945c2015-03-19 14:29:04 -06001407 rs.cullMode = XGL_CULL_BACK;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001408 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001409
1410 memset(&cb, 0, sizeof(cb));
1411 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001412 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1413 memset(att_state, 0, sizeof(att_state));
1414 att_state[0].format = demo->format;
1415 att_state[0].channelWriteMask = 0xf;
1416 att_state[0].blendEnable = XGL_FALSE;
1417 cb.attachmentCount = 1;
1418 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001419
Tony Barbourfa6cac72015-01-16 14:27:35 -07001420 memset(&vp, 0, sizeof(vp));
1421 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001422 vp.numViewports = 1;
Mike Stroyan97e79b32015-03-24 15:13:34 -06001423 vp.clipOrigin = XGL_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001424
1425 memset(&ds, 0, sizeof(ds));
1426 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1427 ds.format = demo->depth.format;
1428 ds.depthTestEnable = XGL_TRUE;
1429 ds.depthWriteEnable = XGL_TRUE;
1430 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1431 ds.depthBoundsEnable = XGL_FALSE;
1432 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1433 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1434 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1435 ds.stencilTestEnable = XGL_FALSE;
1436 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001437
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001438 memset(&vs, 0, sizeof(vs));
1439 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1440 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001441 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001442 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001443
1444 memset(&fs, 0, sizeof(fs));
1445 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1446 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1447 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001448 assert(fs.shader.shader != NULL);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001449
1450 memset(&ms, 0, sizeof(ms));
1451 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1452 ms.sampleMask = 1;
1453 ms.multisampleEnable = XGL_FALSE;
1454 ms.samples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001455
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001456 pipeline.pNext = (const void *) &ia;
1457 ia.pNext = (const void *) &rs;
1458 rs.pNext = (const void *) &cb;
1459 cb.pNext = (const void *) &ms;
1460 ms.pNext = (const void *) &vp;
1461 vp.pNext = (const void *) &ds;
1462 ds.pNext = (const void *) &vs;
1463 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001464
1465 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1466 assert(!err);
1467
1468 xglDestroyObject(vs.shader.shader);
1469 xglDestroyObject(fs.shader.shader);
1470}
1471
1472static void demo_prepare_dynamic_states(struct demo *demo)
1473{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001474 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1475 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1476 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1477 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001478 XGL_RESULT err;
1479
Tony Barbourfa6cac72015-01-16 14:27:35 -07001480 memset(&viewport_create, 0, sizeof(viewport_create));
1481 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001482 viewport_create.viewportAndScissorCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001483 XGL_VIEWPORT viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001484 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001485 viewport.height = (float) demo->height;
1486 viewport.width = (float) demo->width;
1487 viewport.minDepth = (float) 0.0f;
1488 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001489 viewport_create.pViewports = &viewport;
1490 XGL_RECT scissor;
1491 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001492 scissor.extent.width = demo->width;
1493 scissor.extent.height = demo->height;
1494 scissor.offset.x = 0;
1495 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001496 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001497
1498 memset(&raster, 0, sizeof(raster));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001499 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001500 raster.pointSize = 1.0;
1501 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001502
1503 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001504 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001505 color_blend.blendConst[0] = 1.0f;
1506 color_blend.blendConst[1] = 1.0f;
1507 color_blend.blendConst[2] = 1.0f;
1508 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001509
1510 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001511 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001512 depth_stencil.minDepth = 0.0f;
1513 depth_stencil.maxDepth = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001514 depth_stencil.stencilBackRef = 0;
1515 depth_stencil.stencilFrontRef = 0;
1516 depth_stencil.stencilReadMask = 0xff;
1517 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001518
Tony Barbourfa6cac72015-01-16 14:27:35 -07001519 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001520 assert(!err);
1521
Tony Barbourfa6cac72015-01-16 14:27:35 -07001522 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001523 assert(!err);
1524
Tony Barbourfa6cac72015-01-16 14:27:35 -07001525 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001526 &color_blend, &demo->color_blend);
1527 assert(!err);
1528
Tony Barbourfa6cac72015-01-16 14:27:35 -07001529 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001530 &depth_stencil, &demo->depth_stencil);
1531 assert(!err);
1532}
1533
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001534static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001535{
1536 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1537 [0] = {
1538 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1539 .count = 1,
1540 },
1541 [1] = {
1542 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1543 .count = DEMO_TEXTURE_COUNT,
1544 },
1545 };
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001546 const XGL_DESCRIPTOR_POOL_CREATE_INFO descriptor_pool = {
1547 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001548 .pNext = NULL,
1549 .count = 2,
1550 .pTypeCount = type_counts,
1551 };
1552 XGL_RESULT err;
1553
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001554 err = xglCreateDescriptorPool(demo->device,
1555 XGL_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
1556 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001557 assert(!err);
1558}
1559
1560static void demo_prepare_descriptor_set(struct demo *demo)
1561{
Chia-I Wuf8385062015-01-04 16:27:24 +08001562 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1563 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1564 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1565 XGL_UPDATE_BUFFERS update_vs;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001566 const void *update_array[2] = { &update_vs, &update_fs };
Chia-I Wuf8385062015-01-04 16:27:24 +08001567 XGL_RESULT err;
1568 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001569 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001570
1571 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1572 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1573 view_info[i].pNext = NULL;
1574 view_info[i].view = demo->textures[i].view,
1575 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1576
Chia-I Wu7732cb22015-03-26 15:27:55 +08001577 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wuf8385062015-01-04 16:27:24 +08001578 combined_info[i].pImageView = &view_info[i];
1579 }
1580
1581 memset(&update_vs, 0, sizeof(update_vs));
1582 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1583 update_vs.pNext = &update_fs;
1584 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1585 update_vs.count = 1;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001586 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wuf8385062015-01-04 16:27:24 +08001587
1588 memset(&update_fs, 0, sizeof(update_fs));
1589 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001590 update_fs.binding = 1;
Chia-I Wuf8385062015-01-04 16:27:24 +08001591 update_fs.count = DEMO_TEXTURE_COUNT;
1592 update_fs.pSamplerImageViews = combined_info;
1593
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001594 err = xglAllocDescriptorSets(demo->desc_pool,
Chia-I Wuf8385062015-01-04 16:27:24 +08001595 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001596 1, &demo->desc_layout,
Chia-I Wuf8385062015-01-04 16:27:24 +08001597 &demo->desc_set, &count);
1598 assert(!err && count == 1);
1599
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001600 xglBeginDescriptorPoolUpdate(demo->device,
Chia-I Wuf8385062015-01-04 16:27:24 +08001601 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1602
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001603 xglClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set);
Chia-I Wu7732cb22015-03-26 15:27:55 +08001604 xglUpdateDescriptors(demo->desc_set, 2, update_array);
Chia-I Wuf8385062015-01-04 16:27:24 +08001605
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001606 xglEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wuf8385062015-01-04 16:27:24 +08001607}
1608
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001609static void demo_prepare(struct demo *demo)
1610{
1611 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1612 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1613 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001614 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001615 .flags = 0,
1616 };
1617 XGL_RESULT err;
1618
1619 demo_prepare_buffers(demo);
1620 demo_prepare_depth(demo);
1621 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001622 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001623
Chia-I Wuf8385062015-01-04 16:27:24 +08001624 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001625 demo_prepare_pipeline(demo);
1626 demo_prepare_dynamic_states(demo);
1627
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001628 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1629 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1630 assert(!err);
1631 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001632
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001633 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001634 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001635
1636
1637 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1638 demo->current_buffer = i;
1639 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1640 }
1641
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001642 /*
1643 * Prepare functions above may generate pipeline commands
1644 * that need to be flushed before beginning the render loop.
1645 */
1646 demo_flush_init_cmd(demo);
1647
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001648 demo->current_buffer = 0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001649}
1650
1651static void demo_handle_event(struct demo *demo,
1652 const xcb_generic_event_t *event)
1653{
Piers Daniell886be472015-02-23 16:23:13 -07001654 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001655 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001656 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001657 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001658 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001659 case XCB_CLIENT_MESSAGE:
1660 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1661 (*demo->atom_wm_delete_window).atom) {
1662 demo->quit = true;
1663 }
1664 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001665 case XCB_KEY_RELEASE:
1666 {
1667 const xcb_key_release_event_t *key =
1668 (const xcb_key_release_event_t *) event;
1669
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001670 switch (key->detail) {
1671 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001672 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001673 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001674 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001675 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001676 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001677 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001678 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001679 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001680 case 0x41:
1681 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001682 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001683 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001684 }
1685 break;
1686 default:
1687 break;
1688 }
1689}
1690
1691static void demo_run(struct demo *demo)
1692{
1693 xcb_flush(demo->connection);
1694
1695 while (!demo->quit) {
1696 xcb_generic_event_t *event;
1697
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001698 if (demo->pause) {
1699 event = xcb_wait_for_event(demo->connection);
1700 } else {
1701 event = xcb_poll_for_event(demo->connection);
1702 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001703 if (event) {
1704 demo_handle_event(demo, event);
1705 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001706 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001707
1708 // Wait for work to finish before updating MVP.
1709 xglDeviceWaitIdle(demo->device);
1710 demo_update_data_buffer(demo);
1711
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001712 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001713
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001714 // Wait for work to finish before updating MVP.
1715 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001716 }
1717}
1718
1719static void demo_create_window(struct demo *demo)
1720{
1721 uint32_t value_mask, value_list[32];
1722
1723 demo->window = xcb_generate_id(demo->connection);
1724
1725 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1726 value_list[0] = demo->screen->black_pixel;
1727 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1728 XCB_EVENT_MASK_EXPOSURE;
1729
1730 xcb_create_window(demo->connection,
1731 XCB_COPY_FROM_PARENT,
1732 demo->window, demo->screen->root,
1733 0, 0, demo->width, demo->height, 0,
1734 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1735 demo->screen->root_visual,
1736 value_mask, value_list);
1737
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001738 /* Magic code that will send notification when window is destroyed */
1739 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1740 "WM_PROTOCOLS");
1741 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1742
1743 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1744 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1745
1746 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1747 demo->window, (*reply).atom, 4, 32, 1,
1748 &(*demo->atom_wm_delete_window).atom);
1749 free(reply);
1750
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001751 xcb_map_window(demo->connection, demo->window);
1752}
1753
1754static void demo_init_xgl(struct demo *demo)
1755{
1756 const XGL_APPLICATION_INFO app = {
1757 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1758 .pNext = NULL,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001759 .pAppName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001760 .appVersion = 0,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001761 .pEngineName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001762 .engineVersion = 0,
Courtney Goeltzenleuchter211cc542015-02-23 17:40:15 -07001763 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001764 };
1765 const XGL_WSI_X11_CONNECTION_INFO connection = {
1766 .pConnection = demo->connection,
1767 .root = demo->screen->root,
1768 .provider = 0,
1769 };
1770 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1771 .queueNodeIndex = 0,
1772 .queueCount = 1,
1773 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001774 const char *ext_names[] = {
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001775 "XGL_WSI_X11",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001776 };
1777 const XGL_DEVICE_CREATE_INFO device = {
1778 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1779 .pNext = NULL,
1780 .queueRecordCount = 1,
1781 .pRequestedQueues = &queue,
1782 .extensionCount = 1,
1783 .ppEnabledExtensionNames = ext_names,
1784 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1785 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1786 };
1787 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001788 uint32_t gpu_count;
1789 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001790 size_t data_size;
1791 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001792
Jon Ashburn92e80132015-01-29 15:47:01 -07001793 err = xglCreateInstance(&app, NULL, &demo->inst);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001794 if (err == XGL_ERROR_INCOMPATIBLE_DRIVER) {
1795 printf("Cannot find a compatible Vulkan installable client driver "
1796 "(ICD).\nExiting ...\n");
1797 fflush(stdout);
1798 exit(1);
1799 } else {
1800 assert(!err);
1801 }
Jon Ashburn92e80132015-01-29 15:47:01 -07001802 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001803 assert(!err && gpu_count == 1);
1804
1805 for (i = 0; i < device.extensionCount; i++) {
1806 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1807 assert(!err);
1808 }
1809
1810 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1811 assert(!err);
1812
1813 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1814 assert(!err);
1815
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001816 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
1817 &data_size, NULL);
1818 assert(!err);
1819
1820 demo->gpu_props = (XGL_PHYSICAL_GPU_PROPERTIES *) malloc(data_size);
1821 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
1822 &data_size, demo->gpu_props);
1823 assert(!err);
1824
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001825 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1826 &data_size, NULL);
1827 assert(!err);
1828
1829 demo->queue_props = (XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *) malloc(data_size);
1830 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1831 &data_size, demo->queue_props);
1832 assert(!err);
Ian Elliott0fd7b6e2015-04-03 09:54:13 -06001833 queue_count = (uint32_t)(data_size / sizeof(XGL_PHYSICAL_GPU_QUEUE_PROPERTIES));
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001834 assert(queue_count >= 1);
1835
1836 for (i = 0; i < queue_count; i++) {
1837 if (demo->queue_props[i].queueFlags & XGL_QUEUE_GRAPHICS_BIT)
1838 break;
1839 }
1840 assert(i < queue_count);
1841 demo->graphics_queue_node_index = i;
1842
1843 err = xglGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001844 0, &demo->queue);
1845 assert(!err);
1846}
1847
1848static void demo_init_connection(struct demo *demo)
1849{
1850 const xcb_setup_t *setup;
1851 xcb_screen_iterator_t iter;
1852 int scr;
1853
1854 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001855 if (demo->connection == NULL) {
1856 printf("Cannot find a compatible Vulkan installable client driver "
1857 "(ICD).\nExiting ...\n");
1858 fflush(stdout);
1859 exit(1);
1860 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001861
1862 setup = xcb_get_setup(demo->connection);
1863 iter = xcb_setup_roots_iterator(setup);
1864 while (scr-- > 0)
1865 xcb_screen_next(&iter);
1866
1867 demo->screen = iter.data;
1868}
1869
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001870static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001871{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001872 vec3 eye = {0.0f, 3.0f, 5.0f};
1873 vec3 origin = {0, 0, 0};
1874 vec3 up = {0.0f, -1.0f, 0.0};
1875
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001876 memset(demo, 0, sizeof(*demo));
1877
Piers Daniell886be472015-02-23 16:23:13 -07001878 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001879 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1880 demo->use_staging_buffer = true;
1881 }
1882
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001883 demo_init_connection(demo);
1884 demo_init_xgl(demo);
1885
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001886 demo->width = 500;
1887 demo->height = 500;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001888 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001889
1890 demo->spin_angle = 0.01f;
1891 demo->spin_increment = 0.01f;
1892 demo->pause = false;
1893
Piers Daniell886be472015-02-23 16:23:13 -07001894 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001895 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1896 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001897}
1898
1899static void demo_cleanup(struct demo *demo)
1900{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001901 uint32_t i, j;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001902
Chia-I Wuf8385062015-01-04 16:27:24 +08001903 xglDestroyObject(demo->desc_set);
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001904 xglDestroyObject(demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001905
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001906 xglDestroyObject(demo->viewport);
1907 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001908 xglDestroyObject(demo->color_blend);
1909 xglDestroyObject(demo->depth_stencil);
1910
1911 xglDestroyObject(demo->pipeline);
Chia-I Wu7732cb22015-03-26 15:27:55 +08001912 xglDestroyObject(demo->desc_layout_chain);
Chia-I Wu6e68a892015-02-23 10:41:08 -07001913 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001914
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001915 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1916 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001917 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001918 xglDestroyObject(demo->textures[i].image);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06001919 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburna9ae3832015-01-16 09:37:43 -07001920 for (j = 0; j < demo->textures[i].num_mem; j++)
1921 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001922 xglDestroyObject(demo->textures[i].sampler);
1923 }
1924
1925 xglDestroyObject(demo->depth.view);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001926 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001927 xglDestroyObject(demo->depth.image);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06001928 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
1929 for (j = 0; j < demo->depth.num_mem; j++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -07001930 xglFreeMemory(demo->depth.mem[j]);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06001931 }
Mark Lobodzinski15427102015-02-18 16:38:17 -06001932
1933 xglDestroyObject(demo->uniform_data.view);
1934 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001935 xglDestroyObject(demo->uniform_data.buf);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06001936 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001937 for (j = 0; j < demo->uniform_data.num_mem; j++)
1938 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001939
1940 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wu68040a42014-11-07 14:30:34 +08001941 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001942 xglDestroyObject(demo->buffers[i].view);
1943 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001944 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06001945 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001946 }
1947
1948 xglDestroyDevice(demo->device);
Jon Ashburn92e80132015-01-29 15:47:01 -07001949 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001950
1951 xcb_destroy_window(demo->connection, demo->window);
1952 xcb_disconnect(demo->connection);
1953}
1954
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001955int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001956{
1957 struct demo demo;
1958
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001959 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001960
1961 demo_prepare(&demo);
1962 demo_create_window(&demo);
1963 demo_run(&demo);
1964
1965 demo_cleanup(&demo);
1966
1967 return 0;
1968}