blob: 71181e5712ef26dcac3710c5a82370d68cdd0793 [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;
Tony Barbour72304ef2015-04-16 15:59:00 -060031 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060032 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;
Tony Barbour72304ef2015-04-16 15:59:00 -0600206 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600207 VkDevice device;
208 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700209 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600210 VkPhysicalDeviceProperties *gpu_props;
211 VkPhysicalDeviceQueueProperties *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;
Tony Barbour72304ef2015-04-16 15:59:00 -0600219 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600220 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;
Tony Barbour72304ef2015-04-16 15:59:00 -0600231 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600232 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;
Tony Barbour72304ef2015-04-16 15:59:00 -0600240 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600241 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,
Tony Barbour72304ef2015-04-16 15:59:00 -0600297 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600298{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600299 vkQueueAddMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600300}
301
302static void demo_remove_mem_refs(
303 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600304 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600305{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600306 vkQueueRemoveMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600307}
308
309static void demo_set_image_layout(
310 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600311 VkImage image,
312 VkImageLayout old_image_layout,
313 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600314{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkResult err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600316
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600317 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600318 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600319 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600320 .pNext = NULL,
321 .queueNodeIndex = demo->graphics_queue_node_index,
322 .flags = 0,
323 };
324
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600325 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600326 assert(!err);
327
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600329 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600330 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600331 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600332 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600333 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600334 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600335 }
336
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600337 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600338 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600339 .pNext = NULL,
340 .outputMask = 0,
341 .inputMask = 0,
342 .oldLayout = old_image_layout,
343 .newLayout = new_image_layout,
344 .image = image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600345 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600346 };
347
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600348 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600349 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600350 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600351 }
352
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600353 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600354 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600355 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600356 }
357
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600359
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600361
Tony Barbour72304ef2015-04-16 15:59:00 -0600362 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600363}
364
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600365static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600368 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600369 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600371 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600373 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600375 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600376 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
377 .useRawValue = false,
378 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600379 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 VkImageSubresourceRange clear_range;
381 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600382 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600383 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600384 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600385 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700386 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600387 VkResult err;
388 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
389 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
390 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600391 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700392 .pNext = NULL,
393 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600394 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
395 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700396 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600397 .width = demo->width,
398 .height = demo->height,
399 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700400 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600401 VkRenderPassCreateInfo rp_info;
402 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700403
404 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600405 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700406 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600407 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700408 rp_info.renderArea.extent.width = demo->width;
409 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600410 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
411 rp_info.pColorFormats = &demo->format;
412 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700413 rp_info.pColorLoadOps = &load_op;
414 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600415 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600416 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600417 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600418 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600419 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600420 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
421 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600422 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600423 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
424 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700425 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600426
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600427 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600428 assert(!err);
429
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600430 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600431 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600432 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wu6521e9e2015-04-17 02:00:54 +0800433 0, 1, &demo->desc_set, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600434
Tony Barbour72304ef2015-04-16 15:59:00 -0600435 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
436 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
437 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600438 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600439 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600440 demo->depth_stencil);
441
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600442 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
443 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600444 clear_range.baseMipLevel = 0;
445 clear_range.mipLevels = 1;
446 clear_range.baseArraySlice = 0;
447 clear_range.arraySize = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600448 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600449 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600450 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600451 clear_color, 1, &clear_range);
452
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600453 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
454 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
455 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600456 clear_depth, 0, 1, &clear_range);
457
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600458 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
459 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600460
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600462 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700463
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600464 vkDestroyObject(rp_begin.renderPass);
465 vkDestroyObject(rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600466}
467
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600468
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600469void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600470{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600471 mat4x4 MVP, Model, VP;
472 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600473 uint8_t *pData;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600474 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600475
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600476 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600477
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600478 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600479 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700480 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600481 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600482
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700483 assert(demo->uniform_data.num_mem == 1);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600484 err = vkMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600485 assert(!err);
486
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600487 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600488
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 err = vkUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600490 assert(!err);
491}
492
493static void demo_draw(struct demo *demo)
494{
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600495 const VK_WSI_X11_PRESENT_INFO present = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600496 .destWindow = demo->window,
497 .srcImage = demo->buffers[demo->current_buffer].image,
Chia-I Wubb57f642014-11-07 14:30:34 +0800498 .async = true,
499 .flip = false,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600500 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600501 VkFence fence = demo->buffers[demo->current_buffer].fence;
502 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600503
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600504 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, ~((uint64_t) 0));
505 assert(err == VK_SUCCESS || err == VK_ERROR_UNAVAILABLE);
Chia-I Wubb57f642014-11-07 14:30:34 +0800506
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600507 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
508 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600509 assert(!err);
510
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600511 err = vkWsiX11QueuePresent(demo->queue, &present, fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600512 assert(!err);
513
514 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
515}
516
517static void demo_prepare_buffers(struct demo *demo)
518{
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600519 const VK_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600520 .format = demo->format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600521 .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600522 .extent = {
523 .width = demo->width,
524 .height = demo->height,
525 },
526 .flags = 0,
527 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600528 const VkFenceCreateInfo fence = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600529 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
Chia-I Wubb57f642014-11-07 14:30:34 +0800530 .pNext = NULL,
531 .flags = 0,
532 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600533 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600534 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535
536 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600537 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600538 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539 .pNext = NULL,
540 .format = demo->format,
541 .mipLevel = 0,
542 .baseArraySlice = 0,
543 .arraySize = 1,
544 };
545
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600546 err = vkWsiX11CreatePresentableImage(demo->device, &presentable_image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600547 &demo->buffers[i].image, &demo->buffers[i].mem);
548 assert(!err);
549
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600550 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600551
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600552 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600553 VK_IMAGE_LAYOUT_UNDEFINED,
554 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600555
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600556 color_attachment_view.image = demo->buffers[i].image;
557
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600558 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600559 &color_attachment_view, &demo->buffers[i].view);
560 assert(!err);
Chia-I Wubb57f642014-11-07 14:30:34 +0800561
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600562 err = vkCreateFence(demo->device,
Chia-I Wubb57f642014-11-07 14:30:34 +0800563 &fence, &demo->buffers[i].fence);
564 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600565 }
566}
567
568static void demo_prepare_depth(struct demo *demo)
569{
Tony Barbour72304ef2015-04-16 15:59:00 -0600570 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600571 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600572 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600573 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600574 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575 .format = depth_format,
576 .extent = { demo->width, demo->height, 1 },
577 .mipLevels = 1,
578 .arraySize = 1,
579 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600580 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600581 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600582 .flags = 0,
583 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600584 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600585 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500586 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600587 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600588 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600589 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600590 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600591 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600592 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600593 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600594 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600595 .mipLevel = 0,
596 .baseArraySlice = 0,
597 .arraySize = 1,
598 .flags = 0,
599 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600600 VkMemoryRequirements *mem_reqs;
601 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600602 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600603 uint32_t num_allocations = 0;
604 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600605
606 demo->depth.format = depth_format;
607
608 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600609 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600610 &demo->depth.image);
611 assert(!err);
612
Tony Barbour72304ef2015-04-16 15:59:00 -0600613 err = vkGetObjectInfo(demo->depth.image, VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700614 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600615 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600616 demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700617 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600618 err = vkGetObjectInfo(demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600619 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700620 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600621 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600622 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700623 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624
Jon Ashburnb2a66652015-01-16 09:37:43 -0700625 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600626 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700627 &(demo->depth.mem[i]));
628 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600629
Jon Ashburnb2a66652015-01-16 09:37:43 -0700630 /* bind memory */
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500631 err = vkQueueBindObjectMemory(demo->queue, demo->depth.image, i,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700632 demo->depth.mem[i], 0);
633 assert(!err);
634 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600635
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600636 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600637 VK_IMAGE_LAYOUT_UNDEFINED,
638 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600639
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600640 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600641
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600642 /* create image view */
643 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600644 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600645 &demo->depth.view);
646 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600647}
648
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600649/** loadTexture
650 * loads a png file into an memory object, using cstdio , libpng.
651 *
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600652 * \param demo : Needed to access VK calls
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600653 * \param filename : the png file to be loaded
654 * \param width : width of png, to be updated as a side effect of this function
655 * \param height : height of png, to be updated as a side effect of this function
656 *
657 * \return bool : an opengl texture id. true if successful?,
658 * should be validated by the client of this function.
659 *
660 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
661 * Modified to copy image to memory
662 *
663 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700664bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600665 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600666 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600667{
668 //header for testing if it is a png
669 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700670 int is_png, bit_depth, color_type,rowbytes;
671 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600672 png_structp png_ptr;
673 png_infop info_ptr, end_info;
674 png_byte *image_data;
675 png_bytep *row_pointers;
676
677 //open file as binary
678 FILE *fp = fopen(filename, "rb");
679 if (!fp) {
680 return false;
681 }
682
683 //read the header
684 fread(header, 1, 8, fp);
685
686 //test if png
687 is_png = !png_sig_cmp(header, 0, 8);
688 if (!is_png) {
689 fclose(fp);
690 return false;
691 }
692
693 //create png struct
694 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
695 NULL, NULL);
696 if (!png_ptr) {
697 fclose(fp);
698 return (false);
699 }
700
701 //create png info struct
702 info_ptr = png_create_info_struct(png_ptr);
703 if (!info_ptr) {
704 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
705 fclose(fp);
706 return (false);
707 }
708
709 //create png info struct
710 end_info = png_create_info_struct(png_ptr);
711 if (!end_info) {
712 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
713 fclose(fp);
714 return (false);
715 }
716
717 //png error stuff, not sure libpng man suggests this.
718 if (setjmp(png_jmpbuf(png_ptr))) {
719 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
720 fclose(fp);
721 return (false);
722 }
723
724 //init png reading
725 png_init_io(png_ptr, fp);
726
727 //let libpng know you already read the first 8 bytes
728 png_set_sig_bytes(png_ptr, 8);
729
730 // read all the info up to the image data
731 png_read_info(png_ptr, info_ptr);
732
733 // get info about png
734 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
735 NULL, NULL, NULL);
736
737 //update width and height based on png info
738 *width = twidth;
739 *height = theight;
740
741 // Require that incoming texture be 8bits per color component
742 // and 4 components (RGBA).
743 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
744 png_get_channels(png_ptr, info_ptr) != 4) {
745 return false;
746 }
747
748 if (rgba_data == NULL) {
749 // If data pointer is null, we just want the width & height
750 // clean up memory and close stuff
751 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
752 fclose(fp);
753
754 return true;
755 }
756
757 // Update the png info struct.
758 png_read_update_info(png_ptr, info_ptr);
759
760 // Row size in bytes.
761 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
762
763 // Allocate the image_data as a big block, to be given to opengl
764 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
765 if (!image_data) {
766 //clean up memory and close stuff
767 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
768 fclose(fp);
769 return false;
770 }
771
772 // row_pointers is for pointing to image_data for reading the png with libpng
773 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
774 if (!row_pointers) {
775 //clean up memory and close stuff
776 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
777 // delete[] image_data;
778 fclose(fp);
779 return false;
780 }
781 // set the individual row_pointers to point at the correct offsets of image_data
782 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700783 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600784
785 // read the png into image_data through row_pointers
786 png_read_image(png_ptr, row_pointers);
787
788 // clean up memory and close stuff
789 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
790 free(row_pointers);
791 free(image_data);
792 fclose(fp);
793
794 return true;
795}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600796
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700797static void demo_prepare_texture_image(struct demo *demo,
798 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600799 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600800 VkImageTiling tiling,
801 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600802{
Tony Barbour72304ef2015-04-16 15:59:00 -0600803 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600804 int32_t tex_width;
805 int32_t tex_height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600806 VkResult err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700807
808 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
809 assert(err);
810
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600811 tex_obj->tex_width = tex_width;
812 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700813
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600814 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600815 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700816 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600817 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700818 .format = tex_format,
819 .extent = { tex_width, tex_height, 1 },
820 .mipLevels = 1,
821 .arraySize = 1,
822 .samples = 1,
823 .tiling = tiling,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600824 .usage = VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700825 .flags = 0,
826 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600827 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600828 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500829 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700830 .allocationSize = 0,
831 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600832 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700833 };
834
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600835 VkMemoryRequirements *mem_reqs;
836 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700837 uint32_t num_allocations = 0;
838 size_t num_alloc_size = sizeof(num_allocations);
839
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600840 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600841 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700842 assert(!err);
843
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600844 err = vkGetObjectInfo(tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600845 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700846 &num_alloc_size, &num_allocations);
847 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600848 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600849 tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600850 err = vkGetObjectInfo(tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600851 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700852 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600853 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600854 mem_alloc.memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700855 for (uint32_t j = 0; j < num_allocations; j ++) {
Piers Daniell735ee532015-02-23 16:23:13 -0700856 mem_alloc.allocationSize = mem_reqs[j].size;
857
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700858 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600859 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600860 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700861 assert(!err);
862
863 /* bind memory */
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500864 err = vkQueueBindObjectMemory(demo->queue, tex_obj->image, j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700865 assert(!err);
866 }
867 free(mem_reqs);
868 mem_reqs = NULL;
869
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600870 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700871
Tony Barbour72304ef2015-04-16 15:59:00 -0600872 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600873 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600874 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700875 .mipLevel = 0,
876 .arraySlice = 0,
877 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600878 VkSubresourceLayout layout;
879 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700880 void *data;
881
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600882 err = vkGetImageSubresourceInfo(tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600883 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700884 &layout_size, &layout);
885 assert(!err && layout_size == sizeof(layout));
886 /* Linear texture must be within a single memory object */
887 assert(num_allocations == 1);
888
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600889 err = vkMapMemory(tex_obj->mem[0], 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700890 assert(!err);
891
892 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
893 fprintf(stderr, "Error loading texture: %s\n", filename);
894 }
895
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600896 err = vkUnmapMemory(tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700897 assert(!err);
898 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600899
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600900 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600901 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600902 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600903 tex_obj->imageLayout);
904 /* 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 -0700905}
906
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500907static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700908{
909 /* clean up staging resources */
910 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500911 vkQueueBindObjectMemory(demo->queue, tex_objs->image, j, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600912 vkFreeMemory(tex_objs->mem[j]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700913 }
914
915 free(tex_objs->mem);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600916 vkDestroyObject(tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700917}
918
919static void demo_prepare_textures(struct demo *demo)
920{
Tony Barbour72304ef2015-04-16 15:59:00 -0600921 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600922 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700923 size_t size = sizeof(props);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600924 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600925 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600926
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600927 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -0600928 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700929 &size, &props);
930 assert(!err);
931
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600932 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700933
Tony Barbour72304ef2015-04-16 15:59:00 -0600934 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700935 /* Device can texture using linear textures */
936 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Tony Barbour72304ef2015-04-16 15:59:00 -0600937 VK_IMAGE_TILING_LINEAR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
938 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700939 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600940 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700941
942 memset(&staging_texture, 0, sizeof(staging_texture));
943 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Tony Barbour72304ef2015-04-16 15:59:00 -0600944 VK_IMAGE_TILING_LINEAR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700945
946 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Tony Barbour72304ef2015-04-16 15:59:00 -0600947 VK_IMAGE_TILING_OPTIMAL, VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700948
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600949 demo_set_image_layout(demo, staging_texture.image,
950 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600951 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700952
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600953 demo_set_image_layout(demo, demo->textures[i].image,
954 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600955 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700956
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600957 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600958 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700959 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600960 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700961 .destOffset = { 0, 0, 0 },
962 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
963 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600964 vkCmdCopyImage(demo->cmd,
965 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
966 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -0600967 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700968
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600969 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
970 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700971
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600972 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600973 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600974 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700975
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600976 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700977
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500978 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600979 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980 } else {
Tony Barbour72304ef2015-04-16 15:59:00 -0600981 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700982 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
983 }
984
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600985 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600986 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600987 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600988 .magFilter = VK_TEX_FILTER_NEAREST,
989 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -0600990 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600991 .addressU = VK_TEX_ADDRESS_CLAMP,
992 .addressV = VK_TEX_ADDRESS_CLAMP,
993 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600994 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700995 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600996 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600997 .minLod = 0.0f,
998 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -0600999 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001000 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001001
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001002 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001003 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001005 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001006 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001007 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001008 .channels = { VK_CHANNEL_SWIZZLE_R,
1009 VK_CHANNEL_SWIZZLE_G,
1010 VK_CHANNEL_SWIZZLE_B,
1011 VK_CHANNEL_SWIZZLE_A, },
1012 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001013 .minLod = 0.0f,
1014 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001015
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001016 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001017 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018 &demo->textures[i].sampler);
1019 assert(!err);
1020
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001021 /* create image view */
1022 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001023 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001024 &demo->textures[i].view);
1025 assert(!err);
1026 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001027}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001028
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001029void demo_prepare_cube_data_buffer(struct demo *demo)
1030{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001031 VkBufferCreateInfo buf_info;
1032 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001033 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001034 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001035 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001036 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001037 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001038 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001039 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001040 VkMemoryRequirements *mem_reqs;
1041 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001042 uint32_t num_allocations = 0;
1043 size_t num_alloc_size = sizeof(num_allocations);
1044 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001045 int i;
1046 mat4x4 MVP, VP;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001047 VkResult err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001048 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001049
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001050 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001051 mat4x4_mul(MVP, VP, demo->model_matrix);
1052 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001053// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001054
1055 for (i=0; i<12*3; i++) {
1056 data.position[i][0] = g_vertex_buffer_data[i*3];
1057 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1058 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1059 data.position[i][3] = 1.0f;
1060 data.attr[i][0] = g_uv_buffer_data[2*i];
1061 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1062 data.attr[i][2] = 0;
1063 data.attr[i][3] = 0;
1064 }
1065
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001066 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001067 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001068 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001069 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001070 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001071 assert(!err);
1072
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001073 err = vkGetObjectInfo(demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001074 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001075 &num_alloc_size, &num_allocations);
1076 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001077 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -06001078 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001079 demo->uniform_data.num_mem = num_allocations;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001080 err = vkGetObjectInfo(demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001081 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001082 &mem_reqs_size, mem_reqs);
1083 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001084 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001085 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001086
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001087 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001088 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001089
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001090 err = vkMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001091 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001092
Piers Daniell735ee532015-02-23 16:23:13 -07001093 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001094
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001095 err = vkUnmapMemory(demo->uniform_data.mem[i]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001096 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001097
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001098 err = vkQueueBindObjectMemory(demo->queue, demo->uniform_data.buf, i,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001099 demo->uniform_data.mem[i], 0);
1100 assert(!err);
1101 }
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001102 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001103
1104 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001105 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001106 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001107 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001108 view_info.offset = 0;
1109 view_info.range = sizeof(data);
1110
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001111 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001112 assert(!err);
1113
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001114 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001115 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001116}
1117
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001118static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001119{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001120 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001121 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001122 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001123 .count = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001124 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001125 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001126 },
1127 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001128 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001129 .count = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001130 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001131 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001132 },
1133 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001134 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001135 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001136 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001137 .count = 2,
1138 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001139 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001140 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001141
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001142 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001143 &descriptor_layout, &demo->desc_layout);
1144 assert(!err);
1145
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001146 err = vkCreateDescriptorSetLayoutChain(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001147 1, &demo->desc_layout, &demo->desc_layout_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001148 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001149}
1150
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001151static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001152 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001153 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001154 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001155{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001156 VkShaderCreateInfo createInfo;
1157 VkShader shader;
1158 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001159
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001160
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001161 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001162 createInfo.pNext = NULL;
1163
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001164#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001165 createInfo.codeSize = size;
1166 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001167 createInfo.flags = 0;
1168
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001169 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001170 if (err) {
1171 free((void *) createInfo.pCode);
1172 }
1173#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001174 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001175 // to the driver "under the covers"
1176 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1177 createInfo.pCode = malloc(createInfo.codeSize);
1178 createInfo.flags = 0;
1179
Tony Barbour72304ef2015-04-16 15:59:00 -06001180 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001181 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001182 ((uint32_t *) createInfo.pCode)[1] = 0;
1183 ((uint32_t *) createInfo.pCode)[2] = stage;
1184 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1185
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001186 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001187 if (err) {
1188 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001189 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001190 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001191#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001192
1193 return shader;
1194}
1195
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001196char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001197{
1198 long int size;
1199 void *shader_code;
1200
1201 FILE *fp = fopen(filename, "rb");
1202 if (!fp) return NULL;
1203
1204 fseek(fp, 0L, SEEK_END);
1205 size = ftell(fp);
1206
1207 fseek(fp, 0L, SEEK_SET);
1208
1209 shader_code = malloc(size);
1210 fread(shader_code, size, 1, fp);
1211
1212 *psize = size;
1213
1214 return shader_code;
1215}
1216
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001217static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001218{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001219#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001220 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001221 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001222
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001223 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001224
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001225 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001226 vertShaderCode, size);
1227#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001228 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001229 "#version 140\n"
1230 "#extension GL_ARB_separate_shader_objects : enable\n"
1231 "#extension GL_ARB_shading_language_420pack : enable\n"
1232 "\n"
1233 "layout(binding = 0) uniform buf {\n"
1234 " mat4 MVP;\n"
1235 " vec4 position[12*3];\n"
1236 " vec4 attr[12*3];\n"
1237 "} ubuf;\n"
1238 "\n"
1239 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001240 "\n"
1241 "void main() \n"
1242 "{\n"
1243 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001244 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1245 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001246
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001247 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001248 (const void *) vertShaderText,
1249 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001250#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001251}
1252
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001253static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001254{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001255#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001256 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001257 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001258
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001259 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001260
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001261 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001262 fragShaderCode, size);
1263#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001264 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001265 "#version 140\n"
1266 "#extension GL_ARB_separate_shader_objects : enable\n"
1267 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001268 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001269 "\n"
1270 "layout (location = 0) in vec4 texcoord;\n"
1271 "void main() {\n"
1272 " gl_FragColor = texture(tex, texcoord.xy);\n"
1273 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001274
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001275 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001276 (const void *) fragShaderText,
1277 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001278#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001279}
1280
1281static void demo_prepare_pipeline(struct demo *demo)
1282{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001283 VkGraphicsPipelineCreateInfo pipeline;
1284 VkPipelineIaStateCreateInfo ia;
1285 VkPipelineRsStateCreateInfo rs;
1286 VkPipelineCbStateCreateInfo cb;
1287 VkPipelineDsStateCreateInfo ds;
1288 VkPipelineShaderStageCreateInfo vs;
1289 VkPipelineShaderStageCreateInfo fs;
1290 VkPipelineVpStateCreateInfo vp;
1291 VkPipelineMsStateCreateInfo ms;
1292 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001293
1294 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001295 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001296 pipeline.pSetLayoutChain = demo->desc_layout_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001297
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001298 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001299 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001300 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001301
1302 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001303 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001304 rs.fillMode = VK_FILL_MODE_SOLID;
1305 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001306 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307
1308 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001309 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001310 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001311 memset(att_state, 0, sizeof(att_state));
1312 att_state[0].format = demo->format;
1313 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001314 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001315 cb.attachmentCount = 1;
1316 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317
Tony Barbour29645d02015-01-16 14:27:35 -07001318 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001319 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001320 vp.viewportCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001321 vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001322
1323 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001324 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001325 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001326 ds.depthTestEnable = VK_TRUE;
1327 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001328 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001329 ds.depthBoundsEnable = VK_FALSE;
1330 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1331 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001332 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001333 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001334 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001336 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001337 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1338 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001339 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001340 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001341
1342 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001343 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1344 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001345 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001346 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001347
1348 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001349 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001350 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001351 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001352 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001354 pipeline.pNext = (const void *) &ia;
1355 ia.pNext = (const void *) &rs;
1356 rs.pNext = (const void *) &cb;
1357 cb.pNext = (const void *) &ms;
1358 ms.pNext = (const void *) &vp;
1359 vp.pNext = (const void *) &ds;
1360 ds.pNext = (const void *) &vs;
1361 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001363 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001364 assert(!err);
1365
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001366 vkDestroyObject(vs.shader.shader);
1367 vkDestroyObject(fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001368}
1369
1370static void demo_prepare_dynamic_states(struct demo *demo)
1371{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001372 VkDynamicVpStateCreateInfo viewport_create;
1373 VkDynamicRsStateCreateInfo raster;
1374 VkDynamicCbStateCreateInfo color_blend;
1375 VkDynamicDsStateCreateInfo depth_stencil;
1376 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001377
Tony Barbour29645d02015-01-16 14:27:35 -07001378 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001379 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001380 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001381 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001382 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001383 viewport.height = (float) demo->height;
1384 viewport.width = (float) demo->width;
1385 viewport.minDepth = (float) 0.0f;
1386 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001387 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001388 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001389 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001390 scissor.extent.width = demo->width;
1391 scissor.extent.height = demo->height;
1392 scissor.offset.x = 0;
1393 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001394 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001395
1396 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001397 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001398 raster.pointSize = 1.0;
1399 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400
1401 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001402 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001403 color_blend.blendConst[0] = 1.0f;
1404 color_blend.blendConst[1] = 1.0f;
1405 color_blend.blendConst[2] = 1.0f;
1406 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407
1408 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001409 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001410 depth_stencil.minDepth = 0.0f;
1411 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001412 depth_stencil.stencilBackRef = 0;
1413 depth_stencil.stencilFrontRef = 0;
1414 depth_stencil.stencilReadMask = 0xff;
1415 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001417 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418 assert(!err);
1419
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001420 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421 assert(!err);
1422
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001423 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424 &color_blend, &demo->color_blend);
1425 assert(!err);
1426
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001427 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428 &depth_stencil, &demo->depth_stencil);
1429 assert(!err);
1430}
1431
Chia-I Wu63ea9262015-03-26 13:14:16 +08001432static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001433{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001434 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001435 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001436 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001437 .count = 1,
1438 },
1439 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001440 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001441 .count = DEMO_TEXTURE_COUNT,
1442 },
1443 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001444 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001445 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001446 .pNext = NULL,
1447 .count = 2,
1448 .pTypeCount = type_counts,
1449 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001450 VkResult err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001451
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001452 err = vkCreateDescriptorPool(demo->device,
1453 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001454 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001455 assert(!err);
1456}
1457
1458static void demo_prepare_descriptor_set(struct demo *demo)
1459{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001460 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1461 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1462 VkUpdateSamplerTextures update_fs;
1463 VkUpdateBuffers update_vs;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001464 const void *update_array[2] = { &update_vs, &update_fs };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001465 VkResult err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001466 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001467 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001468
1469 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001470 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001471 view_info[i].pNext = NULL;
1472 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001473 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001474
Courtney Goeltzenleuchtereb4754f2015-04-09 11:43:10 -06001475 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001476 combined_info[i].pImageView = &view_info[i];
1477 }
1478
1479 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001480 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001481 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001482 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001483 update_vs.count = 1;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001484 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001485
1486 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001487 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001488 update_fs.binding = 1;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001489 update_fs.count = DEMO_TEXTURE_COUNT;
1490 update_fs.pSamplerImageViews = combined_info;
1491
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001492 err = vkAllocDescriptorSets(demo->desc_pool,
1493 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001494 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001495 &demo->desc_set, &count);
1496 assert(!err && count == 1);
1497
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001498 vkBeginDescriptorPoolUpdate(demo->device,
1499 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001500
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001501 vkClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set);
1502 vkUpdateDescriptors(demo->desc_set, 2, update_array);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001503
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001504 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001505}
1506
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001507static void demo_prepare(struct demo *demo)
1508{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001509 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001510 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001511 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001512 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001513 .flags = 0,
1514 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001515 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001516
1517 demo_prepare_buffers(demo);
1518 demo_prepare_depth(demo);
1519 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001520 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001521
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001522 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001523 demo_prepare_pipeline(demo);
1524 demo_prepare_dynamic_states(demo);
1525
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001526 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001527 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001528 assert(!err);
1529 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001530
Chia-I Wu63ea9262015-03-26 13:14:16 +08001531 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001532 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001533
1534
1535 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1536 demo->current_buffer = i;
1537 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1538 }
1539
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001540 /*
1541 * Prepare functions above may generate pipeline commands
1542 * that need to be flushed before beginning the render loop.
1543 */
1544 demo_flush_init_cmd(demo);
1545
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001546 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001547}
1548
1549static void demo_handle_event(struct demo *demo,
1550 const xcb_generic_event_t *event)
1551{
Piers Daniell735ee532015-02-23 16:23:13 -07001552 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001553 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001554 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001555 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001556 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001557 case XCB_CLIENT_MESSAGE:
1558 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1559 (*demo->atom_wm_delete_window).atom) {
1560 demo->quit = true;
1561 }
1562 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001563 case XCB_KEY_RELEASE:
1564 {
1565 const xcb_key_release_event_t *key =
1566 (const xcb_key_release_event_t *) event;
1567
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001568 switch (key->detail) {
1569 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001571 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001572 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001573 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001574 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001575 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001576 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001577 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001578 case 0x41:
1579 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001580 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001581 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582 }
1583 break;
1584 default:
1585 break;
1586 }
1587}
1588
1589static void demo_run(struct demo *demo)
1590{
1591 xcb_flush(demo->connection);
1592
1593 while (!demo->quit) {
1594 xcb_generic_event_t *event;
1595
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001596 if (demo->pause) {
1597 event = xcb_wait_for_event(demo->connection);
1598 } else {
1599 event = xcb_poll_for_event(demo->connection);
1600 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001601 if (event) {
1602 demo_handle_event(demo, event);
1603 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001604 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001605
1606 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001607 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001608 demo_update_data_buffer(demo);
1609
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001610 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001611
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001612 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001613 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001614 }
1615}
1616
1617static void demo_create_window(struct demo *demo)
1618{
1619 uint32_t value_mask, value_list[32];
1620
1621 demo->window = xcb_generate_id(demo->connection);
1622
1623 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1624 value_list[0] = demo->screen->black_pixel;
1625 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1626 XCB_EVENT_MASK_EXPOSURE;
1627
1628 xcb_create_window(demo->connection,
1629 XCB_COPY_FROM_PARENT,
1630 demo->window, demo->screen->root,
1631 0, 0, demo->width, demo->height, 0,
1632 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1633 demo->screen->root_visual,
1634 value_mask, value_list);
1635
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001636 /* Magic code that will send notification when window is destroyed */
1637 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1638 "WM_PROTOCOLS");
1639 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1640
1641 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1642 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1643
1644 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1645 demo->window, (*reply).atom, 4, 32, 1,
1646 &(*demo->atom_wm_delete_window).atom);
1647 free(reply);
1648
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001649 xcb_map_window(demo->connection, demo->window);
1650}
1651
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001652static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001653{
Tobin Ehlis7537db92015-04-16 10:04:35 -06001654 // TODO : Should query count w/ GetGlobalExtensionInfo, then enable via CreateInstance
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001655 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001656 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001657 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001658 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001659 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001660 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001661 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001662 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001663 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001664 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001665 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001666 .pNext = NULL,
1667 .pAppInfo = &app,
1668 .pAllocCb = NULL,
1669 .extensionCount = 0,
1670 .ppEnabledExtensionNames = NULL,
1671 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001672 const VK_WSI_X11_CONNECTION_INFO connection = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001673 .pConnection = demo->connection,
1674 .root = demo->screen->root,
1675 .provider = 0,
1676 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001677 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001678 .queueNodeIndex = 0,
1679 .queueCount = 1,
1680 };
Tobin Ehlis7537db92015-04-16 10:04:35 -06001681
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001682 const char *ext_names[] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001683 "VK_WSI_X11",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001684 };
Tobin Ehlis7537db92015-04-16 10:04:35 -06001685
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001686 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001687 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001688 .pNext = NULL,
1689 .queueRecordCount = 1,
1690 .pRequestedQueues = &queue,
Tobin Ehlis7537db92015-04-16 10:04:35 -06001691 .extensionCount = 1, // TODO : Should query count w/ GetGlobalExtensionInfo
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001692 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001693 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001695 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001696 uint32_t gpu_count;
1697 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001698 size_t data_size;
1699 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001700
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001701 err = vkCreateInstance(&inst_info, &demo->inst);
1702 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Ian Elliott3979e282015-04-03 15:24:55 -06001703 printf("Cannot find a compatible Vulkan installable client driver "
1704 "(ICD).\nExiting ...\n");
1705 fflush(stdout);
1706 exit(1);
1707 } else {
1708 assert(!err);
1709 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001710
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001711 gpu_count = 1;
1712 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001713 assert(!err && gpu_count == 1);
1714
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001715 err = vkWsiX11AssociateConnection(demo->gpu, &connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001716 assert(!err);
1717
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001718 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719 assert(!err);
1720
Tony Barbour72304ef2015-04-16 15:59:00 -06001721 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001722 &data_size, NULL);
1723 assert(!err);
1724
Tony Barbour72304ef2015-04-16 15:59:00 -06001725 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1726 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001727 &data_size, demo->gpu_props);
1728 assert(!err);
1729
Tony Barbour72304ef2015-04-16 15:59:00 -06001730 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001731 &data_size, NULL);
1732 assert(!err);
1733
Tony Barbour72304ef2015-04-16 15:59:00 -06001734 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1735 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001736 &data_size, demo->queue_props);
1737 assert(!err);
Tony Barbour72304ef2015-04-16 15:59:00 -06001738 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001739 assert(queue_count >= 1);
1740
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001741 // Graphics queue and MemMgr queue can be separate.
1742 // TODO: Add support for separate queues, including synchronization,
1743 // and appropriate tracking for QueueSubmit and QueueBindObjectMemory
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001744 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001745 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
1746 (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) )
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001747 break;
1748 }
1749 assert(i < queue_count);
1750 demo->graphics_queue_node_index = i;
1751
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001752 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753 0, &demo->queue);
1754 assert(!err);
1755}
1756
1757static void demo_init_connection(struct demo *demo)
1758{
1759 const xcb_setup_t *setup;
1760 xcb_screen_iterator_t iter;
1761 int scr;
1762
1763 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001764 if (demo->connection == NULL) {
1765 printf("Cannot find a compatible Vulkan installable client driver "
1766 "(ICD).\nExiting ...\n");
1767 fflush(stdout);
1768 exit(1);
1769 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001770
1771 setup = xcb_get_setup(demo->connection);
1772 iter = xcb_setup_roots_iterator(setup);
1773 while (scr-- > 0)
1774 xcb_screen_next(&iter);
1775
1776 demo->screen = iter.data;
1777}
1778
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001779static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001780{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001781 vec3 eye = {0.0f, 3.0f, 5.0f};
1782 vec3 origin = {0, 0, 0};
1783 vec3 up = {0.0f, -1.0f, 0.0};
1784
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001785 memset(demo, 0, sizeof(*demo));
1786
Piers Daniell735ee532015-02-23 16:23:13 -07001787 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001788 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1789 demo->use_staging_buffer = true;
1790 }
1791
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001792 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001793 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001794
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001795 demo->width = 500;
1796 demo->height = 500;
Tony Barbour72304ef2015-04-16 15:59:00 -06001797 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001798
1799 demo->spin_angle = 0.01f;
1800 demo->spin_increment = 0.01f;
1801 demo->pause = false;
1802
Piers Daniell735ee532015-02-23 16:23:13 -07001803 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001804 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1805 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001806}
1807
1808static void demo_cleanup(struct demo *demo)
1809{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001810 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001811
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001812 vkDestroyObject(demo->desc_set);
1813 vkDestroyObject(demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001814
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001815 vkDestroyObject(demo->viewport);
1816 vkDestroyObject(demo->raster);
1817 vkDestroyObject(demo->color_blend);
1818 vkDestroyObject(demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001819
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001820 vkDestroyObject(demo->pipeline);
1821 vkDestroyObject(demo->desc_layout_chain);
1822 vkDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001823
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001824 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001825 vkDestroyObject(demo->textures[i].view);
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001826 vkQueueBindObjectMemory(demo->queue, demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001827 vkDestroyObject(demo->textures[i].image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001828 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001829 for (j = 0; j < demo->textures[i].num_mem; j++)
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001830 vkFreeMemory(demo->textures[i].mem[j]);
1831 vkDestroyObject(demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001832 }
1833
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001834 vkDestroyObject(demo->depth.view);
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001835 vkQueueBindObjectMemory(demo->queue, demo->depth.image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001836 vkDestroyObject(demo->depth.image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001837 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
1838 for (j = 0; j < demo->depth.num_mem; j++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001839 vkFreeMemory(demo->depth.mem[j]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001840 }
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001841
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001842 vkDestroyObject(demo->uniform_data.view);
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001843 vkQueueBindObjectMemory(demo->queue, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001844 vkDestroyObject(demo->uniform_data.buf);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001845 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001846 for (j = 0; j < demo->uniform_data.num_mem; j++)
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001847 vkFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001848
1849 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001850 vkDestroyObject(demo->buffers[i].fence);
1851 vkDestroyObject(demo->buffers[i].view);
1852 vkDestroyObject(demo->buffers[i].image);
1853 vkDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001854 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001855 }
1856
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001857 vkDestroyDevice(demo->device);
1858 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001859
1860 xcb_destroy_window(demo->connection, demo->window);
1861 xcb_disconnect(demo->connection);
1862}
1863
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001864int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001865{
1866 struct demo demo;
1867
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001868 demo_init(&demo, argc, argv);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001869
1870 demo_prepare(&demo);
1871 demo_create_window(&demo);
1872 demo_run(&demo);
1873
1874 demo_cleanup(&demo);
1875
1876 return 0;
1877}