blob: b4018668feb83a002bcbfdb1e63b6e9b6cec3bcb [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>
Chia-I Wucbb564e2015-04-16 22:02:10 +080011#include <vk_wsi_lunarg.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;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800203 xcb_window_t window;
204 xcb_intern_atom_reply_t *atom_wm_delete_window;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700205 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600206
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600207 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600208 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600209 VkDevice device;
210 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700211 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600212 VkPhysicalDeviceProperties *gpu_props;
213 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600214
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600215 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600216 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600217 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600218
Chia-I Wucbb564e2015-04-16 22:02:10 +0800219 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600220 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600221 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600222 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600223 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600224
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600225 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600226 } buffers[DEMO_BUFFER_COUNT];
227
228 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600229 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600230
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600231 VkImage image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600232 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600233 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600234 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600235 } depth;
236
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600237 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600238
239 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600240 VkBuffer buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600241 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600242 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600243 VkBufferView view;
244 VkBufferViewAttachInfo attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600245 } uniform_data;
246
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600247 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500248 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600249 VkDescriptorSetLayout desc_layout;
250 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600251
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600252 VkDynamicVpState viewport;
253 VkDynamicRsState raster;
254 VkDynamicCbState color_blend;
255 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600256
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600257 mat4x4 projection_matrix;
258 mat4x4 view_matrix;
259 mat4x4 model_matrix;
260
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600261 float spin_angle;
262 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600263 bool pause;
264
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600265 VkDescriptorPool desc_pool;
266 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800267
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600268 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600269 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600270};
271
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600272static void demo_flush_init_cmd(struct demo *demo)
273{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600274 VkResult err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600275
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600276 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600277 return;
278
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600279 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600280 assert(!err);
281
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600282 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600283
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600284 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600285 assert(!err);
286
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600287 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600288 assert(!err);
289
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600290 vkDestroyObject(demo->cmd);
291 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600292}
293
294static void demo_add_mem_refs(
295 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600296 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600297{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600298 vkQueueAddMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600299}
300
301static void demo_remove_mem_refs(
302 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600303 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600304{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600305 vkQueueRemoveMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600306}
307
308static void demo_set_image_layout(
309 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600310 VkImage image,
311 VkImageLayout old_image_layout,
312 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600313{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600314 VkResult err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600315
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600316 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600317 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600318 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600319 .pNext = NULL,
320 .queueNodeIndex = demo->graphics_queue_node_index,
321 .flags = 0,
322 };
323
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600324 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600325 assert(!err);
326
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600328 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600329 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600330 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600331 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600332 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600333 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600334 }
335
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600336 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600337 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600338 .pNext = NULL,
339 .outputMask = 0,
340 .inputMask = 0,
341 .oldLayout = old_image_layout,
342 .newLayout = new_image_layout,
343 .image = image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600344 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600345 };
346
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600347 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600348 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600349 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600350 }
351
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600352 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600353 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600354 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600355 }
356
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600357 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600358
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600359 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600360
Tony Barbour72304ef2015-04-16 15:59:00 -0600361 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600362}
363
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600364static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600365{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600367 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600368 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600370 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600372 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600374 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600375 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
376 .useRawValue = false,
377 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600378 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600379 VkImageSubresourceRange clear_range;
380 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600381 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600382 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600383 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600384 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700385 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600386 VkResult err;
387 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
388 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
389 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600390 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700391 .pNext = NULL,
392 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600393 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
394 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700395 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600396 .width = demo->width,
397 .height = demo->height,
398 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700399 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600400 VkRenderPassCreateInfo rp_info;
401 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700402
403 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600404 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700405 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600406 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700407 rp_info.renderArea.extent.width = demo->width;
408 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600409 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
410 rp_info.pColorFormats = &demo->format;
411 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700412 rp_info.pColorLoadOps = &load_op;
413 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600414 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600415 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600416 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600417 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600418 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600419 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
420 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600421 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600422 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
423 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700424 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600425
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600426 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600427 assert(!err);
428
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600429 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600430 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600431 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600432 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600433
Tony Barbour72304ef2015-04-16 15:59:00 -0600434 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
435 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
436 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600437 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600438 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600439 demo->depth_stencil);
440
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600441 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
442 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600443 clear_range.baseMipLevel = 0;
444 clear_range.mipLevels = 1;
445 clear_range.baseArraySlice = 0;
446 clear_range.arraySize = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600447 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600448 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600449 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600450 clear_color, 1, &clear_range);
451
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600452 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
453 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
454 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600455 clear_depth, 0, 1, &clear_range);
456
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600457 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
458 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600459
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600460 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600461 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700462
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600463 vkDestroyObject(rp_begin.renderPass);
464 vkDestroyObject(rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600465}
466
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600467
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600468void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600469{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600470 mat4x4 MVP, Model, VP;
471 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600472 uint8_t *pData;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600473 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600474
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600475 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600476
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600477 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600478 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700479 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600480 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600481
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700482 assert(demo->uniform_data.num_mem == 1);
Tony Barbour3213c752015-04-16 19:09:28 -0600483 err = vkMapMemory(demo->uniform_data.mem[0], 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600484 assert(!err);
485
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600486 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600487
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600488 err = vkUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600489 assert(!err);
490}
491
492static void demo_draw(struct demo *demo)
493{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800494 const VkPresentInfoWSI present = {
495 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
496 .pNext = NULL,
497 .image = demo->buffers[demo->current_buffer].image,
498 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600499 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600500 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600501
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600502 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
503 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600504 assert(!err);
505
Chia-I Wucbb564e2015-04-16 22:02:10 +0800506 err = vkQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600507 assert(!err);
508
509 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800510
511 err = vkQueueWaitIdle(demo->queue);
512 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600513}
514
515static void demo_prepare_buffers(struct demo *demo)
516{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800517 const VkSwapChainCreateInfoWSI swap_chain = {
518 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
519 .pNext = NULL,
520 .pNativeWindowSystemHandle = demo->connection,
521 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
522 .imageCount = DEMO_BUFFER_COUNT,
523 .imageFormat = demo->format,
524 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525 .width = demo->width,
526 .height = demo->height,
527 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800528 .imageArraySize = 1,
529 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600530 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800531 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
532 size_t images_size = sizeof(images);
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
Chia-I Wucbb564e2015-04-16 22:02:10 +0800536 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
537 assert(!err);
538
539 err = vkGetSwapChainInfoWSI(demo->swap_chain,
540 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
541 &images_size, images);
542 assert(!err && images_size == sizeof(images));
543
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600545 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600546 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600547 .pNext = NULL,
548 .format = demo->format,
549 .mipLevel = 0,
550 .baseArraySlice = 0,
551 .arraySize = 1,
552 };
553
Chia-I Wucbb564e2015-04-16 22:02:10 +0800554 demo->buffers[i].image = images[i].image;
555 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600556
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600557 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600558
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600559 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600560 VK_IMAGE_LAYOUT_UNDEFINED,
561 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600562
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600563 color_attachment_view.image = demo->buffers[i].image;
564
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600565 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566 &color_attachment_view, &demo->buffers[i].view);
567 assert(!err);
568 }
569}
570
571static void demo_prepare_depth(struct demo *demo)
572{
Tony Barbour72304ef2015-04-16 15:59:00 -0600573 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600574 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600575 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600576 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600577 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578 .format = depth_format,
579 .extent = { demo->width, demo->height, 1 },
580 .mipLevels = 1,
581 .arraySize = 1,
582 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600583 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600584 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600585 .flags = 0,
586 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600587 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600588 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500589 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600590 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600591 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600592 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600593 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600594 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600595 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600597 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600598 .mipLevel = 0,
599 .baseArraySlice = 0,
600 .arraySize = 1,
601 .flags = 0,
602 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600603 VkMemoryRequirements *mem_reqs;
604 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600605 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600606 uint32_t num_allocations = 0;
607 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600608
609 demo->depth.format = depth_format;
610
611 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600612 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600613 &demo->depth.image);
614 assert(!err);
615
Tony Barbour72304ef2015-04-16 15:59:00 -0600616 err = vkGetObjectInfo(demo->depth.image, VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700617 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600618 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600619 demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700620 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600621 err = vkGetObjectInfo(demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600622 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700623 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600624 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600625 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700626 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600627
Jon Ashburnb2a66652015-01-16 09:37:43 -0700628 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600629 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700630 &(demo->depth.mem[i]));
631 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600632
Jon Ashburnb2a66652015-01-16 09:37:43 -0700633 /* bind memory */
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500634 err = vkQueueBindObjectMemory(demo->queue, demo->depth.image, i,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700635 demo->depth.mem[i], 0);
636 assert(!err);
637 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600638
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600639 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600640 VK_IMAGE_LAYOUT_UNDEFINED,
641 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600642
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600643 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600644
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600645 /* create image view */
646 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600647 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600648 &demo->depth.view);
649 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600650}
651
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600652/** loadTexture
653 * loads a png file into an memory object, using cstdio , libpng.
654 *
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600655 * \param demo : Needed to access VK calls
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600656 * \param filename : the png file to be loaded
657 * \param width : width of png, to be updated as a side effect of this function
658 * \param height : height of png, to be updated as a side effect of this function
659 *
660 * \return bool : an opengl texture id. true if successful?,
661 * should be validated by the client of this function.
662 *
663 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
664 * Modified to copy image to memory
665 *
666 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700667bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600668 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600669 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600670{
671 //header for testing if it is a png
672 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700673 int is_png, bit_depth, color_type,rowbytes;
674 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600675 png_structp png_ptr;
676 png_infop info_ptr, end_info;
677 png_byte *image_data;
678 png_bytep *row_pointers;
679
680 //open file as binary
681 FILE *fp = fopen(filename, "rb");
682 if (!fp) {
683 return false;
684 }
685
686 //read the header
687 fread(header, 1, 8, fp);
688
689 //test if png
690 is_png = !png_sig_cmp(header, 0, 8);
691 if (!is_png) {
692 fclose(fp);
693 return false;
694 }
695
696 //create png struct
697 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
698 NULL, NULL);
699 if (!png_ptr) {
700 fclose(fp);
701 return (false);
702 }
703
704 //create png info struct
705 info_ptr = png_create_info_struct(png_ptr);
706 if (!info_ptr) {
707 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
708 fclose(fp);
709 return (false);
710 }
711
712 //create png info struct
713 end_info = png_create_info_struct(png_ptr);
714 if (!end_info) {
715 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
716 fclose(fp);
717 return (false);
718 }
719
720 //png error stuff, not sure libpng man suggests this.
721 if (setjmp(png_jmpbuf(png_ptr))) {
722 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
723 fclose(fp);
724 return (false);
725 }
726
727 //init png reading
728 png_init_io(png_ptr, fp);
729
730 //let libpng know you already read the first 8 bytes
731 png_set_sig_bytes(png_ptr, 8);
732
733 // read all the info up to the image data
734 png_read_info(png_ptr, info_ptr);
735
736 // get info about png
737 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
738 NULL, NULL, NULL);
739
740 //update width and height based on png info
741 *width = twidth;
742 *height = theight;
743
744 // Require that incoming texture be 8bits per color component
745 // and 4 components (RGBA).
746 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
747 png_get_channels(png_ptr, info_ptr) != 4) {
748 return false;
749 }
750
751 if (rgba_data == NULL) {
752 // If data pointer is null, we just want the width & height
753 // clean up memory and close stuff
754 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
755 fclose(fp);
756
757 return true;
758 }
759
760 // Update the png info struct.
761 png_read_update_info(png_ptr, info_ptr);
762
763 // Row size in bytes.
764 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
765
766 // Allocate the image_data as a big block, to be given to opengl
767 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
768 if (!image_data) {
769 //clean up memory and close stuff
770 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
771 fclose(fp);
772 return false;
773 }
774
775 // row_pointers is for pointing to image_data for reading the png with libpng
776 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
777 if (!row_pointers) {
778 //clean up memory and close stuff
779 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
780 // delete[] image_data;
781 fclose(fp);
782 return false;
783 }
784 // set the individual row_pointers to point at the correct offsets of image_data
785 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700786 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600787
788 // read the png into image_data through row_pointers
789 png_read_image(png_ptr, row_pointers);
790
791 // clean up memory and close stuff
792 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
793 free(row_pointers);
794 free(image_data);
795 fclose(fp);
796
797 return true;
798}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600799
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700800static void demo_prepare_texture_image(struct demo *demo,
801 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600802 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600803 VkImageTiling tiling,
804 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600805{
Tony Barbour72304ef2015-04-16 15:59:00 -0600806 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600807 int32_t tex_width;
808 int32_t tex_height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600809 VkResult err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700810
811 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
812 assert(err);
813
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600814 tex_obj->tex_width = tex_width;
815 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700816
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600817 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600818 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700819 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600820 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700821 .format = tex_format,
822 .extent = { tex_width, tex_height, 1 },
823 .mipLevels = 1,
824 .arraySize = 1,
825 .samples = 1,
826 .tiling = tiling,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600827 .usage = VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700828 .flags = 0,
829 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600830 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600831 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500832 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700833 .allocationSize = 0,
834 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600835 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700836 };
837
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600838 VkMemoryRequirements *mem_reqs;
839 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700840 uint32_t num_allocations = 0;
841 size_t num_alloc_size = sizeof(num_allocations);
842
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600843 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600844 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700845 assert(!err);
846
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600847 err = vkGetObjectInfo(tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600848 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700849 &num_alloc_size, &num_allocations);
850 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600851 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600852 tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600853 err = vkGetObjectInfo(tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600854 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700855 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600856 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600857 mem_alloc.memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700858 for (uint32_t j = 0; j < num_allocations; j ++) {
Piers Daniell735ee532015-02-23 16:23:13 -0700859 mem_alloc.allocationSize = mem_reqs[j].size;
860
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700861 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600862 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600863 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700864 assert(!err);
865
866 /* bind memory */
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500867 err = vkQueueBindObjectMemory(demo->queue, tex_obj->image, j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700868 assert(!err);
869 }
870 free(mem_reqs);
871 mem_reqs = NULL;
872
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600873 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700874
Tony Barbour72304ef2015-04-16 15:59:00 -0600875 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600876 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600877 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700878 .mipLevel = 0,
879 .arraySlice = 0,
880 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600881 VkSubresourceLayout layout;
882 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700883 void *data;
884
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600885 err = vkGetImageSubresourceInfo(tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600886 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700887 &layout_size, &layout);
888 assert(!err && layout_size == sizeof(layout));
889 /* Linear texture must be within a single memory object */
890 assert(num_allocations == 1);
891
Tony Barbour3213c752015-04-16 19:09:28 -0600892 err = vkMapMemory(tex_obj->mem[0], 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700893 assert(!err);
894
895 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
896 fprintf(stderr, "Error loading texture: %s\n", filename);
897 }
898
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600899 err = vkUnmapMemory(tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700900 assert(!err);
901 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600902
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600903 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600904 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600905 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600906 tex_obj->imageLayout);
907 /* 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 -0700908}
909
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500910static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700911{
912 /* clean up staging resources */
913 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500914 vkQueueBindObjectMemory(demo->queue, tex_objs->image, j, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600915 vkFreeMemory(tex_objs->mem[j]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700916 }
917
918 free(tex_objs->mem);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600919 vkDestroyObject(tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700920}
921
922static void demo_prepare_textures(struct demo *demo)
923{
Tony Barbour72304ef2015-04-16 15:59:00 -0600924 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600925 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700926 size_t size = sizeof(props);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600927 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600928 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600929
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600930 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -0600931 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700932 &size, &props);
933 assert(!err);
934
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600935 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700936
Tony Barbour72304ef2015-04-16 15:59:00 -0600937 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700938 /* Device can texture using linear textures */
939 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Tony Barbour72304ef2015-04-16 15:59:00 -0600940 VK_IMAGE_TILING_LINEAR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
941 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700942 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600943 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700944
945 memset(&staging_texture, 0, sizeof(staging_texture));
946 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Tony Barbour72304ef2015-04-16 15:59:00 -0600947 VK_IMAGE_TILING_LINEAR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700948
949 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Tony Barbour72304ef2015-04-16 15:59:00 -0600950 VK_IMAGE_TILING_OPTIMAL, VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700951
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600952 demo_set_image_layout(demo, staging_texture.image,
953 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600954 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700955
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600956 demo_set_image_layout(demo, demo->textures[i].image,
957 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600958 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700959
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600960 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600961 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700962 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600963 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700964 .destOffset = { 0, 0, 0 },
965 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
966 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600967 vkCmdCopyImage(demo->cmd,
968 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
969 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -0600970 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700971
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600972 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
973 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700974
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600975 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600976 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600977 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700978
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600979 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500981 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600982 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700983 } else {
Tony Barbour72304ef2015-04-16 15:59:00 -0600984 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
986 }
987
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600988 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600989 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600991 .magFilter = VK_TEX_FILTER_NEAREST,
992 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -0600993 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600994 .addressU = VK_TEX_ADDRESS_CLAMP,
995 .addressV = VK_TEX_ADDRESS_CLAMP,
996 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600997 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700998 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600999 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001000 .minLod = 0.0f,
1001 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001002 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001003 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001004
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001005 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001006 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001007 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001008 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001009 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001010 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001011 .channels = { VK_CHANNEL_SWIZZLE_R,
1012 VK_CHANNEL_SWIZZLE_G,
1013 VK_CHANNEL_SWIZZLE_B,
1014 VK_CHANNEL_SWIZZLE_A, },
1015 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001016 .minLod = 0.0f,
1017 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001018
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001019 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001020 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001021 &demo->textures[i].sampler);
1022 assert(!err);
1023
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001024 /* create image view */
1025 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001026 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001027 &demo->textures[i].view);
1028 assert(!err);
1029 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001030}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001031
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001032void demo_prepare_cube_data_buffer(struct demo *demo)
1033{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001034 VkBufferCreateInfo buf_info;
1035 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001036 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001037 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001038 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001039 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001040 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001041 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001042 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001043 VkMemoryRequirements *mem_reqs;
1044 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001045 uint32_t num_allocations = 0;
1046 size_t num_alloc_size = sizeof(num_allocations);
1047 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001048 int i;
1049 mat4x4 MVP, VP;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001050 VkResult err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001051 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001052
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001053 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001054 mat4x4_mul(MVP, VP, demo->model_matrix);
1055 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001056// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001057
1058 for (i=0; i<12*3; i++) {
1059 data.position[i][0] = g_vertex_buffer_data[i*3];
1060 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1061 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1062 data.position[i][3] = 1.0f;
1063 data.attr[i][0] = g_uv_buffer_data[2*i];
1064 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1065 data.attr[i][2] = 0;
1066 data.attr[i][3] = 0;
1067 }
1068
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001069 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001070 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001071 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001072 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001073 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001074 assert(!err);
1075
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001076 err = vkGetObjectInfo(demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001077 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001078 &num_alloc_size, &num_allocations);
1079 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001080 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -06001081 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001082 demo->uniform_data.num_mem = num_allocations;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001083 err = vkGetObjectInfo(demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001084 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001085 &mem_reqs_size, mem_reqs);
1086 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001087 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001088 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001089
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001090 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001091 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001092
Tony Barbour3213c752015-04-16 19:09:28 -06001093 err = vkMapMemory(demo->uniform_data.mem[i], 0, 0, 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001094 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001095
Piers Daniell735ee532015-02-23 16:23:13 -07001096 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001097
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001098 err = vkUnmapMemory(demo->uniform_data.mem[i]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001099 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001100
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001101 err = vkQueueBindObjectMemory(demo->queue, demo->uniform_data.buf, i,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001102 demo->uniform_data.mem[i], 0);
1103 assert(!err);
1104 }
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001105 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001106
1107 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001108 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001109 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001110 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001111 view_info.offset = 0;
1112 view_info.range = sizeof(data);
1113
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001114 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001115 assert(!err);
1116
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001117 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001118 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001119}
1120
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001121static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001122{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001123 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001124 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001125 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001126 .count = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001127 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001128 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001129 },
1130 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001131 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001132 .count = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001133 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001134 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001135 },
1136 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001137 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001138 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001139 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001140 .count = 2,
1141 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001142 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001143 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001144
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001145 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001146 &descriptor_layout, &demo->desc_layout);
1147 assert(!err);
1148
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001149 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1150 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1151 .pNext = NULL,
1152 .descriptorSetCount = 1,
1153 .pSetLayouts = &demo->desc_layout,
1154 };
1155
1156 err = vkCreatePipelineLayout(demo->device,
1157 &pPipelineLayoutCreateInfo,
1158 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001159 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001160}
1161
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001162static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001163 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001164 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001165 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001166{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001167 VkShaderCreateInfo createInfo;
1168 VkShader shader;
1169 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001170
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001171
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001172 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001173 createInfo.pNext = NULL;
1174
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001175#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001176 createInfo.codeSize = size;
1177 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001178 createInfo.flags = 0;
1179
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001180 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001181 if (err) {
1182 free((void *) createInfo.pCode);
1183 }
1184#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001185 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001186 // to the driver "under the covers"
1187 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1188 createInfo.pCode = malloc(createInfo.codeSize);
1189 createInfo.flags = 0;
1190
Tony Barbour72304ef2015-04-16 15:59:00 -06001191 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001192 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001193 ((uint32_t *) createInfo.pCode)[1] = 0;
1194 ((uint32_t *) createInfo.pCode)[2] = stage;
1195 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1196
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001197 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001198 if (err) {
1199 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001200 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001201 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001202#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203
1204 return shader;
1205}
1206
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001207char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001208{
1209 long int size;
1210 void *shader_code;
1211
1212 FILE *fp = fopen(filename, "rb");
1213 if (!fp) return NULL;
1214
1215 fseek(fp, 0L, SEEK_END);
1216 size = ftell(fp);
1217
1218 fseek(fp, 0L, SEEK_SET);
1219
1220 shader_code = malloc(size);
1221 fread(shader_code, size, 1, fp);
1222
1223 *psize = size;
1224
1225 return shader_code;
1226}
1227
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001228static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001229{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001230#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001231 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001232 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001233
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001234 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001235
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001236 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001237 vertShaderCode, size);
1238#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001239 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001240 "#version 140\n"
1241 "#extension GL_ARB_separate_shader_objects : enable\n"
1242 "#extension GL_ARB_shading_language_420pack : enable\n"
1243 "\n"
1244 "layout(binding = 0) uniform buf {\n"
1245 " mat4 MVP;\n"
1246 " vec4 position[12*3];\n"
1247 " vec4 attr[12*3];\n"
1248 "} ubuf;\n"
1249 "\n"
1250 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001251 "\n"
1252 "void main() \n"
1253 "{\n"
1254 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001255 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1256 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001257
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001258 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001259 (const void *) vertShaderText,
1260 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001261#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001262}
1263
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001264static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001265{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001266#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001267 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001268 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001269
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001270 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001271
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001272 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001273 fragShaderCode, size);
1274#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001275 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001276 "#version 140\n"
1277 "#extension GL_ARB_separate_shader_objects : enable\n"
1278 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001279 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001280 "\n"
1281 "layout (location = 0) in vec4 texcoord;\n"
1282 "void main() {\n"
1283 " gl_FragColor = texture(tex, texcoord.xy);\n"
1284 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001285
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001286 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001287 (const void *) fragShaderText,
1288 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001289#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001290}
1291
1292static void demo_prepare_pipeline(struct demo *demo)
1293{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001294 VkGraphicsPipelineCreateInfo pipeline;
1295 VkPipelineIaStateCreateInfo ia;
1296 VkPipelineRsStateCreateInfo rs;
1297 VkPipelineCbStateCreateInfo cb;
1298 VkPipelineDsStateCreateInfo ds;
1299 VkPipelineShaderStageCreateInfo vs;
1300 VkPipelineShaderStageCreateInfo fs;
1301 VkPipelineVpStateCreateInfo vp;
1302 VkPipelineMsStateCreateInfo ms;
1303 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001304
1305 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001306 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001307 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001308
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001309 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001310 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001311 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001312
1313 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001314 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001315 rs.fillMode = VK_FILL_MODE_SOLID;
1316 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001317 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001318
1319 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001320 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001321 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001322 memset(att_state, 0, sizeof(att_state));
1323 att_state[0].format = demo->format;
1324 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001325 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001326 cb.attachmentCount = 1;
1327 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001328
Tony Barbour29645d02015-01-16 14:27:35 -07001329 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001330 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001331 vp.viewportCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001332 vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001333
1334 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001335 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001336 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001337 ds.depthTestEnable = VK_TRUE;
1338 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001339 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001340 ds.depthBoundsEnable = VK_FALSE;
1341 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1342 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001343 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001344 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001345 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001347 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001348 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1349 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001350 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001351 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001352
1353 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001354 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1355 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001356 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001357 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001358
1359 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001360 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001361 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001362 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001363 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001364
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001365 pipeline.pNext = (const void *) &ia;
1366 ia.pNext = (const void *) &rs;
1367 rs.pNext = (const void *) &cb;
1368 cb.pNext = (const void *) &ms;
1369 ms.pNext = (const void *) &vp;
1370 vp.pNext = (const void *) &ds;
1371 ds.pNext = (const void *) &vs;
1372 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001373
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001374 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001375 assert(!err);
1376
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001377 vkDestroyObject(vs.shader.shader);
1378 vkDestroyObject(fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001379}
1380
1381static void demo_prepare_dynamic_states(struct demo *demo)
1382{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001383 VkDynamicVpStateCreateInfo viewport_create;
1384 VkDynamicRsStateCreateInfo raster;
1385 VkDynamicCbStateCreateInfo color_blend;
1386 VkDynamicDsStateCreateInfo depth_stencil;
1387 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388
Tony Barbour29645d02015-01-16 14:27:35 -07001389 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001390 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001391 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001392 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001393 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001394 viewport.height = (float) demo->height;
1395 viewport.width = (float) demo->width;
1396 viewport.minDepth = (float) 0.0f;
1397 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001398 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001399 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001400 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001401 scissor.extent.width = demo->width;
1402 scissor.extent.height = demo->height;
1403 scissor.offset.x = 0;
1404 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001405 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406
1407 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001408 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001409 raster.pointSize = 1.0;
1410 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001411
1412 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001413 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001414 color_blend.blendConst[0] = 1.0f;
1415 color_blend.blendConst[1] = 1.0f;
1416 color_blend.blendConst[2] = 1.0f;
1417 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418
1419 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001420 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001421 depth_stencil.minDepth = 0.0f;
1422 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001423 depth_stencil.stencilBackRef = 0;
1424 depth_stencil.stencilFrontRef = 0;
1425 depth_stencil.stencilReadMask = 0xff;
1426 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001428 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429 assert(!err);
1430
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001431 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432 assert(!err);
1433
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001434 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435 &color_blend, &demo->color_blend);
1436 assert(!err);
1437
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001438 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001439 &depth_stencil, &demo->depth_stencil);
1440 assert(!err);
1441}
1442
Chia-I Wu63ea9262015-03-26 13:14:16 +08001443static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001444{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001445 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001446 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001447 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001448 .count = 1,
1449 },
1450 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001451 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001452 .count = DEMO_TEXTURE_COUNT,
1453 },
1454 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001455 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001456 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001457 .pNext = NULL,
1458 .count = 2,
1459 .pTypeCount = type_counts,
1460 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001461 VkResult err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001462
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001463 err = vkCreateDescriptorPool(demo->device,
1464 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001465 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001466 assert(!err);
1467}
1468
1469static void demo_prepare_descriptor_set(struct demo *demo)
1470{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001471 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1472 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1473 VkUpdateSamplerTextures update_fs;
1474 VkUpdateBuffers update_vs;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001475 const void *update_array[2] = { &update_vs, &update_fs };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001476 VkResult err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001477 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001478 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001479
1480 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001481 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001482 view_info[i].pNext = NULL;
1483 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001484 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001485
Courtney Goeltzenleuchtereb4754f2015-04-09 11:43:10 -06001486 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001487 combined_info[i].pImageView = &view_info[i];
1488 }
1489
1490 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001491 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001492 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001493 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001494 update_vs.count = 1;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001495 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001496
1497 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001498 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001499 update_fs.binding = 1;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001500 update_fs.count = DEMO_TEXTURE_COUNT;
1501 update_fs.pSamplerImageViews = combined_info;
1502
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001503 err = vkAllocDescriptorSets(demo->desc_pool,
1504 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001505 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001506 &demo->desc_set, &count);
1507 assert(!err && count == 1);
1508
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001509 vkBeginDescriptorPoolUpdate(demo->device,
1510 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001511
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001512 vkClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set);
1513 vkUpdateDescriptors(demo->desc_set, 2, update_array);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001514
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001515 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001516}
1517
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001518static void demo_prepare(struct demo *demo)
1519{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001520 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001521 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001522 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001523 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001524 .flags = 0,
1525 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001526 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001527
1528 demo_prepare_buffers(demo);
1529 demo_prepare_depth(demo);
1530 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001531 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001533 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001534 demo_prepare_pipeline(demo);
1535 demo_prepare_dynamic_states(demo);
1536
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001537 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001538 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001539 assert(!err);
1540 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001541
Chia-I Wu63ea9262015-03-26 13:14:16 +08001542 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001543 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001544
1545
1546 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1547 demo->current_buffer = i;
1548 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1549 }
1550
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001551 /*
1552 * Prepare functions above may generate pipeline commands
1553 * that need to be flushed before beginning the render loop.
1554 */
1555 demo_flush_init_cmd(demo);
1556
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001557 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001558}
1559
1560static void demo_handle_event(struct demo *demo,
1561 const xcb_generic_event_t *event)
1562{
Piers Daniell735ee532015-02-23 16:23:13 -07001563 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001564 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001565 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001566 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001568 case XCB_CLIENT_MESSAGE:
1569 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1570 (*demo->atom_wm_delete_window).atom) {
1571 demo->quit = true;
1572 }
1573 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001574 case XCB_KEY_RELEASE:
1575 {
1576 const xcb_key_release_event_t *key =
1577 (const xcb_key_release_event_t *) event;
1578
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001579 switch (key->detail) {
1580 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001581 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001582 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001583 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001584 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001585 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001586 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001587 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001588 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001589 case 0x41:
1590 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001591 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001592 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001593 }
1594 break;
1595 default:
1596 break;
1597 }
1598}
1599
1600static void demo_run(struct demo *demo)
1601{
1602 xcb_flush(demo->connection);
1603
1604 while (!demo->quit) {
1605 xcb_generic_event_t *event;
1606
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001607 if (demo->pause) {
1608 event = xcb_wait_for_event(demo->connection);
1609 } else {
1610 event = xcb_poll_for_event(demo->connection);
1611 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001612 if (event) {
1613 demo_handle_event(demo, event);
1614 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001615 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001616
1617 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001618 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001619 demo_update_data_buffer(demo);
1620
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001621 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001622
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001623 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001624 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001625 }
1626}
1627
1628static void demo_create_window(struct demo *demo)
1629{
1630 uint32_t value_mask, value_list[32];
1631
1632 demo->window = xcb_generate_id(demo->connection);
1633
1634 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1635 value_list[0] = demo->screen->black_pixel;
1636 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1637 XCB_EVENT_MASK_EXPOSURE;
1638
1639 xcb_create_window(demo->connection,
1640 XCB_COPY_FROM_PARENT,
1641 demo->window, demo->screen->root,
1642 0, 0, demo->width, demo->height, 0,
1643 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1644 demo->screen->root_visual,
1645 value_mask, value_list);
1646
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001647 /* Magic code that will send notification when window is destroyed */
1648 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1649 "WM_PROTOCOLS");
1650 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1651
1652 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1653 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1654
1655 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1656 demo->window, (*reply).atom, 4, 32, 1,
1657 &(*demo->atom_wm_delete_window).atom);
1658 free(reply);
1659
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001660 xcb_map_window(demo->connection, demo->window);
1661}
1662
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001663static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001664{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001665 VkResult err;
1666 // Extensions to enable
1667 const char *ext_names[] = {
Chia-I Wucbb564e2015-04-16 22:02:10 +08001668 "VK_WSI_LunarG",
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001669 };
1670 size_t extSize = sizeof(uint32_t);
1671 uint32_t extCount = 0;
1672 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1673 assert(!err);
1674
1675 VkExtensionProperties extProp;
1676 extSize = sizeof(VkExtensionProperties);
1677 bool32_t extFound = 0;
1678 for (uint32_t i = 0; i < extCount; i++) {
1679 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1680 if (!strcmp(ext_names[0], extProp.extName))
1681 extFound = 1;
1682 }
1683 assert(extFound);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001684 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001685 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001686 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001687 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001688 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001689 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001690 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001691 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001692 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001693 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001694 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001695 .pNext = NULL,
1696 .pAppInfo = &app,
1697 .pAllocCb = NULL,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001698 .extensionCount = 1,
1699 .ppEnabledExtensionNames = ext_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06001700 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001701 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001702 .queueNodeIndex = 0,
1703 .queueCount = 1,
1704 };
Tobin Ehlis7537db92015-04-16 10:04:35 -06001705
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001706 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001707 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001708 .pNext = NULL,
1709 .queueRecordCount = 1,
1710 .pRequestedQueues = &queue,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001711 .extensionCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001712 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001713 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001714 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001715 uint32_t gpu_count;
1716 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001717 size_t data_size;
1718 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001720 err = vkCreateInstance(&inst_info, &demo->inst);
1721 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Ian Elliott3979e282015-04-03 15:24:55 -06001722 printf("Cannot find a compatible Vulkan installable client driver "
1723 "(ICD).\nExiting ...\n");
1724 fflush(stdout);
1725 exit(1);
1726 } else {
1727 assert(!err);
1728 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001729
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001730 gpu_count = 1;
1731 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001732 assert(!err && gpu_count == 1);
1733
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001734 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001735 assert(!err);
1736
Tony Barbour72304ef2015-04-16 15:59:00 -06001737 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001738 &data_size, NULL);
1739 assert(!err);
1740
Tony Barbour72304ef2015-04-16 15:59:00 -06001741 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1742 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001743 &data_size, demo->gpu_props);
1744 assert(!err);
1745
Tony Barbour72304ef2015-04-16 15:59:00 -06001746 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001747 &data_size, NULL);
1748 assert(!err);
1749
Tony Barbour72304ef2015-04-16 15:59:00 -06001750 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1751 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001752 &data_size, demo->queue_props);
1753 assert(!err);
Tony Barbour72304ef2015-04-16 15:59:00 -06001754 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001755 assert(queue_count >= 1);
1756
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001757 // Graphics queue and MemMgr queue can be separate.
1758 // TODO: Add support for separate queues, including synchronization,
1759 // and appropriate tracking for QueueSubmit and QueueBindObjectMemory
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001760 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001761 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
1762 (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) )
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001763 break;
1764 }
1765 assert(i < queue_count);
1766 demo->graphics_queue_node_index = i;
1767
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001768 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001769 0, &demo->queue);
1770 assert(!err);
1771}
1772
1773static void demo_init_connection(struct demo *demo)
1774{
1775 const xcb_setup_t *setup;
1776 xcb_screen_iterator_t iter;
1777 int scr;
1778
1779 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001780 if (demo->connection == NULL) {
1781 printf("Cannot find a compatible Vulkan installable client driver "
1782 "(ICD).\nExiting ...\n");
1783 fflush(stdout);
1784 exit(1);
1785 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001786
1787 setup = xcb_get_setup(demo->connection);
1788 iter = xcb_setup_roots_iterator(setup);
1789 while (scr-- > 0)
1790 xcb_screen_next(&iter);
1791
1792 demo->screen = iter.data;
1793}
1794
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001795static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001796{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001797 vec3 eye = {0.0f, 3.0f, 5.0f};
1798 vec3 origin = {0, 0, 0};
1799 vec3 up = {0.0f, -1.0f, 0.0};
1800
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001801 memset(demo, 0, sizeof(*demo));
1802
Piers Daniell735ee532015-02-23 16:23:13 -07001803 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001804 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1805 demo->use_staging_buffer = true;
1806 }
1807
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001808 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001809 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001810
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001811 demo->width = 500;
1812 demo->height = 500;
Tony Barbour72304ef2015-04-16 15:59:00 -06001813 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001814
1815 demo->spin_angle = 0.01f;
1816 demo->spin_increment = 0.01f;
1817 demo->pause = false;
1818
Piers Daniell735ee532015-02-23 16:23:13 -07001819 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001820 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1821 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001822}
1823
1824static void demo_cleanup(struct demo *demo)
1825{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001826 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001827
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001828 vkDestroyObject(demo->desc_set);
1829 vkDestroyObject(demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001830
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001831 vkDestroyObject(demo->viewport);
1832 vkDestroyObject(demo->raster);
1833 vkDestroyObject(demo->color_blend);
1834 vkDestroyObject(demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001835
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001836 vkDestroyObject(demo->pipeline);
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001837 vkDestroyObject(demo->pipeline_layout);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001838 vkDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001839
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001840 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001841 vkDestroyObject(demo->textures[i].view);
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001842 vkQueueBindObjectMemory(demo->queue, demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001843 vkDestroyObject(demo->textures[i].image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001844 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001845 for (j = 0; j < demo->textures[i].num_mem; j++)
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001846 vkFreeMemory(demo->textures[i].mem[j]);
1847 vkDestroyObject(demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001848 }
Chia-I Wucbb564e2015-04-16 22:02:10 +08001849 vkDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001850
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001851 vkDestroyObject(demo->depth.view);
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001852 vkQueueBindObjectMemory(demo->queue, demo->depth.image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001853 vkDestroyObject(demo->depth.image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001854 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
1855 for (j = 0; j < demo->depth.num_mem; j++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001856 vkFreeMemory(demo->depth.mem[j]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001857 }
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001858
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001859 vkDestroyObject(demo->uniform_data.view);
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001860 vkQueueBindObjectMemory(demo->queue, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001861 vkDestroyObject(demo->uniform_data.buf);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001862 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001863 for (j = 0; j < demo->uniform_data.num_mem; j++)
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001864 vkFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001865
1866 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001867 vkDestroyObject(demo->buffers[i].view);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001868 vkDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001869 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001870 }
1871
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001872 vkDestroyDevice(demo->device);
1873 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001874
1875 xcb_destroy_window(demo->connection, demo->window);
1876 xcb_disconnect(demo->connection);
1877}
1878
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001879int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001880{
1881 struct demo demo;
1882
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001883 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001884 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001885
1886 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001887 demo_run(&demo);
1888
1889 demo_cleanup(&demo);
1890
1891 return 0;
1892}