blob: 7c7eb825090d4ffc3d62b2fdcb30fbbba1390456 [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>
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06009#include <vulkan.h>
10#include <vkDbg.h>
11#include <vkWsiX11Ext.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060012
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 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060024struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060025 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070026
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060027 VkImage image;
28 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060029
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070030 uint32_t num_mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060031 VkGpuMemory *mem;
32 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070033 int32_t tex_width, tex_height;
34};
35
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060036static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060037 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060038};
39
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060040struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060041 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060042 float mvp[4][4];
43 float position[12*3][4];
44 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060045};
46
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060047struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060048 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060049 float mvp[4][4];
50 float position[12*3][4];
51 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060052};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060053
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060054//--------------------------------------------------------------------------------------
55// Mesh and VertexFormat Data
56//--------------------------------------------------------------------------------------
57struct Vertex
58{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060059 float posX, posY, posZ, posW; // Position data
60 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060061};
62
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060063struct VertexPosTex
64{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060065 float posX, posY, posZ, posW; // Position data
66 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060067};
68
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060069#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060070#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060071
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060072static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060073 -1.0f,-1.0f,-1.0f, // Vertex 0
74 -1.0f,-1.0f, 1.0f,
75 -1.0f, 1.0f, 1.0f,
76
77 -1.0f, 1.0f, 1.0f, // Vertex 1
78 -1.0f, 1.0f,-1.0f,
79 -1.0f,-1.0f,-1.0f,
80
81 -1.0f,-1.0f,-1.0f, // Vertex 2
82 1.0f, 1.0f,-1.0f,
83 1.0f,-1.0f,-1.0f,
84
85 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060086 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060087 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060088
89 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060090 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060091 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060092
93 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060094 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060095 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060096
97 -1.0f, 1.0f,-1.0f, // Vertex 6
98 -1.0f, 1.0f, 1.0f,
99 1.0f, 1.0f, 1.0f,
100
101 -1.0f, 1.0f,-1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600102 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600103 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600104
105 1.0f, 1.0f,-1.0f, // Vertex 8
106 1.0f, 1.0f, 1.0f,
107 1.0f,-1.0f, 1.0f,
108
109 1.0f,-1.0f, 1.0f, // Vertex 9
110 1.0f,-1.0f,-1.0f,
111 1.0f, 1.0f,-1.0f,
112
113 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600114 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600115 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600116
117 -1.0f,-1.0f, 1.0f, // Vertex 11
118 1.0f,-1.0f, 1.0f,
119 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600120};
121
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600122static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600123 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600124 0.0f, 0.0f,
125 0.0f, 1.0f,
126
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600127 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600128 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600129 1.0f, 0.0f,
130
131// 0.0f, 1.0f, // Vertex 2
132// 1.0f, 0.0f,
133// 0.0f, 0.0f,
134
135// 0.0f, 1.0f, // Vertex 3
136// 1.0f, 0.0f,
137// 1.0f, 1.0f,
138
139 0.0f, 0.0f, // Vertex 2
140 1.0f, 1.0f,
141 1.0f, 0.0f,
142
143 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600144 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600145 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600146
147 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600148 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600149 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600150
151 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600153 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154
155 0.0f, 1.0f, // Vertex 6
156 1.0f, 1.0f,
157 1.0f, 0.0f,
158
159 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600161 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600163 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600166
167 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 0.0f, 1.0f,
170
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600171 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600173 0.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600174
175 1.0f, 0.0f, // Vertex 11
176 0.0f, 0.0f,
177 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600178};
179
180void dumpMatrix(const char *note, mat4x4 MVP)
181{
182 int i;
183
184 printf("%s: \n", note);
185 for (i=0; i<4; i++) {
186 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
187 }
188 printf("\n");
189 fflush(stdout);
190}
191
192void dumpVec4(const char *note, vec4 vector)
193{
194 printf("%s: \n", note);
195 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
196 printf("\n");
197 fflush(stdout);
198}
199
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600200struct demo {
201 xcb_connection_t *connection;
202 xcb_screen_t *screen;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700203 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600204
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600205 VkInstance inst;
206 VkPhysicalGpu gpu;
207 VkDevice device;
208 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700209 uint32_t graphics_queue_node_index;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600210 VkPhysicalGpuProperties *gpu_props;
211 VkPhysicalGpuQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600212
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600213 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600214 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600215 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600216
217 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600218 VkImage image;
219 VkGpuMemory mem;
220 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600221
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600222 VkColorAttachmentView view;
223 VkFence fence;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600224 } buffers[DEMO_BUFFER_COUNT];
225
226 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600227 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600228
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600229 VkImage image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600230 uint32_t num_mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600231 VkGpuMemory *mem;
232 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600233 } depth;
234
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600235 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600236
237 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600238 VkBuffer buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600239 uint32_t num_mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600240 VkGpuMemory *mem;
241 VkBufferView view;
242 VkBufferViewAttachInfo attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600243 } uniform_data;
244
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600245 VkCmdBuffer cmd; // Buffer for initialization commands
246 VkDescriptorSetLayoutChain desc_layout_chain;
247 VkDescriptorSetLayout desc_layout;
248 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600249
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600250 VkDynamicVpState viewport;
251 VkDynamicRsState raster;
252 VkDynamicCbState color_blend;
253 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600254
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600255 mat4x4 projection_matrix;
256 mat4x4 view_matrix;
257 mat4x4 model_matrix;
258
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600259 float spin_angle;
260 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600261 bool pause;
262
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600263 VkDescriptorPool desc_pool;
264 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800265
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600266 xcb_window_t window;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -0700267 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600268
269 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600270 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600271};
272
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600273static void demo_flush_init_cmd(struct demo *demo)
274{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600275 VkResult err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600276
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600277 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600278 return;
279
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600280 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600281 assert(!err);
282
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600283 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600284
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600285 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600286 assert(!err);
287
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600288 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600289 assert(!err);
290
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600291 vkDestroyObject(demo->cmd);
292 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600293}
294
295static void demo_add_mem_refs(
296 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600297 int num_refs, VkGpuMemory *mem)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600298{
299 for (int i = 0; i < num_refs; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600300 vkQueueAddMemReference(demo->queue, mem[i]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600301 }
302}
303
304static void demo_remove_mem_refs(
305 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600306 int num_refs, VkGpuMemory *mem)
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600307{
308 for (int i = 0; i < num_refs; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600309 vkQueueRemoveMemReference(demo->queue, mem[i]);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600310 }
311}
312
313static void demo_set_image_layout(
314 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkImage image,
316 VkImageLayout old_image_layout,
317 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600318{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600319 VkResult err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600320
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600321 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600323 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600324 .pNext = NULL,
325 .queueNodeIndex = demo->graphics_queue_node_index,
326 .flags = 0,
327 };
328
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600329 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600330 assert(!err);
331
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600332 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600333 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600334 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600335 .flags = VK_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
336 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600337 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600338 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600339 }
340
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600341 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600342 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600343 .pNext = NULL,
344 .outputMask = 0,
345 .inputMask = 0,
346 .oldLayout = old_image_layout,
347 .newLayout = new_image_layout,
348 .image = image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600349 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600350 };
351
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600352 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600353 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600354 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600355 }
356
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600357 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600358 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600359 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600360 }
361
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600362 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600363
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600364 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600365
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 VkPipelineBarrier pipeline_barrier;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600367 pipeline_barrier.sType = VK_STRUCTURE_TYPE_PIPELINE_BARRIER;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600368 pipeline_barrier.pNext = NULL;
369 pipeline_barrier.eventCount = 1;
370 pipeline_barrier.pEvents = set_events;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600371 pipeline_barrier.waitEvent = VK_WAIT_EVENT_TOP_OF_PIPE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600372 pipeline_barrier.memBarrierCount = 1;
373 pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier;
374
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600375 vkCmdPipelineBarrier(demo->cmd, &pipeline_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600376}
377
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600378static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600381 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600382 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600384 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600385 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600386 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600387 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600388 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600389 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
390 .useRawValue = false,
391 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600392 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600393 VkImageSubresourceRange clear_range;
394 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600395 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600396 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600397 .flags = VK_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
398 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700399 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600400 VkResult err;
401 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
402 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
403 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600404 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700405 .pNext = NULL,
406 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
408 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700409 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600410 .width = demo->width,
411 .height = demo->height,
412 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700413 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600414 VkRenderPassCreateInfo rp_info;
415 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700416
417 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600418 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700419 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600420 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700421 rp_info.renderArea.extent.width = demo->width;
422 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600423 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
424 rp_info.pColorFormats = &demo->format;
425 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700426 rp_info.pColorLoadOps = &load_op;
427 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600428 rp_info.pColorLoadClearValues = &clear_color;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600429 rp_info.depthStencilFormat = VK_FMT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600430 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600431 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600432 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600433 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
434 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600435 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600436 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
437 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700438 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600439
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600440 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600441 assert(!err);
442
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600444 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600445 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wua28bdde2015-03-28 15:23:55 +0800446 demo->desc_layout_chain, 0, 1, &demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600447
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600448 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_VIEWPORT, demo->viewport);
449 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_RASTER, demo->raster);
450 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600451 demo->color_blend);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600452 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600453 demo->depth_stencil);
454
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
456 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600457 clear_range.baseMipLevel = 0;
458 clear_range.mipLevels = 1;
459 clear_range.baseArraySlice = 0;
460 clear_range.arraySize = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600462 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600463 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600464 clear_color, 1, &clear_range);
465
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600466 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
467 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
468 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600469 clear_depth, 0, 1, &clear_range);
470
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600471 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
472 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600474 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600475 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700476
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600477 vkDestroyObject(rp_begin.renderPass);
478 vkDestroyObject(rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600479}
480
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600481
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600482void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600483{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600484 mat4x4 MVP, Model, VP;
485 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600486 uint8_t *pData;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600487 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600488
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600489 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600490
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600491 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600492 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700493 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600494 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600495
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700496 assert(demo->uniform_data.num_mem == 1);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 err = vkMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600498 assert(!err);
499
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600500 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600501
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600502 err = vkUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600503 assert(!err);
504}
505
506static void demo_draw(struct demo *demo)
507{
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600508 const VK_WSI_X11_PRESENT_INFO present = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600509 .destWindow = demo->window,
510 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800511 .async = true,
512 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600513 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600514 VkFence fence = demo->buffers[demo->current_buffer].fence;
515 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600516
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600517 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, ~((uint64_t) 0));
518 assert(err == VK_SUCCESS || err == VK_ERROR_UNAVAILABLE);
Chia-I Wubb57f642014-11-07 14:30:34 +0800519
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600520 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
521 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600522 assert(!err);
523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600524 err = vkWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525 assert(!err);
526
527 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
528}
529
530static void demo_prepare_buffers(struct demo *demo)
531{
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600532 const VK_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600533 .format = demo->format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600534 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535 .extent = {
536 .width = demo->width,
537 .height = demo->height,
538 },
539 .flags = 0,
540 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600541 const VkFenceCreateInfo fence = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600542 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
Chia-I Wubb57f642014-11-07 14:30:34 +0800543 .pNext = NULL,
544 .flags = 0,
545 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600546 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600547 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600548
549 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600550 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600551 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552 .pNext = NULL,
553 .format = demo->format,
554 .mipLevel = 0,
555 .baseArraySlice = 0,
556 .arraySize = 1,
557 };
558
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600559 err = vkWsiX11CreatePresentableImage(demo->device, &presentable_image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600560 &demo->buffers[i].image, &demo->buffers[i].mem);
561 assert(!err);
562
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600563 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600564
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600565 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600566 VK_IMAGE_LAYOUT_UNDEFINED,
567 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600568
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600569 color_attachment_view.image = demo->buffers[i].image;
570
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600571 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600572 &color_attachment_view, &demo->buffers[i].view);
573 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800574
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600575 err = vkCreateFence(demo->device,
Chia-I Wubb57f642014-11-07 14:30:34 +0800576 &fence, &demo->buffers[i].fence);
577 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578 }
579}
580
581static void demo_prepare_depth(struct demo *demo)
582{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600583 const VkFormat depth_format = VK_FMT_D16_UNORM;
584 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600585 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600586 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600587 .imageType = VK_IMAGE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600588 .format = depth_format,
589 .extent = { demo->width, demo->height, 1 },
590 .mipLevels = 1,
591 .arraySize = 1,
592 .samples = 1,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600593 .tiling = VK_OPTIMAL_TILING,
594 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600595 .flags = 0,
596 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600597 VkMemoryAllocImageInfo img_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600598 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700599 .pNext = NULL,
600 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600601 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600602 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700603 .pNext = &img_alloc,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600604 .allocationSize = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600605 .memProps = VK_MEMORY_PROPERTY_GPU_ONLY,
606 .memType = VK_MEMORY_TYPE_IMAGE,
607 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600608 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600609 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600610 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600611 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600612 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600613 .mipLevel = 0,
614 .baseArraySlice = 0,
615 .arraySize = 1,
616 .flags = 0,
617 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600618 VkMemoryRequirements *mem_reqs;
619 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
620 VkImageMemoryRequirements img_reqs;
621 size_t img_reqs_size = sizeof(VkImageMemoryRequirements);
622 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600623 uint32_t num_allocations = 0;
624 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600625
626 demo->depth.format = depth_format;
627
628 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600629 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600630 &demo->depth.image);
631 assert(!err);
632
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600633 err = vkGetObjectInfo(demo->depth.image, VK_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700634 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600635 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
636 demo->depth.mem = malloc(num_allocations * sizeof(VkGpuMemory));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700637 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600638 err = vkGetObjectInfo(demo->depth.image,
639 VK_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700640 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600641 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600642 err = vkGetObjectInfo(demo->depth.image,
643 VK_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700644 &img_reqs_size, &img_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600645 assert(!err && img_reqs_size == sizeof(VkImageMemoryRequirements));
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700646 img_alloc.usage = img_reqs.usage;
647 img_alloc.formatClass = img_reqs.formatClass;
648 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600649 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700650 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600651
Jon Ashburnb2a66652015-01-16 09:37:43 -0700652 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600653 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700654 &(demo->depth.mem[i]));
655 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600656
Jon Ashburnb2a66652015-01-16 09:37:43 -0700657 /* bind memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600658 err = vkBindObjectMemory(demo->depth.image, i,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700659 demo->depth.mem[i], 0);
660 assert(!err);
661 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600663 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600664 VK_IMAGE_LAYOUT_UNDEFINED,
665 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600666
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600667 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600668
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600669 /* create image view */
670 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600671 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600672 &demo->depth.view);
673 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674}
675
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600676/** loadTexture
677 * loads a png file into an memory object, using cstdio , libpng.
678 *
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600679 * \param demo : Needed to access VK calls
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600680 * \param filename : the png file to be loaded
681 * \param width : width of png, to be updated as a side effect of this function
682 * \param height : height of png, to be updated as a side effect of this function
683 *
684 * \return bool : an opengl texture id. true if successful?,
685 * should be validated by the client of this function.
686 *
687 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
688 * Modified to copy image to memory
689 *
690 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700691bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600692 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600693 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600694{
695 //header for testing if it is a png
696 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700697 int is_png, bit_depth, color_type,rowbytes;
698 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600699 png_structp png_ptr;
700 png_infop info_ptr, end_info;
701 png_byte *image_data;
702 png_bytep *row_pointers;
703
704 //open file as binary
705 FILE *fp = fopen(filename, "rb");
706 if (!fp) {
707 return false;
708 }
709
710 //read the header
711 fread(header, 1, 8, fp);
712
713 //test if png
714 is_png = !png_sig_cmp(header, 0, 8);
715 if (!is_png) {
716 fclose(fp);
717 return false;
718 }
719
720 //create png struct
721 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
722 NULL, NULL);
723 if (!png_ptr) {
724 fclose(fp);
725 return (false);
726 }
727
728 //create png info struct
729 info_ptr = png_create_info_struct(png_ptr);
730 if (!info_ptr) {
731 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
732 fclose(fp);
733 return (false);
734 }
735
736 //create png info struct
737 end_info = png_create_info_struct(png_ptr);
738 if (!end_info) {
739 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
740 fclose(fp);
741 return (false);
742 }
743
744 //png error stuff, not sure libpng man suggests this.
745 if (setjmp(png_jmpbuf(png_ptr))) {
746 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
747 fclose(fp);
748 return (false);
749 }
750
751 //init png reading
752 png_init_io(png_ptr, fp);
753
754 //let libpng know you already read the first 8 bytes
755 png_set_sig_bytes(png_ptr, 8);
756
757 // read all the info up to the image data
758 png_read_info(png_ptr, info_ptr);
759
760 // get info about png
761 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
762 NULL, NULL, NULL);
763
764 //update width and height based on png info
765 *width = twidth;
766 *height = theight;
767
768 // Require that incoming texture be 8bits per color component
769 // and 4 components (RGBA).
770 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
771 png_get_channels(png_ptr, info_ptr) != 4) {
772 return false;
773 }
774
775 if (rgba_data == NULL) {
776 // If data pointer is null, we just want the width & height
777 // clean up memory and close stuff
778 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
779 fclose(fp);
780
781 return true;
782 }
783
784 // Update the png info struct.
785 png_read_update_info(png_ptr, info_ptr);
786
787 // Row size in bytes.
788 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
789
790 // Allocate the image_data as a big block, to be given to opengl
791 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
792 if (!image_data) {
793 //clean up memory and close stuff
794 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
795 fclose(fp);
796 return false;
797 }
798
799 // row_pointers is for pointing to image_data for reading the png with libpng
800 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
801 if (!row_pointers) {
802 //clean up memory and close stuff
803 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
804 // delete[] image_data;
805 fclose(fp);
806 return false;
807 }
808 // set the individual row_pointers to point at the correct offsets of image_data
809 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700810 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600811
812 // read the png into image_data through row_pointers
813 png_read_image(png_ptr, row_pointers);
814
815 // clean up memory and close stuff
816 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
817 free(row_pointers);
818 free(image_data);
819 fclose(fp);
820
821 return true;
822}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600823
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700824static void demo_prepare_texture_image(struct demo *demo,
825 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600826 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600827 VkImageTiling tiling,
828 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600829{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600830 const VkFormat tex_format = VK_FMT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600831 int32_t tex_width;
832 int32_t tex_height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600833 VkResult err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700834
835 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
836 assert(err);
837
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600838 tex_obj->tex_width = tex_width;
839 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700840
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600841 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600842 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700843 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600844 .imageType = VK_IMAGE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700845 .format = tex_format,
846 .extent = { tex_width, tex_height, 1 },
847 .mipLevels = 1,
848 .arraySize = 1,
849 .samples = 1,
850 .tiling = tiling,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600851 .usage = VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700852 .flags = 0,
853 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600854 VkMemoryAllocBufferInfo buf_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600855 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
Piers Daniell735ee532015-02-23 16:23:13 -0700856 .pNext = NULL,
857 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600858 VkMemoryAllocImageInfo img_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600859 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO,
Piers Daniell735ee532015-02-23 16:23:13 -0700860 .pNext = &buf_alloc,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700861 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600862 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600863 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700864 .pNext = &img_alloc,
865 .allocationSize = 0,
866 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600867 .memType = VK_MEMORY_TYPE_IMAGE,
868 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700869 };
870
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600871 VkMemoryRequirements *mem_reqs;
872 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
873 VkBufferMemoryRequirements buf_reqs;
874 size_t buf_reqs_size = sizeof(VkBufferMemoryRequirements);
875 VkImageMemoryRequirements img_reqs;
876 size_t img_reqs_size = sizeof(VkImageMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700877 uint32_t num_allocations = 0;
878 size_t num_alloc_size = sizeof(num_allocations);
879
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600880 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600881 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700882 assert(!err);
883
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600884 err = vkGetObjectInfo(tex_obj->image,
885 VK_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700886 &num_alloc_size, &num_allocations);
887 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600888 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
889 tex_obj->mem = malloc(num_allocations * sizeof(VkGpuMemory));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600890 err = vkGetObjectInfo(tex_obj->image,
891 VK_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700892 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600893 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600894 err = vkGetObjectInfo(tex_obj->image,
895 VK_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700896 &img_reqs_size, &img_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600897 assert(!err && img_reqs_size == sizeof(VkImageMemoryRequirements));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700898 img_alloc.usage = img_reqs.usage;
899 img_alloc.formatClass = img_reqs.formatClass;
900 img_alloc.samples = img_reqs.samples;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600901 mem_alloc.memProps = VK_MEMORY_PROPERTY_CPU_VISIBLE_BIT;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700902 for (uint32_t j = 0; j < num_allocations; j ++) {
Courtney Goeltzenleuchter02ba99a2015-02-25 11:48:28 -0700903 mem_alloc.memType = mem_reqs[j].memType;
Piers Daniell735ee532015-02-23 16:23:13 -0700904 mem_alloc.allocationSize = mem_reqs[j].size;
905
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600906 if (mem_alloc.memType == VK_MEMORY_TYPE_BUFFER) {
907 err = vkGetObjectInfo(tex_obj->image,
908 VK_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
Piers Daniell735ee532015-02-23 16:23:13 -0700909 &buf_reqs_size, &buf_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600910 assert(!err && buf_reqs_size == sizeof(VkBufferMemoryRequirements));
Piers Daniell735ee532015-02-23 16:23:13 -0700911 buf_alloc.usage = buf_reqs.usage;
912 img_alloc.pNext = &buf_alloc;
913 } else {
914 img_alloc.pNext = 0;
915 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700916
917 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600918 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600919 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700920 assert(!err);
921
922 /* bind memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600923 err = vkBindObjectMemory(tex_obj->image, j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700924 assert(!err);
925 }
926 free(mem_reqs);
927 mem_reqs = NULL;
928
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600929 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700930
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600931 if (mem_props & VK_MEMORY_PROPERTY_CPU_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600932 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600933 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934 .mipLevel = 0,
935 .arraySlice = 0,
936 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600937 VkSubresourceLayout layout;
938 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700939 void *data;
940
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600941 err = vkGetImageSubresourceInfo(tex_obj->image, &subres,
942 VK_INFO_TYPE_SUBRESOURCE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943 &layout_size, &layout);
944 assert(!err && layout_size == sizeof(layout));
945 /* Linear texture must be within a single memory object */
946 assert(num_allocations == 1);
947
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600948 err = vkMapMemory(tex_obj->mem[0], 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700949 assert(!err);
950
951 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
952 fprintf(stderr, "Error loading texture: %s\n", filename);
953 }
954
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600955 err = vkUnmapMemory(tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700956 assert(!err);
957 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600958
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600959 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600960 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600961 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600962 tex_obj->imageLayout);
963 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700964}
965
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600966static void demo_destroy_texture_image(struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700967{
968 /* clean up staging resources */
969 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600970 vkBindObjectMemory(tex_objs->image, j, VK_NULL_HANDLE, 0);
971 vkFreeMemory(tex_objs->mem[j]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700972 }
973
974 free(tex_objs->mem);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600975 vkDestroyObject(tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700976}
977
978static void demo_prepare_textures(struct demo *demo)
979{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600980 const VkFormat tex_format = VK_FMT_R8G8B8A8_UNORM;
981 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700982 size_t size = sizeof(props);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600983 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600984 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600985
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600986 err = vkGetFormatInfo(demo->device, tex_format,
987 VK_INFO_TYPE_FORMAT_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700988 &size, &props);
989 assert(!err);
990
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600991 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700992
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600993 if (props.linearTilingFeatures & VK_FORMAT_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700994 /* Device can texture using linear textures */
995 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600996 VK_LINEAR_TILING, VK_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600997 } else if (props.optimalTilingFeatures & VK_FORMAT_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700998 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600999 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001000
1001 memset(&staging_texture, 0, sizeof(staging_texture));
1002 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001003 VK_LINEAR_TILING, VK_MEMORY_PROPERTY_CPU_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001004
1005 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001006 VK_OPTIMAL_TILING, VK_MEMORY_PROPERTY_GPU_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001007
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001008 demo_set_image_layout(demo, staging_texture.image,
1009 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001010 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001012 demo_set_image_layout(demo, demo->textures[i].image,
1013 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001014 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001015
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001016 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001017 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001019 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001020 .destOffset = { 0, 0, 0 },
1021 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1022 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001023 vkCmdCopyImage(demo->cmd,
1024 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1025 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001026 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001027
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001028 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
1029 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001030
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001031 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001032 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001033 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001034
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001035 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036
1037 demo_destroy_texture_image(&staging_texture);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001038 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001039 } else {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001040 /* Can't support VK_FMT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1042 }
1043
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001044 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001045 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001046 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001047 .magFilter = VK_TEX_FILTER_NEAREST,
1048 .minFilter = VK_TEX_FILTER_NEAREST,
1049 .mipMode = VK_TEX_MIPMAP_BASE,
1050 .addressU = VK_TEX_ADDRESS_CLAMP,
1051 .addressV = VK_TEX_ADDRESS_CLAMP,
1052 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001053 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001054 .maxAnisotropy = 1,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001055 .compareFunc = VK_COMPARE_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001056 .minLod = 0.0f,
1057 .maxLod = 0.0f,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001058 .borderColorType = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001059 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001060
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001061 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001062 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001063 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001064 .image = VK_NULL_HANDLE,
1065 .viewType = VK_IMAGE_VIEW_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001067 .channels = { VK_CHANNEL_SWIZZLE_R,
1068 VK_CHANNEL_SWIZZLE_G,
1069 VK_CHANNEL_SWIZZLE_B,
1070 VK_CHANNEL_SWIZZLE_A, },
1071 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001072 .minLod = 0.0f,
1073 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001074
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001076 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001077 &demo->textures[i].sampler);
1078 assert(!err);
1079
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001080 /* create image view */
1081 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001082 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001083 &demo->textures[i].view);
1084 assert(!err);
1085 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001086}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001087
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001088void demo_prepare_cube_data_buffer(struct demo *demo)
1089{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001090 VkBufferCreateInfo buf_info;
1091 VkBufferViewCreateInfo view_info;
1092 VkMemoryAllocBufferInfo buf_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001093 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001094 .pNext = NULL,
1095 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001096 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001097 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001098 .pNext = &buf_alloc,
1099 .allocationSize = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001100 .memProps = VK_MEMORY_PROPERTY_CPU_VISIBLE_BIT,
1101 .memType = VK_MEMORY_TYPE_BUFFER,
1102 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001103 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001104 VkMemoryRequirements *mem_reqs;
1105 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
1106 VkBufferMemoryRequirements buf_reqs;
1107 size_t buf_reqs_size = sizeof(VkBufferMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001108 uint32_t num_allocations = 0;
1109 size_t num_alloc_size = sizeof(num_allocations);
1110 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001111 int i;
1112 mat4x4 MVP, VP;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001113 VkResult err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001114 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001115
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001116 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001117 mat4x4_mul(MVP, VP, demo->model_matrix);
1118 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001119// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001120
1121 for (i=0; i<12*3; i++) {
1122 data.position[i][0] = g_vertex_buffer_data[i*3];
1123 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1124 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1125 data.position[i][3] = 1.0f;
1126 data.attr[i][0] = g_uv_buffer_data[2*i];
1127 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1128 data.attr[i][2] = 0;
1129 data.attr[i][3] = 0;
1130 }
1131
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001132 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001133 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001134 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001135 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001136 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001137 assert(!err);
1138
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001139 err = vkGetObjectInfo(demo->uniform_data.buf,
1140 VK_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001141 &num_alloc_size, &num_allocations);
1142 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001143 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
1144 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkGpuMemory));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001145 demo->uniform_data.num_mem = num_allocations;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001146 err = vkGetObjectInfo(demo->uniform_data.buf,
1147 VK_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001148 &mem_reqs_size, mem_reqs);
1149 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001150 err = vkGetObjectInfo(demo->uniform_data.buf,
1151 VK_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001152 &buf_reqs_size, &buf_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001153 assert(!err && buf_reqs_size == sizeof(VkBufferMemoryRequirements));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001154 buf_alloc.usage = buf_reqs.usage;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001155 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001156 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001157
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001158 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001159 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001160
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001161 err = vkMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001162 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001163
Piers Daniell735ee532015-02-23 16:23:13 -07001164 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001165
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001166 err = vkUnmapMemory(demo->uniform_data.mem[i]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001167 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001168
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001169 err = vkBindObjectMemory(demo->uniform_data.buf, i,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001170 demo->uniform_data.mem[i], 0);
1171 assert(!err);
1172 }
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001173 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001174
1175 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001176 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001177 view_info.buffer = demo->uniform_data.buf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001178 view_info.viewType = VK_BUFFER_VIEW_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001179 view_info.offset = 0;
1180 view_info.range = sizeof(data);
1181
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001182 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001183 assert(!err);
1184
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001185 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001186 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001187}
1188
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001189static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001190{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001191 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001192 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001193 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001194 .count = 1,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001195 .stageFlags = VK_SHADER_STAGE_FLAGS_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001196 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001197 },
1198 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001199 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001200 .count = DEMO_TEXTURE_COUNT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001201 .stageFlags = VK_SHADER_STAGE_FLAGS_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001202 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001203 },
1204 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001205 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001206 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001207 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001208 .count = 2,
1209 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001210 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001211 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001213 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001214 &descriptor_layout, &demo->desc_layout);
1215 assert(!err);
1216
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001217 err = vkCreateDescriptorSetLayoutChain(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001218 1, &demo->desc_layout, &demo->desc_layout_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001219 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001220}
1221
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001222static VkShader demo_prepare_shader(struct demo *demo,
1223 VkPipelineShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001224 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001225 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001226{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001227 VkShaderCreateInfo createInfo;
1228 VkShader shader;
1229 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001230
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001231
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001232 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001233 createInfo.pNext = NULL;
1234
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001235#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001236 createInfo.codeSize = size;
1237 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001238 createInfo.flags = 0;
1239
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001240 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001241 if (err) {
1242 free((void *) createInfo.pCode);
1243 }
1244#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001245 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001246 // to the driver "under the covers"
1247 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1248 createInfo.pCode = malloc(createInfo.codeSize);
1249 createInfo.flags = 0;
1250
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001251 /* try version 0 first: VkPipelineShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001252 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001253 ((uint32_t *) createInfo.pCode)[1] = 0;
1254 ((uint32_t *) createInfo.pCode)[2] = stage;
1255 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1256
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001257 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001258 if (err) {
1259 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001260 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001261 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001262#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001263
1264 return shader;
1265}
1266
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001267char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001268{
1269 long int size;
1270 void *shader_code;
1271
1272 FILE *fp = fopen(filename, "rb");
1273 if (!fp) return NULL;
1274
1275 fseek(fp, 0L, SEEK_END);
1276 size = ftell(fp);
1277
1278 fseek(fp, 0L, SEEK_SET);
1279
1280 shader_code = malloc(size);
1281 fread(shader_code, size, 1, fp);
1282
1283 *psize = size;
1284
1285 return shader_code;
1286}
1287
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001288static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001289{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001290#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001291 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001292 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001293
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001294 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001295
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001296 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001297 vertShaderCode, size);
1298#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001299 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001300 "#version 140\n"
1301 "#extension GL_ARB_separate_shader_objects : enable\n"
1302 "#extension GL_ARB_shading_language_420pack : enable\n"
1303 "\n"
1304 "layout(binding = 0) uniform buf {\n"
1305 " mat4 MVP;\n"
1306 " vec4 position[12*3];\n"
1307 " vec4 attr[12*3];\n"
1308 "} ubuf;\n"
1309 "\n"
1310 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001311 "\n"
1312 "void main() \n"
1313 "{\n"
1314 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001315 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1316 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001317
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001318 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001319 (const void *) vertShaderText,
1320 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001321#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001322}
1323
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001324static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001325{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001326#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001327 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001328 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001329
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001330 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001331
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001332 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001333 fragShaderCode, size);
1334#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001335 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001336 "#version 140\n"
1337 "#extension GL_ARB_separate_shader_objects : enable\n"
1338 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001339 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001340 "\n"
1341 "layout (location = 0) in vec4 texcoord;\n"
1342 "void main() {\n"
1343 " gl_FragColor = texture(tex, texcoord.xy);\n"
1344 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001345
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001346 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001347 (const void *) fragShaderText,
1348 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001349#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001350}
1351
1352static void demo_prepare_pipeline(struct demo *demo)
1353{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001354 VkGraphicsPipelineCreateInfo pipeline;
1355 VkPipelineIaStateCreateInfo ia;
1356 VkPipelineRsStateCreateInfo rs;
1357 VkPipelineCbStateCreateInfo cb;
1358 VkPipelineDsStateCreateInfo ds;
1359 VkPipelineShaderStageCreateInfo vs;
1360 VkPipelineShaderStageCreateInfo fs;
1361 VkPipelineVpStateCreateInfo vp;
1362 VkPipelineMsStateCreateInfo ms;
1363 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001364
1365 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001366 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001367 pipeline.pSetLayoutChain = demo->desc_layout_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001368
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001369 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001370 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1371 ia.topology = VK_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001372
1373 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001374 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
1375 rs.fillMode = VK_FILL_SOLID;
1376 rs.cullMode = VK_CULL_BACK;
1377 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001378
1379 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001380 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001381 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001382 memset(att_state, 0, sizeof(att_state));
1383 att_state[0].format = demo->format;
1384 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001385 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001386 cb.attachmentCount = 1;
1387 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388
Tony Barbour29645d02015-01-16 14:27:35 -07001389 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001390 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001391 vp.numViewports = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001392 vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001393
1394 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001395 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001396 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001397 ds.depthTestEnable = VK_TRUE;
1398 ds.depthWriteEnable = VK_TRUE;
1399 ds.depthFunc = VK_COMPARE_LESS_EQUAL;
1400 ds.depthBoundsEnable = VK_FALSE;
1401 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1402 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
1403 ds.back.stencilFunc = VK_COMPARE_ALWAYS;
1404 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001405 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001408 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1409 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001410 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001411 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412
1413 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001414 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1415 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001417 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001418
1419 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001420 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001421 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001422 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001423 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001425 pipeline.pNext = (const void *) &ia;
1426 ia.pNext = (const void *) &rs;
1427 rs.pNext = (const void *) &cb;
1428 cb.pNext = (const void *) &ms;
1429 ms.pNext = (const void *) &vp;
1430 vp.pNext = (const void *) &ds;
1431 ds.pNext = (const void *) &vs;
1432 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001433
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001434 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435 assert(!err);
1436
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001437 vkDestroyObject(vs.shader.shader);
1438 vkDestroyObject(fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001439}
1440
1441static void demo_prepare_dynamic_states(struct demo *demo)
1442{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001443 VkDynamicVpStateCreateInfo viewport_create;
1444 VkDynamicRsStateCreateInfo raster;
1445 VkDynamicCbStateCreateInfo color_blend;
1446 VkDynamicDsStateCreateInfo depth_stencil;
1447 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001448
Tony Barbour29645d02015-01-16 14:27:35 -07001449 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001450 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001451 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001452 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001453 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001454 viewport.height = (float) demo->height;
1455 viewport.width = (float) demo->width;
1456 viewport.minDepth = (float) 0.0f;
1457 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001458 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001459 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001460 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001461 scissor.extent.width = demo->width;
1462 scissor.extent.height = demo->height;
1463 scissor.offset.x = 0;
1464 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001465 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001466
1467 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001468 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001469 raster.pointSize = 1.0;
1470 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001471
1472 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001473 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001474 color_blend.blendConst[0] = 1.0f;
1475 color_blend.blendConst[1] = 1.0f;
1476 color_blend.blendConst[2] = 1.0f;
1477 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478
1479 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001480 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001481 depth_stencil.minDepth = 0.0f;
1482 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001483 depth_stencil.stencilBackRef = 0;
1484 depth_stencil.stencilFrontRef = 0;
1485 depth_stencil.stencilReadMask = 0xff;
1486 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001487
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001488 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001489 assert(!err);
1490
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001491 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001492 assert(!err);
1493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001494 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001495 &color_blend, &demo->color_blend);
1496 assert(!err);
1497
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001498 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001499 &depth_stencil, &demo->depth_stencil);
1500 assert(!err);
1501}
1502
Chia-I Wu63ea9262015-03-26 13:14:16 +08001503static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001504{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001505 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001506 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001507 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001508 .count = 1,
1509 },
1510 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001511 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001512 .count = DEMO_TEXTURE_COUNT,
1513 },
1514 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001515 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001516 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001517 .pNext = NULL,
1518 .count = 2,
1519 .pTypeCount = type_counts,
1520 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001521 VkResult err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001522
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001523 err = vkCreateDescriptorPool(demo->device,
1524 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001525 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001526 assert(!err);
1527}
1528
1529static void demo_prepare_descriptor_set(struct demo *demo)
1530{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001531 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1532 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1533 VkUpdateSamplerTextures update_fs;
1534 VkUpdateBuffers update_vs;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001535 const void *update_array[2] = { &update_vs, &update_fs };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001536 VkResult err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001537 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001538 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001539
1540 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001541 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001542 view_info[i].pNext = NULL;
1543 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001544 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001545
Courtney Goeltzenleuchtereb4754f2015-04-09 11:43:10 -06001546 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001547 combined_info[i].pImageView = &view_info[i];
1548 }
1549
1550 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001551 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001552 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001553 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001554 update_vs.count = 1;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001555 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001556
1557 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001558 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001559 update_fs.binding = 1;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001560 update_fs.count = DEMO_TEXTURE_COUNT;
1561 update_fs.pSamplerImageViews = combined_info;
1562
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001563 err = vkAllocDescriptorSets(demo->desc_pool,
1564 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001565 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001566 &demo->desc_set, &count);
1567 assert(!err && count == 1);
1568
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001569 vkBeginDescriptorPoolUpdate(demo->device,
1570 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001571
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001572 vkClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set);
1573 vkUpdateDescriptors(demo->desc_set, 2, update_array);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001574
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001575 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001576}
1577
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001578static void demo_prepare(struct demo *demo)
1579{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001580 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001581 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001583 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001584 .flags = 0,
1585 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001586 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001587
1588 demo_prepare_buffers(demo);
1589 demo_prepare_depth(demo);
1590 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001591 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001592
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001593 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001594 demo_prepare_pipeline(demo);
1595 demo_prepare_dynamic_states(demo);
1596
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001597 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001598 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001599 assert(!err);
1600 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001601
Chia-I Wu63ea9262015-03-26 13:14:16 +08001602 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001603 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001604
1605
1606 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1607 demo->current_buffer = i;
1608 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1609 }
1610
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001611 /*
1612 * Prepare functions above may generate pipeline commands
1613 * that need to be flushed before beginning the render loop.
1614 */
1615 demo_flush_init_cmd(demo);
1616
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001617 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001618}
1619
1620static void demo_handle_event(struct demo *demo,
1621 const xcb_generic_event_t *event)
1622{
Piers Daniell735ee532015-02-23 16:23:13 -07001623 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001624 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001625 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001626 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001628 case XCB_CLIENT_MESSAGE:
1629 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1630 (*demo->atom_wm_delete_window).atom) {
1631 demo->quit = true;
1632 }
1633 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001634 case XCB_KEY_RELEASE:
1635 {
1636 const xcb_key_release_event_t *key =
1637 (const xcb_key_release_event_t *) event;
1638
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001639 switch (key->detail) {
1640 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001641 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001642 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001643 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001644 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001645 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001646 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001647 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001648 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001649 case 0x41:
1650 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001651 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001652 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001653 }
1654 break;
1655 default:
1656 break;
1657 }
1658}
1659
1660static void demo_run(struct demo *demo)
1661{
1662 xcb_flush(demo->connection);
1663
1664 while (!demo->quit) {
1665 xcb_generic_event_t *event;
1666
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001667 if (demo->pause) {
1668 event = xcb_wait_for_event(demo->connection);
1669 } else {
1670 event = xcb_poll_for_event(demo->connection);
1671 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001672 if (event) {
1673 demo_handle_event(demo, event);
1674 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001675 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001676
1677 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001678 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001679 demo_update_data_buffer(demo);
1680
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001681 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001682
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001683 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001684 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001685 }
1686}
1687
1688static void demo_create_window(struct demo *demo)
1689{
1690 uint32_t value_mask, value_list[32];
1691
1692 demo->window = xcb_generate_id(demo->connection);
1693
1694 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1695 value_list[0] = demo->screen->black_pixel;
1696 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1697 XCB_EVENT_MASK_EXPOSURE;
1698
1699 xcb_create_window(demo->connection,
1700 XCB_COPY_FROM_PARENT,
1701 demo->window, demo->screen->root,
1702 0, 0, demo->width, demo->height, 0,
1703 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1704 demo->screen->root_visual,
1705 value_mask, value_list);
1706
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001707 /* Magic code that will send notification when window is destroyed */
1708 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1709 "WM_PROTOCOLS");
1710 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1711
1712 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1713 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1714
1715 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1716 demo->window, (*reply).atom, 4, 32, 1,
1717 &(*demo->atom_wm_delete_window).atom);
1718 free(reply);
1719
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001720 xcb_map_window(demo->connection, demo->window);
1721}
1722
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001723static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001724{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001725 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001726 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001728 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001729 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001730 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001731 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001732 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001734 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001735 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001736 .pNext = NULL,
1737 .pAppInfo = &app,
1738 .pAllocCb = NULL,
1739 .extensionCount = 0,
1740 .ppEnabledExtensionNames = NULL,
1741 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001742 const VK_WSI_X11_CONNECTION_INFO connection = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001743 .pConnection = demo->connection,
1744 .root = demo->screen->root,
1745 .provider = 0,
1746 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001747 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001748 .queueNodeIndex = 0,
1749 .queueCount = 1,
1750 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001751 const char *ext_names[] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001752 "VK_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001754 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001755 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001756 .pNext = NULL,
1757 .queueRecordCount = 1,
1758 .pRequestedQueues = &queue,
1759 .extensionCount = 1,
1760 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001761 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001762 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001763 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001764 uint32_t gpu_count;
1765 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001766 size_t data_size;
1767 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001768
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001769 err = vkCreateInstance(&inst_info, &demo->inst);
1770 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Ian Elliott3979e282015-04-03 15:24:55 -06001771 printf("Cannot find a compatible Vulkan installable client driver "
1772 "(ICD).\nExiting ...\n");
1773 fflush(stdout);
1774 exit(1);
1775 } else {
1776 assert(!err);
1777 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001778
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001779 err = vkEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001780 assert(!err && gpu_count == 1);
1781
1782 for (i = 0; i < device.extensionCount; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001783 err = vkGetExtensionSupport(demo->gpu, ext_names[i]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001784 assert(!err);
1785 }
1786
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001787 err = vkWsiX11AssociateConnection(demo->gpu, &connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001788 assert(!err);
1789
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001790 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001791 assert(!err);
1792
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001793 err = vkGetGpuInfo(demo->gpu, VK_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001794 &data_size, NULL);
1795 assert(!err);
1796
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001797 demo->gpu_props = (VkPhysicalGpuProperties *) malloc(data_size);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001798 err = vkGetGpuInfo(demo->gpu, VK_INFO_TYPE_PHYSICAL_GPU_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001799 &data_size, demo->gpu_props);
1800 assert(!err);
1801
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001802 err = vkGetGpuInfo(demo->gpu, VK_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001803 &data_size, NULL);
1804 assert(!err);
1805
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001806 demo->queue_props = (VkPhysicalGpuQueueProperties *) malloc(data_size);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001807 err = vkGetGpuInfo(demo->gpu, VK_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001808 &data_size, demo->queue_props);
1809 assert(!err);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001810 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalGpuQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001811 assert(queue_count >= 1);
1812
1813 for (i = 0; i < queue_count; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001814 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001815 break;
1816 }
1817 assert(i < queue_count);
1818 demo->graphics_queue_node_index = i;
1819
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001820 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001821 0, &demo->queue);
1822 assert(!err);
1823}
1824
1825static void demo_init_connection(struct demo *demo)
1826{
1827 const xcb_setup_t *setup;
1828 xcb_screen_iterator_t iter;
1829 int scr;
1830
1831 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001832 if (demo->connection == NULL) {
1833 printf("Cannot find a compatible Vulkan installable client driver "
1834 "(ICD).\nExiting ...\n");
1835 fflush(stdout);
1836 exit(1);
1837 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838
1839 setup = xcb_get_setup(demo->connection);
1840 iter = xcb_setup_roots_iterator(setup);
1841 while (scr-- > 0)
1842 xcb_screen_next(&iter);
1843
1844 demo->screen = iter.data;
1845}
1846
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001847static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001848{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001849 vec3 eye = {0.0f, 3.0f, 5.0f};
1850 vec3 origin = {0, 0, 0};
1851 vec3 up = {0.0f, -1.0f, 0.0};
1852
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001853 memset(demo, 0, sizeof(*demo));
1854
Piers Daniell735ee532015-02-23 16:23:13 -07001855 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001856 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1857 demo->use_staging_buffer = true;
1858 }
1859
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001860 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001861 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001862
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001863 demo->width = 500;
1864 demo->height = 500;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001865 demo->format = VK_FMT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001866
1867 demo->spin_angle = 0.01f;
1868 demo->spin_increment = 0.01f;
1869 demo->pause = false;
1870
Piers Daniell735ee532015-02-23 16:23:13 -07001871 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001872 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1873 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001874}
1875
1876static void demo_cleanup(struct demo *demo)
1877{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001878 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001879
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001880 vkDestroyObject(demo->desc_set);
1881 vkDestroyObject(demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001882
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001883 vkDestroyObject(demo->viewport);
1884 vkDestroyObject(demo->raster);
1885 vkDestroyObject(demo->color_blend);
1886 vkDestroyObject(demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001887
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001888 vkDestroyObject(demo->pipeline);
1889 vkDestroyObject(demo->desc_layout_chain);
1890 vkDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001891
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001892 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001893 vkDestroyObject(demo->textures[i].view);
1894 vkBindObjectMemory(demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
1895 vkDestroyObject(demo->textures[i].image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001896 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001897 for (j = 0; j < demo->textures[i].num_mem; j++)
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001898 vkFreeMemory(demo->textures[i].mem[j]);
1899 vkDestroyObject(demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001900 }
1901
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001902 vkDestroyObject(demo->depth.view);
1903 vkBindObjectMemory(demo->depth.image, 0, VK_NULL_HANDLE, 0);
1904 vkDestroyObject(demo->depth.image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001905 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
1906 for (j = 0; j < demo->depth.num_mem; j++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001907 vkFreeMemory(demo->depth.mem[j]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001908 }
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001909
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001910 vkDestroyObject(demo->uniform_data.view);
1911 vkBindObjectMemory(demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
1912 vkDestroyObject(demo->uniform_data.buf);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001913 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001914 for (j = 0; j < demo->uniform_data.num_mem; j++)
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001915 vkFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001916
1917 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001918 vkDestroyObject(demo->buffers[i].fence);
1919 vkDestroyObject(demo->buffers[i].view);
1920 vkDestroyObject(demo->buffers[i].image);
1921 vkDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001922 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001923 }
1924
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001925 vkDestroyDevice(demo->device);
1926 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001927
1928 xcb_destroy_window(demo->connection, demo->window);
1929 xcb_disconnect(demo->connection);
1930}
1931
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001932int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001933{
1934 struct demo demo;
1935
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001936 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001937
1938 demo_prepare(&demo);
1939 demo_create_window(&demo);
1940 demo_run(&demo);
1941
1942 demo_cleanup(&demo);
1943
1944 return 0;
1945}