blob: 11041ac19fe15c70c6f81e8ea802b4a22269ac1a [file] [log] [blame]
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <assert.h>
7
8#include <xcb/xcb.h>
9#include <xgl.h>
10#include <xglDbg.h>
11#include <xglWsiX11Ext.h>
12
Cody Northropfe3d8bc2015-03-17 14:54:35 -060013#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060014
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060016#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060017
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060018#define DEMO_BUFFER_COUNT 2
19#define DEMO_TEXTURE_COUNT 1
20
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060021/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070022 * structure to track all objects related to a texture.
23 */
24struct texture_objects {
25 XGL_SAMPLER sampler;
26
27 XGL_IMAGE image;
28 uint32_t num_mem;
29 XGL_GPU_MEMORY *mem;
30 XGL_IMAGE_VIEW view;
31 int32_t tex_width, tex_height;
32};
33
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060034static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060035 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060036};
37
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060038struct xglcube_vs_uniform {
39 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060040 float mvp[4][4];
41 float position[12*3][4];
42 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060043};
44
45struct xgltexcube_vs_uniform {
46 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060047 float mvp[4][4];
48 float position[12*3][4];
49 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060050};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060051
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052//--------------------------------------------------------------------------------------
53// Mesh and VertexFormat Data
54//--------------------------------------------------------------------------------------
55struct Vertex
56{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060057 float posX, posY, posZ, posW; // Position data
58 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060059};
60
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060061struct VertexPosTex
62{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060063 float posX, posY, posZ, posW; // Position data
64 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060065};
66
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060067#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060068#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060069
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060070static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060071 -1.0f,-1.0f,-1.0f, // Vertex 0
72 -1.0f,-1.0f, 1.0f,
73 -1.0f, 1.0f, 1.0f,
74
75 -1.0f, 1.0f, 1.0f, // Vertex 1
76 -1.0f, 1.0f,-1.0f,
77 -1.0f,-1.0f,-1.0f,
78
79 -1.0f,-1.0f,-1.0f, // Vertex 2
80 1.0f, 1.0f,-1.0f,
81 1.0f,-1.0f,-1.0f,
82
83 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060084 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060085 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060086
87 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060088 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060089 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060090
91 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060092 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060093 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060094
95 -1.0f, 1.0f,-1.0f, // Vertex 6
96 -1.0f, 1.0f, 1.0f,
97 1.0f, 1.0f, 1.0f,
98
99 -1.0f, 1.0f,-1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600100 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600101 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600102
103 1.0f, 1.0f,-1.0f, // Vertex 8
104 1.0f, 1.0f, 1.0f,
105 1.0f,-1.0f, 1.0f,
106
107 1.0f,-1.0f, 1.0f, // Vertex 9
108 1.0f,-1.0f,-1.0f,
109 1.0f, 1.0f,-1.0f,
110
111 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600112 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600113 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600114
115 -1.0f,-1.0f, 1.0f, // Vertex 11
116 1.0f,-1.0f, 1.0f,
117 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600118};
119
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600120static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600121 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600122 0.0f, 0.0f,
123 0.0f, 1.0f,
124
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600125 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600126 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600127 1.0f, 0.0f,
128
129// 0.0f, 1.0f, // Vertex 2
130// 1.0f, 0.0f,
131// 0.0f, 0.0f,
132
133// 0.0f, 1.0f, // Vertex 3
134// 1.0f, 0.0f,
135// 1.0f, 1.0f,
136
137 0.0f, 0.0f, // Vertex 2
138 1.0f, 1.0f,
139 1.0f, 0.0f,
140
141 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600142 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600143 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600144
145 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600146 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600147 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600148
149 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600150 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600151 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152
153 0.0f, 1.0f, // Vertex 6
154 1.0f, 1.0f,
155 1.0f, 0.0f,
156
157 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600158 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600159 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600161 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600164
165 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 0.0f, 1.0f,
168
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600169 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600171 0.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600172
173 1.0f, 0.0f, // Vertex 11
174 0.0f, 0.0f,
175 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600176};
177
178void dumpMatrix(const char *note, mat4x4 MVP)
179{
180 int i;
181
182 printf("%s: \n", note);
183 for (i=0; i<4; i++) {
184 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
185 }
186 printf("\n");
187 fflush(stdout);
188}
189
190void dumpVec4(const char *note, vec4 vector)
191{
192 printf("%s: \n", note);
193 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
194 printf("\n");
195 fflush(stdout);
196}
197
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600198struct demo {
199 xcb_connection_t *connection;
200 xcb_screen_t *screen;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700201 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600202
Jon Ashburn93cfc432015-01-29 15:47:01 -0700203 XGL_INSTANCE inst;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600204 XGL_PHYSICAL_GPU gpu;
205 XGL_DEVICE device;
206 XGL_QUEUE queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700207 uint32_t graphics_queue_node_index;
208 XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600209
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600210 XGL_FRAMEBUFFER framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600211 int width, height;
212 XGL_FORMAT format;
213
214 struct {
215 XGL_IMAGE image;
216 XGL_GPU_MEMORY mem;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700217 XGL_CMD_BUFFER cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600218
219 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wubb57f642014-11-07 14:30:34 +0800220 XGL_FENCE fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600221 } buffers[DEMO_BUFFER_COUNT];
222
223 struct {
224 XGL_FORMAT format;
225
226 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_DEPTH_STENCIL_VIEW view;
230 } depth;
231
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700232 struct texture_objects textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600233
234 struct {
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800235 XGL_BUFFER buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600236 uint32_t num_mem;
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700237 XGL_GPU_MEMORY *mem;
Chia-I Wu8ea21c72015-01-01 07:55:04 +0800238 XGL_BUFFER_VIEW view;
239 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600240 } uniform_data;
241
Chia-I Wu87544e72015-02-23 10:41:08 -0700242 XGL_DESCRIPTOR_SET_LAYOUT desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600243 XGL_PIPELINE pipeline;
244
Tony Barbour29645d02015-01-16 14:27:35 -0700245 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
246 XGL_DYNAMIC_RS_STATE_OBJECT raster;
247 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
248 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600249
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600250 mat4x4 projection_matrix;
251 mat4x4 view_matrix;
252 mat4x4 model_matrix;
253
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600254 float spin_angle;
255 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600256 bool pause;
257
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800258 XGL_DESCRIPTOR_REGION desc_region;
259 XGL_DESCRIPTOR_SET desc_set;
260
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600261 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700262 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600263
264 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600265 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600266};
267
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700268static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600269{
270 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
271 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000272 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600273 };
274 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
275 .view = demo->depth.view,
Mike Stroyan3bd2d892014-12-04 11:08:39 +0000276 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600277 };
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600278 const XGL_CLEAR_COLOR clear_color = {
279 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
280 .useRawValue = false,
281 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600282 const float clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600283 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700284 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
285 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600286 .pNext = NULL,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700287 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
288 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
289 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600290 XGL_RESULT err;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700291 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
292 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
293 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
294 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
295 .pNext = NULL,
296 .colorAttachmentCount = 1,
297 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
298 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
299 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600300 .width = demo->width,
301 .height = demo->height,
302 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700303 };
304 XGL_RENDER_PASS_CREATE_INFO rp_info;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600305 XGL_RENDER_PASS_BEGIN rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700306
307 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600308 err = xglCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700309 assert(!err);
310 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
311 rp_info.renderArea.extent.width = demo->width;
312 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600313 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
314 rp_info.pColorFormats = &demo->format;
315 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700316 rp_info.pColorLoadOps = &load_op;
317 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600318 rp_info.pColorLoadClearValues = &clear_color;
319 rp_info.depthStencilFormat = XGL_FMT_D16_UNORM;
320 rp_info.depthStencilLayout = depth_stencil.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700321 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600322 rp_info.depthLoadClearValue = clear_depth;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700323 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
324 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600325 rp_info.stencilLoadClearValue = 0;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700326 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600327 err = xglCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700328 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700330 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331 assert(!err);
332
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700333 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334 demo->pipeline);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700335 xglCmdBindDescriptorSet(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800336 demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600337
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700338 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
339 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
340 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600341 demo->color_blend);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700342 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600343 demo->depth_stencil);
344
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600345 xglCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600346 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
347 clear_range.baseMipLevel = 0;
348 clear_range.mipLevels = 1;
349 clear_range.baseArraySlice = 0;
350 clear_range.arraySize = 1;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700351 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600352 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -0600353 XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600354 clear_color, 1, &clear_range);
355
356 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700357 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -0600358 XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600359 clear_depth, 0, 1, &clear_range);
360
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700361 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600362 xglCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700364 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600365 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700366
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600367 xglDestroyObject(rp_begin.renderPass);
368 xglDestroyObject(rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369}
370
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600371
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600372void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600374 mat4x4 MVP, Model, VP;
375 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600376 uint8_t *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600377 XGL_RESULT err;
378
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600379 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600381 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600382 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700383 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600384 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600385
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700386 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600387 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388 assert(!err);
389
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600390 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600391
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700392 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600393 assert(!err);
394}
395
396static void demo_draw(struct demo *demo)
397{
398 const XGL_WSI_X11_PRESENT_INFO present = {
399 .destWindow = demo->window,
400 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800401 .async = true,
402 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800404 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600405 XGL_RESULT err;
406
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600407 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wubb57f642014-11-07 14:30:34 +0800408 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
409
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -0600410 uint32_t i, idx = 0;
411 XGL_MEMORY_REF *memRefs;
412 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
413 demo->depth.num_mem +
414 demo->textures[0].num_mem +
415 demo->uniform_data.num_mem));
416 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
417 memRefs[idx].mem = demo->depth.mem[i];
418 memRefs[idx].flags = 0;
419 }
420 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
421 memRefs[idx].mem = demo->textures[0].mem[i];
422 memRefs[idx].flags = 0;
423 }
424 memRefs[idx].mem = demo->buffers[0].mem;
425 memRefs[idx++].flags = 0;
426 memRefs[idx].mem = demo->buffers[1].mem;
427 memRefs[idx++].flags = 0;
428 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
429 memRefs[idx].mem = demo->uniform_data.mem[i];
430 memRefs[idx].flags = 0;
431 }
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -0700432 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Piers Daniell735ee532015-02-23 16:23:13 -0700433 idx, memRefs, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600434 assert(!err);
435
Chia-I Wubb57f642014-11-07 14:30:34 +0800436 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600437 assert(!err);
438
439 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
440}
441
442static void demo_prepare_buffers(struct demo *demo)
443{
444 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
445 .format = demo->format,
446 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
447 .extent = {
448 .width = demo->width,
449 .height = demo->height,
450 },
451 .flags = 0,
452 };
Chia-I Wubb57f642014-11-07 14:30:34 +0800453 const XGL_FENCE_CREATE_INFO fence = {
454 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
455 .pNext = NULL,
456 .flags = 0,
457 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600458 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600459 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600460
461 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
462 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
463 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
464 .pNext = NULL,
465 .format = demo->format,
466 .mipLevel = 0,
467 .baseArraySlice = 0,
468 .arraySize = 1,
469 };
470
471 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
472 &demo->buffers[i].image, &demo->buffers[i].mem);
473 assert(!err);
474
475 color_attachment_view.image = demo->buffers[i].image;
476
477 err = xglCreateColorAttachmentView(demo->device,
478 &color_attachment_view, &demo->buffers[i].view);
479 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800480
481 err = xglCreateFence(demo->device,
482 &fence, &demo->buffers[i].fence);
483 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600484 }
485}
486
487static void demo_prepare_depth(struct demo *demo)
488{
Jeremy Hayes9958ea82015-01-23 08:51:43 -0700489 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600490 const XGL_IMAGE_CREATE_INFO image = {
491 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
492 .pNext = NULL,
493 .imageType = XGL_IMAGE_2D,
494 .format = depth_format,
495 .extent = { demo->width, demo->height, 1 },
496 .mipLevels = 1,
497 .arraySize = 1,
498 .samples = 1,
499 .tiling = XGL_OPTIMAL_TILING,
500 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
501 .flags = 0,
502 };
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700503 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
504 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
505 .pNext = NULL,
506 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600507 XGL_MEMORY_ALLOC_INFO mem_alloc = {
508 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700509 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600510 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -0700511 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburnbdee4e82015-01-20 15:06:59 -0700512 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600513 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
514 };
515 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
516 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
517 .pNext = NULL,
518 .image = XGL_NULL_HANDLE,
519 .mipLevel = 0,
520 .baseArraySlice = 0,
521 .arraySize = 1,
522 .flags = 0,
523 };
Jon Ashburnb2a66652015-01-16 09:37:43 -0700524 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600525 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700526 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600527 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600528 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600529 uint32_t num_allocations = 0;
530 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600531
532 demo->depth.format = depth_format;
533
534 /* create image */
535 err = xglCreateImage(demo->device, &image,
536 &demo->depth.image);
537 assert(!err);
538
Jon Ashburnb2a66652015-01-16 09:37:43 -0700539
540 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
541 assert(!err && num_alloc_size == sizeof(num_allocations));
542 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
543 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
544 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600545 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700546 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
547 &mem_reqs_size, mem_reqs);
548 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700549 err = xglGetObjectInfo(demo->depth.image,
550 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
551 &img_reqs_size, &img_reqs);
552 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
553 img_alloc.usage = img_reqs.usage;
554 img_alloc.formatClass = img_reqs.formatClass;
555 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600556 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700557 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558
Jon Ashburnb2a66652015-01-16 09:37:43 -0700559 /* allocate memory */
560 err = xglAllocMemory(demo->device, &mem_alloc,
561 &(demo->depth.mem[i]));
562 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600563
Jon Ashburnb2a66652015-01-16 09:37:43 -0700564 /* bind memory */
565 err = xglBindObjectMemory(demo->depth.image, i,
566 demo->depth.mem[i], 0);
567 assert(!err);
568 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600569
570 /* create image view */
571 view.image = demo->depth.image;
572 err = xglCreateDepthStencilView(demo->device, &view,
573 &demo->depth.view);
574 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575}
576
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600577/** loadTexture
578 * loads a png file into an memory object, using cstdio , libpng.
579 *
580 * \param demo : Needed to access XGL calls
581 * \param filename : the png file to be loaded
582 * \param width : width of png, to be updated as a side effect of this function
583 * \param height : height of png, to be updated as a side effect of this function
584 *
585 * \return bool : an opengl texture id. true if successful?,
586 * should be validated by the client of this function.
587 *
588 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
589 * Modified to copy image to memory
590 *
591 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700592bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600593 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600594 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600595{
596 //header for testing if it is a png
597 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700598 int is_png, bit_depth, color_type,rowbytes;
599 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600600 png_structp png_ptr;
601 png_infop info_ptr, end_info;
602 png_byte *image_data;
603 png_bytep *row_pointers;
604
605 //open file as binary
606 FILE *fp = fopen(filename, "rb");
607 if (!fp) {
608 return false;
609 }
610
611 //read the header
612 fread(header, 1, 8, fp);
613
614 //test if png
615 is_png = !png_sig_cmp(header, 0, 8);
616 if (!is_png) {
617 fclose(fp);
618 return false;
619 }
620
621 //create png struct
622 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
623 NULL, NULL);
624 if (!png_ptr) {
625 fclose(fp);
626 return (false);
627 }
628
629 //create png info struct
630 info_ptr = png_create_info_struct(png_ptr);
631 if (!info_ptr) {
632 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
633 fclose(fp);
634 return (false);
635 }
636
637 //create png info struct
638 end_info = png_create_info_struct(png_ptr);
639 if (!end_info) {
640 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
641 fclose(fp);
642 return (false);
643 }
644
645 //png error stuff, not sure libpng man suggests this.
646 if (setjmp(png_jmpbuf(png_ptr))) {
647 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
648 fclose(fp);
649 return (false);
650 }
651
652 //init png reading
653 png_init_io(png_ptr, fp);
654
655 //let libpng know you already read the first 8 bytes
656 png_set_sig_bytes(png_ptr, 8);
657
658 // read all the info up to the image data
659 png_read_info(png_ptr, info_ptr);
660
661 // get info about png
662 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
663 NULL, NULL, NULL);
664
665 //update width and height based on png info
666 *width = twidth;
667 *height = theight;
668
669 // Require that incoming texture be 8bits per color component
670 // and 4 components (RGBA).
671 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
672 png_get_channels(png_ptr, info_ptr) != 4) {
673 return false;
674 }
675
676 if (rgba_data == NULL) {
677 // If data pointer is null, we just want the width & height
678 // clean up memory and close stuff
679 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
680 fclose(fp);
681
682 return true;
683 }
684
685 // Update the png info struct.
686 png_read_update_info(png_ptr, info_ptr);
687
688 // Row size in bytes.
689 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
690
691 // Allocate the image_data as a big block, to be given to opengl
692 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
693 if (!image_data) {
694 //clean up memory and close stuff
695 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
696 fclose(fp);
697 return false;
698 }
699
700 // row_pointers is for pointing to image_data for reading the png with libpng
701 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
702 if (!row_pointers) {
703 //clean up memory and close stuff
704 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
705 // delete[] image_data;
706 fclose(fp);
707 return false;
708 }
709 // set the individual row_pointers to point at the correct offsets of image_data
710 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700711 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600712
713 // read the png into image_data through row_pointers
714 png_read_image(png_ptr, row_pointers);
715
716 // clean up memory and close stuff
717 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
718 free(row_pointers);
719 free(image_data);
720 fclose(fp);
721
722 return true;
723}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600724
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700725static void demo_prepare_texture_image(struct demo *demo,
726 const char *filename,
727 struct texture_objects *tex_objs,
728 XGL_IMAGE_TILING tiling,
729 XGL_FLAGS mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600730{
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700731 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600732 int32_t tex_width;
733 int32_t tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600734 XGL_RESULT err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700735
736 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
737 assert(err);
738
739 tex_objs->tex_width = tex_width;
740 tex_objs->tex_height = tex_height;
741
742 const XGL_IMAGE_CREATE_INFO image_create_info = {
743 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
744 .pNext = NULL,
745 .imageType = XGL_IMAGE_2D,
746 .format = tex_format,
747 .extent = { tex_width, tex_height, 1 },
748 .mipLevels = 1,
749 .arraySize = 1,
750 .samples = 1,
751 .tiling = tiling,
752 .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
753 .flags = 0,
754 };
Piers Daniell735ee532015-02-23 16:23:13 -0700755 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
756 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
757 .pNext = NULL,
758 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700759 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
760 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
Piers Daniell735ee532015-02-23 16:23:13 -0700761 .pNext = &buf_alloc,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700762 };
763 XGL_MEMORY_ALLOC_INFO mem_alloc = {
764 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
765 .pNext = &img_alloc,
766 .allocationSize = 0,
767 .memProps = mem_props,
768 .memType = XGL_MEMORY_TYPE_IMAGE,
769 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
770 };
771
772 XGL_MEMORY_REQUIREMENTS *mem_reqs;
773 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Piers Daniell735ee532015-02-23 16:23:13 -0700774 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
775 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700776 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
777 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
778 uint32_t num_allocations = 0;
779 size_t num_alloc_size = sizeof(num_allocations);
780
781 err = xglCreateImage(demo->device, &image_create_info,
782 &tex_objs->image);
783 assert(!err);
784
785 err = xglGetObjectInfo(tex_objs->image,
786 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
787 &num_alloc_size, &num_allocations);
788 assert(!err && num_alloc_size == sizeof(num_allocations));
789 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
790 tex_objs->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
791 err = xglGetObjectInfo(tex_objs->image,
792 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
793 &mem_reqs_size, mem_reqs);
794 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
795 err = xglGetObjectInfo(tex_objs->image,
796 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
797 &img_reqs_size, &img_reqs);
798 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
799 img_alloc.usage = img_reqs.usage;
800 img_alloc.formatClass = img_reqs.formatClass;
801 img_alloc.samples = img_reqs.samples;
802 mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
803 for (uint32_t j = 0; j < num_allocations; j ++) {
Courtney Goeltzenleuchter02ba99a2015-02-25 11:48:28 -0700804 mem_alloc.memType = mem_reqs[j].memType;
Piers Daniell735ee532015-02-23 16:23:13 -0700805 mem_alloc.allocationSize = mem_reqs[j].size;
806
807 if (mem_alloc.memType == XGL_MEMORY_TYPE_BUFFER) {
808 err = xglGetObjectInfo(tex_objs->image,
809 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
810 &buf_reqs_size, &buf_reqs);
811 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
812 buf_alloc.usage = buf_reqs.usage;
813 img_alloc.pNext = &buf_alloc;
814 } else {
815 img_alloc.pNext = 0;
816 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700817
818 /* allocate memory */
819 err = xglAllocMemory(demo->device, &mem_alloc,
820 &(tex_objs->mem[j]));
821 assert(!err);
822
823 /* bind memory */
824 err = xglBindObjectMemory(tex_objs->image, j, tex_objs->mem[j], 0);
825 assert(!err);
826 }
827 free(mem_reqs);
828 mem_reqs = NULL;
829
830 tex_objs->num_mem = num_allocations;
831
832 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
833 const XGL_IMAGE_SUBRESOURCE subres = {
834 .aspect = XGL_IMAGE_ASPECT_COLOR,
835 .mipLevel = 0,
836 .arraySlice = 0,
837 };
838 XGL_SUBRESOURCE_LAYOUT layout;
839 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
840 void *data;
841
842 err = xglGetImageSubresourceInfo(tex_objs->image, &subres,
843 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
844 &layout_size, &layout);
845 assert(!err && layout_size == sizeof(layout));
846 /* Linear texture must be within a single memory object */
847 assert(num_allocations == 1);
848
849 err = xglMapMemory(tex_objs->mem[0], 0, &data);
850 assert(!err);
851
852 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
853 fprintf(stderr, "Error loading texture: %s\n", filename);
854 }
855
856 err = xglUnmapMemory(tex_objs->mem[0]);
857 assert(!err);
858 }
859}
860
861static void demo_destroy_texture_image(struct texture_objects *tex_objs)
862{
863 /* clean up staging resources */
864 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
865 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
866 xglFreeMemory(tex_objs->mem[j]);
867 }
868
869 free(tex_objs->mem);
870 xglDestroyObject(tex_objs->image);
871}
872
873static void demo_prepare_textures(struct demo *demo)
874{
875 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
876 XGL_FORMAT_PROPERTIES props;
877 size_t size = sizeof(props);
878 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600879 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600880
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700881 err = xglGetFormatInfo(demo->device, tex_format,
882 XGL_INFO_TYPE_FORMAT_PROPERTIES,
883 &size, &props);
884 assert(!err);
885
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600886 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700887
888 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
889 /* Device can texture using linear textures */
890 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
891 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
892 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT){
893 /* Must use staging buffer to copy linear texture to optimized */
894 struct texture_objects staging_texture;
895
896 memset(&staging_texture, 0, sizeof(staging_texture));
897 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
898 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
899
900 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
901 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
902
903 XGL_CMD_BUFFER staging_cmd_buf;
904 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {
905 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
906 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700907 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700908 .flags = 0
909 };
910
911 err = xglCreateCommandBuffer(demo->device, &cmd_buf_create_info, &staging_cmd_buf);
912 assert(!err);
913
914 /* Copy staging texture to usable texture */
915 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {
916 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
917 .pNext = NULL,
918 .flags = 0
919 };
920
921 err = xglResetCommandBuffer(staging_cmd_buf);
922 assert(!err);
923
924 err = xglBeginCommandBuffer(staging_cmd_buf, &cmd_buf_begin_info);
925 assert(!err);
926
927 XGL_IMAGE_COPY copy_region = {
928 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
929 .srcOffset = { 0, 0, 0 },
930 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
931 .destOffset = { 0, 0, 0 },
932 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
933 };
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -0600934 xglCmdCopyImage(staging_cmd_buf,
935 staging_texture.image,
936 XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
937 demo->textures[i].image,
938 XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
939 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700940
941 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
942 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
943 .pNext = NULL,
944 .outputMask = XGL_MEMORY_OUTPUT_COPY_BIT,
945 .inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT,
946 .oldLayout = XGL_IMAGE_LAYOUT_GENERAL,
947 .newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
948 .image = staging_texture.image,
949 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
950 };
951 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
952
Courtney Goeltzenleuchterd695c822015-03-24 18:02:34 -0600953 XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_GPU_COMMANDS_COMPLETE };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700954 XGL_PIPELINE_BARRIER pipeline_barrier;
955 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
956 pipeline_barrier.pNext = NULL;
957 pipeline_barrier.eventCount = 1;
958 pipeline_barrier.pEvents = set_events;
959 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
960 pipeline_barrier.memBarrierCount = 1;
961 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
962
963 // write barrier to the command buffer
964 xglCmdPipelineBarrier(staging_cmd_buf, &pipeline_barrier);
965
966 err = xglEndCommandBuffer(staging_cmd_buf);
967 assert(!err);
968
969 const XGL_CMD_BUFFER cmd_bufs[] = { staging_cmd_buf };
970 XGL_MEMORY_REF mem_refs[16];
971 uint32_t num_refs = 0;
972
973 for (uint32_t j = 0; j < staging_texture.num_mem; j++) {
974 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
975 mem_refs[num_refs].mem = staging_texture.mem[j];
976 num_refs++;
977 assert(num_refs < 16);
978 }
979
980 for (uint32_t j = 0; j < demo->textures[i].num_mem; j++) {
981 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
982 mem_refs[num_refs].mem = demo->textures[i].mem[j];
983 num_refs++;
984 assert(num_refs < 16);
985 }
986
987 err = xglQueueSubmit(demo->queue, 1, cmd_bufs,
988 num_refs, mem_refs, XGL_NULL_HANDLE);
989 assert(!err);
990
991 err = xglQueueWaitIdle(demo->queue);
992 assert(!err);
993
994 demo_destroy_texture_image(&staging_texture);
995
996 xglDestroyObject(staging_cmd_buf);
997 } else {
998 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
999 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1000 }
1001
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001002 const XGL_SAMPLER_CREATE_INFO sampler = {
1003 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
1004 .pNext = NULL,
1005 .magFilter = XGL_TEX_FILTER_NEAREST,
1006 .minFilter = XGL_TEX_FILTER_NEAREST,
1007 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001008 .addressU = XGL_TEX_ADDRESS_CLAMP,
1009 .addressV = XGL_TEX_ADDRESS_CLAMP,
1010 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001011 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001012 .maxAnisotropy = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001013 .compareFunc = XGL_COMPARE_NEVER,
1014 .minLod = 0.0f,
1015 .maxLod = 0.0f,
1016 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
1017 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001018
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001019 XGL_IMAGE_VIEW_CREATE_INFO view = {
1020 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1021 .pNext = NULL,
1022 .image = XGL_NULL_HANDLE,
1023 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001024 .format = tex_format,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001025 .channels = { XGL_CHANNEL_SWIZZLE_R,
1026 XGL_CHANNEL_SWIZZLE_G,
1027 XGL_CHANNEL_SWIZZLE_B,
1028 XGL_CHANNEL_SWIZZLE_A, },
1029 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
1030 .minLod = 0.0f,
1031 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001032
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001033 /* create sampler */
1034 err = xglCreateSampler(demo->device, &sampler,
1035 &demo->textures[i].sampler);
1036 assert(!err);
1037
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038 /* create image view */
1039 view.image = demo->textures[i].image;
1040 err = xglCreateImageView(demo->device, &view,
1041 &demo->textures[i].view);
1042 assert(!err);
1043 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001045
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001046void demo_prepare_cube_data_buffer(struct demo *demo)
1047{
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001048 XGL_BUFFER_CREATE_INFO buf_info;
1049 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001050 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1051 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1052 .pNext = NULL,
1053 };
1054 XGL_MEMORY_ALLOC_INFO alloc_info = {
1055 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1056 .pNext = &buf_alloc,
1057 .allocationSize = 0,
Jon Ashburnc483a8c2015-01-20 13:55:32 -07001058 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburnbdee4e82015-01-20 15:06:59 -07001059 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001060 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1061 };
1062 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001063 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001064 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001065 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1066 uint32_t num_allocations = 0;
1067 size_t num_alloc_size = sizeof(num_allocations);
1068 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001069 int i;
1070 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001071 XGL_RESULT err;
1072 struct xgltexcube_vs_uniform data;
1073
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001074 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001075 mat4x4_mul(MVP, VP, demo->model_matrix);
1076 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001077// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001078
1079 for (i=0; i<12*3; i++) {
1080 data.position[i][0] = g_vertex_buffer_data[i*3];
1081 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1082 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1083 data.position[i][3] = 1.0f;
1084 data.attr[i][0] = g_uv_buffer_data[2*i];
1085 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1086 data.attr[i][2] = 0;
1087 data.attr[i][3] = 0;
1088 }
1089
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001090 memset(&buf_info, 0, sizeof(buf_info));
1091 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1092 buf_info.size = sizeof(data);
1093 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1094 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1095 assert(!err);
1096
1097 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001098 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1099 &num_alloc_size, &num_allocations);
1100 assert(!err && num_alloc_size == sizeof(num_allocations));
1101 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1102 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1103 demo->uniform_data.num_mem = num_allocations;
1104 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001105 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001106 &mem_reqs_size, mem_reqs);
1107 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1108 err = xglGetObjectInfo(demo->uniform_data.buf,
1109 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1110 &buf_reqs_size, &buf_reqs);
1111 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1112 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001113 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001114 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001115
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001116 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1117 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001118
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001119 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001120 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001121
Piers Daniell735ee532015-02-23 16:23:13 -07001122 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001123
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001124 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1125 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001126
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001127 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1128 demo->uniform_data.mem[i], 0);
1129 assert(!err);
1130 }
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001131
1132 memset(&view_info, 0, sizeof(view_info));
1133 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1134 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu378caea2015-01-16 22:31:25 +08001135 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001136 view_info.offset = 0;
1137 view_info.range = sizeof(data);
1138
1139 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1140 assert(!err);
1141
1142 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1143 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001144}
1145
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001146static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001147{
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001148 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
1149 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1150 .pNext = NULL,
1151 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1152 .count = DEMO_TEXTURE_COUNT,
1153 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
1154 .immutableSampler = XGL_NULL_HANDLE,
1155 };
Chia-I Wu87544e72015-02-23 10:41:08 -07001156 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
1157 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1158 .pNext = &descriptor_layout_fs,
1159 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1160 .count = 1,
1161 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
1162 .immutableSampler = XGL_NULL_HANDLE,
1163 };
Chia-I Wu06f82812015-02-23 10:41:08 -07001164
1165 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001166 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001167
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001168 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu06f82812015-02-23 10:41:08 -07001169 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001170 XGL_NULL_HANDLE, &descriptor_layout_vs,
Chia-I Wu87544e72015-02-23 10:41:08 -07001171 &demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001172 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001173}
1174
1175static XGL_SHADER demo_prepare_shader(struct demo *demo,
1176 XGL_PIPELINE_SHADER_STAGE stage,
1177 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001178 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001179{
1180 XGL_SHADER_CREATE_INFO createInfo;
1181 XGL_SHADER shader;
1182 XGL_RESULT err;
1183
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001184
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001185 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1186 createInfo.pNext = NULL;
1187
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001188#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001189 createInfo.codeSize = size;
1190 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001191 createInfo.flags = 0;
1192
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001193 err = xglCreateShader(demo->device, &createInfo, &shader);
1194 if (err) {
1195 free((void *) createInfo.pCode);
1196 }
1197#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001198 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001199 // to the driver "under the covers"
1200 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1201 createInfo.pCode = malloc(createInfo.codeSize);
1202 createInfo.flags = 0;
1203
1204 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001205 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206 ((uint32_t *) createInfo.pCode)[1] = 0;
1207 ((uint32_t *) createInfo.pCode)[2] = stage;
1208 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1209
1210 err = xglCreateShader(demo->device, &createInfo, &shader);
1211 if (err) {
1212 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001213 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001214 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001215#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001216
1217 return shader;
1218}
1219
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001220char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001221{
1222 long int size;
1223 void *shader_code;
1224
1225 FILE *fp = fopen(filename, "rb");
1226 if (!fp) return NULL;
1227
1228 fseek(fp, 0L, SEEK_END);
1229 size = ftell(fp);
1230
1231 fseek(fp, 0L, SEEK_SET);
1232
1233 shader_code = malloc(size);
1234 fread(shader_code, size, 1, fp);
1235
1236 *psize = size;
1237
1238 return shader_code;
1239}
1240
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241static XGL_SHADER demo_prepare_vs(struct demo *demo)
1242{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001243#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001244 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001245 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001246
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001247 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001248
1249 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1250 vertShaderCode, size);
1251#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001252 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001253 "#version 140\n"
1254 "#extension GL_ARB_separate_shader_objects : enable\n"
1255 "#extension GL_ARB_shading_language_420pack : enable\n"
1256 "\n"
1257 "layout(binding = 0) uniform buf {\n"
1258 " mat4 MVP;\n"
1259 " vec4 position[12*3];\n"
1260 " vec4 attr[12*3];\n"
1261 "} ubuf;\n"
1262 "\n"
1263 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001264 "\n"
1265 "void main() \n"
1266 "{\n"
1267 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001268 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1269 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001270
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001271 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1272 (const void *) vertShaderText,
1273 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001274#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001275}
1276
1277static XGL_SHADER demo_prepare_fs(struct demo *demo)
1278{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001279#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001280 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001281 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001282
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001283 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001284
1285 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1286 fragShaderCode, size);
1287#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001288 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001289 "#version 140\n"
1290 "#extension GL_ARB_separate_shader_objects : enable\n"
1291 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001292 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001293 "\n"
1294 "layout (location = 0) in vec4 texcoord;\n"
1295 "void main() {\n"
1296 " gl_FragColor = texture(tex, texcoord.xy);\n"
1297 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001298
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001299 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1300 (const void *) fragShaderText,
1301 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001302#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001303}
1304
1305static void demo_prepare_pipeline(struct demo *demo)
1306{
1307 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001308 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1309 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbour29645d02015-01-16 14:27:35 -07001310 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1311 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001312 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1313 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbour29645d02015-01-16 14:27:35 -07001314 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1315 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001316 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317
1318 memset(&pipeline, 0, sizeof(pipeline));
1319 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu87544e72015-02-23 10:41:08 -07001320 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001321
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001322 memset(&ia, 0, sizeof(ia));
1323 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1324 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1325
1326 memset(&rs, 0, sizeof(rs));
1327 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001328 rs.fillMode = XGL_FILL_SOLID;
Mike Stroyan16b3d982015-03-19 14:29:04 -06001329 rs.cullMode = XGL_CULL_BACK;
Tony Barbour29645d02015-01-16 14:27:35 -07001330 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001331
1332 memset(&cb, 0, sizeof(cb));
1333 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001334 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1335 memset(att_state, 0, sizeof(att_state));
1336 att_state[0].format = demo->format;
1337 att_state[0].channelWriteMask = 0xf;
1338 att_state[0].blendEnable = XGL_FALSE;
1339 cb.attachmentCount = 1;
1340 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001341
Tony Barbour29645d02015-01-16 14:27:35 -07001342 memset(&vp, 0, sizeof(vp));
1343 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001344 vp.numViewports = 1;
Mike Stroyan259bc542015-03-24 15:13:34 -06001345 vp.clipOrigin = XGL_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001346
1347 memset(&ds, 0, sizeof(ds));
1348 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1349 ds.format = demo->depth.format;
1350 ds.depthTestEnable = XGL_TRUE;
1351 ds.depthWriteEnable = XGL_TRUE;
1352 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1353 ds.depthBoundsEnable = XGL_FALSE;
1354 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1355 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1356 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1357 ds.stencilTestEnable = XGL_FALSE;
1358 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001359
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001360 memset(&vs, 0, sizeof(vs));
1361 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1362 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001363 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001364 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365
1366 memset(&fs, 0, sizeof(fs));
1367 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1368 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1369 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001370 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001371
1372 memset(&ms, 0, sizeof(ms));
1373 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1374 ms.sampleMask = 1;
1375 ms.multisampleEnable = XGL_FALSE;
1376 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001377
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001378 pipeline.pNext = (const void *) &ia;
1379 ia.pNext = (const void *) &rs;
1380 rs.pNext = (const void *) &cb;
1381 cb.pNext = (const void *) &ms;
1382 ms.pNext = (const void *) &vp;
1383 vp.pNext = (const void *) &ds;
1384 ds.pNext = (const void *) &vs;
1385 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001386
1387 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1388 assert(!err);
1389
1390 xglDestroyObject(vs.shader.shader);
1391 xglDestroyObject(fs.shader.shader);
1392}
1393
1394static void demo_prepare_dynamic_states(struct demo *demo)
1395{
Tony Barbour29645d02015-01-16 14:27:35 -07001396 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1397 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1398 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1399 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 XGL_RESULT err;
1401
Tony Barbour29645d02015-01-16 14:27:35 -07001402 memset(&viewport_create, 0, sizeof(viewport_create));
1403 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001404 viewport_create.viewportAndScissorCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001405 XGL_VIEWPORT viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001406 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001407 viewport.height = (float) demo->height;
1408 viewport.width = (float) demo->width;
1409 viewport.minDepth = (float) 0.0f;
1410 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001411 viewport_create.pViewports = &viewport;
1412 XGL_RECT scissor;
1413 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001414 scissor.extent.width = demo->width;
1415 scissor.extent.height = demo->height;
1416 scissor.offset.x = 0;
1417 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001418 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419
1420 memset(&raster, 0, sizeof(raster));
Tony Barbour29645d02015-01-16 14:27:35 -07001421 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001422 raster.pointSize = 1.0;
1423 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424
1425 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour29645d02015-01-16 14:27:35 -07001426 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001427 color_blend.blendConst[0] = 1.0f;
1428 color_blend.blendConst[1] = 1.0f;
1429 color_blend.blendConst[2] = 1.0f;
1430 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001431
1432 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour29645d02015-01-16 14:27:35 -07001433 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001434 depth_stencil.minDepth = 0.0f;
1435 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001436 depth_stencil.stencilBackRef = 0;
1437 depth_stencil.stencilFrontRef = 0;
1438 depth_stencil.stencilReadMask = 0xff;
1439 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001440
Tony Barbour29645d02015-01-16 14:27:35 -07001441 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001442 assert(!err);
1443
Tony Barbour29645d02015-01-16 14:27:35 -07001444 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001445 assert(!err);
1446
Tony Barbour29645d02015-01-16 14:27:35 -07001447 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001448 &color_blend, &demo->color_blend);
1449 assert(!err);
1450
Tony Barbour29645d02015-01-16 14:27:35 -07001451 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001452 &depth_stencil, &demo->depth_stencil);
1453 assert(!err);
1454}
1455
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001456static void demo_prepare_descriptor_region(struct demo *demo)
1457{
1458 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1459 [0] = {
1460 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1461 .count = 1,
1462 },
1463 [1] = {
1464 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1465 .count = DEMO_TEXTURE_COUNT,
1466 },
1467 };
1468 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1469 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1470 .pNext = NULL,
1471 .count = 2,
1472 .pTypeCount = type_counts,
1473 };
1474 XGL_RESULT err;
1475
1476 err = xglCreateDescriptorRegion(demo->device,
1477 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1478 &descriptor_region, &demo->desc_region);
1479 assert(!err);
1480}
1481
1482static void demo_prepare_descriptor_set(struct demo *demo)
1483{
1484 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1485 &demo->uniform_data.attach;
1486 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1487 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1488 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1489 XGL_UPDATE_BUFFERS update_vs;
1490 XGL_RESULT err;
1491 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001492 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001493
1494 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1495 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1496 view_info[i].pNext = NULL;
1497 view_info[i].view = demo->textures[i].view,
1498 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1499
1500 combined_info[i].pSampler = demo->textures[i].sampler;
1501 combined_info[i].pImageView = &view_info[i];
1502 }
1503
1504 memset(&update_vs, 0, sizeof(update_vs));
1505 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1506 update_vs.pNext = &update_fs;
1507 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1508 update_vs.count = 1;
1509 update_vs.pBufferViews = &view_info_vs;
1510
1511 memset(&update_fs, 0, sizeof(update_fs));
1512 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1513 update_fs.index = 1;
1514 update_fs.count = DEMO_TEXTURE_COUNT;
1515 update_fs.pSamplerImageViews = combined_info;
1516
1517 err = xglAllocDescriptorSets(demo->desc_region,
1518 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001519 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001520 &demo->desc_set, &count);
1521 assert(!err && count == 1);
1522
1523 xglBeginDescriptorRegionUpdate(demo->device,
1524 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1525
1526 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1527 xglUpdateDescriptors(demo->desc_set, &update_vs);
1528
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001529 xglEndDescriptorRegionUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001530}
1531
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532static void demo_prepare(struct demo *demo)
1533{
1534 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1535 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1536 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001537 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001538 .flags = 0,
1539 };
1540 XGL_RESULT err;
1541
1542 demo_prepare_buffers(demo);
1543 demo_prepare_depth(demo);
1544 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001545 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001546
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001547 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001548 demo_prepare_pipeline(demo);
1549 demo_prepare_dynamic_states(demo);
1550
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001551 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1552 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1553 assert(!err);
1554 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001555
1556 demo_prepare_descriptor_region(demo);
1557 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001558
1559
1560 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1561 demo->current_buffer = i;
1562 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1563 }
1564
1565 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001566}
1567
1568static void demo_handle_event(struct demo *demo,
1569 const xcb_generic_event_t *event)
1570{
Piers Daniell735ee532015-02-23 16:23:13 -07001571 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001572 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001573 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001574 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001575 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001576 case XCB_CLIENT_MESSAGE:
1577 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1578 (*demo->atom_wm_delete_window).atom) {
1579 demo->quit = true;
1580 }
1581 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582 case XCB_KEY_RELEASE:
1583 {
1584 const xcb_key_release_event_t *key =
1585 (const xcb_key_release_event_t *) event;
1586
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001587 switch (key->detail) {
1588 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001589 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001590 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001591 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001592 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001593 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001594 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001595 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001596 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001597 case 0x41:
1598 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001599 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001600 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001601 }
1602 break;
1603 default:
1604 break;
1605 }
1606}
1607
1608static void demo_run(struct demo *demo)
1609{
1610 xcb_flush(demo->connection);
1611
1612 while (!demo->quit) {
1613 xcb_generic_event_t *event;
1614
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001615 if (demo->pause) {
1616 event = xcb_wait_for_event(demo->connection);
1617 } else {
1618 event = xcb_poll_for_event(demo->connection);
1619 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001620 if (event) {
1621 demo_handle_event(demo, event);
1622 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001623 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001624
1625 // Wait for work to finish before updating MVP.
1626 xglDeviceWaitIdle(demo->device);
1627 demo_update_data_buffer(demo);
1628
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001629 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001630
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001631 // Wait for work to finish before updating MVP.
1632 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001633 }
1634}
1635
1636static void demo_create_window(struct demo *demo)
1637{
1638 uint32_t value_mask, value_list[32];
1639
1640 demo->window = xcb_generate_id(demo->connection);
1641
1642 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1643 value_list[0] = demo->screen->black_pixel;
1644 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1645 XCB_EVENT_MASK_EXPOSURE;
1646
1647 xcb_create_window(demo->connection,
1648 XCB_COPY_FROM_PARENT,
1649 demo->window, demo->screen->root,
1650 0, 0, demo->width, demo->height, 0,
1651 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1652 demo->screen->root_visual,
1653 value_mask, value_list);
1654
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001655 /* Magic code that will send notification when window is destroyed */
1656 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1657 "WM_PROTOCOLS");
1658 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1659
1660 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1661 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1662
1663 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1664 demo->window, (*reply).atom, 4, 32, 1,
1665 &(*demo->atom_wm_delete_window).atom);
1666 free(reply);
1667
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001668 xcb_map_window(demo->connection, demo->window);
1669}
1670
1671static void demo_init_xgl(struct demo *demo)
1672{
1673 const XGL_APPLICATION_INFO app = {
1674 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1675 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001676 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001677 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001678 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001679 .engineVersion = 0,
Courtney Goeltzenleuchter97a89c42015-02-23 17:40:15 -07001680 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001681 };
1682 const XGL_WSI_X11_CONNECTION_INFO connection = {
1683 .pConnection = demo->connection,
1684 .root = demo->screen->root,
1685 .provider = 0,
1686 };
1687 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1688 .queueNodeIndex = 0,
1689 .queueCount = 1,
1690 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001691 const char *ext_names[] = {
Chia-I Wub2c81932014-12-27 15:16:07 +08001692 "XGL_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001693 };
1694 const XGL_DEVICE_CREATE_INFO device = {
1695 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1696 .pNext = NULL,
1697 .queueRecordCount = 1,
1698 .pRequestedQueues = &queue,
1699 .extensionCount = 1,
1700 .ppEnabledExtensionNames = ext_names,
1701 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1702 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1703 };
1704 XGL_RESULT err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001705 uint32_t gpu_count;
1706 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001707 size_t data_size;
1708 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001709
Jon Ashburn93cfc432015-01-29 15:47:01 -07001710 err = xglCreateInstance(&app, NULL, &demo->inst);
Ian Elliott3979e282015-04-03 15:24:55 -06001711 if (err == XGL_ERROR_INCOMPATIBLE_DRIVER) {
1712 printf("Cannot find a compatible Vulkan installable client driver "
1713 "(ICD).\nExiting ...\n");
1714 fflush(stdout);
1715 exit(1);
1716 } else {
1717 assert(!err);
1718 }
Jon Ashburn93cfc432015-01-29 15:47:01 -07001719 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001720 assert(!err && gpu_count == 1);
1721
1722 for (i = 0; i < device.extensionCount; i++) {
1723 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1724 assert(!err);
1725 }
1726
1727 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1728 assert(!err);
1729
1730 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1731 assert(!err);
1732
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001733 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1734 &data_size, NULL);
1735 assert(!err);
1736
1737 demo->queue_props = (XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *) malloc(data_size);
1738 err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
1739 &data_size, demo->queue_props);
1740 assert(!err);
1741 queue_count = data_size / sizeof(XGL_PHYSICAL_GPU_QUEUE_PROPERTIES);
1742 assert(queue_count >= 1);
1743
1744 for (i = 0; i < queue_count; i++) {
1745 if (demo->queue_props[i].queueFlags & XGL_QUEUE_GRAPHICS_BIT)
1746 break;
1747 }
1748 assert(i < queue_count);
1749 demo->graphics_queue_node_index = i;
1750
1751 err = xglGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752 0, &demo->queue);
1753 assert(!err);
1754}
1755
1756static void demo_init_connection(struct demo *demo)
1757{
1758 const xcb_setup_t *setup;
1759 xcb_screen_iterator_t iter;
1760 int scr;
1761
1762 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001763 if (demo->connection == NULL) {
1764 printf("Cannot find a compatible Vulkan installable client driver "
1765 "(ICD).\nExiting ...\n");
1766 fflush(stdout);
1767 exit(1);
1768 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001769
1770 setup = xcb_get_setup(demo->connection);
1771 iter = xcb_setup_roots_iterator(setup);
1772 while (scr-- > 0)
1773 xcb_screen_next(&iter);
1774
1775 demo->screen = iter.data;
1776}
1777
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001778static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001779{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001780 vec3 eye = {0.0f, 3.0f, 5.0f};
1781 vec3 origin = {0, 0, 0};
1782 vec3 up = {0.0f, -1.0f, 0.0};
1783
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001784 memset(demo, 0, sizeof(*demo));
1785
Piers Daniell735ee532015-02-23 16:23:13 -07001786 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001787 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1788 demo->use_staging_buffer = true;
1789 }
1790
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001791 demo_init_connection(demo);
1792 demo_init_xgl(demo);
1793
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001794 demo->width = 500;
1795 demo->height = 500;
Jeremy Hayes9958ea82015-01-23 08:51:43 -07001796 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001797
1798 demo->spin_angle = 0.01f;
1799 demo->spin_increment = 0.01f;
1800 demo->pause = false;
1801
Piers Daniell735ee532015-02-23 16:23:13 -07001802 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001803 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1804 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001805}
1806
1807static void demo_cleanup(struct demo *demo)
1808{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001809 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001810
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001811 xglDestroyObject(demo->desc_set);
1812 xglDestroyObject(demo->desc_region);
1813
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001814 xglDestroyObject(demo->viewport);
1815 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001816 xglDestroyObject(demo->color_blend);
1817 xglDestroyObject(demo->depth_stencil);
1818
1819 xglDestroyObject(demo->pipeline);
Chia-I Wu87544e72015-02-23 10:41:08 -07001820 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001821
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001822 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1823 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001824 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001825 xglDestroyObject(demo->textures[i].image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001826 for (j = 0; j < demo->textures[i].num_mem; j++)
1827 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001828 xglDestroyObject(demo->textures[i].sampler);
1829 }
1830
1831 xglDestroyObject(demo->depth.view);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001832 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001833 xglDestroyObject(demo->depth.image);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001834 for (j = 0; j < demo->depth.num_mem; j++)
1835 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001836
1837 xglDestroyObject(demo->uniform_data.view);
1838 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001839 xglDestroyObject(demo->uniform_data.buf);
1840 for (j = 0; j < demo->uniform_data.num_mem; j++)
1841 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001842
1843 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wubb57f642014-11-07 14:30:34 +08001844 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001845 xglDestroyObject(demo->buffers[i].view);
1846 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001847 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001848 }
1849
1850 xglDestroyDevice(demo->device);
Jon Ashburn93cfc432015-01-29 15:47:01 -07001851 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852
1853 xcb_destroy_window(demo->connection, demo->window);
1854 xcb_disconnect(demo->connection);
1855}
1856
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001857int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858{
1859 struct demo demo;
1860
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001861 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001862
1863 demo_prepare(&demo);
1864 demo_create_window(&demo);
1865 demo_run(&demo);
1866
1867 demo_cleanup(&demo);
1868
1869 return 0;
1870}