blob: 6553962e83f3ce21adc3dddc0ed754cb01ddf5c7 [file] [log] [blame]
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <assert.h>
7
8#include <xcb/xcb.h>
9#include <xgl.h>
10#include <xglDbg.h>
11#include <xglWsiX11Ext.h>
12
Cody Northropd4e020a2015-03-17 14:54:35 -060013#include "icd-spv.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060014
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060016#include <png.h>
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060017
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060018#define DEMO_BUFFER_COUNT 2
19#define DEMO_TEXTURE_COUNT 1
20
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060021/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070022 * structure to track all objects related to a texture.
23 */
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 Goeltzenleuchter17223602014-10-29 15:59:49 -060034static char *tex_files[] = {
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060035 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060036};
37
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060038struct xglcube_vs_uniform {
39 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060040 float mvp[4][4];
41 float position[12*3][4];
42 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060043};
44
45struct xgltexcube_vs_uniform {
46 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060047 float mvp[4][4];
48 float position[12*3][4];
49 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060050};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060051
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060052//--------------------------------------------------------------------------------------
53// Mesh and VertexFormat Data
54//--------------------------------------------------------------------------------------
55struct Vertex
56{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060057 float posX, posY, posZ, posW; // Position data
58 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060059};
60
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060061struct VertexPosTex
62{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060063 float posX, posY, posZ, posW; // Position data
64 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060065};
66
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060067#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060068#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060069
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060070static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchter17223602014-10-29 15:59:49 -060084 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -060085 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060086
87 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060088 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -060089 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060090
91 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060092 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -060093 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600100 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600101 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600112 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600113 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchtere3342402014-10-28 14:50:30 -0600118};
119
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600120static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600121 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600122 0.0f, 0.0f,
123 0.0f, 1.0f,
124
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600125 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600126 1.0f, 1.0f,
Courtney Goeltzenleuchter3eeff432014-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 Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600142 0.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600143 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600144
145 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600146 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600147 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600148
149 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600150 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600151 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-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 Goeltzenleuchter17223602014-10-29 15:59:49 -0600158 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600159 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600160
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600161 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600162 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600163 1.0f, 0.0f,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600164
165 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600166 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600167 0.0f, 1.0f,
168
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600169 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600170 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600171 0.0f, 1.0f,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600172
173 1.0f, 0.0f, // Vertex 11
174 0.0f, 0.0f,
175 0.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600198struct demo {
199 xcb_connection_t *connection;
200 xcb_screen_t *screen;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700201 bool use_staging_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600202
Jon Ashburn92e80132015-01-29 15:47:01 -0700203 XGL_INSTANCE inst;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600204 XGL_PHYSICAL_GPU gpu;
205 XGL_DEVICE device;
206 XGL_QUEUE queue;
207
208 int width, height;
209 XGL_FORMAT format;
210
211 struct {
212 XGL_IMAGE image;
213 XGL_GPU_MEMORY mem;
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700214 XGL_CMD_BUFFER cmd;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600215
216 XGL_COLOR_ATTACHMENT_VIEW view;
Chia-I Wu68040a42014-11-07 14:30:34 +0800217 XGL_FENCE fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600218 } buffers[DEMO_BUFFER_COUNT];
219
220 struct {
221 XGL_FORMAT format;
222
223 XGL_IMAGE image;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600224 uint32_t num_mem;
Jon Ashburna9ae3832015-01-16 09:37:43 -0700225 XGL_GPU_MEMORY *mem;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600226 XGL_DEPTH_STENCIL_VIEW view;
227 } depth;
228
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700229 struct texture_objects textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600230
231 struct {
Chia-I Wu714df452015-01-01 07:55:04 +0800232 XGL_BUFFER buf;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600233 uint32_t num_mem;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700234 XGL_GPU_MEMORY *mem;
Chia-I Wu714df452015-01-01 07:55:04 +0800235 XGL_BUFFER_VIEW view;
236 XGL_BUFFER_VIEW_ATTACH_INFO attach;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600237 } uniform_data;
238
Chia-I Wu6e68a892015-02-23 10:41:08 -0700239 XGL_DESCRIPTOR_SET_LAYOUT desc_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600240 XGL_PIPELINE pipeline;
241
Tony Barbourfa6cac72015-01-16 14:27:35 -0700242 XGL_DYNAMIC_VP_STATE_OBJECT viewport;
243 XGL_DYNAMIC_RS_STATE_OBJECT raster;
244 XGL_DYNAMIC_CB_STATE_OBJECT color_blend;
245 XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600246
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600247 mat4x4 projection_matrix;
248 mat4x4 view_matrix;
249 mat4x4 model_matrix;
250
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600251 float spin_angle;
252 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600253 bool pause;
254
Chia-I Wuf8385062015-01-04 16:27:24 +0800255 XGL_DESCRIPTOR_REGION desc_region;
256 XGL_DESCRIPTOR_SET desc_set;
257
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600258 xcb_window_t window;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -0700259 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600260
261 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600262 uint32_t current_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600263};
264
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700265static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600266{
267 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
268 .view = demo->buffers[demo->current_buffer].view,
Mike Stroyan55658c22014-12-04 11:08:39 +0000269 .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600270 };
271 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
272 .view = demo->depth.view,
Mike Stroyan55658c22014-12-04 11:08:39 +0000273 .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600274 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600275 const float clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
276 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600277 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700278 XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = {
279 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO,
280 .pNext = NULL,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700281 };
Jon Ashburn53d27af2014-12-31 17:08:35 -0700282 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = {
283 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700284 .pNext = &graphics_cmd_buf_info,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700285 .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
286 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
287 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600288 XGL_RESULT err;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700289 XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
290 XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
291 const XGL_FRAMEBUFFER_CREATE_INFO fb_info = {
292 .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
293 .pNext = NULL,
294 .colorAttachmentCount = 1,
295 .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment,
296 .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil,
297 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600298 .width = demo->width,
299 .height = demo->height,
300 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700301 };
302 XGL_RENDER_PASS_CREATE_INFO rp_info;
303
304 memset(&rp_info, 0 , sizeof(rp_info));
305 err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer));
306 assert(!err);
307 rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
308 rp_info.renderArea.extent.width = demo->width;
309 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchterb21fe902015-02-10 14:06:25 -0700310 rp_info.colorAttachmentCount = 1;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700311 rp_info.pColorLoadOps = &load_op;
312 rp_info.pColorStoreOps = &store_op;
313 rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
314 rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
315 rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE;
316 rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE;
317 err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass));
318 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600319
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700320 err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600321 assert(!err);
322
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700323 xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600324 demo->pipeline);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700325 xglCmdBindDescriptorSet(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuf8385062015-01-04 16:27:24 +0800326 demo->desc_set, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600327
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700328 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport);
329 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster);
330 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600331 demo->color_blend);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700332 xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600333 demo->depth_stencil);
334
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700335 xglCmdBeginRenderPass(cmd_buf, graphics_cmd_buf_info.renderPass);
Courtney Goeltzenleuchter6f88d4c2014-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 Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700341 xglCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchter6f88d4c2014-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 Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700346 xglCmdClearDepthStencil(cmd_buf, demo->depth.image,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600347 clear_depth, 0, 1, &clear_range);
348
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700349 xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700350 xglCmdEndRenderPass(cmd_buf, graphics_cmd_buf_info.renderPass);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600351
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700352 err = xglEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600353 assert(!err);
Courtney Goeltzenleuchter04fc2fb2015-02-25 17:53:18 -0700354
355 xglDestroyObject(graphics_cmd_buf_info.renderPass);
356 xglDestroyObject(rp_info.framebuffer);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600357}
358
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600359
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600360void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600361{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600362 mat4x4 MVP, Model, VP;
363 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600364 uint8_t *pData;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600365 XGL_RESULT err;
366
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600367 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600368
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600369 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600370 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700371 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600372 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600373
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700374 assert(demo->uniform_data.num_mem == 1);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600375 err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600376 assert(!err);
377
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600378 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600379
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700380 err = xglUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600381 assert(!err);
382}
383
384static void demo_draw(struct demo *demo)
385{
386 const XGL_WSI_X11_PRESENT_INFO present = {
387 .destWindow = demo->window,
388 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wu68040a42014-11-07 14:30:34 +0800389 .async = true,
390 .flip = false,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600391 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800392 XGL_FENCE fence = demo->buffers[demo->current_buffer].fence;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600393 XGL_RESULT err;
394
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600395 err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0));
Chia-I Wu68040a42014-11-07 14:30:34 +0800396 assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE);
397
Mark Lobodzinski15427102015-02-18 16:38:17 -0600398 uint32_t i, idx = 0;
399 XGL_MEMORY_REF *memRefs;
400 memRefs = malloc(sizeof(XGL_MEMORY_REF) * (DEMO_BUFFER_COUNT +
401 demo->depth.num_mem +
402 demo->textures[0].num_mem +
403 demo->uniform_data.num_mem));
404 for (i = 0; i < demo->depth.num_mem; i++, idx++) {
405 memRefs[idx].mem = demo->depth.mem[i];
406 memRefs[idx].flags = 0;
407 }
408 for (i = 0; i < demo->textures[0].num_mem; i++, idx++) {
409 memRefs[idx].mem = demo->textures[0].mem[i];
410 memRefs[idx].flags = 0;
411 }
412 memRefs[idx].mem = demo->buffers[0].mem;
413 memRefs[idx++].flags = 0;
414 memRefs[idx].mem = demo->buffers[1].mem;
415 memRefs[idx++].flags = 0;
416 for (i = 0; i < demo->uniform_data.num_mem; i++, idx++) {
417 memRefs[idx].mem = demo->uniform_data.mem[i];
418 memRefs[idx].flags = 0;
419 }
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -0700420 err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Piers Daniell886be472015-02-23 16:23:13 -0700421 idx, memRefs, XGL_NULL_HANDLE);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600422 assert(!err);
423
Chia-I Wu68040a42014-11-07 14:30:34 +0800424 err = xglWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600425 assert(!err);
426
427 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
428}
429
430static void demo_prepare_buffers(struct demo *demo)
431{
432 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
433 .format = demo->format,
434 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
435 .extent = {
436 .width = demo->width,
437 .height = demo->height,
438 },
439 .flags = 0,
440 };
Chia-I Wu68040a42014-11-07 14:30:34 +0800441 const XGL_FENCE_CREATE_INFO fence = {
442 .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO,
443 .pNext = NULL,
444 .flags = 0,
445 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600446 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600447 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600448
449 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
450 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
451 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
452 .pNext = NULL,
453 .format = demo->format,
454 .mipLevel = 0,
455 .baseArraySlice = 0,
456 .arraySize = 1,
457 };
458
459 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
460 &demo->buffers[i].image, &demo->buffers[i].mem);
461 assert(!err);
462
463 color_attachment_view.image = demo->buffers[i].image;
464
465 err = xglCreateColorAttachmentView(demo->device,
466 &color_attachment_view, &demo->buffers[i].view);
467 assert(!err);
Chia-I Wu68040a42014-11-07 14:30:34 +0800468
469 err = xglCreateFence(demo->device,
470 &fence, &demo->buffers[i].fence);
471 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600472 }
473}
474
475static void demo_prepare_depth(struct demo *demo)
476{
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700477 const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600478 const XGL_IMAGE_CREATE_INFO image = {
479 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
480 .pNext = NULL,
481 .imageType = XGL_IMAGE_2D,
482 .format = depth_format,
483 .extent = { demo->width, demo->height, 1 },
484 .mipLevels = 1,
485 .arraySize = 1,
486 .samples = 1,
487 .tiling = XGL_OPTIMAL_TILING,
488 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
489 .flags = 0,
490 };
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700491 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
492 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
493 .pNext = NULL,
494 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600495 XGL_MEMORY_ALLOC_INFO mem_alloc = {
496 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700497 .pNext = &img_alloc,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600498 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -0700499 .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY,
Jon Ashburn32769172015-01-20 15:06:59 -0700500 .memType = XGL_MEMORY_TYPE_IMAGE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600501 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
502 };
503 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
504 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
505 .pNext = NULL,
506 .image = XGL_NULL_HANDLE,
507 .mipLevel = 0,
508 .baseArraySlice = 0,
509 .arraySize = 1,
510 .flags = 0,
511 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700512 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600513 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700514 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600515 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600516 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600517 uint32_t num_allocations = 0;
518 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600519
520 demo->depth.format = depth_format;
521
522 /* create image */
523 err = xglCreateImage(demo->device, &image,
524 &demo->depth.image);
525 assert(!err);
526
Jon Ashburna9ae3832015-01-16 09:37:43 -0700527
528 err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
529 assert(!err && num_alloc_size == sizeof(num_allocations));
530 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
531 demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
532 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600533 err = xglGetObjectInfo(demo->depth.image,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700534 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
535 &mem_reqs_size, mem_reqs);
536 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700537 err = xglGetObjectInfo(demo->depth.image,
538 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
539 &img_reqs_size, &img_reqs);
540 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
541 img_alloc.usage = img_reqs.usage;
542 img_alloc.formatClass = img_reqs.formatClass;
543 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600544 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -0700545 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600546
Jon Ashburna9ae3832015-01-16 09:37:43 -0700547 /* allocate memory */
548 err = xglAllocMemory(demo->device, &mem_alloc,
549 &(demo->depth.mem[i]));
550 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600551
Jon Ashburna9ae3832015-01-16 09:37:43 -0700552 /* bind memory */
553 err = xglBindObjectMemory(demo->depth.image, i,
554 demo->depth.mem[i], 0);
555 assert(!err);
556 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600557
558 /* create image view */
559 view.image = demo->depth.image;
560 err = xglCreateDepthStencilView(demo->device, &view,
561 &demo->depth.view);
562 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600563}
564
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600565/** loadTexture
566 * loads a png file into an memory object, using cstdio , libpng.
567 *
568 * \param demo : Needed to access XGL calls
569 * \param filename : the png file to be loaded
570 * \param width : width of png, to be updated as a side effect of this function
571 * \param height : height of png, to be updated as a side effect of this function
572 *
573 * \return bool : an opengl texture id. true if successful?,
574 * should be validated by the client of this function.
575 *
576 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
577 * Modified to copy image to memory
578 *
579 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700580bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600581 XGL_SUBRESOURCE_LAYOUT *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600582 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600583{
584 //header for testing if it is a png
585 png_byte header[8];
Ian Elliott642f8922015-02-13 14:29:21 -0700586 int is_png, bit_depth, color_type,rowbytes;
587 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600588 png_structp png_ptr;
589 png_infop info_ptr, end_info;
590 png_byte *image_data;
591 png_bytep *row_pointers;
592
593 //open file as binary
594 FILE *fp = fopen(filename, "rb");
595 if (!fp) {
596 return false;
597 }
598
599 //read the header
600 fread(header, 1, 8, fp);
601
602 //test if png
603 is_png = !png_sig_cmp(header, 0, 8);
604 if (!is_png) {
605 fclose(fp);
606 return false;
607 }
608
609 //create png struct
610 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
611 NULL, NULL);
612 if (!png_ptr) {
613 fclose(fp);
614 return (false);
615 }
616
617 //create png info struct
618 info_ptr = png_create_info_struct(png_ptr);
619 if (!info_ptr) {
620 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
621 fclose(fp);
622 return (false);
623 }
624
625 //create png info struct
626 end_info = png_create_info_struct(png_ptr);
627 if (!end_info) {
628 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
629 fclose(fp);
630 return (false);
631 }
632
633 //png error stuff, not sure libpng man suggests this.
634 if (setjmp(png_jmpbuf(png_ptr))) {
635 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
636 fclose(fp);
637 return (false);
638 }
639
640 //init png reading
641 png_init_io(png_ptr, fp);
642
643 //let libpng know you already read the first 8 bytes
644 png_set_sig_bytes(png_ptr, 8);
645
646 // read all the info up to the image data
647 png_read_info(png_ptr, info_ptr);
648
649 // get info about png
650 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
651 NULL, NULL, NULL);
652
653 //update width and height based on png info
654 *width = twidth;
655 *height = theight;
656
657 // Require that incoming texture be 8bits per color component
658 // and 4 components (RGBA).
659 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
660 png_get_channels(png_ptr, info_ptr) != 4) {
661 return false;
662 }
663
664 if (rgba_data == NULL) {
665 // If data pointer is null, we just want the width & height
666 // clean up memory and close stuff
667 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
668 fclose(fp);
669
670 return true;
671 }
672
673 // Update the png info struct.
674 png_read_update_info(png_ptr, info_ptr);
675
676 // Row size in bytes.
677 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
678
679 // Allocate the image_data as a big block, to be given to opengl
680 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
681 if (!image_data) {
682 //clean up memory and close stuff
683 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
684 fclose(fp);
685 return false;
686 }
687
688 // row_pointers is for pointing to image_data for reading the png with libpng
689 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
690 if (!row_pointers) {
691 //clean up memory and close stuff
692 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
693 // delete[] image_data;
694 fclose(fp);
695 return false;
696 }
697 // set the individual row_pointers to point at the correct offsets of image_data
698 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700699 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600700
701 // read the png into image_data through row_pointers
702 png_read_image(png_ptr, row_pointers);
703
704 // clean up memory and close stuff
705 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
706 free(row_pointers);
707 free(image_data);
708 fclose(fp);
709
710 return true;
711}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600712
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700713static void demo_prepare_texture_image(struct demo *demo,
714 const char *filename,
715 struct texture_objects *tex_objs,
716 XGL_IMAGE_TILING tiling,
717 XGL_FLAGS mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600718{
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700719 const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600720 int32_t tex_width;
721 int32_t tex_height;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600722 XGL_RESULT err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700723
724 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
725 assert(err);
726
727 tex_objs->tex_width = tex_width;
728 tex_objs->tex_height = tex_height;
729
730 const XGL_IMAGE_CREATE_INFO image_create_info = {
731 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
732 .pNext = NULL,
733 .imageType = XGL_IMAGE_2D,
734 .format = tex_format,
735 .extent = { tex_width, tex_height, 1 },
736 .mipLevels = 1,
737 .arraySize = 1,
738 .samples = 1,
739 .tiling = tiling,
740 .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
741 .flags = 0,
742 };
Piers Daniell886be472015-02-23 16:23:13 -0700743 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
744 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
745 .pNext = NULL,
746 };
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700747 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = {
748 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
Piers Daniell886be472015-02-23 16:23:13 -0700749 .pNext = &buf_alloc,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700750 };
751 XGL_MEMORY_ALLOC_INFO mem_alloc = {
752 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
753 .pNext = &img_alloc,
754 .allocationSize = 0,
755 .memProps = mem_props,
756 .memType = XGL_MEMORY_TYPE_IMAGE,
757 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
758 };
759
760 XGL_MEMORY_REQUIREMENTS *mem_reqs;
761 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Piers Daniell886be472015-02-23 16:23:13 -0700762 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
763 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700764 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
765 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
766 uint32_t num_allocations = 0;
767 size_t num_alloc_size = sizeof(num_allocations);
768
769 err = xglCreateImage(demo->device, &image_create_info,
770 &tex_objs->image);
771 assert(!err);
772
773 err = xglGetObjectInfo(tex_objs->image,
774 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
775 &num_alloc_size, &num_allocations);
776 assert(!err && num_alloc_size == sizeof(num_allocations));
777 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
778 tex_objs->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
779 err = xglGetObjectInfo(tex_objs->image,
780 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
781 &mem_reqs_size, mem_reqs);
782 assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
783 err = xglGetObjectInfo(tex_objs->image,
784 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
785 &img_reqs_size, &img_reqs);
786 assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
787 img_alloc.usage = img_reqs.usage;
788 img_alloc.formatClass = img_reqs.formatClass;
789 img_alloc.samples = img_reqs.samples;
790 mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
791 for (uint32_t j = 0; j < num_allocations; j ++) {
Courtney Goeltzenleuchter6e5e81c2015-02-25 11:48:28 -0700792 mem_alloc.memType = mem_reqs[j].memType;
Piers Daniell886be472015-02-23 16:23:13 -0700793 mem_alloc.allocationSize = mem_reqs[j].size;
794
795 if (mem_alloc.memType == XGL_MEMORY_TYPE_BUFFER) {
796 err = xglGetObjectInfo(tex_objs->image,
797 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
798 &buf_reqs_size, &buf_reqs);
799 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
800 buf_alloc.usage = buf_reqs.usage;
801 img_alloc.pNext = &buf_alloc;
802 } else {
803 img_alloc.pNext = 0;
804 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700805
806 /* allocate memory */
807 err = xglAllocMemory(demo->device, &mem_alloc,
808 &(tex_objs->mem[j]));
809 assert(!err);
810
811 /* bind memory */
812 err = xglBindObjectMemory(tex_objs->image, j, tex_objs->mem[j], 0);
813 assert(!err);
814 }
815 free(mem_reqs);
816 mem_reqs = NULL;
817
818 tex_objs->num_mem = num_allocations;
819
820 if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
821 const XGL_IMAGE_SUBRESOURCE subres = {
822 .aspect = XGL_IMAGE_ASPECT_COLOR,
823 .mipLevel = 0,
824 .arraySlice = 0,
825 };
826 XGL_SUBRESOURCE_LAYOUT layout;
827 size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT);
828 void *data;
829
830 err = xglGetImageSubresourceInfo(tex_objs->image, &subres,
831 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
832 &layout_size, &layout);
833 assert(!err && layout_size == sizeof(layout));
834 /* Linear texture must be within a single memory object */
835 assert(num_allocations == 1);
836
837 err = xglMapMemory(tex_objs->mem[0], 0, &data);
838 assert(!err);
839
840 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
841 fprintf(stderr, "Error loading texture: %s\n", filename);
842 }
843
844 err = xglUnmapMemory(tex_objs->mem[0]);
845 assert(!err);
846 }
847}
848
849static void demo_destroy_texture_image(struct texture_objects *tex_objs)
850{
851 /* clean up staging resources */
852 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
853 xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0);
854 xglFreeMemory(tex_objs->mem[j]);
855 }
856
857 free(tex_objs->mem);
858 xglDestroyObject(tex_objs->image);
859}
860
861static void demo_prepare_textures(struct demo *demo)
862{
863 const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM;
864 XGL_FORMAT_PROPERTIES props;
865 size_t size = sizeof(props);
866 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600867 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600868
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700869 err = xglGetFormatInfo(demo->device, tex_format,
870 XGL_INFO_TYPE_FORMAT_PROPERTIES,
871 &size, &props);
872 assert(!err);
873
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600874 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700875
876 if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) {
877 /* Device can texture using linear textures */
878 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
879 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
880 } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT){
881 /* Must use staging buffer to copy linear texture to optimized */
882 struct texture_objects staging_texture;
883
884 memset(&staging_texture, 0, sizeof(staging_texture));
885 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
886 XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
887
888 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
889 XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY);
890
891 XGL_CMD_BUFFER staging_cmd_buf;
892 XGL_CMD_BUFFER_CREATE_INFO cmd_buf_create_info = {
893 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
894 .pNext = NULL,
895 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
896 .flags = 0
897 };
898
899 err = xglCreateCommandBuffer(demo->device, &cmd_buf_create_info, &staging_cmd_buf);
900 assert(!err);
901
902 /* Copy staging texture to usable texture */
903 XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_begin_info = {
904 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
905 .pNext = NULL,
906 .flags = 0
907 };
908
909 err = xglResetCommandBuffer(staging_cmd_buf);
910 assert(!err);
911
912 err = xglBeginCommandBuffer(staging_cmd_buf, &cmd_buf_begin_info);
913 assert(!err);
914
915 XGL_IMAGE_COPY copy_region = {
916 .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
917 .srcOffset = { 0, 0, 0 },
918 .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 },
919 .destOffset = { 0, 0, 0 },
920 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
921 };
922 xglCmdCopyImage(staging_cmd_buf, staging_texture.image, demo->textures[i].image, 1, &copy_region);
923
924 XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = {
925 .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
926 .pNext = NULL,
927 .outputMask = XGL_MEMORY_OUTPUT_COPY_BIT,
928 .inputMask = XGL_MEMORY_INPUT_SHADER_READ_BIT | XGL_MEMORY_INPUT_COPY_BIT,
929 .oldLayout = XGL_IMAGE_LAYOUT_GENERAL,
930 .newLayout = XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
931 .image = staging_texture.image,
932 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
933 };
934 XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier;
935
936 XGL_SET_EVENT set_events[] = { XGL_SET_EVENT_GPU_COMMANDS_COMPLETE };
937 XGL_PIPELINE_BARRIER pipeline_barrier;
938 pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER;
939 pipeline_barrier.pNext = NULL;
940 pipeline_barrier.eventCount = 1;
941 pipeline_barrier.pEvents = set_events;
942 pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE;
943 pipeline_barrier.memBarrierCount = 1;
944 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
945
946 // write barrier to the command buffer
947 xglCmdPipelineBarrier(staging_cmd_buf, &pipeline_barrier);
948
949 err = xglEndCommandBuffer(staging_cmd_buf);
950 assert(!err);
951
952 const XGL_CMD_BUFFER cmd_bufs[] = { staging_cmd_buf };
953 XGL_MEMORY_REF mem_refs[16];
954 uint32_t num_refs = 0;
955
956 for (uint32_t j = 0; j < staging_texture.num_mem; j++) {
957 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
958 mem_refs[num_refs].mem = staging_texture.mem[j];
959 num_refs++;
960 assert(num_refs < 16);
961 }
962
963 for (uint32_t j = 0; j < demo->textures[i].num_mem; j++) {
964 mem_refs[num_refs].flags = XGL_MEMORY_REF_READ_ONLY_BIT;
965 mem_refs[num_refs].mem = demo->textures[i].mem[j];
966 num_refs++;
967 assert(num_refs < 16);
968 }
969
970 err = xglQueueSubmit(demo->queue, 1, cmd_bufs,
971 num_refs, mem_refs, XGL_NULL_HANDLE);
972 assert(!err);
973
974 err = xglQueueWaitIdle(demo->queue);
975 assert(!err);
976
977 demo_destroy_texture_image(&staging_texture);
978
979 xglDestroyObject(staging_cmd_buf);
980 } else {
981 /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */
982 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
983 }
984
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600985 const XGL_SAMPLER_CREATE_INFO sampler = {
986 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
987 .pNext = NULL,
988 .magFilter = XGL_TEX_FILTER_NEAREST,
989 .minFilter = XGL_TEX_FILTER_NEAREST,
990 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600991 .addressU = XGL_TEX_ADDRESS_CLAMP,
992 .addressV = XGL_TEX_ADDRESS_CLAMP,
993 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600994 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700995 .maxAnisotropy = 1,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600996 .compareFunc = XGL_COMPARE_NEVER,
997 .minLod = 0.0f,
998 .maxLod = 0.0f,
999 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
1000 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001001
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001002 XGL_IMAGE_VIEW_CREATE_INFO view = {
1003 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1004 .pNext = NULL,
1005 .image = XGL_NULL_HANDLE,
1006 .viewType = XGL_IMAGE_VIEW_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001007 .format = tex_format,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001008 .channels = { XGL_CHANNEL_SWIZZLE_R,
1009 XGL_CHANNEL_SWIZZLE_G,
1010 XGL_CHANNEL_SWIZZLE_B,
1011 XGL_CHANNEL_SWIZZLE_A, },
1012 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
1013 .minLod = 0.0f,
1014 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001015
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001016 /* create sampler */
1017 err = xglCreateSampler(demo->device, &sampler,
1018 &demo->textures[i].sampler);
1019 assert(!err);
1020
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001021 /* create image view */
1022 view.image = demo->textures[i].image;
1023 err = xglCreateImageView(demo->device, &view,
1024 &demo->textures[i].view);
1025 assert(!err);
1026 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001027}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001028
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001029void demo_prepare_cube_data_buffer(struct demo *demo)
1030{
Chia-I Wu714df452015-01-01 07:55:04 +08001031 XGL_BUFFER_CREATE_INFO buf_info;
1032 XGL_BUFFER_VIEW_CREATE_INFO view_info;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001033 XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = {
1034 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
1035 .pNext = NULL,
1036 };
1037 XGL_MEMORY_ALLOC_INFO alloc_info = {
1038 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
1039 .pNext = &buf_alloc,
1040 .allocationSize = 0,
Jon Ashburn542cd092015-01-20 13:55:32 -07001041 .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
Jon Ashburn32769172015-01-20 15:06:59 -07001042 .memType = XGL_MEMORY_TYPE_BUFFER,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001043 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
1044 };
1045 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001046 size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001047 XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001048 size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS);
1049 uint32_t num_allocations = 0;
1050 size_t num_alloc_size = sizeof(num_allocations);
1051 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001052 int i;
1053 mat4x4 MVP, VP;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001054 XGL_RESULT err;
1055 struct xgltexcube_vs_uniform data;
1056
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001057 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001058 mat4x4_mul(MVP, VP, demo->model_matrix);
1059 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001060// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001061
1062 for (i=0; i<12*3; i++) {
1063 data.position[i][0] = g_vertex_buffer_data[i*3];
1064 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1065 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1066 data.position[i][3] = 1.0f;
1067 data.attr[i][0] = g_uv_buffer_data[2*i];
1068 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1069 data.attr[i][2] = 0;
1070 data.attr[i][3] = 0;
1071 }
1072
Chia-I Wu714df452015-01-01 07:55:04 +08001073 memset(&buf_info, 0, sizeof(buf_info));
1074 buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1075 buf_info.size = sizeof(data);
1076 buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT;
1077 err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
1078 assert(!err);
1079
1080 err = xglGetObjectInfo(demo->uniform_data.buf,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001081 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
1082 &num_alloc_size, &num_allocations);
1083 assert(!err && num_alloc_size == sizeof(num_allocations));
1084 mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
1085 demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
1086 demo->uniform_data.num_mem = num_allocations;
1087 err = xglGetObjectInfo(demo->uniform_data.buf,
Chia-I Wu714df452015-01-01 07:55:04 +08001088 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001089 &mem_reqs_size, mem_reqs);
1090 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
1091 err = xglGetObjectInfo(demo->uniform_data.buf,
1092 XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
1093 &buf_reqs_size, &buf_reqs);
1094 assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS));
1095 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001096 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001097 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu714df452015-01-01 07:55:04 +08001098
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001099 err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
1100 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001101
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001102 err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001103 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001104
Piers Daniell886be472015-02-23 16:23:13 -07001105 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001106
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001107 err = xglUnmapMemory(demo->uniform_data.mem[i]);
1108 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001109
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001110 err = xglBindObjectMemory(demo->uniform_data.buf, i,
1111 demo->uniform_data.mem[i], 0);
1112 assert(!err);
1113 }
Chia-I Wu714df452015-01-01 07:55:04 +08001114
1115 memset(&view_info, 0, sizeof(view_info));
1116 view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
1117 view_info.buffer = demo->uniform_data.buf;
Chia-I Wu28fb4ce2015-01-16 22:31:25 +08001118 view_info.viewType = XGL_BUFFER_VIEW_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001119 view_info.offset = 0;
1120 view_info.range = sizeof(data);
1121
1122 err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
1123 assert(!err);
1124
1125 demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
1126 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001127}
1128
Chia-I Wuf8385062015-01-04 16:27:24 +08001129static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001130{
Chia-I Wuf8385062015-01-04 16:27:24 +08001131 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = {
1132 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1133 .pNext = NULL,
1134 .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1135 .count = DEMO_TEXTURE_COUNT,
1136 .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
1137 .immutableSampler = XGL_NULL_HANDLE,
1138 };
Chia-I Wu6e68a892015-02-23 10:41:08 -07001139 const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = {
1140 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1141 .pNext = &descriptor_layout_fs,
1142 .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1143 .count = 1,
1144 .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT,
1145 .immutableSampler = XGL_NULL_HANDLE,
1146 };
Chia-I Wu8e8fba62015-02-23 10:41:08 -07001147
1148 const uint32_t bind_point = 0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001149 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001150
Chia-I Wuf8385062015-01-04 16:27:24 +08001151 err = xglCreateDescriptorSetLayout(demo->device,
Chia-I Wu8e8fba62015-02-23 10:41:08 -07001152 XGL_SHADER_STAGE_FLAGS_ALL, &bind_point,
Chia-I Wuf8385062015-01-04 16:27:24 +08001153 XGL_NULL_HANDLE, &descriptor_layout_vs,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001154 &demo->desc_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001155 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001156}
1157
1158static XGL_SHADER demo_prepare_shader(struct demo *demo,
1159 XGL_PIPELINE_SHADER_STAGE stage,
1160 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001161 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001162{
1163 XGL_SHADER_CREATE_INFO createInfo;
1164 XGL_SHADER shader;
1165 XGL_RESULT err;
1166
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001167
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001168 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1169 createInfo.pNext = NULL;
1170
Cody Northropacfb0492015-03-17 15:55:58 -06001171#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001172 createInfo.codeSize = size;
1173 createInfo.pCode = code;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001174 createInfo.flags = 0;
1175
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001176 err = xglCreateShader(demo->device, &createInfo, &shader);
1177 if (err) {
1178 free((void *) createInfo.pCode);
1179 }
1180#else
Cody Northropacfb0492015-03-17 15:55:58 -06001181 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001182 // to the driver "under the covers"
1183 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1184 createInfo.pCode = malloc(createInfo.codeSize);
1185 createInfo.flags = 0;
1186
1187 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
Cody Northropacfb0492015-03-17 15:55:58 -06001188 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001189 ((uint32_t *) createInfo.pCode)[1] = 0;
1190 ((uint32_t *) createInfo.pCode)[2] = stage;
1191 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1192
1193 err = xglCreateShader(demo->device, &createInfo, &shader);
1194 if (err) {
1195 free((void *) createInfo.pCode);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001196 return NULL;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001197 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001198#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001199
1200 return shader;
1201}
1202
Cody Northropacfb0492015-03-17 15:55:58 -06001203char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001204{
1205 long int size;
1206 void *shader_code;
1207
1208 FILE *fp = fopen(filename, "rb");
1209 if (!fp) return NULL;
1210
1211 fseek(fp, 0L, SEEK_END);
1212 size = ftell(fp);
1213
1214 fseek(fp, 0L, SEEK_SET);
1215
1216 shader_code = malloc(size);
1217 fread(shader_code, size, 1, fp);
1218
1219 *psize = size;
1220
1221 return shader_code;
1222}
1223
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001224static XGL_SHADER demo_prepare_vs(struct demo *demo)
1225{
Cody Northropacfb0492015-03-17 15:55:58 -06001226#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001227 void *vertShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001228 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001229
Cody Northropacfb0492015-03-17 15:55:58 -06001230 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001231
1232 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1233 vertShaderCode, size);
1234#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001235 static const char *vertShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001236 "#version 140\n"
1237 "#extension GL_ARB_separate_shader_objects : enable\n"
1238 "#extension GL_ARB_shading_language_420pack : enable\n"
1239 "\n"
1240 "layout(binding = 0) uniform buf {\n"
1241 " mat4 MVP;\n"
1242 " vec4 position[12*3];\n"
1243 " vec4 attr[12*3];\n"
1244 "} ubuf;\n"
1245 "\n"
1246 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001247 "\n"
1248 "void main() \n"
1249 "{\n"
1250 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001251 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1252 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001253
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001254 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1255 (const void *) vertShaderText,
1256 strlen(vertShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001257#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001258}
1259
1260static XGL_SHADER demo_prepare_fs(struct demo *demo)
1261{
Cody Northropacfb0492015-03-17 15:55:58 -06001262#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001263 void *fragShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001264 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001265
Cody Northropacfb0492015-03-17 15:55:58 -06001266 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001267
1268 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1269 fragShaderCode, size);
1270#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001271 static const char *fragShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001272 "#version 140\n"
1273 "#extension GL_ARB_separate_shader_objects : enable\n"
1274 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter988cc782015-02-25 15:13:35 -07001275 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001276 "\n"
1277 "layout (location = 0) in vec4 texcoord;\n"
1278 "void main() {\n"
1279 " gl_FragColor = texture(tex, texcoord.xy);\n"
1280 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001281
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001282 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1283 (const void *) fragShaderText,
1284 strlen(fragShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001285#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001286}
1287
1288static void demo_prepare_pipeline(struct demo *demo)
1289{
1290 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001291 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1292 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001293 XGL_PIPELINE_CB_STATE_CREATE_INFO cb;
1294 XGL_PIPELINE_DS_STATE_CREATE_INFO ds;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001295 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1296 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001297 XGL_PIPELINE_VP_STATE_CREATE_INFO vp;
1298 XGL_PIPELINE_MS_STATE_CREATE_INFO ms;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001299 XGL_RESULT err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001300
1301 memset(&pipeline, 0, sizeof(pipeline));
1302 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wu6e68a892015-02-23 10:41:08 -07001303 pipeline.lastSetLayout = demo->desc_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001304
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001305 memset(&ia, 0, sizeof(ia));
1306 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1307 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1308
1309 memset(&rs, 0, sizeof(rs));
1310 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001311 rs.fillMode = XGL_FILL_SOLID;
Mike Stroyanea3945c2015-03-19 14:29:04 -06001312 rs.cullMode = XGL_CULL_BACK;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001313 rs.frontFace = XGL_FRONT_FACE_CCW;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001314
1315 memset(&cb, 0, sizeof(cb));
1316 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001317 XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1];
1318 memset(att_state, 0, sizeof(att_state));
1319 att_state[0].format = demo->format;
1320 att_state[0].channelWriteMask = 0xf;
1321 att_state[0].blendEnable = XGL_FALSE;
1322 cb.attachmentCount = 1;
1323 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001324
Tony Barbourfa6cac72015-01-16 14:27:35 -07001325 memset(&vp, 0, sizeof(vp));
1326 vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001327 vp.numViewports = 1;
Mike Stroyan97e79b32015-03-24 15:13:34 -06001328 vp.clipOrigin = XGL_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001329
1330 memset(&ds, 0, sizeof(ds));
1331 ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
1332 ds.format = demo->depth.format;
1333 ds.depthTestEnable = XGL_TRUE;
1334 ds.depthWriteEnable = XGL_TRUE;
1335 ds.depthFunc = XGL_COMPARE_LESS_EQUAL;
1336 ds.depthBoundsEnable = XGL_FALSE;
1337 ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1338 ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1339 ds.back.stencilFunc = XGL_COMPARE_ALWAYS;
1340 ds.stencilTestEnable = XGL_FALSE;
1341 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001342
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001343 memset(&vs, 0, sizeof(vs));
1344 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1345 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001346 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001347 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001348
1349 memset(&fs, 0, sizeof(fs));
1350 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1351 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1352 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001353 assert(fs.shader.shader != NULL);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001354
1355 memset(&ms, 0, sizeof(ms));
1356 ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
1357 ms.sampleMask = 1;
1358 ms.multisampleEnable = XGL_FALSE;
1359 ms.samples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001360
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001361 pipeline.pNext = (const void *) &ia;
1362 ia.pNext = (const void *) &rs;
1363 rs.pNext = (const void *) &cb;
1364 cb.pNext = (const void *) &ms;
1365 ms.pNext = (const void *) &vp;
1366 vp.pNext = (const void *) &ds;
1367 ds.pNext = (const void *) &vs;
1368 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001369
1370 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1371 assert(!err);
1372
1373 xglDestroyObject(vs.shader.shader);
1374 xglDestroyObject(fs.shader.shader);
1375}
1376
1377static void demo_prepare_dynamic_states(struct demo *demo)
1378{
Tony Barbourfa6cac72015-01-16 14:27:35 -07001379 XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create;
1380 XGL_DYNAMIC_RS_STATE_CREATE_INFO raster;
1381 XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend;
1382 XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001383 XGL_RESULT err;
1384
Tony Barbourfa6cac72015-01-16 14:27:35 -07001385 memset(&viewport_create, 0, sizeof(viewport_create));
1386 viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001387 viewport_create.viewportAndScissorCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001388 XGL_VIEWPORT viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001389 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001390 viewport.height = (float) demo->height;
1391 viewport.width = (float) demo->width;
1392 viewport.minDepth = (float) 0.0f;
1393 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001394 viewport_create.pViewports = &viewport;
1395 XGL_RECT scissor;
1396 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001397 scissor.extent.width = demo->width;
1398 scissor.extent.height = demo->height;
1399 scissor.offset.x = 0;
1400 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001401 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001402
1403 memset(&raster, 0, sizeof(raster));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001404 raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001405 raster.pointSize = 1.0;
1406 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001407
1408 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001409 color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001410 color_blend.blendConst[0] = 1.0f;
1411 color_blend.blendConst[1] = 1.0f;
1412 color_blend.blendConst[2] = 1.0f;
1413 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001414
1415 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001416 depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001417 depth_stencil.minDepth = 0.0f;
1418 depth_stencil.maxDepth = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001419 depth_stencil.stencilBackRef = 0;
1420 depth_stencil.stencilFrontRef = 0;
1421 depth_stencil.stencilReadMask = 0xff;
1422 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001423
Tony Barbourfa6cac72015-01-16 14:27:35 -07001424 err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001425 assert(!err);
1426
Tony Barbourfa6cac72015-01-16 14:27:35 -07001427 err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001428 assert(!err);
1429
Tony Barbourfa6cac72015-01-16 14:27:35 -07001430 err = xglCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001431 &color_blend, &demo->color_blend);
1432 assert(!err);
1433
Tony Barbourfa6cac72015-01-16 14:27:35 -07001434 err = xglCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001435 &depth_stencil, &demo->depth_stencil);
1436 assert(!err);
1437}
1438
Chia-I Wuf8385062015-01-04 16:27:24 +08001439static void demo_prepare_descriptor_region(struct demo *demo)
1440{
1441 const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = {
1442 [0] = {
1443 .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1444 .count = 1,
1445 },
1446 [1] = {
1447 .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE,
1448 .count = DEMO_TEXTURE_COUNT,
1449 },
1450 };
1451 const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = {
1452 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO,
1453 .pNext = NULL,
1454 .count = 2,
1455 .pTypeCount = type_counts,
1456 };
1457 XGL_RESULT err;
1458
1459 err = xglCreateDescriptorRegion(demo->device,
1460 XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1,
1461 &descriptor_region, &demo->desc_region);
1462 assert(!err);
1463}
1464
1465static void demo_prepare_descriptor_set(struct demo *demo)
1466{
1467 const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs =
1468 &demo->uniform_data.attach;
1469 XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT];
1470 XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT];
1471 XGL_UPDATE_SAMPLER_TEXTURES update_fs;
1472 XGL_UPDATE_BUFFERS update_vs;
1473 XGL_RESULT err;
1474 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001475 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001476
1477 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1478 view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1479 view_info[i].pNext = NULL;
1480 view_info[i].view = demo->textures[i].view,
1481 view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL;
1482
1483 combined_info[i].pSampler = demo->textures[i].sampler;
1484 combined_info[i].pImageView = &view_info[i];
1485 }
1486
1487 memset(&update_vs, 0, sizeof(update_vs));
1488 update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS;
1489 update_vs.pNext = &update_fs;
1490 update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1491 update_vs.count = 1;
1492 update_vs.pBufferViews = &view_info_vs;
1493
1494 memset(&update_fs, 0, sizeof(update_fs));
1495 update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
1496 update_fs.index = 1;
1497 update_fs.count = DEMO_TEXTURE_COUNT;
1498 update_fs.pSamplerImageViews = combined_info;
1499
1500 err = xglAllocDescriptorSets(demo->desc_region,
1501 XGL_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001502 1, &demo->desc_layout,
Chia-I Wuf8385062015-01-04 16:27:24 +08001503 &demo->desc_set, &count);
1504 assert(!err && count == 1);
1505
1506 xglBeginDescriptorRegionUpdate(demo->device,
1507 XGL_DESCRIPTOR_UPDATE_MODE_FASTEST);
1508
1509 xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set);
1510 xglUpdateDescriptors(demo->desc_set, &update_vs);
1511
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001512 xglEndDescriptorRegionUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wuf8385062015-01-04 16:27:24 +08001513}
1514
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001515static void demo_prepare(struct demo *demo)
1516{
1517 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1518 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1519 .pNext = NULL,
1520 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1521 .flags = 0,
1522 };
1523 XGL_RESULT err;
1524
1525 demo_prepare_buffers(demo);
1526 demo_prepare_depth(demo);
1527 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001528 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001529
Chia-I Wuf8385062015-01-04 16:27:24 +08001530 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001531 demo_prepare_pipeline(demo);
1532 demo_prepare_dynamic_states(demo);
1533
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001534 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1535 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
1536 assert(!err);
1537 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001538
1539 demo_prepare_descriptor_region(demo);
1540 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001541
1542
1543 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1544 demo->current_buffer = i;
1545 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1546 }
1547
1548 demo->current_buffer = 0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001549}
1550
1551static void demo_handle_event(struct demo *demo,
1552 const xcb_generic_event_t *event)
1553{
Piers Daniell886be472015-02-23 16:23:13 -07001554 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001555 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001556 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001557 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001558 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001559 case XCB_CLIENT_MESSAGE:
1560 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1561 (*demo->atom_wm_delete_window).atom) {
1562 demo->quit = true;
1563 }
1564 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001565 case XCB_KEY_RELEASE:
1566 {
1567 const xcb_key_release_event_t *key =
1568 (const xcb_key_release_event_t *) event;
1569
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001570 switch (key->detail) {
1571 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001572 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001573 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001574 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001575 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001576 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001577 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001578 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001579 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001580 case 0x41:
1581 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001582 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001583 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001584 }
1585 break;
1586 default:
1587 break;
1588 }
1589}
1590
1591static void demo_run(struct demo *demo)
1592{
1593 xcb_flush(demo->connection);
1594
1595 while (!demo->quit) {
1596 xcb_generic_event_t *event;
1597
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001598 if (demo->pause) {
1599 event = xcb_wait_for_event(demo->connection);
1600 } else {
1601 event = xcb_poll_for_event(demo->connection);
1602 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001603 if (event) {
1604 demo_handle_event(demo, event);
1605 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001606 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001607
1608 // Wait for work to finish before updating MVP.
1609 xglDeviceWaitIdle(demo->device);
1610 demo_update_data_buffer(demo);
1611
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001612 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001613
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001614 // Wait for work to finish before updating MVP.
1615 xglDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001616 }
1617}
1618
1619static void demo_create_window(struct demo *demo)
1620{
1621 uint32_t value_mask, value_list[32];
1622
1623 demo->window = xcb_generate_id(demo->connection);
1624
1625 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1626 value_list[0] = demo->screen->black_pixel;
1627 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1628 XCB_EVENT_MASK_EXPOSURE;
1629
1630 xcb_create_window(demo->connection,
1631 XCB_COPY_FROM_PARENT,
1632 demo->window, demo->screen->root,
1633 0, 0, demo->width, demo->height, 0,
1634 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1635 demo->screen->root_visual,
1636 value_mask, value_list);
1637
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001638 /* Magic code that will send notification when window is destroyed */
1639 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1640 "WM_PROTOCOLS");
1641 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1642
1643 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1644 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1645
1646 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1647 demo->window, (*reply).atom, 4, 32, 1,
1648 &(*demo->atom_wm_delete_window).atom);
1649 free(reply);
1650
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001651 xcb_map_window(demo->connection, demo->window);
1652}
1653
1654static void demo_init_xgl(struct demo *demo)
1655{
1656 const XGL_APPLICATION_INFO app = {
1657 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1658 .pNext = NULL,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001659 .pAppName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001660 .appVersion = 0,
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001661 .pEngineName = "cube",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001662 .engineVersion = 0,
Courtney Goeltzenleuchter211cc542015-02-23 17:40:15 -07001663 .apiVersion = XGL_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001664 };
1665 const XGL_WSI_X11_CONNECTION_INFO connection = {
1666 .pConnection = demo->connection,
1667 .root = demo->screen->root,
1668 .provider = 0,
1669 };
1670 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1671 .queueNodeIndex = 0,
1672 .queueCount = 1,
1673 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001674 const char *ext_names[] = {
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001675 "XGL_WSI_X11",
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001676 };
1677 const XGL_DEVICE_CREATE_INFO device = {
1678 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1679 .pNext = NULL,
1680 .queueRecordCount = 1,
1681 .pRequestedQueues = &queue,
1682 .extensionCount = 1,
1683 .ppEnabledExtensionNames = ext_names,
1684 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1685 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1686 };
1687 XGL_RESULT err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001688 uint32_t gpu_count;
1689 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001690
Jon Ashburn92e80132015-01-29 15:47:01 -07001691 err = xglCreateInstance(&app, NULL, &demo->inst);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001692 if (err == XGL_ERROR_INCOMPATIBLE_DRIVER) {
1693 printf("Cannot find a compatible Vulkan installable client driver "
1694 "(ICD).\nExiting ...\n");
1695 fflush(stdout);
1696 exit(1);
1697 } else {
1698 assert(!err);
1699 }
Jon Ashburn92e80132015-01-29 15:47:01 -07001700 err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001701 assert(!err && gpu_count == 1);
1702
1703 for (i = 0; i < device.extensionCount; i++) {
1704 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1705 assert(!err);
1706 }
1707
1708 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1709 assert(!err);
1710
1711 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1712 assert(!err);
1713
1714 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1715 0, &demo->queue);
1716 assert(!err);
1717}
1718
1719static void demo_init_connection(struct demo *demo)
1720{
1721 const xcb_setup_t *setup;
1722 xcb_screen_iterator_t iter;
1723 int scr;
1724
1725 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001726 if (demo->connection == NULL) {
1727 printf("Cannot find a compatible Vulkan installable client driver "
1728 "(ICD).\nExiting ...\n");
1729 fflush(stdout);
1730 exit(1);
1731 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001732
1733 setup = xcb_get_setup(demo->connection);
1734 iter = xcb_setup_roots_iterator(setup);
1735 while (scr-- > 0)
1736 xcb_screen_next(&iter);
1737
1738 demo->screen = iter.data;
1739}
1740
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001741static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001742{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001743 vec3 eye = {0.0f, 3.0f, 5.0f};
1744 vec3 origin = {0, 0, 0};
1745 vec3 up = {0.0f, -1.0f, 0.0};
1746
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001747 memset(demo, 0, sizeof(*demo));
1748
Piers Daniell886be472015-02-23 16:23:13 -07001749 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001750 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1751 demo->use_staging_buffer = true;
1752 }
1753
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001754 demo_init_connection(demo);
1755 demo_init_xgl(demo);
1756
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001757 demo->width = 500;
1758 demo->height = 500;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001759 demo->format = XGL_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001760
1761 demo->spin_angle = 0.01f;
1762 demo->spin_increment = 0.01f;
1763 demo->pause = false;
1764
Piers Daniell886be472015-02-23 16:23:13 -07001765 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001766 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1767 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001768}
1769
1770static void demo_cleanup(struct demo *demo)
1771{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001772 uint32_t i, j;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001773
Chia-I Wuf8385062015-01-04 16:27:24 +08001774 xglDestroyObject(demo->desc_set);
1775 xglDestroyObject(demo->desc_region);
1776
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001777 xglDestroyObject(demo->viewport);
1778 xglDestroyObject(demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001779 xglDestroyObject(demo->color_blend);
1780 xglDestroyObject(demo->depth_stencil);
1781
1782 xglDestroyObject(demo->pipeline);
Chia-I Wu6e68a892015-02-23 10:41:08 -07001783 xglDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001784
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001785 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1786 xglDestroyObject(demo->textures[i].view);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001787 xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001788 xglDestroyObject(demo->textures[i].image);
Jon Ashburna9ae3832015-01-16 09:37:43 -07001789 for (j = 0; j < demo->textures[i].num_mem; j++)
1790 xglFreeMemory(demo->textures[i].mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001791 xglDestroyObject(demo->textures[i].sampler);
1792 }
1793
1794 xglDestroyObject(demo->depth.view);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001795 xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001796 xglDestroyObject(demo->depth.image);
Jon Ashburna9ae3832015-01-16 09:37:43 -07001797 for (j = 0; j < demo->depth.num_mem; j++)
1798 xglFreeMemory(demo->depth.mem[j]);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001799
1800 xglDestroyObject(demo->uniform_data.view);
1801 xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001802 xglDestroyObject(demo->uniform_data.buf);
1803 for (j = 0; j < demo->uniform_data.num_mem; j++)
1804 xglFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001805
1806 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wu68040a42014-11-07 14:30:34 +08001807 xglDestroyObject(demo->buffers[i].fence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001808 xglDestroyObject(demo->buffers[i].view);
1809 xglDestroyObject(demo->buffers[i].image);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001810 xglDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001811 }
1812
1813 xglDestroyDevice(demo->device);
Jon Ashburn92e80132015-01-29 15:47:01 -07001814 xglDestroyInstance(demo->inst);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001815
1816 xcb_destroy_window(demo->connection, demo->window);
1817 xcb_disconnect(demo->connection);
1818}
1819
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001820int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001821{
1822 struct demo demo;
1823
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001824 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001825
1826 demo_prepare(&demo);
1827 demo_create_window(&demo);
1828 demo_run(&demo);
1829
1830 demo_cleanup(&demo);
1831
1832 return 0;
1833}