blob: 9d28e76b69d4e322185bb0942b486568ef777252 [file] [log] [blame]
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <assert.h>
7
8#include <xcb/xcb.h>
9#include <xgl.h>
10#include <xglDbg.h>
11#include <xglWsiX11Ext.h>
12
13#include "icd-bil.h"
14
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -060016#include <unistd.h>
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060017#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060018
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060019#define DEMO_BUFFER_COUNT 2
20#define DEMO_TEXTURE_COUNT 1
21
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060022/*
23 * When not defined, code will use built-in GLSL compiler
24 * which may not be supported on all drivers
25 */
26#define EXTERNAL_BIL
27
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060029 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060030};
31
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060032struct xglcube_vs_uniform {
33 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060034 float mvp[4][4];
35 float position[12*3][4];
36 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060037};
38
39struct xgltexcube_vs_uniform {
40 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060041 float mvp[4][4];
42 float position[12*3][4];
43 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060044};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060046//--------------------------------------------------------------------------------------
47// Mesh and VertexFormat Data
48//--------------------------------------------------------------------------------------
49struct Vertex
50{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060051 float posX, posY, posZ, posW; // Position data
52 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053};
54
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060055struct VertexPosTex
56{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060057 float posX, posY, posZ, posW; // Position data
58 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060059};
60
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060061#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060062#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060063
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060064static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060065 -1.0f,-1.0f,-1.0f, // Vertex 0
66 -1.0f,-1.0f, 1.0f,
67 -1.0f, 1.0f, 1.0f,
68
69 -1.0f, 1.0f, 1.0f, // Vertex 1
70 -1.0f, 1.0f,-1.0f,
71 -1.0f,-1.0f,-1.0f,
72
73 -1.0f,-1.0f,-1.0f, // Vertex 2
74 1.0f, 1.0f,-1.0f,
75 1.0f,-1.0f,-1.0f,
76
77 -1.0f,-1.0f,-1.0f, // Vertex 3
78 1.0f, 1.0f,-1.0f,
79 -1.0f, 1.0f,-1.0f,
80
81 -1.0f,-1.0f,-1.0f, // Vertex 4
82 1.0f,-1.0f, 1.0f,
83 1.0f,-1.0f,-1.0f,
84
85 -1.0f,-1.0f,-1.0f, // Vertex 5
86 -1.0f,-1.0f, 1.0f,
87 1.0f,-1.0f, 1.0f,
88
89 -1.0f, 1.0f,-1.0f, // Vertex 6
90 -1.0f, 1.0f, 1.0f,
91 1.0f, 1.0f, 1.0f,
92
93 -1.0f, 1.0f,-1.0f, // Vertex 7
94 1.0f, 1.0f,-1.0f,
95 1.0f, 1.0f, 1.0f,
96
97 1.0f, 1.0f,-1.0f, // Vertex 8
98 1.0f, 1.0f, 1.0f,
99 1.0f,-1.0f, 1.0f,
100
101 1.0f,-1.0f, 1.0f, // Vertex 9
102 1.0f,-1.0f,-1.0f,
103 1.0f, 1.0f,-1.0f,
104
105 -1.0f, 1.0f, 1.0f, // Vertex 10
106 1.0f, 1.0f, 1.0f,
107 -1.0f,-1.0f, 1.0f,
108
109 -1.0f,-1.0f, 1.0f, // Vertex 11
110 1.0f,-1.0f, 1.0f,
111 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600112};
113
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600114static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600115 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600116 0.0f, 0.0f,
117 0.0f, 1.0f,
118
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600119 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600120 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600121 1.0f, 0.0f,
122
123// 0.0f, 1.0f, // Vertex 2
124// 1.0f, 0.0f,
125// 0.0f, 0.0f,
126
127// 0.0f, 1.0f, // Vertex 3
128// 1.0f, 0.0f,
129// 1.0f, 1.0f,
130
131 0.0f, 0.0f, // Vertex 2
132 1.0f, 1.0f,
133 1.0f, 0.0f,
134
135 0.0f, 0.0f, // Vertex 3
136 1.0f, 1.0f,
137 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600138
139 0.0f, 1.0f, // Vertex 4
140 1.0f, 0.0f,
141 0.0f, 0.0f,
142
143 0.0f, 1.0f, // Vertex 5
144 1.0f, 1.0f,
145 1.0f, 0.0f,
146
147 0.0f, 1.0f, // Vertex 6
148 1.0f, 1.0f,
149 1.0f, 0.0f,
150
151 0.0f, 1.0f, // Vertex 7
152 0.0f, 0.0f,
153 1.0f, 0.0f,
154
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600155 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600158
159 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 0.0f, 1.0f,
162
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600163 1.0f, 1.0f, // Vertex 10
164 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600166
167 1.0f, 0.0f, // Vertex 11
168 0.0f, 0.0f,
169 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600170};
171
172void dumpMatrix(const char *note, mat4x4 MVP)
173{
174 int i;
175
176 printf("%s: \n", note);
177 for (i=0; i<4; i++) {
178 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
179 }
180 printf("\n");
181 fflush(stdout);
182}
183
184void dumpVec4(const char *note, vec4 vector)
185{
186 printf("%s: \n", note);
187 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
188 printf("\n");
189 fflush(stdout);
190}
191
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600192struct demo {
193 xcb_connection_t *connection;
194 xcb_screen_t *screen;
195
Jon Ashburn93cfc432015-01-29 15:47:01 -0700196 XGL_INSTANCE inst;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600197 XGL_PHYSICAL_GPU gpu;
198 XGL_DEVICE device;
199 XGL_QUEUE queue;
200
201 int width, height;
202 XGL_FORMAT format;
203
204 struct {
205 XGL_IMAGE image;
206 XGL_GPU_MEMORY mem;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700207 XGL_CMD_BUFFER cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600208
209 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800210 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600211 } buffers[DEMO_BUFFER_COUNT];
212
213 struct {
214 XGL_FORMAT format;
215
216 XGL_IMAGE image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600217 uint32_t num_mem;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700218 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600219 XGL_DEPTH_STENCIL_VIEW view;
220 } depth;
221
222 struct {
223 XGL_SAMPLER sampler;
224
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 char *filename;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600226 XGL_IMAGE image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600227 uint32_t num_mem;
Jon Ashburnb2a66652015-01-16 09:37:43 -0700228 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600229 XGL_IMAGE_VIEW view;
230 } textures[DEMO_TEXTURE_COUNT];
231
232 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800233 XGL_BUFFER buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600234 uint32_t num_mem;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700235 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800236 XGL_BUFFER_VIEW view;
237 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600238 } uniform_data;
239
Chia-I Wu87544e72015-02-23 10:41:08 -0700240 XGL_DESCRIPTOR_SET_LAYOUT desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600241 XGL_PIPELINE pipeline;
242
Tony Barbour29645d02015-01-16 14:27:35 -0700243 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
244 XGL_DYNAMIC_RS_STATE_OBJECT raster;
245 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
246 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600247
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600248 mat4x4 projection_matrix;
249 mat4x4 view_matrix;
250 mat4x4 model_matrix;
251
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600252 float spin_angle;
253 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600254 bool pause;
255
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800256 XGL_DESCRIPTOR_REGION desc_region;
257 XGL_DESCRIPTOR_SET desc_set;
258
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600259 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700260 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600261
262 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600263 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600264};
265
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700266static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600267{
268 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
269 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000270 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600271 };
272 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
273 .view = demo->depth.view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000274 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600275 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600276 const float clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
277 const float clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600278 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700279 XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = {
280 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO,
281 .pNext = NULL,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700282 };
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700283 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
284 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700285 .pNext = &graphics_cmd_buf_info,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700286 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
287 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
288 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600289 XGL_RESULT err;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700290 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
291 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
292 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
293 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
294 .pNext = NULL,
295 .colorAttachmentCount = 1,
296 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
297 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
298 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600299 .width = demo->width,
300 .height = demo->height,
301 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700302 };
303 XGL_RENDER_PASS_CREATE_INFO rp_info;
304
305 memset(&rp_info, 0 , sizeof(rp_info));
306 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
307 assert(!err);
308 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
309 rp_info.renderArea.extent.width = demo->width;
310 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchterc17d7de2015-02-10 14:06:25 -0700311 rp_info.colorAttachmentCount = 1;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700312 rp_info.pColorLoadOps = &load_op;
313 rp_info.pColorStoreOps = &store_op;
314 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
315 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
316 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
317 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
318 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
319 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600320
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700321 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322 assert(!err);
323
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700324 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600325 demo->pipeline);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700326 xglCmdBindDescriptorSet(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800327 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600328
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700329 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
330 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
331 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600332 demo->color_blend);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700333 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334 demo->depth_stencil);
335
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600336 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
337 clear_range.baseMipLevel = 0;
338 clear_range.mipLevels = 1;
339 clear_range.baseArraySlice = 0;
340 clear_range.arraySize = 1;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700341 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600342 demo->buffers[demo->current_buffer].image,
343 clear_color, 1, &clear_range);
344
345 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700346 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600347 clear_depth, 0, 1, &clear_range);
348
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700349 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600350
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700351 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600352 assert(!err);
353}
354
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600355
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600356void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600357{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600358 mat4x4 MVP, Model, VP;
359 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600360 uint8_t *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600361 XGL_RESULT err;
362
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600363 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600364
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600365 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600366 mat4x4_dup(Model, demo->model_matrix);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600367 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600368 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600369
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700370 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600371 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372 assert(!err);
373
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600374 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700376 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600377 assert(!err);
378}
379
380static void demo_draw(struct demo *demo)
381{
382 const XGL_WSI_X11_PRESENT_INFO present = {
383 .destWindow = demo->window,
384 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800385 .async = true,
386 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600387 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800388 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600389 XGL_RESULT err;
390
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600391 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wubb57f642014-11-07 14:30:34 +0800392 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
393
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -0600394 uint32_t i, idx = 0;
395 XGL_MEMORY_REF *memRefs;
396 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
397 demo->depth.num_mem +
398 demo->textures[0].num_mem +
399 demo->uniform_data.num_mem));
400 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
401 memRefs[idx].mem = demo->depth.mem[i];
402 memRefs[idx].flags = 0;
403 }
404 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
405 memRefs[idx].mem = demo->textures[0].mem[i];
406 memRefs[idx].flags = 0;
407 }
408 memRefs[idx].mem = demo->buffers[0].mem;
409 memRefs[idx++].flags = 0;
410 memRefs[idx].mem = demo->buffers[1].mem;
411 memRefs[idx++].flags = 0;
412 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
413 memRefs[idx].mem = demo->uniform_data.mem[i];
414 memRefs[idx].flags = 0;
415 }
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700416 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
417 0, NULL, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600418 assert(!err);
419
Chia-I Wubb57f642014-11-07 14:30:34 +0800420 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600421 assert(!err);
422
423 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
424}
425
426static void demo_prepare_buffers(struct demo *demo)
427{
428 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
429 .format = demo->format,
430 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
431 .extent = {
432 .width = demo->width,
433 .height = demo->height,
434 },
435 .flags = 0,
436 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800437 const XGL_FENCE_CREATE_INFO fence = {
438 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
439 .pNext = NULL,
440 .flags = 0,
441 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600442 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600443 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600444
445 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
446 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
447 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
448 .pNext = NULL,
449 .format = demo->format,
450 .mipLevel = 0,
451 .baseArraySlice = 0,
452 .arraySize = 1,
453 };
454
455 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
456 &demo->buffers[i].image, &demo->buffers[i].mem);
457 assert(!err);
458
459 color_attachment_view.image = demo->buffers[i].image;
460
461 err = xglCreateColorAttachmentView(demo->device,
462 &color_attachment_view, &demo->buffers[i].view);
463 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800464
465 err = xglCreateFence(demo->device,
466 &fence, &demo->buffers[i].fence);
467 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600468 }
469}
470
471static void demo_prepare_depth(struct demo *demo)
472{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700473 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600474 const XGL_IMAGE_CREATE_INFO image = {
475 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
476 .pNext = NULL,
477 .imageType = XGL_IMAGE_2D,
478 .format = depth_format,
479 .extent = { demo->width, demo->height, 1 },
480 .mipLevels = 1,
481 .arraySize = 1,
482 .samples = 1,
483 .tiling = XGL_OPTIMAL_TILING,
484 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
485 .flags = 0,
486 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700487 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
488 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
489 .pNext = NULL,
490 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600491 XGL_MEMORY_ALLOC_INFO mem_alloc = {
492 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700493 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600494 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700495 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700496 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600497 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
498 };
499 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
500 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
501 .pNext = NULL,
502 .image = XGL_NULL_HANDLE,
503 .mipLevel = 0,
504 .baseArraySlice = 0,
505 .arraySize = 1,
506 .flags = 0,
507 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700508 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600509 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700510 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600511 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600512 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600513 uint32_t num_allocations = 0;
514 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600515
516 demo->depth.format = depth_format;
517
518 /* create image */
519 err = xglCreateImage(demo->device, &image,
520 &demo->depth.image);
521 assert(!err);
522
Jon Ashburnb2a66652015-01-16 09:37:43 -0700523
524 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
525 assert(!err && num_alloc_size == sizeof(num_allocations));
526 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
527 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
528 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600529 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700530 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
531 &mem_reqs_size, mem_reqs);
532 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700533 err = xglGetObjectInfo(demo->depth.image,
534 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
535 &img_reqs_size, &img_reqs);
536 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
537 img_alloc.usage = img_reqs.usage;
538 img_alloc.formatClass = img_reqs.formatClass;
539 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600540 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700541 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542
Jon Ashburnb2a66652015-01-16 09:37:43 -0700543 /* allocate memory */
544 err = xglAllocMemory(demo->device, &mem_alloc,
545 &(demo->depth.mem[i]));
546 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600547
Jon Ashburnb2a66652015-01-16 09:37:43 -0700548 /* bind memory */
549 err = xglBindObjectMemory(demo->depth.image, i,
550 demo->depth.mem[i], 0);
551 assert(!err);
552 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553
554 /* create image view */
555 view.image = demo->depth.image;
556 err = xglCreateDepthStencilView(demo->device, &view,
557 &demo->depth.view);
558 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600559}
560
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600561/** loadTexture
562 * loads a png file into an memory object, using cstdio , libpng.
563 *
564 * \param demo : Needed to access XGL calls
565 * \param filename : the png file to be loaded
566 * \param width : width of png, to be updated as a side effect of this function
567 * \param height : height of png, to be updated as a side effect of this function
568 *
569 * \return bool : an opengl texture id. true if successful?,
570 * should be validated by the client of this function.
571 *
572 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
573 * Modified to copy image to memory
574 *
575 */
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600576bool loadTexture(char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600577 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600578 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600579{
580 //header for testing if it is a png
581 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700582 int is_png, bit_depth, color_type,rowbytes;
583 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600584 png_structp png_ptr;
585 png_infop info_ptr, end_info;
586 png_byte *image_data;
587 png_bytep *row_pointers;
588
589 //open file as binary
590 FILE *fp = fopen(filename, "rb");
591 if (!fp) {
592 return false;
593 }
594
595 //read the header
596 fread(header, 1, 8, fp);
597
598 //test if png
599 is_png = !png_sig_cmp(header, 0, 8);
600 if (!is_png) {
601 fclose(fp);
602 return false;
603 }
604
605 //create png struct
606 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
607 NULL, NULL);
608 if (!png_ptr) {
609 fclose(fp);
610 return (false);
611 }
612
613 //create png info struct
614 info_ptr = png_create_info_struct(png_ptr);
615 if (!info_ptr) {
616 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
617 fclose(fp);
618 return (false);
619 }
620
621 //create png info struct
622 end_info = png_create_info_struct(png_ptr);
623 if (!end_info) {
624 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
625 fclose(fp);
626 return (false);
627 }
628
629 //png error stuff, not sure libpng man suggests this.
630 if (setjmp(png_jmpbuf(png_ptr))) {
631 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
632 fclose(fp);
633 return (false);
634 }
635
636 //init png reading
637 png_init_io(png_ptr, fp);
638
639 //let libpng know you already read the first 8 bytes
640 png_set_sig_bytes(png_ptr, 8);
641
642 // read all the info up to the image data
643 png_read_info(png_ptr, info_ptr);
644
645 // get info about png
646 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
647 NULL, NULL, NULL);
648
649 //update width and height based on png info
650 *width = twidth;
651 *height = theight;
652
653 // Require that incoming texture be 8bits per color component
654 // and 4 components (RGBA).
655 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
656 png_get_channels(png_ptr, info_ptr) != 4) {
657 return false;
658 }
659
660 if (rgba_data == NULL) {
661 // If data pointer is null, we just want the width & height
662 // clean up memory and close stuff
663 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
664 fclose(fp);
665
666 return true;
667 }
668
669 // Update the png info struct.
670 png_read_update_info(png_ptr, info_ptr);
671
672 // Row size in bytes.
673 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
674
675 // Allocate the image_data as a big block, to be given to opengl
676 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
677 if (!image_data) {
678 //clean up memory and close stuff
679 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
680 fclose(fp);
681 return false;
682 }
683
684 // row_pointers is for pointing to image_data for reading the png with libpng
685 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
686 if (!row_pointers) {
687 //clean up memory and close stuff
688 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
689 // delete[] image_data;
690 fclose(fp);
691 return false;
692 }
693 // set the individual row_pointers to point at the correct offsets of image_data
694 for (i = 0; i < theight; ++i)
695 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
696
697 // read the png into image_data through row_pointers
698 png_read_image(png_ptr, row_pointers);
699
700 // clean up memory and close stuff
701 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
702 free(row_pointers);
703 free(image_data);
704 fclose(fp);
705
706 return true;
707}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600708
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600709static void demo_prepare_textures(struct demo *demo)
710{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700711 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600712 int32_t tex_width;
713 int32_t tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600714 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600715 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600716
717 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
718 const XGL_SAMPLER_CREATE_INFO sampler = {
719 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
720 .pNext = NULL,
721 .magFilter = XGL_TEX_FILTER_NEAREST,
722 .minFilter = XGL_TEX_FILTER_NEAREST,
723 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600724 .addressU = XGL_TEX_ADDRESS_CLAMP,
725 .addressV = XGL_TEX_ADDRESS_CLAMP,
726 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600727 .mipLodBias = 0.0f,
728 .maxAnisotropy = 0,
729 .compareFunc = XGL_COMPARE_NEVER,
730 .minLod = 0.0f,
731 .maxLod = 0.0f,
732 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
733 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600734
Tony Barbour276d9822015-02-05 14:14:33 -0700735 err = loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height);
736 assert(err);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600737
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600738 const XGL_IMAGE_CREATE_INFO image = {
739 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
740 .pNext = NULL,
741 .imageType = XGL_IMAGE_2D,
742 .format = tex_format,
743 .extent = { tex_width, tex_height, 1 },
744 .mipLevels = 1,
745 .arraySize = 1,
746 .samples = 1,
747 .tiling = XGL_LINEAR_TILING,
748 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
749 .flags = 0,
750 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700751 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
752 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
753 .pNext = NULL,
754 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600755 XGL_MEMORY_ALLOC_INFO mem_alloc = {
756 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700757 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600758 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700759 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700760 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600761 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
762 };
763 XGL_IMAGE_VIEW_CREATE_INFO view = {
764 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
765 .pNext = NULL,
766 .image = XGL_NULL_HANDLE,
767 .viewType = XGL_IMAGE_VIEW_2D,
768 .format = image.format,
769 .channels = { XGL_CHANNEL_SWIZZLE_R,
770 XGL_CHANNEL_SWIZZLE_G,
771 XGL_CHANNEL_SWIZZLE_B,
772 XGL_CHANNEL_SWIZZLE_A, },
773 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
774 .minLod = 0.0f,
775 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700776
777 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600778 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700779 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600780 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
781 uint32_t num_allocations = 0;
782 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600783
784 /* create sampler */
785 err = xglCreateSampler(demo->device, &sampler,
786 &demo->textures[i].sampler);
787 assert(!err);
788
789 /* create image */
790 err = xglCreateImage(demo->device, &image,
791 &demo->textures[i].image);
792 assert(!err);
793
794 err = xglGetObjectInfo(demo->textures[i].image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700795 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
796 &num_alloc_size, &num_allocations);
797 assert(!err && num_alloc_size == sizeof(num_allocations));
798 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
799 demo->textures[i].mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
800 demo->textures[i].num_mem = num_allocations;
801 err = xglGetObjectInfo(demo->textures[i].image,
802 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
803 &mem_reqs_size, mem_reqs);
804 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700805 err = xglGetObjectInfo(demo->textures[i].image,
806 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
807 &img_reqs_size, &img_reqs);
808 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
809 img_alloc.usage = img_reqs.usage;
810 img_alloc.formatClass = img_reqs.formatClass;
811 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600812 for (uint32_t j = 0; j < num_allocations; j ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700813 mem_alloc.allocationSize = mem_reqs[j].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600814
Jon Ashburnb2a66652015-01-16 09:37:43 -0700815 /* allocate memory */
816 err = xglAllocMemory(demo->device, &mem_alloc,
817 &(demo->textures[i].mem[j]));
818 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819
Jon Ashburnb2a66652015-01-16 09:37:43 -0700820 /* bind memory */
821 err = xglBindObjectMemory(demo->textures[i].image, j,
822 demo->textures[i].mem[j], 0);
823 assert(!err);
824 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600825
826 /* create image view */
827 view.image = demo->textures[i].image;
828 err = xglCreateImageView(demo->device, &view,
829 &demo->textures[i].view);
830 assert(!err);
831 }
832
833 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
834 const XGL_IMAGE_SUBRESOURCE subres = {
835 .aspect = XGL_IMAGE_ASPECT_COLOR,
836 .mipLevel = 0,
837 .arraySlice = 0,
838 };
839 XGL_SUBRESOURCE_LAYOUT layout;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600840 size_t layout_size = sizeof(layout);
841 void *data;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600842
843 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
844 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
845 assert(!err && layout_size == sizeof(layout));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700846 assert(demo->textures[i].num_mem == 1);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847
Jon Ashburnb2a66652015-01-16 09:37:43 -0700848 err = xglMapMemory(demo->textures[i].mem[0], 0, &data);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600849 assert(!err);
850
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600851 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600852
Jon Ashburnb2a66652015-01-16 09:37:43 -0700853 err = xglUnmapMemory(demo->textures[i].mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600854 assert(!err);
855 }
856}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600857
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600858void demo_prepare_cube_data_buffer(struct demo *demo)
859{
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800860 XGL_BUFFER_CREATE_INFO buf_info;
861 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700862 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
863 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
864 .pNext = NULL,
865 };
866 XGL_MEMORY_ALLOC_INFO alloc_info = {
867 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
868 .pNext = &buf_alloc,
869 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700870 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700871 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700872 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
873 };
874 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600875 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700876 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600877 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
878 uint32_t num_allocations = 0;
879 size_t num_alloc_size = sizeof(num_allocations);
880 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600881 int i;
882 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600883 XGL_RESULT err;
884 struct xgltexcube_vs_uniform data;
885
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600886 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600887 mat4x4_mul(MVP, VP, demo->model_matrix);
888 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600889// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600890
891 for (i=0; i<12*3; i++) {
892 data.position[i][0] = g_vertex_buffer_data[i*3];
893 data.position[i][1] = g_vertex_buffer_data[i*3+1];
894 data.position[i][2] = g_vertex_buffer_data[i*3+2];
895 data.position[i][3] = 1.0f;
896 data.attr[i][0] = g_uv_buffer_data[2*i];
897 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
898 data.attr[i][2] = 0;
899 data.attr[i][3] = 0;
900 }
901
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800902 memset(&buf_info, 0, sizeof(buf_info));
903 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
904 buf_info.size = sizeof(data);
905 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
906 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
907 assert(!err);
908
909 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700910 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
911 &num_alloc_size, &num_allocations);
912 assert(!err && num_alloc_size == sizeof(num_allocations));
913 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
914 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
915 demo->uniform_data.num_mem = num_allocations;
916 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800917 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700918 &mem_reqs_size, mem_reqs);
919 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
920 err = xglGetObjectInfo(demo->uniform_data.buf,
921 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
922 &buf_reqs_size, &buf_reqs);
923 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
924 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600925 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700926 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800927
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700928 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
929 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600930
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600931 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700932 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600933
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700934 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600935
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700936 err = xglUnmapMemory(demo->uniform_data.mem[i]);
937 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600938
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700939 err = xglBindObjectMemory(demo->uniform_data.buf, i,
940 demo->uniform_data.mem[i], 0);
941 assert(!err);
942 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800943
944 memset(&view_info, 0, sizeof(view_info));
945 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
946 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +0800947 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800948 view_info.offset = 0;
949 view_info.range = sizeof(data);
950
951 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
952 assert(!err);
953
954 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
955 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600956}
957
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800958static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600959{
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800960 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
961 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
962 .pNext = NULL,
963 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
964 .count = DEMO_TEXTURE_COUNT,
965 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
966 .immutableSampler = XGL_NULL_HANDLE,
967 };
Chia-I Wu87544e72015-02-23 10:41:08 -0700968 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
969 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
970 .pNext = &descriptor_layout_fs,
971 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
972 .count = 1,
973 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
974 .immutableSampler = XGL_NULL_HANDLE,
975 };
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800976 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600977 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800979 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu87544e72015-02-23 10:41:08 -0700980 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800981 XGL_NULL_HANDLE, &descriptor_layout_vs,
Chia-I Wu87544e72015-02-23 10:41:08 -0700982 &demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600983 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600984}
985
986static XGL_SHADER demo_prepare_shader(struct demo *demo,
987 XGL_PIPELINE_SHADER_STAGE stage,
988 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600989 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990{
991 XGL_SHADER_CREATE_INFO createInfo;
992 XGL_SHADER shader;
993 XGL_RESULT err;
994
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600995
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600996 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
997 createInfo.pNext = NULL;
998
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600999#ifdef EXTERNAL_BIL
1000 createInfo.codeSize = size;
1001 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001002 createInfo.flags = 0;
1003
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001004 err = xglCreateShader(demo->device, &createInfo, &shader);
1005 if (err) {
1006 free((void *) createInfo.pCode);
1007 }
1008#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001009 // Create fake BIL structure to feed GLSL
1010 // to the driver "under the covers"
1011 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1012 createInfo.pCode = malloc(createInfo.codeSize);
1013 createInfo.flags = 0;
1014
1015 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1016 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1017 ((uint32_t *) createInfo.pCode)[1] = 0;
1018 ((uint32_t *) createInfo.pCode)[2] = stage;
1019 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1020
1021 err = xglCreateShader(demo->device, &createInfo, &shader);
1022 if (err) {
1023 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001024 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001025 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001026#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001027
1028 return shader;
1029}
1030
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001031char *demo_read_bil(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001032{
1033 long int size;
1034 void *shader_code;
1035
1036 FILE *fp = fopen(filename, "rb");
1037 if (!fp) return NULL;
1038
1039 fseek(fp, 0L, SEEK_END);
1040 size = ftell(fp);
1041
1042 fseek(fp, 0L, SEEK_SET);
1043
1044 shader_code = malloc(size);
1045 fread(shader_code, size, 1, fp);
1046
1047 *psize = size;
1048
1049 return shader_code;
1050}
1051
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052static XGL_SHADER demo_prepare_vs(struct demo *demo)
1053{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001054#ifdef EXTERNAL_BIL
1055 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001056 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001057
1058 vertShaderCode = demo_read_bil("cube-vert.bil", &size);
1059
1060 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1061 vertShaderCode, size);
1062#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001063 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001064 "#version 140\n"
1065 "#extension GL_ARB_separate_shader_objects : enable\n"
1066 "#extension GL_ARB_shading_language_420pack : enable\n"
1067 "\n"
1068 "layout(binding = 0) uniform buf {\n"
1069 " mat4 MVP;\n"
1070 " vec4 position[12*3];\n"
1071 " vec4 attr[12*3];\n"
1072 "} ubuf;\n"
1073 "\n"
1074 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001075 "\n"
1076 "void main() \n"
1077 "{\n"
1078 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001079 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1080 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001081
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001082 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1083 (const void *) vertShaderText,
1084 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001085#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001086}
1087
1088static XGL_SHADER demo_prepare_fs(struct demo *demo)
1089{
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001090#ifdef EXTERNAL_BIL
1091 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001092 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001093
1094 fragShaderCode = demo_read_bil("cube-frag.bil", &size);
1095
1096 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1097 fragShaderCode, size);
1098#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001099 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001100 "#version 140\n"
1101 "#extension GL_ARB_separate_shader_objects : enable\n"
1102 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001103 "layout (binding = 0) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001104 "\n"
1105 "layout (location = 0) in vec4 texcoord;\n"
1106 "void main() {\n"
1107 " gl_FragColor = texture(tex, texcoord.xy);\n"
1108 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001109
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001110 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1111 (const void *) fragShaderText,
1112 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001113#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114}
1115
1116static void demo_prepare_pipeline(struct demo *demo)
1117{
1118 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001119 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1120 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001121 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1122 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001123 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1124 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001125 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1126 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001127 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001128
1129 memset(&pipeline, 0, sizeof(pipeline));
1130 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu87544e72015-02-23 10:41:08 -07001131 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001132
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001133 memset(&ia, 0, sizeof(ia));
1134 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1135 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1136
1137 memset(&rs, 0, sizeof(rs));
1138 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001139 rs.fillMode = XGL_FILL_SOLID;
1140 rs.cullMode = XGL_CULL_NONE;
1141 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001142
1143 memset(&cb, 0, sizeof(cb));
1144 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001145 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1146 memset(att_state, 0, sizeof(att_state));
1147 att_state[0].format = demo->format;
1148 att_state[0].channelWriteMask = 0xf;
1149 att_state[0].blendEnable = XGL_FALSE;
1150 cb.attachmentCount = 1;
1151 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001152
Tony Barbour29645d02015-01-16 14:27:35 -07001153 memset(&vp, 0, sizeof(vp));
1154 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001155
1156 memset(&ds, 0, sizeof(ds));
1157 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1158 ds.format = demo->depth.format;
1159 ds.depthTestEnable = XGL_TRUE;
1160 ds.depthWriteEnable = XGL_TRUE;
1161 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1162 ds.depthBoundsEnable = XGL_FALSE;
1163 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1164 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1165 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1166 ds.stencilTestEnable = XGL_FALSE;
1167 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001168
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001169 memset(&vs, 0, sizeof(vs));
1170 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1171 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001172 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001173 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001174
1175 memset(&fs, 0, sizeof(fs));
1176 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1177 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1178 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001179 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001180
1181 memset(&ms, 0, sizeof(ms));
1182 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1183 ms.sampleMask = 1;
1184 ms.multisampleEnable = XGL_FALSE;
1185 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001186
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001187 pipeline.pNext = (const void *) &ia;
1188 ia.pNext = (const void *) &rs;
1189 rs.pNext = (const void *) &cb;
1190 cb.pNext = (const void *) &ms;
1191 ms.pNext = (const void *) &vp;
1192 vp.pNext = (const void *) &ds;
1193 ds.pNext = (const void *) &vs;
1194 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001195
1196 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1197 assert(!err);
1198
1199 xglDestroyObject(vs.shader.shader);
1200 xglDestroyObject(fs.shader.shader);
1201}
1202
1203static void demo_prepare_dynamic_states(struct demo *demo)
1204{
Tony Barbour29645d02015-01-16 14:27:35 -07001205 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1206 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1207 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1208 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209 XGL_RESULT err;
1210
Tony Barbour29645d02015-01-16 14:27:35 -07001211 memset(&viewport_create, 0, sizeof(viewport_create));
1212 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001213 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001214 XGL_VIEWPORT viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001215 XGL_RECT scissor;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001216 viewport.height = (float) demo->height;
1217 viewport.width = (float) demo->width;
1218 viewport.minDepth = (float) 0.0f;
1219 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001220 scissor.extent.width = demo->width;
1221 scissor.extent.height = demo->height;
1222 scissor.offset.x = 0;
1223 scissor.offset.y = 0;
Tony Barbour29645d02015-01-16 14:27:35 -07001224 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001225 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001226
1227 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001228 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001229
1230 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001231 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232
1233 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001234 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
1235 depth_stencil.stencilBackRef = 0;
1236 depth_stencil.stencilFrontRef = 0;
1237 depth_stencil.stencilReadMask = 0xff;
1238 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001239
Tony Barbour29645d02015-01-16 14:27:35 -07001240 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241 assert(!err);
1242
Tony Barbour29645d02015-01-16 14:27:35 -07001243 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001244 assert(!err);
1245
Tony Barbour29645d02015-01-16 14:27:35 -07001246 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247 &color_blend, &demo->color_blend);
1248 assert(!err);
1249
Tony Barbour29645d02015-01-16 14:27:35 -07001250 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001251 &depth_stencil, &demo->depth_stencil);
1252 assert(!err);
1253}
1254
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001255static void demo_prepare_descriptor_region(struct demo *demo)
1256{
1257 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1258 [0] = {
1259 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1260 .count = 1,
1261 },
1262 [1] = {
1263 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1264 .count = DEMO_TEXTURE_COUNT,
1265 },
1266 };
1267 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1268 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1269 .pNext = NULL,
1270 .count = 2,
1271 .pTypeCount = type_counts,
1272 };
1273 XGL_RESULT err;
1274
1275 err = xglCreateDescriptorRegion(demo->device,
1276 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1277 &descriptor_region, &demo->desc_region);
1278 assert(!err);
1279}
1280
1281static void demo_prepare_descriptor_set(struct demo *demo)
1282{
1283 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1284 &demo->uniform_data.attach;
1285 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1286 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1287 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1288 XGL_UPDATE_BUFFERS update_vs;
1289 XGL_RESULT err;
1290 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001291 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001292
1293 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1294 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1295 view_info[i].pNext = NULL;
1296 view_info[i].view = demo->textures[i].view,
1297 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1298
1299 combined_info[i].pSampler = demo->textures[i].sampler;
1300 combined_info[i].pImageView = &view_info[i];
1301 }
1302
1303 memset(&update_vs, 0, sizeof(update_vs));
1304 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1305 update_vs.pNext = &update_fs;
1306 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1307 update_vs.count = 1;
1308 update_vs.pBufferViews = &view_info_vs;
1309
1310 memset(&update_fs, 0, sizeof(update_fs));
1311 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1312 update_fs.index = 1;
1313 update_fs.count = DEMO_TEXTURE_COUNT;
1314 update_fs.pSamplerImageViews = combined_info;
1315
1316 err = xglAllocDescriptorSets(demo->desc_region,
1317 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001318 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001319 &demo->desc_set, &count);
1320 assert(!err && count == 1);
1321
1322 xglBeginDescriptorRegionUpdate(demo->device,
1323 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1324
1325 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1326 xglUpdateDescriptors(demo->desc_set, &update_vs);
1327
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001328 xglEndDescriptorRegionUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001329}
1330
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001331static void demo_prepare(struct demo *demo)
1332{
1333 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1334 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1335 .pNext = NULL,
1336 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1337 .flags = 0,
1338 };
1339 XGL_RESULT err;
1340
1341 demo_prepare_buffers(demo);
1342 demo_prepare_depth(demo);
1343 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001344 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001345
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001346 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001347 demo_prepare_pipeline(demo);
1348 demo_prepare_dynamic_states(demo);
1349
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001350 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1351 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1352 assert(!err);
1353 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001354
1355 demo_prepare_descriptor_region(demo);
1356 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001357
1358
1359 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1360 demo->current_buffer = i;
1361 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1362 }
1363
1364 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365}
1366
1367static void demo_handle_event(struct demo *demo,
1368 const xcb_generic_event_t *event)
1369{
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001370 u_int8_t event_code = event->response_type & 0x7f;
1371 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001372 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001373 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001374 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001375 case XCB_CLIENT_MESSAGE:
1376 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1377 (*demo->atom_wm_delete_window).atom) {
1378 demo->quit = true;
1379 }
1380 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001381 case XCB_KEY_RELEASE:
1382 {
1383 const xcb_key_release_event_t *key =
1384 (const xcb_key_release_event_t *) event;
1385
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001386 switch (key->detail) {
1387 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001389 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001390 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001391 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001392 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001393 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001394 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001395 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001396 case 0x41:
1397 demo->pause = !demo->pause;
1398 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001399 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 }
1401 break;
1402 default:
1403 break;
1404 }
1405}
1406
1407static void demo_run(struct demo *demo)
1408{
1409 xcb_flush(demo->connection);
1410
1411 while (!demo->quit) {
1412 xcb_generic_event_t *event;
1413
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001414 if (demo->pause) {
1415 event = xcb_wait_for_event(demo->connection);
1416 } else {
1417 event = xcb_poll_for_event(demo->connection);
1418 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419 if (event) {
1420 demo_handle_event(demo, event);
1421 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001422 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001423
1424 // Wait for work to finish before updating MVP.
1425 xglDeviceWaitIdle(demo->device);
1426 demo_update_data_buffer(demo);
1427
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001428 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001429
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001430 // Wait for work to finish before updating MVP.
1431 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432 }
1433}
1434
1435static void demo_create_window(struct demo *demo)
1436{
1437 uint32_t value_mask, value_list[32];
1438
1439 demo->window = xcb_generate_id(demo->connection);
1440
1441 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1442 value_list[0] = demo->screen->black_pixel;
1443 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1444 XCB_EVENT_MASK_EXPOSURE;
1445
1446 xcb_create_window(demo->connection,
1447 XCB_COPY_FROM_PARENT,
1448 demo->window, demo->screen->root,
1449 0, 0, demo->width, demo->height, 0,
1450 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1451 demo->screen->root_visual,
1452 value_mask, value_list);
1453
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001454 /* Magic code that will send notification when window is destroyed */
1455 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1456 "WM_PROTOCOLS");
1457 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1458
1459 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1460 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1461
1462 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1463 demo->window, (*reply).atom, 4, 32, 1,
1464 &(*demo->atom_wm_delete_window).atom);
1465 free(reply);
1466
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001467 xcb_map_window(demo->connection, demo->window);
1468}
1469
1470static void demo_init_xgl(struct demo *demo)
1471{
1472 const XGL_APPLICATION_INFO app = {
1473 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1474 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001475 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001476 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001477 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001479 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001480 };
1481 const XGL_WSI_X11_CONNECTION_INFO connection = {
1482 .pConnection = demo->connection,
1483 .root = demo->screen->root,
1484 .provider = 0,
1485 };
1486 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1487 .queueNodeIndex = 0,
1488 .queueCount = 1,
1489 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001490 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001491 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001492 };
1493 const XGL_DEVICE_CREATE_INFO device = {
1494 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1495 .pNext = NULL,
1496 .queueRecordCount = 1,
1497 .pRequestedQueues = &queue,
1498 .extensionCount = 1,
1499 .ppEnabledExtensionNames = ext_names,
1500 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1501 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1502 };
1503 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001504 uint32_t gpu_count;
1505 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001506
Jon Ashburn93cfc432015-01-29 15:47:01 -07001507 err = xglCreateInstance(&app, NULL, &demo->inst);
1508 assert(!err);
1509 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001510 assert(!err && gpu_count == 1);
1511
1512 for (i = 0; i < device.extensionCount; i++) {
1513 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1514 assert(!err);
1515 }
1516
1517 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1518 assert(!err);
1519
1520 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1521 assert(!err);
1522
1523 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1524 0, &demo->queue);
1525 assert(!err);
1526}
1527
1528static void demo_init_connection(struct demo *demo)
1529{
1530 const xcb_setup_t *setup;
1531 xcb_screen_iterator_t iter;
1532 int scr;
1533
1534 demo->connection = xcb_connect(NULL, &scr);
1535
1536 setup = xcb_get_setup(demo->connection);
1537 iter = xcb_setup_roots_iterator(setup);
1538 while (scr-- > 0)
1539 xcb_screen_next(&iter);
1540
1541 demo->screen = iter.data;
1542}
1543
1544static void demo_init(struct demo *demo)
1545{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001546 vec3 eye = {0.0f, 3.0f, 5.0f};
1547 vec3 origin = {0, 0, 0};
1548 vec3 up = {0.0f, -1.0f, 0.0};
1549
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001550 memset(demo, 0, sizeof(*demo));
1551
1552 demo_init_connection(demo);
1553 demo_init_xgl(demo);
1554
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001555 demo->width = 500;
1556 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001557 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001558
1559 demo->spin_angle = 0.01f;
1560 demo->spin_increment = 0.01f;
1561 demo->pause = false;
1562
1563 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
1564 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1565 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001566}
1567
1568static void demo_cleanup(struct demo *demo)
1569{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001570 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001571
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001572 xglDestroyObject(demo->desc_set);
1573 xglDestroyObject(demo->desc_region);
1574
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001575 xglDestroyObject(demo->viewport);
1576 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001577 xglDestroyObject(demo->color_blend);
1578 xglDestroyObject(demo->depth_stencil);
1579
1580 xglDestroyObject(demo->pipeline);
Chia-I Wu87544e72015-02-23 10:41:08 -07001581 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001583 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1584 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001585 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001586 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001587 for (j = 0; j < demo->textures[i].num_mem; j++)
1588 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001589 xglDestroyObject(demo->textures[i].sampler);
1590 }
1591
1592 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001593 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001594 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001595 for (j = 0; j < demo->depth.num_mem; j++)
1596 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001597
1598 xglDestroyObject(demo->uniform_data.view);
1599 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001600 xglDestroyObject(demo->uniform_data.buf);
1601 for (j = 0; j < demo->uniform_data.num_mem; j++)
1602 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001603
1604 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001605 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001606 xglDestroyObject(demo->buffers[i].view);
1607 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001608 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001609 }
1610
1611 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001612 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001613
1614 xcb_destroy_window(demo->connection, demo->window);
1615 xcb_disconnect(demo->connection);
1616}
1617
1618int main(void)
1619{
1620 struct demo demo;
1621
1622 demo_init(&demo);
1623
1624 demo_prepare(&demo);
1625 demo_create_window(&demo);
1626 demo_run(&demo);
1627
1628 demo_cleanup(&demo);
1629
1630 return 0;
1631}