blob: 277e53af84d30e9cc2d179cf15956e5d74720a60 [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
Ian Elliotta748eaf2015-04-28 15:50:36 -06002 *
Courtney Goeltzenleuchterbb387b72015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Ian Elliotta748eaf2015-04-28 15:50:36 -06004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060022 *
23 * Author: Chia-I Wu <olv@lunarg.com>
24 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
25 * Author: Ian Elliott <ian@LunarG.com>
26 * Author: Jon Ashburn <jon@lunarg.com>
Ian Elliotta748eaf2015-04-28 15:50:36 -060027 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdbool.h>
33#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070034#include <signal.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060035
Ian Elliott639ca472015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060038#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060039#endif // _WIN32
40
David Pinedoc5193c12015-11-06 12:54:48 -070041#include <vulkan/vulkan.h>
Ian Elliottf4a4e442015-11-17 17:29:40 -070042
Ian Elliottf4a4e442015-11-17 17:29:40 -070043#include "vulkan/vk_lunarg_debug_report.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060044
Ian Elliottb08e0d92015-11-20 11:55:46 -070045
David Pinedo541526f2015-11-24 09:00:24 -070046#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047#include "linmath.h"
48
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060049#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbourfdc2d352015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliott07264132015-04-28 11:35:02 -060061#ifdef _WIN32
62#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
67
Ian Elliott07264132015-04-28 11:35:02 -060068#else // _WIN32
69
70#define ERR_EXIT(err_msg, err_class) \
71 do { \
72 printf(err_msg); \
73 fflush(stdout); \
74 exit(1); \
75 } while (0)
76#endif // _WIN32
77
Ian Elliottaaae5352015-07-06 14:27:58 -060078#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
79{ \
80 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
81 if (demo->fp##entrypoint == NULL) { \
82 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
83 "vkGetInstanceProcAddr Failure"); \
84 } \
85}
86
Jon Ashburn7d8342c2015-10-08 15:58:23 -060087static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
88
Ian Elliott673898b2015-06-22 15:07:49 -060089#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
90{ \
Jon Ashburn7d8342c2015-10-08 15:58:23 -060091 if(!g_gdpa) \
92 g_gdpa = (PFN_vkGetDeviceProcAddr) vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
93 demo->fp##entrypoint = (PFN_vk##entrypoint) g_gdpa(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060094 if (demo->fp##entrypoint == NULL) { \
95 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
96 "vkGetDeviceProcAddr Failure"); \
97 } \
98}
99
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600100/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700101 * structure to track all objects related to a texture.
102 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600103struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600104 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700105
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600106 VkImage image;
107 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600108
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800109 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500110 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600111 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700112 int32_t tex_width, tex_height;
113};
114
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600115static char *tex_files[] = {
Tony Barbour1a86d032015-09-21 15:17:33 -0600116 "lunarg.ppm"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600117};
118
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600119struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600120 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600121 float mvp[4][4];
122 float position[12*3][4];
123 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600124};
125
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600126struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600128 float mvp[4][4];
129 float position[12*3][4];
130 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600131};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600132
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600133//--------------------------------------------------------------------------------------
134// Mesh and VertexFormat Data
135//--------------------------------------------------------------------------------------
136struct Vertex
137{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600138 float posX, posY, posZ, posW; // Position data
139 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600140};
141
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600142struct VertexPosTex
143{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600144 float posX, posY, posZ, posW; // Position data
145 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146};
147
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600148#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600149#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600150
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600151static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800152 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600153 -1.0f,-1.0f, 1.0f,
154 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800155 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 -1.0f, 1.0f,-1.0f,
157 -1.0f,-1.0f,-1.0f,
158
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800159 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 1.0f, 1.0f,-1.0f,
161 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600164 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800166 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600168 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600171 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800173 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 -1.0f, 1.0f, 1.0f,
175 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600178 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800180 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181 1.0f, 1.0f, 1.0f,
182 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800183 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 1.0f,-1.0f,-1.0f,
185 1.0f, 1.0f,-1.0f,
186
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600189 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800190 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600191 1.0f,-1.0f, 1.0f,
192 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600193};
194
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600195static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800196 0.0f, 0.0f, // -X side
197 1.0f, 0.0f,
198 1.0f, 1.0f,
199 1.0f, 1.0f,
200 0.0f, 1.0f,
201 0.0f, 0.0f,
202
203 1.0f, 0.0f, // -Z side
204 0.0f, 1.0f,
205 0.0f, 0.0f,
206 1.0f, 0.0f,
207 1.0f, 1.0f,
208 0.0f, 1.0f,
209
210 1.0f, 1.0f, // -Y side
211 1.0f, 0.0f,
212 0.0f, 0.0f,
213 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214 0.0f, 0.0f,
215 0.0f, 1.0f,
216
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800217 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600218 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800219 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600220 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600222 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800224 1.0f, 1.0f, // +X side
225 0.0f, 1.0f,
226 0.0f, 0.0f,
227 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600229 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600230
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800231 0.0f, 1.0f, // +Z side
232 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600234 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600237};
238
239void dumpMatrix(const char *note, mat4x4 MVP)
240{
241 int i;
242
243 printf("%s: \n", note);
244 for (i=0; i<4; i++) {
245 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
246 }
247 printf("\n");
248 fflush(stdout);
249}
250
251void dumpVec4(const char *note, vec4 vector)
252{
253 printf("%s: \n", note);
254 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
255 printf("\n");
256 fflush(stdout);
257}
258
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600259VkBool32 dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600260 VkFlags msgFlags,
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700261 VkDebugReportObjectTypeLUNARG objType,
Tony Barbour155cce82015-07-03 10:33:54 -0600262 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600263 size_t location,
264 int32_t msgCode,
265 const char* pLayerPrefix,
266 const char* pMsg,
Courtney Goeltzenleuchter04ae89b2015-11-30 11:52:06 -0700267 const void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600268{
269 char *message = (char *) malloc(strlen(pMsg)+100);
270
271 assert (message);
272
Courtney Goeltzenleuchter15735822015-11-25 10:30:56 -0700273 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600274 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Courtney Goeltzenleuchter15735822015-11-25 10:30:56 -0700275 } else if (msgFlags & VK_DEBUG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600276 // We know that we're submitting queues without fences, ignore this warning
277 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600278 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600279 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600280 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600281 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600282 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600283 }
284
285#ifdef _WIN32
286 MessageBox(NULL, message, "Alert", MB_OK);
287#else
288 printf("%s\n",message);
289 fflush(stdout);
290#endif
291 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600292
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600293 /*
294 * false indicates that layer should not bail-out of an
295 * API call that had validation failures. This may mean that the
296 * app dies inside the driver due to invalid parameter(s).
297 * That's what would happen without validation layers, so we'll
298 * keep that behavior here.
299 */
300 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600301}
302
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700303VkBool32 BreakCallback(
304 VkFlags msgFlags,
305 VkDebugReportObjectTypeLUNARG objType,
306 uint64_t srcObject,
307 size_t location,
308 int32_t msgCode,
309 const char* pLayerPrefix,
310 const char* pMsg,
311 const void* pUserData)
312{
313#ifndef WIN32
314 raise(SIGTRAP);
315#else
316 DebugBreak();
317#endif
318
319 return false;
320}
321
Ian Elliotta0781972015-08-21 15:09:33 -0600322typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600323 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800324 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600325 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600326} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600327
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600328struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600329#ifdef _WIN32
330#define APP_NAME_STR_LEN 80
331 HINSTANCE connection; // hInstance - Windows Instance
332 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
333 HWND window; // hWnd - window handle
334#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335 xcb_connection_t *connection;
336 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800337 xcb_window_t window;
338 xcb_intern_atom_reply_t *atom_wm_delete_window;
Tony Barbour6839bec2015-07-13 16:37:21 -0600339#endif // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -0700340 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600341 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700342 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600343
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600344 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600345 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600346 VkDevice device;
347 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700348 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600349 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600350 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600351 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600352
353 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600354 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600355 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600356
Ian Elliotta0781972015-08-21 15:09:33 -0600357 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
Ian Elliottf4a4e442015-11-17 17:29:40 -0700358 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
359 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
360 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600361 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
362 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
363 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
364 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
365 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600366 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600367 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600368 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800370 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371
372 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600373 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600375 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800376 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500377 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600378 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379 } depth;
380
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600381 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600382
383 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600384 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800385 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500386 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600387 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600388 } uniform_data;
389
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800390 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500391 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600392 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600393 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800394 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600395 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600396
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600397 mat4x4 projection_matrix;
398 mat4x4 view_matrix;
399 mat4x4 model_matrix;
400
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600401 float spin_angle;
402 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600403 bool pause;
404
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600405 VkShaderModule vert_shader_module;
406 VkShaderModule frag_shader_module;
407
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600408 VkDescriptorPool desc_pool;
409 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800410
Tony Barbourd8e504f2015-10-08 14:26:24 -0600411 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800412
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600413 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600414 int32_t curFrame;
415 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600416 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600417 bool use_break;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -0700418 PFN_vkCreateDebugReportCallbackLUNARG CreateDebugReportCallback;
419 PFN_vkDestroyDebugReportCallbackLUNARG DestroyDebugReportCallback;
Courtney Goeltzenleuchterdd0e6172015-11-25 14:07:05 -0700420 VkDebugReportCallbackLUNARG msg_callback;
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700421 PFN_vkDebugReportMessageLUNARG DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600422
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600423 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600424 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600425};
426
Ian Elliottcf074d32015-10-16 18:02:43 -0600427// Forward declaration:
428static void demo_resize(struct demo *demo);
429
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600430static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600431{
432 // Search memtypes to find first index with those properties
433 for (uint32_t i = 0; i < 32; i++) {
434 if ((typeBits & 1) == 1) {
435 // Type is available, does it match user properties?
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600436 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600437 *typeIndex = i;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600438 return true;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600439 }
440 }
441 typeBits >>= 1;
442 }
443 // No memory types matched, return failure
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600444 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600445}
446
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600447static void demo_flush_init_cmd(struct demo *demo)
448{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600449 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600450
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600451 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600452 return;
453
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600454 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600455 assert(!err);
456
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800457 const VkCommandBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800458 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600459 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800460 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
461 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800462 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600463 .pWaitSemaphores = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800464 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600465 .pCommandBuffers = cmd_bufs,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800466 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600467 .pSignalSemaphores = NULL
468 };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600469
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600470 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471 assert(!err);
472
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600473 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600474 assert(!err);
475
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600476 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600477 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600478}
479
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600480static void demo_set_image_layout(
481 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600482 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600483 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600484 VkImageLayout old_image_layout,
485 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600486{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600487 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800490 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800491 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800493 .commandPool = demo->cmd_pool,
494 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800495 .bufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496 };
497
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800498 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600499 assert(!err);
500
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800501 VkCommandBufferBeginInfo cmd_buf_info = {
502 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600503 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600504 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800505 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600506 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800507 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800508 .occlusionQueryEnable = VK_FALSE,
509 .queryFlags = 0,
510 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600512 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600513 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600514 }
515
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600516 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600517 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600518 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800519 .srcAccessMask = 0,
520 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600521 .oldLayout = old_image_layout,
522 .newLayout = new_image_layout,
523 .image = image,
Jeremy Hayes105ca502015-11-17 18:19:55 -0700524 .subresourceRange = { aspectMask, 0, 1, 0, 1 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600525 };
526
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800527 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600528 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800529 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530 }
531
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700532 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Mark Lobodzinskid51e5a92015-12-03 15:42:32 -0700533 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700534 }
535
536 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Mark Lobodzinskid51e5a92015-12-03 15:42:32 -0700537 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700538 }
539
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600540 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600541 /* Make sure any Copy or CPU writes to image are flushed */
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700542 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600543 }
544
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600545 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600546
Tony Barbour50bbca42015-06-29 16:20:35 -0600547 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
548 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549
Chia-I Wu86365072015-10-26 17:08:33 +0800550 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600551}
552
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800553static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600554{
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800555 const VkCommandBufferBeginInfo cmd_buf_info = {
556 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600557 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600558 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800559 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600560 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800561 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800562 .occlusionQueryEnable = VK_FALSE,
563 .queryFlags = 0,
564 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700565 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800566 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600567 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
568 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800569 };
570 const VkRenderPassBeginInfo rp_begin = {
571 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
572 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800573 .renderPass = demo->render_pass,
574 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800575 .renderArea.offset.x = 0,
576 .renderArea.offset.y = 0,
577 .renderArea.extent.width = demo->width,
578 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600579 .clearValueCount = 2,
580 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700581 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800582 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600583
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600584 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600585 assert(!err);
586
Chia-I Wuf051d262015-10-27 19:25:11 +0800587 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800588
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600589 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600590 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600591 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600592 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600593
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600594 VkViewport viewport;
595 memset(&viewport, 0, sizeof(viewport));
596 viewport.height = (float) demo->height;
597 viewport.width = (float) demo->width;
598 viewport.minDepth = (float) 0.0f;
599 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600600 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600601
602 VkRect2D scissor;
603 memset(&scissor, 0, sizeof(scissor));
604 scissor.extent.width = demo->width;
605 scissor.extent.height = demo->height;
606 scissor.offset.x = 0;
607 scissor.offset.y = 0;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600608 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600609
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600610 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800611 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600612
Tony Barbour15a4ef62015-10-21 10:14:48 -0600613 VkImageMemoryBarrier prePresentBarrier = {
614 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
615 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800616 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
617 .dstAccessMask = 0,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600618 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700619 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600620 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800621 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600622 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
623 };
624
625 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
626 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Chia-I Wu082a6202015-10-31 00:31:16 +0800627 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Chia-I Wu86365072015-10-26 17:08:33 +0800628 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600629
630
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600631 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600632 assert(!err);
633}
634
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600635
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600636void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600637{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600638 mat4x4 MVP, Model, VP;
639 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600640 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600641 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600642
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600643 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600644
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600645 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600646 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700647 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600648 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600649
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200650 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600651 assert(!err);
652
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600653 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600654
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600655 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600656}
657
658static void demo_draw(struct demo *demo)
659{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600660 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600661 VkSemaphore presentCompleteSemaphore;
662 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
663 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
664 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600665 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600666 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800667 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600668
Ian Elliottaaae5352015-07-06 14:27:58 -0600669 err = vkCreateSemaphore(demo->device,
670 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +0800671 NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600672 &presentCompleteSemaphore);
673 assert(!err);
674
675 // Get the index of the next available swapchain image:
Ian Elliottcf074d32015-10-16 18:02:43 -0600676 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600677 UINT64_MAX,
678 presentCompleteSemaphore,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700679 NULL,// TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600680 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600681 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
682 // demo->swapchain is out of date (e.g. the window was resized) and
683 // must be recreated:
684 demo_resize(demo);
685 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800686 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600687 return;
688 } else if (err == VK_SUBOPTIMAL_KHR) {
689 // demo->swapchain is not as optimal as it could be, but the platform's
690 // presentation engine will still present the image correctly.
691 } else {
692 assert(!err);
693 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600694
Tony Barbour15a4ef62015-10-21 10:14:48 -0600695 // Assume the command buffer has been run on current_buffer before so
696 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
697 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
698 VK_IMAGE_ASPECT_COLOR_BIT,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700699 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600700 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
701 demo_flush_init_cmd(demo);
702
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600703 // Wait for the present complete semaphore to be signaled to ensure
704 // that the image won't be rendered to until the presentation
705 // engine has fully released ownership to the application, and it is
706 // okay to render to the image.
707
Ian Elliottf4a4e442015-11-17 17:29:40 -0700708// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600709 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800710 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
711 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800712 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600713 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800714 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600715 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800716 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600717 .pSignalSemaphores = NULL
718 };
719
720 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600721 assert(!err);
722
Ian Elliotta0781972015-08-21 15:09:33 -0600723 VkPresentInfoKHR present = {
724 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600725 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600726 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700727 .pSwapchains = &demo->swapchain,
728 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600729 };
730
731// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600732 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600733 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
734 // demo->swapchain is out of date (e.g. the window was resized) and
735 // must be recreated:
736 demo_resize(demo);
737 } else if (err == VK_SUBOPTIMAL_KHR) {
738 // demo->swapchain is not as optimal as it could be, but the platform's
739 // presentation engine will still present the image correctly.
740 } else {
741 assert(!err);
742 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743
Chia-I Wucbb564e2015-04-16 22:02:10 +0800744 err = vkQueueWaitIdle(demo->queue);
745 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600746
Chia-I Wu63636062015-10-26 21:10:41 +0800747 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600748}
749
750static void demo_prepare_buffers(struct demo *demo)
751{
Ian Elliottaaae5352015-07-06 14:27:58 -0600752 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600753 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600754
Ian Elliottb08e0d92015-11-20 11:55:46 -0700755 // Check the surface capabilities and formats
756 VkSurfaceCapabilitiesKHR surfCapabilities;
757 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu,
758 demo->surface,
759 &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600760 assert(!err);
761
Ian Elliott8528eff2015-08-07 15:56:59 -0600762 uint32_t presentModeCount;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700763 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu,
764 demo->surface,
Ian Elliott8528eff2015-08-07 15:56:59 -0600765 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600766 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600767 VkPresentModeKHR *presentModes =
768 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600769 assert(presentModes);
Ian Elliottb08e0d92015-11-20 11:55:46 -0700770 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu,
771 demo->surface,
Ian Elliott8528eff2015-08-07 15:56:59 -0600772 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600773 assert(!err);
774
Ian Elliotta0781972015-08-21 15:09:33 -0600775 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600776 // width and height are either both -1, or both not -1.
Ian Elliottb08e0d92015-11-20 11:55:46 -0700777 if (surfCapabilities.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600778 {
779 // If the surface size is undefined, the size is set to
780 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600781 swapchainExtent.width = demo->width;
782 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600783 }
784 else
785 {
786 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700787 swapchainExtent = surfCapabilities.currentExtent;
788 demo->width = surfCapabilities.currentExtent.width;
789 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600790 }
791
792 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600793 // tearing mode. If not, try IMMEDIATE which will usually be available,
794 // and is fastest (though it tears). If not, fall back to FIFO which is
795 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600796 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600797 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600798 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
799 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600800 break;
801 }
Ian Elliotta0781972015-08-21 15:09:33 -0600802 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
803 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
804 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600805 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600806 }
807
808 // Determine the number of VkImage's to use in the swap chain (we desire to
809 // own only 1 image at a time, besides the images being displayed and
810 // queued for display):
Ian Elliottb08e0d92015-11-20 11:55:46 -0700811 uint32_t desiredNumberOfSwapchainImages = surfCapabilities.minImageCount + 1;
812 if ((surfCapabilities.maxImageCount > 0) &&
813 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600814 {
815 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700816 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600817 }
818
Tony Barbour9fd6d352015-09-16 15:01:32 -0600819 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700820 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
821 preTransform = VK_SURFACE_TRANSFORM_NONE_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600822 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700823 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600824 }
825
Ian Elliottcf074d32015-10-16 18:02:43 -0600826 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600827 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800828 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700829 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600830 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800831 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600832 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800833 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600834 .width = swapchainExtent.width,
835 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600836 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700837 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600838 .preTransform = preTransform,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700839 .imageArrayLayers = 1,
840 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
841 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600842 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600843 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600844 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600845 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600846 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600847 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600848
Ian Elliottb08e0d92015-11-20 11:55:46 -0700849 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800850 assert(!err);
851
Ian Elliottcf074d32015-10-16 18:02:43 -0600852 // If we just re-created an existing swapchain, we should destroy the old
853 // swapchain at this point.
854 // Note: destroying the swapchain also cleans up all its associated
855 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800856 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700857 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600858 }
859
860 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600861 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600862 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800863
Ian Elliotta0781972015-08-21 15:09:33 -0600864 VkImage* swapchainImages =
865 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
866 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600867 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600868 &demo->swapchainImageCount,
869 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600870 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600871
Ian Elliotta0781972015-08-21 15:09:33 -0600872 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600873 assert(demo->buffers);
874
Ian Elliotta0781972015-08-21 15:09:33 -0600875 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600876 VkImageViewCreateInfo color_image_view = {
877 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600878 .pNext = NULL,
879 .format = demo->format,
Chia-I Wub6d30c62015-10-27 19:00:15 +0800880 .components = {
Chia-I Wuf051d262015-10-27 19:25:11 +0800881 .r = VK_COMPONENT_SWIZZLE_R,
882 .g = VK_COMPONENT_SWIZZLE_G,
883 .b = VK_COMPONENT_SWIZZLE_B,
884 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600885 },
886 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600887 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600888 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800889 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600890 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800891 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600892 },
893 .viewType = VK_IMAGE_VIEW_TYPE_2D,
894 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600895 };
896
Ian Elliotta0781972015-08-21 15:09:33 -0600897 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600898
Ian Elliottf4a4e442015-11-17 17:29:40 -0700899 // Render loop will expect image to have been used before and in VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
Tony Barbour15a4ef62015-10-21 10:14:48 -0600900 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600901 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -0600902 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600903 VK_IMAGE_LAYOUT_UNDEFINED,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700904 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600905
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600906 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600907
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600908 err = vkCreateImageView(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +0800909 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600910 assert(!err);
911 }
912}
913
914static void demo_prepare_depth(struct demo *demo)
915{
Tony Barbour72304ef2015-04-16 15:59:00 -0600916 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600917 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600918 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600919 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600920 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600921 .format = depth_format,
922 .extent = { demo->width, demo->height, 1 },
923 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600924 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800925 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600926 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600927 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600928 .flags = 0,
929 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200930
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600931 VkImageViewCreateInfo view = {
932 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600933 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800934 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600935 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600936 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600937 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600938 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800939 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600940 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800941 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600942 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600943 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600944 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600945 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600946
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500947 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600948 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600949 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600950
951 demo->depth.format = depth_format;
952
953 /* create image */
Chia-I Wu63636062015-10-26 21:10:41 +0800954 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600955 &demo->depth.image);
956 assert(!err);
957
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -0600958 vkGetImageMemoryRequirements(demo->device,
Tony Barbour155cce82015-07-03 10:33:54 -0600959 demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600960 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600961
Chia-I Wuf48700b2015-11-10 16:21:09 +0800962 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200963 demo->depth.mem_alloc.pNext = NULL;
964 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
965 demo->depth.mem_alloc.memoryTypeIndex = 0;
966
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600967 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600968 mem_reqs.memoryTypeBits,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600969 0, /* No requirements */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200970 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600971 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600972
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500973 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800974 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +0800975 NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500976 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600977
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500978 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600979 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500980 demo->depth.mem, 0);
981 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600982
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600983 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600984 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600985 VK_IMAGE_LAYOUT_UNDEFINED,
986 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600987
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600988 /* create image view */
989 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800990 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600991 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600992}
993
Tony Barbour1a86d032015-09-21 15:17:33 -0600994/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700995bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600996 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600997 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600998{
Tony Barbour1a86d032015-09-21 15:17:33 -0600999 FILE *fPtr = fopen(filename,"rb");
1000 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001001
Tony Barbour1a86d032015-09-21 15:17:33 -06001002 if (!fPtr)
1003 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001004
Tony Barbour1a86d032015-09-21 15:17:33 -06001005 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001006 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1007 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001008 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001009 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001010
Tony Barbour1a86d032015-09-21 15:17:33 -06001011 do {
1012 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001013 if (cPtr == NULL) {
1014 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001015 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001016 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001017 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001018
Tony Barbour1a86d032015-09-21 15:17:33 -06001019 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001020 if (rgba_data == NULL) {
1021 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001022 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001023 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001024 fgets(header, 256, fPtr); // Format
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001025 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1026 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001027 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001028 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001029
Tony Barbour1a86d032015-09-21 15:17:33 -06001030 for(int y = 0; y < *height; y++)
1031 {
1032 uint8_t *rowPtr = rgba_data;
1033 for(int x = 0; x < *width; x++)
1034 {
1035 fread(rowPtr, 3, 1, fPtr);
1036 rowPtr[3] = 255; /* Alpha of 1 */
1037 rowPtr += 4;
1038 }
1039 rgba_data += layout->rowPitch;
1040 }
1041 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001042 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001043}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001044
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001045static void demo_prepare_texture_image(struct demo *demo,
1046 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001047 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001048 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001049 VkImageUsageFlags usage,
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001050 VkFlags required_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001051{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001052 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001053 int32_t tex_width;
1054 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001055 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001056 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001057
David Pinedo30bd71d2015-04-23 08:16:57 -06001058 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1059 {
1060 printf("Failed to load textures\n");
1061 fflush(stdout);
1062 exit(1);
1063 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001065 tex_obj->tex_width = tex_width;
1066 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001067
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001068 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001069 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001070 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001071 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001072 .format = tex_format,
1073 .extent = { tex_width, tex_height, 1 },
1074 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001075 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001076 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001077 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001078 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001079 .flags = 0,
1080 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001081
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001082 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001083
Chia-I Wu63636062015-10-26 21:10:41 +08001084 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001085 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086 assert(!err);
1087
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001088 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001089
Chia-I Wuf48700b2015-11-10 16:21:09 +08001090 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001091 tex_obj->mem_alloc.pNext = NULL;
1092 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1093 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001094
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001095 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1096 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001097
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001098 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001099 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001100 &(tex_obj->mem));
1101 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001102
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001103 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001104 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001105 tex_obj->mem, 0);
1106 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001107
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001108 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001109 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001110 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001112 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001113 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001114 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115 void *data;
1116
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001117 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001119 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001120 assert(!err);
1121
1122 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1123 fprintf(stderr, "Error loading texture: %s\n", filename);
1124 }
1125
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001126 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001127 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001128
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001129 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001130 demo_set_image_layout(demo, tex_obj->image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001131 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001132 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001133 tex_obj->imageLayout);
1134 /* 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 -07001135}
1136
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001137static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001138{
1139 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001140 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1141 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001142}
1143
1144static void demo_prepare_textures(struct demo *demo)
1145{
Tony Barbour72304ef2015-04-16 15:59:00 -06001146 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001147 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001148 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001149
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001150 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001151
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001152 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001153 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001154
FslNopper6a7e50e2015-05-06 21:42:01 +02001155 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001156 /* Device can texture using linear textures */
1157 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001158 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001159 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001161 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162
1163 memset(&staging_texture, 0, sizeof(staging_texture));
1164 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001165 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166
1167 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001168 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001169 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wuc42afd72015-10-27 18:53:22 +08001170 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001171
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001172 demo_set_image_layout(demo, staging_texture.image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001173 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001174 staging_texture.imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001175 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001176
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001177 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001178 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001179 demo->textures[i].imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001180 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001181
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001182 VkImageCopy copy_region = {
Tony Barbour466f0012015-11-02 11:47:51 -07001183 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001184 .srcOffset = { 0, 0, 0 },
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001185 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
1186 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001187 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1188 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001189 vkCmdCopyImage(demo->cmd,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001190 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1191 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001192 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001193
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001194 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001195 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001196 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001197 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001198
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001199 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001200
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001201 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001202 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001203 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1204 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001205 }
1206
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001207 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001208 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001210 .magFilter = VK_FILTER_NEAREST,
1211 .minFilter = VK_FILTER_NEAREST,
1212 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001213 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1214 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1215 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001216 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001217 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001218 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001219 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001220 .minLod = 0.0f,
1221 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001222 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001223 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001224 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001225
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001226 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001227 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001228 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001229 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001230 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001231 .format = tex_format,
Chia-I Wub6d30c62015-10-27 19:00:15 +08001232 .components = { VK_COMPONENT_SWIZZLE_R,
Chia-I Wuf051d262015-10-27 19:25:11 +08001233 VK_COMPONENT_SWIZZLE_G,
1234 VK_COMPONENT_SWIZZLE_B,
1235 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001236 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001237 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001239
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001240 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001241 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001242 &demo->textures[i].sampler);
1243 assert(!err);
1244
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001245 /* create image view */
1246 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001247 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001248 &demo->textures[i].view);
1249 assert(!err);
1250 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001251}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001253void demo_prepare_cube_data_buffer(struct demo *demo)
1254{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001255 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001256 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001257 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001258 int i;
1259 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001260 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001261 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001262 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001263
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001264 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001265 mat4x4_mul(MVP, VP, demo->model_matrix);
1266 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001267// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001268
1269 for (i=0; i<12*3; i++) {
1270 data.position[i][0] = g_vertex_buffer_data[i*3];
1271 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1272 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1273 data.position[i][3] = 1.0f;
1274 data.attr[i][0] = g_uv_buffer_data[2*i];
1275 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1276 data.attr[i][2] = 0;
1277 data.attr[i][3] = 0;
1278 }
1279
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001280 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001281 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001282 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001283 buf_info.size = sizeof(data);
Chia-I Wu63636062015-10-26 21:10:41 +08001284 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1285 &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001286 assert(!err);
1287
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001288 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001289
Chia-I Wuf48700b2015-11-10 16:21:09 +08001290 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001291 demo->uniform_data.mem_alloc.pNext = NULL;
1292 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1293 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1294
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001295 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001296 mem_reqs.memoryTypeBits,
1297 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001298 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001299 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001300
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001301 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +08001302 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001303 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001304
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001305 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, demo->uniform_data.mem_alloc.allocationSize, 0, (void **) &pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001306 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001307
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001308 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001309
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001310 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001311
Tony Barbour155cce82015-07-03 10:33:54 -06001312 err = vkBindBufferMemory(demo->device,
1313 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001314 demo->uniform_data.mem, 0);
1315 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001316
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001317 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1318 demo->uniform_data.buffer_info.offset = 0;
1319 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001320}
1321
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001322static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001323{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001324 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001325 [0] = {
Chia-I Wued577562015-10-31 00:31:16 +08001326 .binding = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001327 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001328 .descriptorCount = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001329 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001330 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001331 },
1332 [1] = {
Chia-I Wued577562015-10-31 00:31:16 +08001333 .binding = 1,
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001334 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001335 .descriptorCount = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001336 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001337 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001338 },
1339 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001340 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001341 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001342 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001343 .bindingCount = 2,
Chia-I Wu0e76e3f2015-10-31 00:31:16 +08001344 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001345 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001346 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001347
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001348 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001349 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001350 assert(!err);
1351
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001352 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1353 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1354 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001355 .setLayoutCount = 1,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001356 .pSetLayouts = &demo->desc_layout,
1357 };
1358
1359 err = vkCreatePipelineLayout(demo->device,
1360 &pPipelineLayoutCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +08001361 NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001362 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001363 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001364}
1365
Chia-I Wuf973e322015-07-08 13:34:24 +08001366static void demo_prepare_render_pass(struct demo *demo)
1367{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001368 const VkAttachmentDescription attachments[2] = {
1369 [0] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001370 .format = demo->format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001371 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001372 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1373 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1374 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1375 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1376 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1377 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1378 },
1379 [1] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001380 .format = demo->depth.format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001381 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001382 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1383 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1384 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1385 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1386 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1387 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1388 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001389 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001390 const VkAttachmentReference color_reference = {
1391 .attachment = 0,
1392 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1393 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001394 const VkAttachmentReference depth_reference = {
1395 .attachment = 1,
1396 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1397 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001398 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001399 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1400 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001401 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001402 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001403 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001404 .pColorAttachments = &color_reference,
1405 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001406 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001407 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001408 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001409 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001410 const VkRenderPassCreateInfo rp_info = {
1411 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1412 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001413 .attachmentCount = 2,
1414 .pAttachments = attachments,
1415 .subpassCount = 1,
1416 .pSubpasses = &subpass,
1417 .dependencyCount = 0,
1418 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001419 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001420 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001421
Chia-I Wu63636062015-10-26 21:10:41 +08001422 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001423 assert(!err);
1424}
1425
Chia-I Wu46176f12015-10-31 00:31:16 +08001426static VkShaderModule demo_prepare_shader_module(struct demo* demo,
Chia-I Wu46176f12015-10-31 00:31:16 +08001427 const void* code,
1428 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429{
Chia-I Wu46176f12015-10-31 00:31:16 +08001430 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001431 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001432 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001433
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001434 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1435 moduleCreateInfo.pNext = NULL;
1436
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001437 moduleCreateInfo.codeSize = size;
1438 moduleCreateInfo.pCode = code;
1439 moduleCreateInfo.flags = 0;
1440 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1441 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001442
1443 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001444}
1445
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001446char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001447{
1448 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001449 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001450 void *shader_code;
1451
1452 FILE *fp = fopen(filename, "rb");
1453 if (!fp) return NULL;
1454
1455 fseek(fp, 0L, SEEK_END);
1456 size = ftell(fp);
1457
1458 fseek(fp, 0L, SEEK_SET);
1459
1460 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001461 retval = fread(shader_code, size, 1, fp);
1462 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001463
1464 *psize = size;
1465
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001466 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001467 return shader_code;
1468}
1469
Chia-I Wu46176f12015-10-31 00:31:16 +08001470static VkShaderModule demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001471{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001472 void *vertShaderCode;
1473 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001474
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001475 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1476
Tony Barbourf331eb32015-11-06 10:10:37 -07001477 demo->vert_shader_module = demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001478
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001479 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001480
1481 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001482}
1483
Chia-I Wu46176f12015-10-31 00:31:16 +08001484static VkShaderModule demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001485{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001486 void *fragShaderCode;
1487 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001488
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001489 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001490
Tony Barbourf331eb32015-11-06 10:10:37 -07001491 demo->frag_shader_module = demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001492
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001493 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001494
1495 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001496}
1497
1498static void demo_prepare_pipeline(struct demo *demo)
1499{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001500 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001501 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001502 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001503 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001504 VkPipelineRasterizationStateCreateInfo rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001505 VkPipelineColorBlendStateCreateInfo cb;
1506 VkPipelineDepthStencilStateCreateInfo ds;
1507 VkPipelineViewportStateCreateInfo vp;
1508 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001509 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniellba839d12015-09-29 13:01:09 -06001510 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001511 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001512
Piers Daniellba839d12015-09-29 13:01:09 -06001513 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1514 memset(&dynamicState, 0, sizeof dynamicState);
1515 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1516 dynamicState.pDynamicStates = dynamicStateEnables;
1517
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001518 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001519 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001520 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001521
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001522 memset(&vi, 0, sizeof(vi));
1523 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1524
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001525 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001526 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001527 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001528
1529 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001530 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1531 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001532 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001533 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001534 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001535 rs.rasterizerDiscardEnable = VK_FALSE;
1536 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537
1538 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001539 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1540 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001541 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001542 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001543 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001544 cb.attachmentCount = 1;
1545 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001546
Tony Barbour29645d02015-01-16 14:27:35 -07001547 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001548 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001549 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001550 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1551 vp.scissorCount = 1;
1552 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001553
1554 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001555 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001556 ds.depthTestEnable = VK_TRUE;
1557 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001558 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001559 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001560 ds.back.failOp = VK_STENCIL_OP_KEEP;
1561 ds.back.passOp = VK_STENCIL_OP_KEEP;
1562 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001563 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001564 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001565
Tony Barbour29645d02015-01-16 14:27:35 -07001566 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001567 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001568 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001569 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001571 // Two stages: vs and fs
1572 pipeline.stageCount = 2;
1573 VkPipelineShaderStageCreateInfo shaderStages[2];
1574 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1575
1576 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001577 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1578 shaderStages[0].module = demo_prepare_vs(demo);
1579 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001580
1581 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001582 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1583 shaderStages[1].module = demo_prepare_fs(demo);
1584 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001585
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001586 memset(&pipelineCache, 0, sizeof(pipelineCache));
1587 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001588
Chia-I Wu63636062015-10-26 21:10:41 +08001589 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001590 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001591
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001592 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001593 pipeline.pInputAssemblyState = &ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001594 pipeline.pRasterizationState = &rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001595 pipeline.pColorBlendState = &cb;
1596 pipeline.pMultisampleState = &ms;
1597 pipeline.pViewportState = &vp;
1598 pipeline.pDepthStencilState = &ds;
1599 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001600 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001601 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001602
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001603 pipeline.renderPass = demo->render_pass;
1604
Chia-I Wu63636062015-10-26 21:10:41 +08001605 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1606 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001607 assert(!err);
1608
Chia-I Wu63636062015-10-26 21:10:41 +08001609 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1610 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001611}
1612
Chia-I Wu63ea9262015-03-26 13:14:16 +08001613static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001614{
Chia-I Wuf051d262015-10-27 19:25:11 +08001615 const VkDescriptorPoolSize type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001616 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001617 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001618 .descriptorCount = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001619 },
1620 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001621 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001622 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001623 },
1624 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001625 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001626 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001627 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001628 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001629 .poolSizeCount = 2,
1630 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001631 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001632 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001633
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001634 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001635 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001636 assert(!err);
1637}
1638
1639static void demo_prepare_descriptor_set(struct demo *demo)
1640{
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001641 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001642 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001643 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001644 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001645
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001646 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001647 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001648 .pNext = NULL,
1649 .descriptorPool = demo->desc_pool,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001650 .setLayoutCount = 1,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001651 .pSetLayouts = &demo->desc_layout
1652 };
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001653 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001654 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001655
Chia-I Wuae721ba2015-05-25 16:27:55 +08001656 memset(&tex_descs, 0, sizeof(tex_descs));
1657 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001658 tex_descs[i].sampler = demo->textures[i].sampler;
1659 tex_descs[i].imageView = demo->textures[i].view;
1660 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001661 }
1662
1663 memset(&writes, 0, sizeof(writes));
1664
1665 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001666 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001667 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001668 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001669 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001670
1671 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001672 writes[1].dstSet = demo->desc_set;
1673 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001674 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001675 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001676 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001677
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001678 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001679}
1680
Chia-I Wuf973e322015-07-08 13:34:24 +08001681static void demo_prepare_framebuffers(struct demo *demo)
1682{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001683 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001684 attachments[1] = demo->depth.view;
1685
Chia-I Wuf973e322015-07-08 13:34:24 +08001686 const VkFramebufferCreateInfo fb_info = {
1687 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1688 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001689 .renderPass = demo->render_pass,
1690 .attachmentCount = 2,
1691 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001692 .width = demo->width,
1693 .height = demo->height,
1694 .layers = 1,
1695 };
1696 VkResult U_ASSERT_ONLY err;
1697 uint32_t i;
1698
Tony Barbourd8e504f2015-10-08 14:26:24 -06001699 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1700 assert(demo->framebuffers);
1701
1702 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001703 attachments[0] = demo->buffers[i].view;
Chia-I Wu63636062015-10-26 21:10:41 +08001704 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001705 assert(!err);
1706 }
1707}
1708
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001709static void demo_prepare(struct demo *demo)
1710{
Cody Northrop91e221b2015-07-09 18:08:32 -06001711 VkResult U_ASSERT_ONLY err;
1712
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001713 const VkCommandPoolCreateInfo cmd_pool_info = {
1714 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001715 .pNext = NULL,
1716 .queueFamilyIndex = demo->graphics_queue_node_index,
1717 .flags = 0,
1718 };
Chia-I Wu63636062015-10-26 21:10:41 +08001719 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001720 assert(!err);
1721
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001722 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001723 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001724 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001725 .commandPool = demo->cmd_pool,
1726 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001727 .bufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001728 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001729
1730 demo_prepare_buffers(demo);
1731 demo_prepare_depth(demo);
1732 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001733 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001735 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001736 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001737 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001738
Ian Elliotta0781972015-08-21 15:09:33 -06001739 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001740 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001741 assert(!err);
1742 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001743
Chia-I Wu63ea9262015-03-26 13:14:16 +08001744 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001745 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001746
Chia-I Wuf973e322015-07-08 13:34:24 +08001747 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001748
Ian Elliotta0781972015-08-21 15:09:33 -06001749 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001750 demo->current_buffer = i;
1751 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1752 }
1753
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001754 /*
1755 * Prepare functions above may generate pipeline commands
1756 * that need to be flushed before beginning the render loop.
1757 */
1758 demo_flush_init_cmd(demo);
1759
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001760 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001761 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001762}
1763
David Pinedo2bb7c932015-06-18 17:03:14 -06001764static void demo_cleanup(struct demo *demo)
1765{
1766 uint32_t i;
1767
1768 demo->prepared = false;
1769
Tony Barbourd8e504f2015-10-08 14:26:24 -06001770 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001771 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001772 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001773 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001774 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001775
Chia-I Wu63636062015-10-26 21:10:41 +08001776 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1777 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1778 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1779 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1780 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001781
1782 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001783 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1784 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1785 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1786 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001787 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001788 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001789
Chia-I Wu63636062015-10-26 21:10:41 +08001790 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1791 vkDestroyImage(demo->device, demo->depth.image, NULL);
1792 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001793
Chia-I Wu63636062015-10-26 21:10:41 +08001794 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1795 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001796
Ian Elliotta0781972015-08-21 15:09:33 -06001797 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001798 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001799 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001800 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001801 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001802
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001803 free(demo->queue_props);
1804
Chia-I Wu63636062015-10-26 21:10:41 +08001805 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1806 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001807 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001808 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001809 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001810 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001811 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001812
1813#ifndef _WIN32
1814 xcb_destroy_window(demo->connection, demo->window);
1815 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001816 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001817#endif // _WIN32
1818}
1819
Ian Elliottcf074d32015-10-16 18:02:43 -06001820static void demo_resize(struct demo *demo)
1821{
1822 uint32_t i;
1823
Mike Stroyanbb60b382015-10-28 09:46:57 -06001824 // Don't react to resize until after first initialization.
1825 if (!demo->prepared) {
1826 return;
1827 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001828 // In order to properly resize the window, we must re-create the swapchain
1829 // AND redo the command buffers, etc.
1830 //
1831 // First, perform part of the demo_cleanup() function:
1832 demo->prepared = false;
1833
1834 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001835 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001836 }
1837 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001838 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001839
Chia-I Wu63636062015-10-26 21:10:41 +08001840 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1841 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1842 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1843 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1844 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001845
1846 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001847 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1848 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1849 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1850 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001851 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001852
Chia-I Wu63636062015-10-26 21:10:41 +08001853 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1854 vkDestroyImage(demo->device, demo->depth.image, NULL);
1855 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001856
Chia-I Wu63636062015-10-26 21:10:41 +08001857 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1858 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001859
1860 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001861 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001862 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001863 }
Chia-I Wu63636062015-10-26 21:10:41 +08001864 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001865 free(demo->buffers);
1866
Ian Elliottcf074d32015-10-16 18:02:43 -06001867 // Second, re-perform the demo_prepare() function, which will re-create the
1868 // swapchain:
1869 demo_prepare(demo);
1870}
1871
David Pinedo2bb7c932015-06-18 17:03:14 -06001872// On MS-Windows, make this a global, so it's available to WndProc()
1873struct demo demo;
1874
Ian Elliott639ca472015-04-16 15:23:05 -06001875#ifdef _WIN32
1876static void demo_run(struct demo *demo)
1877{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001878 if (!demo->prepared)
1879 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001880 // Wait for work to finish before updating MVP.
1881 vkDeviceWaitIdle(demo->device);
1882 demo_update_data_buffer(demo);
1883
1884 demo_draw(demo);
1885
1886 // Wait for work to finish before updating MVP.
1887 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001888
David Pinedo2bb7c932015-06-18 17:03:14 -06001889 demo->curFrame++;
1890
1891 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1892 {
1893 demo->quit=true;
1894 demo_cleanup(demo);
1895 ExitProcess(0);
1896 }
1897
1898}
Ian Elliott639ca472015-04-16 15:23:05 -06001899
1900// MS-Windows event handling function:
1901LRESULT CALLBACK WndProc(HWND hWnd,
1902 UINT uMsg,
1903 WPARAM wParam,
1904 LPARAM lParam)
1905{
Ian Elliott639ca472015-04-16 15:23:05 -06001906 switch(uMsg)
1907 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001908 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001909 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001910 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001911 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001912 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001913 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001914 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001915 demo.width = lParam & 0xffff;
1916 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001917 demo_resize(&demo);
1918 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001919 default:
1920 break;
1921 }
1922 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1923}
1924
1925static void demo_create_window(struct demo *demo)
1926{
1927 WNDCLASSEX win_class;
1928
1929 // Initialize the window class structure:
1930 win_class.cbSize = sizeof(WNDCLASSEX);
1931 win_class.style = CS_HREDRAW | CS_VREDRAW;
1932 win_class.lpfnWndProc = WndProc;
1933 win_class.cbClsExtra = 0;
1934 win_class.cbWndExtra = 0;
1935 win_class.hInstance = demo->connection; // hInstance
1936 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1937 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1938 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1939 win_class.lpszMenuName = NULL;
1940 win_class.lpszClassName = demo->name;
1941 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1942 // Register window class:
1943 if (!RegisterClassEx(&win_class)) {
1944 // It didn't work, so try to give a useful error:
1945 printf("Unexpected error trying to start the application!\n");
1946 fflush(stdout);
1947 exit(1);
1948 }
1949 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001950 RECT wr = { 0, 0, demo->width, demo->height };
1951 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001952 demo->window = CreateWindowEx(0,
1953 demo->name, // class name
1954 demo->name, // app name
1955 WS_OVERLAPPEDWINDOW | // window style
1956 WS_VISIBLE |
1957 WS_SYSMENU,
1958 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001959 wr.right-wr.left, // width
1960 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001961 NULL, // handle to parent
1962 NULL, // handle to menu
1963 demo->connection, // hInstance
1964 NULL); // no extra parameters
1965 if (!demo->window) {
1966 // It didn't work, so try to give a useful error:
1967 printf("Cannot create a window in which to draw!\n");
1968 fflush(stdout);
1969 exit(1);
1970 }
1971}
1972#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001973static void demo_handle_event(struct demo *demo,
1974 const xcb_generic_event_t *event)
1975{
Piers Daniell735ee532015-02-23 16:23:13 -07001976 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001977 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001978 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001979 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001980 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001981 case XCB_CLIENT_MESSAGE:
1982 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1983 (*demo->atom_wm_delete_window).atom) {
1984 demo->quit = true;
1985 }
1986 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987 case XCB_KEY_RELEASE:
1988 {
1989 const xcb_key_release_event_t *key =
1990 (const xcb_key_release_event_t *) event;
1991
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001992 switch (key->detail) {
1993 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001994 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001995 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001996 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001997 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001998 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001999 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002000 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002001 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002002 case 0x41:
2003 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002004 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002005 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002006 }
2007 break;
Ian Elliottcf074d32015-10-16 18:02:43 -06002008 case XCB_CONFIGURE_NOTIFY:
2009 {
2010 const xcb_configure_notify_event_t *cfg =
2011 (const xcb_configure_notify_event_t *) event;
2012 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2013 demo_resize(demo);
2014 }
2015 }
2016 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002017 default:
2018 break;
2019 }
2020}
2021
2022static void demo_run(struct demo *demo)
2023{
2024 xcb_flush(demo->connection);
2025
2026 while (!demo->quit) {
2027 xcb_generic_event_t *event;
2028
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002029 if (demo->pause) {
2030 event = xcb_wait_for_event(demo->connection);
2031 } else {
2032 event = xcb_poll_for_event(demo->connection);
2033 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002034 if (event) {
2035 demo_handle_event(demo, event);
2036 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002037 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002038
2039 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002040 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002041 demo_update_data_buffer(demo);
2042
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002043 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002044
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002045 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002046 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002047 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002048 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002049 demo->quit = true;
2050
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002051 }
2052}
2053
2054static void demo_create_window(struct demo *demo)
2055{
2056 uint32_t value_mask, value_list[32];
2057
2058 demo->window = xcb_generate_id(demo->connection);
2059
2060 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2061 value_list[0] = demo->screen->black_pixel;
2062 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002063 XCB_EVENT_MASK_EXPOSURE |
2064 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002065
2066 xcb_create_window(demo->connection,
2067 XCB_COPY_FROM_PARENT,
2068 demo->window, demo->screen->root,
2069 0, 0, demo->width, demo->height, 0,
2070 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2071 demo->screen->root_visual,
2072 value_mask, value_list);
2073
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002074 /* Magic code that will send notification when window is destroyed */
2075 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2076 "WM_PROTOCOLS");
2077 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2078
2079 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2080 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2081
2082 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2083 demo->window, (*reply).atom, 4, 32, 1,
2084 &(*demo->atom_wm_delete_window).atom);
2085 free(reply);
2086
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002087 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002088
2089 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2090 const uint32_t coords[] = {100, 100};
2091 xcb_configure_window(demo->connection, demo->window,
2092 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002093}
Ian Elliott639ca472015-04-16 15:23:05 -06002094#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002095
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002096/*
2097 * Return 1 (true) if all layer names specified in check_names
2098 * can be found in given layer properties.
2099 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002100static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002101 uint32_t layer_count, VkLayerProperties *layers)
2102{
2103 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002104 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002105 for (uint32_t j = 0; j < layer_count; j++) {
2106 if (!strcmp(check_names[i], layers[j].layerName)) {
2107 found = 1;
2108 }
2109 }
2110 if (!found) {
2111 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2112 return 0;
2113 }
2114 }
2115 return 1;
2116}
2117
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002118static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002119{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002120 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002121 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002122 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002123 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002124 VkLayerProperties *instance_layers;
2125 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002126 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002127 uint32_t instance_layer_count = 0;
2128 uint32_t enabled_extension_count = 0;
2129 uint32_t enabled_layer_count = 0;
2130
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002131 char *instance_validation_layers[] = {
Jon Ashburnf453b372015-11-25 08:25:11 -07002132 "VK_LAYER_LUNARG_Threading",
2133 "VK_LAYER_LUNARG_MemTracker",
2134 "VK_LAYER_LUNARG_ObjectTracker",
2135 "VK_LAYER_LUNARG_DrawState",
2136 "VK_LAYER_LUNARG_ParamChecker",
Jon Ashburnf453b372015-11-25 08:25:11 -07002137 "VK_LAYER_LUNARG_Swapchain",
2138 "VK_LAYER_LUNARG_DeviceLimits",
2139 "VK_LAYER_LUNARG_Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002140 };
2141
2142 char *device_validation_layers[] = {
Jon Ashburnf453b372015-11-25 08:25:11 -07002143 "VK_LAYER_LUNARG_Threading",
2144 "VK_LAYER_LUNARG_MemTracker",
2145 "VK_LAYER_LUNARG_ObjectTracker",
2146 "VK_LAYER_LUNARG_DrawState",
2147 "VK_LAYER_LUNARG_ParamChecker",
Jon Ashburnf453b372015-11-25 08:25:11 -07002148 "VK_LAYER_LUNARG_Swapchain",
2149 "VK_LAYER_LUNARG_DeviceLimits",
2150 "VK_LAYER_LUNARG_Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002151 };
2152
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002153 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002154 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002155 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002156 assert(!err);
2157
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002158 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002159 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002160 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002161
2162 if (demo->validate) {
2163 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2164 instance_layer_count, instance_layers);
2165 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002166 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002167 "required validation layer.\n\n"
2168 "Please look at the Getting Started guide for additional "
2169 "information.\n",
2170 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002171 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002172 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002173 }
2174
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002175 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002176 assert(!err);
2177
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002178 VkBool32 surfaceExtFound = 0;
2179 VkBool32 platformSurfaceExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002180 memset(extension_names, 0, sizeof(extension_names));
2181 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002182 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002183 assert(!err);
2184 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002185 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002186 surfaceExtFound = 1;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002187 extension_names[enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002188 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002189#ifdef _WIN32
2190 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2191 platformSurfaceExtFound = 1;
2192 extension_names[enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2193 }
2194#else // _WIN32
2195 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2196 platformSurfaceExtFound = 1;
2197 extension_names[enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2198 }
2199#endif // _WIN32
Courtney Goeltzenleuchter96403642015-11-25 13:40:37 -07002200 if (!strcmp(VK_EXT_LUNARG_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002201 if (demo->validate) {
Courtney Goeltzenleuchter96403642015-11-25 13:40:37 -07002202 extension_names[enabled_extension_count++] = VK_EXT_LUNARG_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002203 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002204 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002205 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002206 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002207 if (!surfaceExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002208 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002209 VK_KHR_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002210 "Vulkan installable client driver (ICD) installed?\nPlease "
2211 "look at the Getting Started guide for additional "
2212 "information.\n",
2213 "vkCreateInstance Failure");
2214 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002215 if (!platformSurfaceExtFound) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002216#ifdef _WIN32
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002217 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002218 VK_KHR_WIN32_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002219 "Vulkan installable client driver (ICD) installed?\nPlease "
2220 "look at the Getting Started guide for additional "
2221 "information.\n",
2222 "vkCreateInstance Failure");
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002223#else // _WIN32
2224 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2225 VK_KHR_XCB_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2226 "Vulkan installable client driver (ICD) installed?\nPlease "
2227 "look at the Getting Started guide for additional "
2228 "information.\n",
2229 "vkCreateInstance Failure");
2230#endif // _WIN32
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002231 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002232 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002233 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002234 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002235 .pApplicationName = APP_SHORT_NAME,
2236 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002237 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002238 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002239 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002240 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002241 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002242 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002243 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002244 .pApplicationInfo = &app,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002245 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002246 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002247 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002248 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002249 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002250
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002251 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002252
Chia-I Wu63636062015-10-26 21:10:41 +08002253 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002254 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2255 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002256 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002257 "additional information.\n",
2258 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002259 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002260 ERR_EXIT("Cannot find a specified extension library"
2261 ".\nMake sure your layers path is set appropriately\n",
2262 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002263 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002264 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2265 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002266 "the Getting Started guide for additional information.\n",
2267 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002268 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002269
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002270 free(instance_layers);
2271 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002272
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002273 /* Make initial call to query gpu_count, then second call for gpu info*/
2274 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2275 assert(!err && gpu_count > 0);
2276 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2277 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2278 assert(!err);
2279 /* For cube demo we just grab the first physical device */
2280 demo->gpu = physical_devices[0];
2281 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002282
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002283 /* Look for validation layers */
2284 validation_found = 0;
2285 enabled_layer_count = 0;
2286 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002287 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002288 assert(!err);
2289
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002290 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002291 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002292 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002293
2294 if (demo->validate) {
2295 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2296 device_layer_count, device_layers);
2297 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002298 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002299 "a required validation layer.\n\n"
2300 "Please look at the Getting Started guide for additional "
2301 "information.\n",
2302 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002303 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002304 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002305 }
2306
2307 uint32_t device_extension_count = 0;
2308 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002309 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002310 demo->gpu, NULL, &device_extension_count, NULL);
2311 assert(!err);
2312
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002313 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002314 enabled_extension_count = 0;
2315 memset(extension_names, 0, sizeof(extension_names));
2316 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002317 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002318 demo->gpu, NULL, &device_extension_count, device_extensions);
2319 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002320
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002321 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002322 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002323 swapchainExtFound = 1;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002324 extension_names[enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002325 }
2326 assert(enabled_extension_count < 64);
2327 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002328 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002329 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002330 VK_KHR_SWAPCHAIN_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002331 "Vulkan installable client driver (ICD) installed?\nPlease "
2332 "look at the Getting Started guide for additional "
2333 "information.\n",
2334 "vkCreateInstance Failure");
2335 }
2336
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002337 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002338 demo->CreateDebugReportCallback = (PFN_vkCreateDebugReportCallbackLUNARG) vkGetInstanceProcAddr(demo->inst, "vkCreateDebugReportCallbackLUNARG");
2339 demo->DestroyDebugReportCallback = (PFN_vkDestroyDebugReportCallbackLUNARG) vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugReportCallbackLUNARG");
2340 if (!demo->CreateDebugReportCallback) {
2341 ERR_EXIT("GetProcAddr: Unable to find vkCreateDebugReportCallbackLUNARG\n",
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002342 "vkGetProcAddr Failure");
2343 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002344 if (!demo->DestroyDebugReportCallback) {
2345 ERR_EXIT("GetProcAddr: Unable to find vkDestroyDebugReportCallbackLUNARG\n",
Tony Barbourd70b42c2015-06-30 14:14:19 -06002346 "vkGetProcAddr Failure");
2347 }
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002348 demo->DebugReportMessage = (PFN_vkDebugReportMessageLUNARG) vkGetInstanceProcAddr(demo->inst, "vkDebugReportMessageLUNARG");
2349 if (!demo->DebugReportMessage) {
2350 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageLUNARG\n",
2351 "vkGetProcAddr Failure");
2352 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002353
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002354 PFN_vkDebugReportCallbackLUNARG callback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002355
2356 if (!demo->use_break) {
2357 callback = dbgFunc;
2358 } else {
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002359 callback = dbgFunc;
2360 // TODO add a break callback defined locally since there is no longer
2361 // one included in the loader
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002362 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002363 VkDebugReportCallbackCreateInfoLUNARG dbgCreateInfo;
2364 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_LUNARG;
2365 dbgCreateInfo.pNext = NULL;
2366 dbgCreateInfo.pfnCallback = callback;
2367 dbgCreateInfo.pUserData = NULL;
2368 dbgCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT | VK_DEBUG_REPORT_WARN_BIT;
2369 err = demo->CreateDebugReportCallback(
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002370 demo->inst,
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002371 &dbgCreateInfo,
2372 NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002373 &demo->msg_callback);
2374 switch (err) {
2375 case VK_SUCCESS:
2376 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002377 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002378 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2379 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002380 break;
2381 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002382 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2383 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002384 break;
2385 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002386 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002387 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002388
2389 /* Call with NULL data to get count */
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002390 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002391 assert(demo->queue_count >= 1);
2392
2393 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002394 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002395 // Find a queue that supports gfx
2396 uint32_t gfx_queue_idx = 0;
2397 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2398 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2399 break;
2400 }
2401 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002402 // Query fine-grained feature support for this device.
2403 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002404 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002405 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002406
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002407 float queue_priorities[1] = { 0.0 };
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002408 const VkDeviceQueueCreateInfo queue = {
2409 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2410 .pNext = NULL,
2411 .queueFamilyIndex = gfx_queue_idx,
Chia-I Wu8b497442015-11-06 06:42:02 +08002412 .queueCount = 1,
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002413 .pQueuePriorities = queue_priorities
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002414 };
2415
2416 VkDeviceCreateInfo device = {
2417 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2418 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002419 .queueCreateInfoCount = 1,
2420 .pQueueCreateInfos = &queue,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002421 .enabledLayerNameCount = enabled_layer_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002422 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002423 .enabledExtensionNameCount = enabled_extension_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002424 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002425 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002426 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002427
Chia-I Wu63636062015-10-26 21:10:41 +08002428 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002429 assert(!err);
2430
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002431 free(device_layers);
2432
Ian Elliotta0781972015-08-21 15:09:33 -06002433 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
Ian Elliottf2ff8022015-11-23 12:48:15 -07002434 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2435 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2436 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002437 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002438 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2439 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2440 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2441 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002442}
2443
Tony Barbourcc69f452015-10-08 13:59:42 -06002444static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002445{
2446 VkResult err;
2447 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002448
Ian Elliottb08e0d92015-11-20 11:55:46 -07002449 // Create a WSI surface for the window:
Ian Elliottaaae5352015-07-06 14:27:58 -06002450#ifdef _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -07002451 err = vkCreateWin32SurfaceKHR(demo->inst, demo->connection, demo->window,
2452 NULL, &demo->surface);
2453
Ian Elliottaaae5352015-07-06 14:27:58 -06002454#else // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -07002455 err = vkCreateXcbSurfaceKHR(demo->inst, demo->connection, demo->window,
2456 NULL, &demo->surface);
Ian Elliottaaae5352015-07-06 14:27:58 -06002457#endif // _WIN32
2458
Tony Barbourcc69f452015-10-08 13:59:42 -06002459 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002460 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2461 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002462 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
Ian Elliottb08e0d92015-11-20 11:55:46 -07002463 demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002464 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002465 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002466
2467 // Search for a graphics and a present queue in the array of queue
2468 // families, try to find one that supports both
2469 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2470 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002471 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002472 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2473 if (graphicsQueueNodeIndex == UINT32_MAX) {
2474 graphicsQueueNodeIndex = i;
2475 }
2476
2477 if (supportsPresent[i] == VK_TRUE) {
2478 graphicsQueueNodeIndex = i;
2479 presentQueueNodeIndex = i;
2480 break;
2481 }
2482 }
2483 }
2484 if (presentQueueNodeIndex == UINT32_MAX) {
2485 // If didn't find a queue that supports both graphics and present, then
2486 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002487 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002488 if (supportsPresent[i] == VK_TRUE) {
2489 presentQueueNodeIndex = i;
2490 break;
2491 }
2492 }
2493 }
2494 free(supportsPresent);
2495
2496 // Generate error if could not find both a graphics and a present queue
2497 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2498 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002499 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002500 }
2501
2502 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002503 // synchronization, and appropriate tracking for QueueSubmit.
2504 // NOTE: While it is possible for an application to use a separate graphics
2505 // and a present queues, this demo program assumes it is only using
2506 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002507 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2508 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002509 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002510 }
2511
2512 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002513
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002514 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002515 0, &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002516
Ian Elliottaaae5352015-07-06 14:27:58 -06002517 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002518 uint32_t formatCount;
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002519 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002520 demo->surface,
2521 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002522 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002523 VkSurfaceFormatKHR *surfFormats =
2524 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002525 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002526 demo->surface,
2527 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002528 assert(!err);
2529 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2530 // the surface has no preferred format. Otherwise, at least one
2531 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002532 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2533 {
2534 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2535 }
2536 else
2537 {
2538 assert(formatCount >= 1);
2539 demo->format = surfFormats[0].format;
2540 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002541 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002542
David Pinedo2bb7c932015-06-18 17:03:14 -06002543 demo->quit = false;
2544 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002545
2546 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002547 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002548}
2549
2550static void demo_init_connection(struct demo *demo)
2551{
Ian Elliott639ca472015-04-16 15:23:05 -06002552#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002553 const xcb_setup_t *setup;
2554 xcb_screen_iterator_t iter;
2555 int scr;
2556
2557 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002558 if (demo->connection == NULL) {
2559 printf("Cannot find a compatible Vulkan installable client driver "
2560 "(ICD).\nExiting ...\n");
2561 fflush(stdout);
2562 exit(1);
2563 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002564
2565 setup = xcb_get_setup(demo->connection);
2566 iter = xcb_setup_roots_iterator(setup);
2567 while (scr-- > 0)
2568 xcb_screen_next(&iter);
2569
2570 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002571#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002572}
2573
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002574static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002575{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002576 vec3 eye = {0.0f, 3.0f, 5.0f};
2577 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002578 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002579
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002580 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002581 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002582
Piers Daniell735ee532015-02-23 16:23:13 -07002583 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002584 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002585 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002586 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002587 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002588 if (strcmp(argv[i], "--break") == 0) {
2589 demo->use_break = true;
2590 continue;
2591 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002592 if (strcmp(argv[i], "--validate") == 0) {
2593 demo->validate = true;
2594 continue;
2595 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002596 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002597 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002598 i < argc-1 &&
2599 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2600 demo->frameCount >= 0)
2601 {
2602 i++;
2603 continue;
2604 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002605
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002606 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002607 fflush(stderr);
2608 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002609 }
2610
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002611 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002612 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002613
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002614 demo->width = 500;
2615 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002616
2617 demo->spin_angle = 0.01f;
2618 demo->spin_increment = 0.01f;
2619 demo->pause = false;
2620
Piers Daniell735ee532015-02-23 16:23:13 -07002621 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002622 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2623 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002624}
2625
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002626
Ian Elliott639ca472015-04-16 15:23:05 -06002627#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002628extern int __getmainargs(
2629 int * _Argc,
2630 char *** _Argv,
2631 char *** _Env,
2632 int _DoWildCard,
2633 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002634
Ian Elliotta748eaf2015-04-28 15:50:36 -06002635int WINAPI WinMain(HINSTANCE hInstance,
2636 HINSTANCE hPrevInstance,
2637 LPSTR pCmdLine,
2638 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002639{
2640 MSG msg; // message
2641 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002642 int argc;
2643 char** argv;
2644 char** env;
2645 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002646
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002647 __getmainargs(&argc,&argv,&env,0,&new_mode);
2648
2649 demo_init(&demo, argc, argv);
2650 demo.connection = hInstance;
2651 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002652 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002653 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002654
2655 demo_prepare(&demo);
2656
2657 done = false; //initialize loop condition variable
2658 /* main message loop*/
2659 while(!done)
2660 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002661 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002662 if (msg.message == WM_QUIT) //check for a quit message
2663 {
2664 done = true; //if found, quit app
2665 }
2666 else
2667 {
2668 /* Translate and dispatch to event queue*/
2669 TranslateMessage(&msg);
2670 DispatchMessage(&msg);
2671 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002672 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002673 }
2674
2675 demo_cleanup(&demo);
2676
Tony Barbourf9921732015-04-22 11:36:22 -06002677 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002678}
2679#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002680int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002681{
2682 struct demo demo;
2683
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002684 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002685 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002686 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002687
2688 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002689 demo_run(&demo);
2690
2691 demo_cleanup(&demo);
2692
2693 return 0;
2694}
Ian Elliott639ca472015-04-16 15:23:05 -06002695#endif // _WIN32