blob: bef3b7bbe1ab0fdda3ecefe1e69b7119ce90e0ed [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
Courtney Goeltzenleuchter24df6832015-12-02 14:53:22 -07004 * Copyright (C) 2015 Google Inc.
Ian Elliotta748eaf2015-04-28 15:50:36 -06005 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060023 *
24 * Author: Chia-I Wu <olv@lunarg.com>
25 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
26 * Author: Ian Elliott <ian@LunarG.com>
27 * Author: Jon Ashburn <jon@lunarg.com>
Ian Elliotta748eaf2015-04-28 15:50:36 -060028 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060029#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070035#include <signal.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036
Ian Elliott639ca472015-04-16 15:23:05 -060037#ifdef _WIN32
38#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060039#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060040#endif // _WIN32
41
David Pinedoc5193c12015-11-06 12:54:48 -070042#include <vulkan/vulkan.h>
Courtney Goeltzenleuchter43b34d52015-12-09 14:33:56 -070043#include <vulkan/vk_lunarg_debug_report.h>
Ian Elliottb08e0d92015-11-20 11:55:46 -070044
David Pinedo541526f2015-11-24 09:00:24 -070045#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060046#include "linmath.h"
47
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060048#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060049#define APP_SHORT_NAME "cube"
50#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060051
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060052#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
53
Tony Barbourfdc2d352015-04-22 09:02:32 -060054#if defined(NDEBUG) && defined(__GNUC__)
55#define U_ASSERT_ONLY __attribute__((unused))
56#else
57#define U_ASSERT_ONLY
58#endif
59
Ian Elliott07264132015-04-28 11:35:02 -060060#ifdef _WIN32
61#define ERR_EXIT(err_msg, err_class) \
62 do { \
63 MessageBox(NULL, err_msg, err_class, MB_OK); \
64 exit(1); \
65 } while (0)
66
Ian Elliott07264132015-04-28 11:35:02 -060067#else // _WIN32
68
69#define ERR_EXIT(err_msg, err_class) \
70 do { \
71 printf(err_msg); \
72 fflush(stdout); \
73 exit(1); \
74 } while (0)
75#endif // _WIN32
76
Ian Elliottaaae5352015-07-06 14:27:58 -060077#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
78{ \
79 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \
80 if (demo->fp##entrypoint == NULL) { \
81 ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \
82 "vkGetInstanceProcAddr Failure"); \
83 } \
84}
85
Jon Ashburn7d8342c2015-10-08 15:58:23 -060086static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
87
Ian Elliott673898b2015-06-22 15:07:49 -060088#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
89{ \
Jon Ashburn7d8342c2015-10-08 15:58:23 -060090 if(!g_gdpa) \
91 g_gdpa = (PFN_vkGetDeviceProcAddr) vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
92 demo->fp##entrypoint = (PFN_vk##entrypoint) g_gdpa(dev, "vk"#entrypoint); \
Ian Elliott673898b2015-06-22 15:07:49 -060093 if (demo->fp##entrypoint == NULL) { \
94 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
95 "vkGetDeviceProcAddr Failure"); \
96 } \
97}
98
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060099/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700100 * structure to track all objects related to a texture.
101 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600102struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600103 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700104
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600105 VkImage image;
106 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600107
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800108 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500109 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600110 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700111 int32_t tex_width, tex_height;
112};
113
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600114static char *tex_files[] = {
Tony Barbour1a86d032015-09-21 15:17:33 -0600115 "lunarg.ppm"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600116};
117
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600118struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600119 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600120 float mvp[4][4];
121 float position[12*3][4];
122 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600123};
124
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600125struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600126 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600127 float mvp[4][4];
128 float position[12*3][4];
129 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600130};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600131
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600132//--------------------------------------------------------------------------------------
133// Mesh and VertexFormat Data
134//--------------------------------------------------------------------------------------
135struct Vertex
136{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600137 float posX, posY, posZ, posW; // Position data
138 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600139};
140
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600141struct VertexPosTex
142{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600143 float posX, posY, posZ, posW; // Position data
144 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600145};
146
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600147#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600148#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600149
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600150static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800151 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152 -1.0f,-1.0f, 1.0f,
153 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800154 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600155 -1.0f, 1.0f,-1.0f,
156 -1.0f,-1.0f,-1.0f,
157
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 1.0f, 1.0f,-1.0f,
160 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800161 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600163 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600167 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800168 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600170 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 -1.0f, 1.0f, 1.0f,
174 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800175 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600177 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f, 1.0f, 1.0f,
181 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800182 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183 1.0f,-1.0f,-1.0f,
184 1.0f, 1.0f,-1.0f,
185
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600188 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800189 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190 1.0f,-1.0f, 1.0f,
191 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600192};
193
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600194static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 0.0f, 0.0f, // -X side
196 1.0f, 0.0f,
197 1.0f, 1.0f,
198 1.0f, 1.0f,
199 0.0f, 1.0f,
200 0.0f, 0.0f,
201
202 1.0f, 0.0f, // -Z side
203 0.0f, 1.0f,
204 0.0f, 0.0f,
205 1.0f, 0.0f,
206 1.0f, 1.0f,
207 0.0f, 1.0f,
208
209 1.0f, 1.0f, // -Y side
210 1.0f, 0.0f,
211 0.0f, 0.0f,
212 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600213 0.0f, 0.0f,
214 0.0f, 1.0f,
215
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600217 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800218 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600219 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600221 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 1.0f, 1.0f, // +X side
224 0.0f, 1.0f,
225 0.0f, 0.0f,
226 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600228 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600229
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800230 0.0f, 1.0f, // +Z side
231 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600233 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600235 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600236};
237
238void dumpMatrix(const char *note, mat4x4 MVP)
239{
240 int i;
241
242 printf("%s: \n", note);
243 for (i=0; i<4; i++) {
244 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
245 }
246 printf("\n");
247 fflush(stdout);
248}
249
250void dumpVec4(const char *note, vec4 vector)
251{
252 printf("%s: \n", note);
253 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
254 printf("\n");
255 fflush(stdout);
256}
257
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600258VkBool32 dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600259 VkFlags msgFlags,
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700260 VkDebugReportObjectTypeLUNARG objType,
Tony Barbour155cce82015-07-03 10:33:54 -0600261 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600262 size_t location,
263 int32_t msgCode,
264 const char* pLayerPrefix,
265 const char* pMsg,
Courtney Goeltzenleuchter04ae89b2015-11-30 11:52:06 -0700266 const void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600267{
268 char *message = (char *) malloc(strlen(pMsg)+100);
269
270 assert (message);
271
Courtney Goeltzenleuchter15735822015-11-25 10:30:56 -0700272 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600273 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Courtney Goeltzenleuchter15735822015-11-25 10:30:56 -0700274 } else if (msgFlags & VK_DEBUG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600275 // We know that we're submitting queues without fences, ignore this warning
276 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600277 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600278 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600279 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600280 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600281 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600282 }
283
284#ifdef _WIN32
285 MessageBox(NULL, message, "Alert", MB_OK);
286#else
287 printf("%s\n",message);
288 fflush(stdout);
289#endif
290 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600291
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600292 /*
293 * false indicates that layer should not bail-out of an
294 * API call that had validation failures. This may mean that the
295 * app dies inside the driver due to invalid parameter(s).
296 * That's what would happen without validation layers, so we'll
297 * keep that behavior here.
298 */
299 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600300}
301
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700302VkBool32 BreakCallback(
303 VkFlags msgFlags,
304 VkDebugReportObjectTypeLUNARG objType,
305 uint64_t srcObject,
306 size_t location,
307 int32_t msgCode,
308 const char* pLayerPrefix,
309 const char* pMsg,
310 const void* pUserData)
311{
312#ifndef WIN32
313 raise(SIGTRAP);
314#else
315 DebugBreak();
316#endif
317
318 return false;
319}
320
Ian Elliotta0781972015-08-21 15:09:33 -0600321typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600322 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800323 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600324 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600325} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600326
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600328#ifdef _WIN32
329#define APP_NAME_STR_LEN 80
330 HINSTANCE connection; // hInstance - Windows Instance
331 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
332 HWND window; // hWnd - window handle
333#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334 xcb_connection_t *connection;
335 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800336 xcb_window_t window;
337 xcb_intern_atom_reply_t *atom_wm_delete_window;
Tony Barbour6839bec2015-07-13 16:37:21 -0600338#endif // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -0700339 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600340 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700341 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600342
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600343 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600344 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600345 VkDevice device;
346 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700347 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600348 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600349 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600350 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
352 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600353 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600354 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355
Ian Elliotta0781972015-08-21 15:09:33 -0600356 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
Ian Elliottf4a4e442015-11-17 17:29:40 -0700357 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
358 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
359 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600360 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
361 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
362 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
363 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
364 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600365 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600366 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600367 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600368
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800369 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370
371 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600372 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600374 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800375 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500376 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600377 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600378 } depth;
379
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600380 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600381
382 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600383 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800384 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500385 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600386 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600387 } uniform_data;
388
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800389 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500390 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600391 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600392 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800393 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600394 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600396 mat4x4 projection_matrix;
397 mat4x4 view_matrix;
398 mat4x4 model_matrix;
399
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600400 float spin_angle;
401 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600402 bool pause;
403
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600404 VkShaderModule vert_shader_module;
405 VkShaderModule frag_shader_module;
406
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 VkDescriptorPool desc_pool;
408 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800409
Tony Barbourd8e504f2015-10-08 14:26:24 -0600410 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800411
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600412 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600413 int32_t curFrame;
414 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600415 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600416 bool use_break;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -0700417 PFN_vkCreateDebugReportCallbackLUNARG CreateDebugReportCallback;
418 PFN_vkDestroyDebugReportCallbackLUNARG DestroyDebugReportCallback;
Courtney Goeltzenleuchterdd0e6172015-11-25 14:07:05 -0700419 VkDebugReportCallbackLUNARG msg_callback;
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700420 PFN_vkDebugReportMessageLUNARG DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600421
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600422 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600423 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600424};
425
Ian Elliottcf074d32015-10-16 18:02:43 -0600426// Forward declaration:
427static void demo_resize(struct demo *demo);
428
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600429static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600430{
431 // Search memtypes to find first index with those properties
432 for (uint32_t i = 0; i < 32; i++) {
433 if ((typeBits & 1) == 1) {
434 // Type is available, does it match user properties?
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600435 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600436 *typeIndex = i;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600437 return true;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600438 }
439 }
440 typeBits >>= 1;
441 }
442 // No memory types matched, return failure
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600443 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600444}
445
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600446static void demo_flush_init_cmd(struct demo *demo)
447{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600448 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600449
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600450 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451 return;
452
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600453 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454 assert(!err);
455
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800456 const VkCommandBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800457 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600458 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800459 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
460 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800461 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600462 .pWaitSemaphores = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800463 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600464 .pCommandBuffers = cmd_bufs,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800465 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600466 .pSignalSemaphores = NULL
467 };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600469 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600470 assert(!err);
471
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600472 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473 assert(!err);
474
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600475 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600476 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477}
478
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600479static void demo_set_image_layout(
480 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600481 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600482 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600483 VkImageLayout old_image_layout,
484 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600485{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600486 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600488 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800489 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800490 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800492 .commandPool = demo->cmd_pool,
493 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800494 .bufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 };
496
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800497 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498 assert(!err);
499
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800500 VkCommandBufferBeginInfo cmd_buf_info = {
501 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600503 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800504 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600505 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800506 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800507 .occlusionQueryEnable = VK_FALSE,
508 .queryFlags = 0,
509 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600510 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600511 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600512 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600513 }
514
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600515 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600516 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600517 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800518 .srcAccessMask = 0,
519 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600520 .oldLayout = old_image_layout,
521 .newLayout = new_image_layout,
522 .image = image,
Jeremy Hayes105ca502015-11-17 18:19:55 -0700523 .subresourceRange = { aspectMask, 0, 1, 0, 1 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600524 };
525
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800526 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600527 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800528 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 }
530
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700531 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Mark Lobodzinskid51e5a92015-12-03 15:42:32 -0700532 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700533 }
534
535 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Mark Lobodzinskid51e5a92015-12-03 15:42:32 -0700536 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700537 }
538
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600539 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600540 /* Make sure any Copy or CPU writes to image are flushed */
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700541 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600542 }
543
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600544 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600545
Tony Barbour50bbca42015-06-29 16:20:35 -0600546 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
547 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600548
Chia-I Wu86365072015-10-26 17:08:33 +0800549 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600550}
551
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800552static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553{
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800554 const VkCommandBufferBeginInfo cmd_buf_info = {
555 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600556 .pNext = NULL,
Courtney Goeltzenleuchter62534c12015-10-21 18:11:04 -0600557 .flags = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800558 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600559 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800560 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800561 .occlusionQueryEnable = VK_FALSE,
562 .queryFlags = 0,
563 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700564 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800565 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600566 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
567 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800568 };
569 const VkRenderPassBeginInfo rp_begin = {
570 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
571 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800572 .renderPass = demo->render_pass,
573 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800574 .renderArea.offset.x = 0,
575 .renderArea.offset.y = 0,
576 .renderArea.extent.width = demo->width,
577 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600578 .clearValueCount = 2,
579 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700580 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800581 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600582
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600583 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600584 assert(!err);
585
Chia-I Wuf051d262015-10-27 19:25:11 +0800586 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800587
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600588 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600589 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600590 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600591 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600592
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600593 VkViewport viewport;
594 memset(&viewport, 0, sizeof(viewport));
595 viewport.height = (float) demo->height;
596 viewport.width = (float) demo->width;
597 viewport.minDepth = (float) 0.0f;
598 viewport.maxDepth = (float) 1.0f;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600599 vkCmdSetViewport(cmd_buf, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600600
601 VkRect2D scissor;
602 memset(&scissor, 0, sizeof(scissor));
603 scissor.extent.width = demo->width;
604 scissor.extent.height = demo->height;
605 scissor.offset.x = 0;
606 scissor.offset.y = 0;
Courtney Goeltzenleuchterd6217bc2015-09-21 11:44:06 -0600607 vkCmdSetScissor(cmd_buf, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600608
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600609 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800610 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600611
Tony Barbour15a4ef62015-10-21 10:14:48 -0600612 VkImageMemoryBarrier prePresentBarrier = {
613 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
614 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800615 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
616 .dstAccessMask = 0,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600617 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700618 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600619 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800620 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600621 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
622 };
623
624 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
625 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Chia-I Wu082a6202015-10-31 00:31:16 +0800626 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Chia-I Wu86365072015-10-26 17:08:33 +0800627 0, 1, (const void * const*)&pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600628
629
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600630 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600631 assert(!err);
632}
633
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600634
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600635void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600636{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600637 mat4x4 MVP, Model, VP;
638 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600639 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600640 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600641
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600642 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600644 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600645 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700646 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600647 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600648
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200649 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 -0600650 assert(!err);
651
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600652 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600654 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655}
656
657static void demo_draw(struct demo *demo)
658{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600659 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600660 VkSemaphore presentCompleteSemaphore;
661 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
662 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
663 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600664 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600665 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800666 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600667
Ian Elliottaaae5352015-07-06 14:27:58 -0600668 err = vkCreateSemaphore(demo->device,
669 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +0800670 NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600671 &presentCompleteSemaphore);
672 assert(!err);
673
674 // Get the index of the next available swapchain image:
Ian Elliottcf074d32015-10-16 18:02:43 -0600675 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600676 UINT64_MAX,
677 presentCompleteSemaphore,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700678 NULL,// TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600679 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600680 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
681 // demo->swapchain is out of date (e.g. the window was resized) and
682 // must be recreated:
683 demo_resize(demo);
684 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800685 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600686 return;
687 } else if (err == VK_SUBOPTIMAL_KHR) {
688 // demo->swapchain is not as optimal as it could be, but the platform's
689 // presentation engine will still present the image correctly.
690 } else {
691 assert(!err);
692 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600693
Tony Barbour15a4ef62015-10-21 10:14:48 -0600694 // Assume the command buffer has been run on current_buffer before so
695 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
696 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
697 VK_IMAGE_ASPECT_COLOR_BIT,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700698 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600699 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
700 demo_flush_init_cmd(demo);
701
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600702 // Wait for the present complete semaphore to be signaled to ensure
703 // that the image won't be rendered to until the presentation
704 // engine has fully released ownership to the application, and it is
705 // okay to render to the image.
706
Ian Elliottf4a4e442015-11-17 17:29:40 -0700707// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600708 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800709 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
710 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800711 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600712 .pWaitSemaphores = &presentCompleteSemaphore,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800713 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600714 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800715 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600716 .pSignalSemaphores = NULL
717 };
718
719 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600720 assert(!err);
721
Ian Elliotta0781972015-08-21 15:09:33 -0600722 VkPresentInfoKHR present = {
723 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600724 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600725 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700726 .pSwapchains = &demo->swapchain,
727 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600728 };
729
730// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600731 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600732 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
733 // demo->swapchain is out of date (e.g. the window was resized) and
734 // must be recreated:
735 demo_resize(demo);
736 } else if (err == VK_SUBOPTIMAL_KHR) {
737 // demo->swapchain is not as optimal as it could be, but the platform's
738 // presentation engine will still present the image correctly.
739 } else {
740 assert(!err);
741 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600742
Chia-I Wucbb564e2015-04-16 22:02:10 +0800743 err = vkQueueWaitIdle(demo->queue);
744 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600745
Chia-I Wu63636062015-10-26 21:10:41 +0800746 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600747}
748
749static void demo_prepare_buffers(struct demo *demo)
750{
Ian Elliottaaae5352015-07-06 14:27:58 -0600751 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600752 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600753
Ian Elliottb08e0d92015-11-20 11:55:46 -0700754 // Check the surface capabilities and formats
755 VkSurfaceCapabilitiesKHR surfCapabilities;
756 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu,
757 demo->surface,
758 &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600759 assert(!err);
760
Ian Elliott8528eff2015-08-07 15:56:59 -0600761 uint32_t presentModeCount;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700762 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu,
763 demo->surface,
Ian Elliott8528eff2015-08-07 15:56:59 -0600764 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600765 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600766 VkPresentModeKHR *presentModes =
767 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600768 assert(presentModes);
Ian Elliottb08e0d92015-11-20 11:55:46 -0700769 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu,
770 demo->surface,
Ian Elliott8528eff2015-08-07 15:56:59 -0600771 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600772 assert(!err);
773
Ian Elliotta0781972015-08-21 15:09:33 -0600774 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600775 // width and height are either both -1, or both not -1.
Ian Elliottb08e0d92015-11-20 11:55:46 -0700776 if (surfCapabilities.currentExtent.width == -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600777 {
778 // If the surface size is undefined, the size is set to
779 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600780 swapchainExtent.width = demo->width;
781 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600782 }
783 else
784 {
785 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700786 swapchainExtent = surfCapabilities.currentExtent;
787 demo->width = surfCapabilities.currentExtent.width;
788 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600789 }
790
791 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600792 // tearing mode. If not, try IMMEDIATE which will usually be available,
793 // and is fastest (though it tears). If not, fall back to FIFO which is
794 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600795 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600796 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600797 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
798 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600799 break;
800 }
Ian Elliotta0781972015-08-21 15:09:33 -0600801 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
802 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
803 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600804 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600805 }
806
807 // Determine the number of VkImage's to use in the swap chain (we desire to
808 // own only 1 image at a time, besides the images being displayed and
809 // queued for display):
Ian Elliottb08e0d92015-11-20 11:55:46 -0700810 uint32_t desiredNumberOfSwapchainImages = surfCapabilities.minImageCount + 1;
811 if ((surfCapabilities.maxImageCount > 0) &&
812 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600813 {
814 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700815 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600816 }
817
Tony Barbour9fd6d352015-09-16 15:01:32 -0600818 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700819 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) {
820 preTransform = VK_SURFACE_TRANSFORM_NONE_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600821 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700822 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600823 }
824
Ian Elliottcf074d32015-10-16 18:02:43 -0600825 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600826 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800827 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700828 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600829 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800830 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600831 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800832 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600833 .width = swapchainExtent.width,
834 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600835 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700836 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600837 .preTransform = preTransform,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700838 .imageArrayLayers = 1,
839 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
840 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600841 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600842 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600843 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600844 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600845 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600846 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600847
Ian Elliottb08e0d92015-11-20 11:55:46 -0700848 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800849 assert(!err);
850
Ian Elliottcf074d32015-10-16 18:02:43 -0600851 // If we just re-created an existing swapchain, we should destroy the old
852 // swapchain at this point.
853 // Note: destroying the swapchain also cleans up all its associated
854 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800855 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700856 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600857 }
858
859 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600860 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600861 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800862
Ian Elliotta0781972015-08-21 15:09:33 -0600863 VkImage* swapchainImages =
864 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
865 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600866 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600867 &demo->swapchainImageCount,
868 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600869 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600870
Ian Elliotta0781972015-08-21 15:09:33 -0600871 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600872 assert(demo->buffers);
873
Ian Elliotta0781972015-08-21 15:09:33 -0600874 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600875 VkImageViewCreateInfo color_image_view = {
876 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600877 .pNext = NULL,
878 .format = demo->format,
Chia-I Wub6d30c62015-10-27 19:00:15 +0800879 .components = {
Chia-I Wuf051d262015-10-27 19:25:11 +0800880 .r = VK_COMPONENT_SWIZZLE_R,
881 .g = VK_COMPONENT_SWIZZLE_G,
882 .b = VK_COMPONENT_SWIZZLE_B,
883 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600884 },
885 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600886 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600887 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800888 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600889 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800890 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600891 },
892 .viewType = VK_IMAGE_VIEW_TYPE_2D,
893 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600894 };
895
Ian Elliotta0781972015-08-21 15:09:33 -0600896 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600897
Ian Elliottf4a4e442015-11-17 17:29:40 -0700898 // 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 -0600899 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600900 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -0600901 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600902 VK_IMAGE_LAYOUT_UNDEFINED,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700903 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600904
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600905 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600906
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600907 err = vkCreateImageView(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +0800908 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600909 assert(!err);
910 }
911}
912
913static void demo_prepare_depth(struct demo *demo)
914{
Tony Barbour72304ef2015-04-16 15:59:00 -0600915 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600916 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600917 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600918 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600919 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600920 .format = depth_format,
921 .extent = { demo->width, demo->height, 1 },
922 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600923 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800924 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600925 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600926 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600927 .flags = 0,
928 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200929
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600930 VkImageViewCreateInfo view = {
931 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600932 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800933 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600934 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600935 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600936 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600937 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800938 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600939 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800940 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600941 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600942 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600943 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600944 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600945
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500946 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600947 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600948 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600949
950 demo->depth.format = depth_format;
951
952 /* create image */
Chia-I Wu63636062015-10-26 21:10:41 +0800953 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600954 &demo->depth.image);
955 assert(!err);
956
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -0600957 vkGetImageMemoryRequirements(demo->device,
Tony Barbour155cce82015-07-03 10:33:54 -0600958 demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600959 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600960
Chia-I Wuf48700b2015-11-10 16:21:09 +0800961 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200962 demo->depth.mem_alloc.pNext = NULL;
963 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
964 demo->depth.mem_alloc.memoryTypeIndex = 0;
965
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600966 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600967 mem_reqs.memoryTypeBits,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600968 0, /* No requirements */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200969 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600970 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600971
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500972 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800973 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +0800974 NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500975 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600976
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500977 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600978 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500979 demo->depth.mem, 0);
980 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600981
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600982 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -0600983 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600984 VK_IMAGE_LAYOUT_UNDEFINED,
985 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600986
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600987 /* create image view */
988 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800989 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600991}
992
Tony Barbour1a86d032015-09-21 15:17:33 -0600993/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700994bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600995 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600996 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600997{
Tony Barbour1a86d032015-09-21 15:17:33 -0600998 FILE *fPtr = fopen(filename,"rb");
999 char header[256], *cPtr;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001000
Tony Barbour1a86d032015-09-21 15:17:33 -06001001 if (!fPtr)
1002 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001003
Tony Barbour1a86d032015-09-21 15:17:33 -06001004 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001005 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1006 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001007 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001008 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001009
Tony Barbour1a86d032015-09-21 15:17:33 -06001010 do {
1011 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001012 if (cPtr == NULL) {
1013 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001014 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001015 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001016 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001017
Tony Barbour1a86d032015-09-21 15:17:33 -06001018 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001019 if (rgba_data == NULL) {
1020 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001021 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001022 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001023 fgets(header, 256, fPtr); // Format
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001024 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1025 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001026 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001027 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001028
Tony Barbour1a86d032015-09-21 15:17:33 -06001029 for(int y = 0; y < *height; y++)
1030 {
1031 uint8_t *rowPtr = rgba_data;
1032 for(int x = 0; x < *width; x++)
1033 {
1034 fread(rowPtr, 3, 1, fPtr);
1035 rowPtr[3] = 255; /* Alpha of 1 */
1036 rowPtr += 4;
1037 }
1038 rgba_data += layout->rowPitch;
1039 }
1040 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001041 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001042}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001043
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001044static void demo_prepare_texture_image(struct demo *demo,
1045 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001046 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001047 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001048 VkImageUsageFlags usage,
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001049 VkFlags required_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001050{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001051 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001052 int32_t tex_width;
1053 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001054 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001055 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001056
David Pinedo30bd71d2015-04-23 08:16:57 -06001057 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1058 {
1059 printf("Failed to load textures\n");
1060 fflush(stdout);
1061 exit(1);
1062 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001063
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001064 tex_obj->tex_width = tex_width;
1065 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001067 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001068 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001070 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071 .format = tex_format,
1072 .extent = { tex_width, tex_height, 1 },
1073 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001074 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001075 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001076 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001077 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078 .flags = 0,
1079 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001081 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082
Chia-I Wu63636062015-10-26 21:10:41 +08001083 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001084 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085 assert(!err);
1086
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001087 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001088
Chia-I Wuf48700b2015-11-10 16:21:09 +08001089 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001090 tex_obj->mem_alloc.pNext = NULL;
1091 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1092 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001093
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001094 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1095 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001096
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001097 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001098 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001099 &(tex_obj->mem));
1100 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001101
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001102 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001103 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001104 tex_obj->mem, 0);
1105 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001106
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001107 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001108 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001109 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001110 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001111 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001112 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001113 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001114 void *data;
1115
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001116 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001118 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119 assert(!err);
1120
1121 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1122 fprintf(stderr, "Error loading texture: %s\n", filename);
1123 }
1124
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001125 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001126 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001127
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001128 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001129 demo_set_image_layout(demo, tex_obj->image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001130 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001131 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001132 tex_obj->imageLayout);
1133 /* 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 -07001134}
1135
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001136static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001137{
1138 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001139 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1140 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001141}
1142
1143static void demo_prepare_textures(struct demo *demo)
1144{
Tony Barbour72304ef2015-04-16 15:59:00 -06001145 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001146 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001147 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001148
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001149 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001150
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001151 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001152 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001153
FslNopper6a7e50e2015-05-06 21:42:01 +02001154 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155 /* Device can texture using linear textures */
1156 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001157 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001158 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001159 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001160 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001161
1162 memset(&staging_texture, 0, sizeof(staging_texture));
1163 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001164 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001165
1166 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001167 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001168 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wuc42afd72015-10-27 18:53:22 +08001169 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001170
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001171 demo_set_image_layout(demo, staging_texture.image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001172 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001173 staging_texture.imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001174 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001175
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001176 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001177 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001178 demo->textures[i].imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001179 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001180
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001181 VkImageCopy copy_region = {
Tony Barbour466f0012015-11-02 11:47:51 -07001182 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001183 .srcOffset = { 0, 0, 0 },
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001184 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
1185 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001186 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1187 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001188 vkCmdCopyImage(demo->cmd,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001189 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1190 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001191 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001192
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001193 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001194 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001195 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001196 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001197
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001198 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001199
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001200 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001201 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001202 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1203 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001204 }
1205
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001206 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001207 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001208 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001209 .magFilter = VK_FILTER_NEAREST,
1210 .minFilter = VK_FILTER_NEAREST,
1211 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_BASE,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001212 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1213 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1214 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001215 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001216 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001218 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001219 .minLod = 0.0f,
1220 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001221 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001222 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001223 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001224
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001225 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001226 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001227 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001228 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001229 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001230 .format = tex_format,
Chia-I Wub6d30c62015-10-27 19:00:15 +08001231 .components = { VK_COMPONENT_SWIZZLE_R,
Chia-I Wuf051d262015-10-27 19:25:11 +08001232 VK_COMPONENT_SWIZZLE_G,
1233 VK_COMPONENT_SWIZZLE_B,
1234 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001235 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001236 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001237 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001238
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001239 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001240 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241 &demo->textures[i].sampler);
1242 assert(!err);
1243
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001244 /* create image view */
1245 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001246 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247 &demo->textures[i].view);
1248 assert(!err);
1249 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001250}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001251
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001252void demo_prepare_cube_data_buffer(struct demo *demo)
1253{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001254 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001255 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001256 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001257 int i;
1258 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001259 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001260 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001261 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001262
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001263 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001264 mat4x4_mul(MVP, VP, demo->model_matrix);
1265 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001266// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001267
1268 for (i=0; i<12*3; i++) {
1269 data.position[i][0] = g_vertex_buffer_data[i*3];
1270 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1271 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1272 data.position[i][3] = 1.0f;
1273 data.attr[i][0] = g_uv_buffer_data[2*i];
1274 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1275 data.attr[i][2] = 0;
1276 data.attr[i][3] = 0;
1277 }
1278
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001279 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001280 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001281 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001282 buf_info.size = sizeof(data);
Chia-I Wu63636062015-10-26 21:10:41 +08001283 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1284 &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001285 assert(!err);
1286
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001287 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001288
Chia-I Wuf48700b2015-11-10 16:21:09 +08001289 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001290 demo->uniform_data.mem_alloc.pNext = NULL;
1291 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1292 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1293
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001294 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001295 mem_reqs.memoryTypeBits,
1296 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001297 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001298 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001299
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001300 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +08001301 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001302 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001303
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001304 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 -05001305 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001306
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001307 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001308
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001309 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001310
Tony Barbour155cce82015-07-03 10:33:54 -06001311 err = vkBindBufferMemory(demo->device,
1312 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001313 demo->uniform_data.mem, 0);
1314 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001315
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001316 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1317 demo->uniform_data.buffer_info.offset = 0;
1318 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001319}
1320
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001321static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001322{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001323 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001324 [0] = {
Chia-I Wued577562015-10-31 00:31:16 +08001325 .binding = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001326 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001327 .descriptorCount = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001328 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001329 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001330 },
1331 [1] = {
Chia-I Wued577562015-10-31 00:31:16 +08001332 .binding = 1,
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001333 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001334 .descriptorCount = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001335 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001336 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001337 },
1338 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001339 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001340 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001341 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001342 .bindingCount = 2,
Chia-I Wu0e76e3f2015-10-31 00:31:16 +08001343 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001344 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001345 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001347 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001348 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001349 assert(!err);
1350
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001351 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1352 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1353 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001354 .setLayoutCount = 1,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001355 .pSetLayouts = &demo->desc_layout,
1356 };
1357
1358 err = vkCreatePipelineLayout(demo->device,
1359 &pPipelineLayoutCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +08001360 NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001361 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001363}
1364
Chia-I Wuf973e322015-07-08 13:34:24 +08001365static void demo_prepare_render_pass(struct demo *demo)
1366{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001367 const VkAttachmentDescription attachments[2] = {
1368 [0] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001369 .format = demo->format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001370 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001371 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1372 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1373 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1374 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1375 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1376 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1377 },
1378 [1] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001379 .format = demo->depth.format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001380 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001381 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1382 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1383 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1384 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1385 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1386 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1387 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001388 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001389 const VkAttachmentReference color_reference = {
1390 .attachment = 0,
1391 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1392 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001393 const VkAttachmentReference depth_reference = {
1394 .attachment = 1,
1395 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1396 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001397 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001398 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1399 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001400 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001401 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001402 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001403 .pColorAttachments = &color_reference,
1404 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001405 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001406 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001407 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001408 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001409 const VkRenderPassCreateInfo rp_info = {
1410 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1411 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001412 .attachmentCount = 2,
1413 .pAttachments = attachments,
1414 .subpassCount = 1,
1415 .pSubpasses = &subpass,
1416 .dependencyCount = 0,
1417 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001418 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001419 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001420
Chia-I Wu63636062015-10-26 21:10:41 +08001421 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001422 assert(!err);
1423}
1424
Chia-I Wu46176f12015-10-31 00:31:16 +08001425static VkShaderModule demo_prepare_shader_module(struct demo* demo,
Chia-I Wu46176f12015-10-31 00:31:16 +08001426 const void* code,
1427 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428{
Chia-I Wu46176f12015-10-31 00:31:16 +08001429 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001430 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001431 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001433 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1434 moduleCreateInfo.pNext = NULL;
1435
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001436 moduleCreateInfo.codeSize = size;
1437 moduleCreateInfo.pCode = code;
1438 moduleCreateInfo.flags = 0;
1439 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1440 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001441
1442 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443}
1444
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001445char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001446{
1447 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001448 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001449 void *shader_code;
1450
1451 FILE *fp = fopen(filename, "rb");
1452 if (!fp) return NULL;
1453
1454 fseek(fp, 0L, SEEK_END);
1455 size = ftell(fp);
1456
1457 fseek(fp, 0L, SEEK_SET);
1458
1459 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001460 retval = fread(shader_code, size, 1, fp);
1461 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001462
1463 *psize = size;
1464
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001465 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001466 return shader_code;
1467}
1468
Chia-I Wu46176f12015-10-31 00:31:16 +08001469static VkShaderModule demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001470{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001471 void *vertShaderCode;
1472 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001473
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001474 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1475
Tony Barbourf331eb32015-11-06 10:10:37 -07001476 demo->vert_shader_module = demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001477
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001478 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001479
1480 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001481}
1482
Chia-I Wu46176f12015-10-31 00:31:16 +08001483static VkShaderModule demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001484{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001485 void *fragShaderCode;
1486 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001487
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001488 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001489
Tony Barbourf331eb32015-11-06 10:10:37 -07001490 demo->frag_shader_module = demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001491
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001492 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001493
1494 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001495}
1496
1497static void demo_prepare_pipeline(struct demo *demo)
1498{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001499 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001500 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001501 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001502 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001503 VkPipelineRasterizationStateCreateInfo rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001504 VkPipelineColorBlendStateCreateInfo cb;
1505 VkPipelineDepthStencilStateCreateInfo ds;
1506 VkPipelineViewportStateCreateInfo vp;
1507 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001508 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniellba839d12015-09-29 13:01:09 -06001509 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001510 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001511
Piers Daniellba839d12015-09-29 13:01:09 -06001512 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1513 memset(&dynamicState, 0, sizeof dynamicState);
1514 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1515 dynamicState.pDynamicStates = dynamicStateEnables;
1516
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001517 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001518 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001519 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001521 memset(&vi, 0, sizeof(vi));
1522 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1523
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001524 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001525 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001526 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001527
1528 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001529 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1530 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001531 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001532 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001533 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001534 rs.rasterizerDiscardEnable = VK_FALSE;
1535 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001536
1537 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001538 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1539 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001540 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001541 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001542 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001543 cb.attachmentCount = 1;
1544 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001545
Tony Barbour29645d02015-01-16 14:27:35 -07001546 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001547 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001548 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001549 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1550 vp.scissorCount = 1;
1551 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001552
1553 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001554 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001555 ds.depthTestEnable = VK_TRUE;
1556 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001557 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001558 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001559 ds.back.failOp = VK_STENCIL_OP_KEEP;
1560 ds.back.passOp = VK_STENCIL_OP_KEEP;
1561 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001562 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001563 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001564
Tony Barbour29645d02015-01-16 14:27:35 -07001565 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001566 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001567 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001568 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001569
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001570 // Two stages: vs and fs
1571 pipeline.stageCount = 2;
1572 VkPipelineShaderStageCreateInfo shaderStages[2];
1573 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1574
1575 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001576 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1577 shaderStages[0].module = demo_prepare_vs(demo);
1578 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001579
1580 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001581 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1582 shaderStages[1].module = demo_prepare_fs(demo);
1583 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001584
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001585 memset(&pipelineCache, 0, sizeof(pipelineCache));
1586 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001587
Chia-I Wu63636062015-10-26 21:10:41 +08001588 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001589 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001590
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001591 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001592 pipeline.pInputAssemblyState = &ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001593 pipeline.pRasterizationState = &rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001594 pipeline.pColorBlendState = &cb;
1595 pipeline.pMultisampleState = &ms;
1596 pipeline.pViewportState = &vp;
1597 pipeline.pDepthStencilState = &ds;
1598 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001599 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001600 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001601
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001602 pipeline.renderPass = demo->render_pass;
1603
Chia-I Wu63636062015-10-26 21:10:41 +08001604 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1605 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001606 assert(!err);
1607
Chia-I Wu63636062015-10-26 21:10:41 +08001608 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1609 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001610}
1611
Chia-I Wu63ea9262015-03-26 13:14:16 +08001612static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001613{
Chia-I Wuf051d262015-10-27 19:25:11 +08001614 const VkDescriptorPoolSize type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001615 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001616 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001617 .descriptorCount = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001618 },
1619 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001620 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001621 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001622 },
1623 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001624 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001625 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001626 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001627 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001628 .poolSizeCount = 2,
1629 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001630 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001631 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001632
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001633 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001634 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001635 assert(!err);
1636}
1637
1638static void demo_prepare_descriptor_set(struct demo *demo)
1639{
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001640 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001641 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001642 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001643 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001644
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001645 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001646 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001647 .pNext = NULL,
1648 .descriptorPool = demo->desc_pool,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001649 .setLayoutCount = 1,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001650 .pSetLayouts = &demo->desc_layout
1651 };
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001652 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001653 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001654
Chia-I Wuae721ba2015-05-25 16:27:55 +08001655 memset(&tex_descs, 0, sizeof(tex_descs));
1656 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001657 tex_descs[i].sampler = demo->textures[i].sampler;
1658 tex_descs[i].imageView = demo->textures[i].view;
1659 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001660 }
1661
1662 memset(&writes, 0, sizeof(writes));
1663
1664 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001665 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001666 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001667 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001668 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001669
1670 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001671 writes[1].dstSet = demo->desc_set;
1672 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001673 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001674 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001675 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001676
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001677 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001678}
1679
Chia-I Wuf973e322015-07-08 13:34:24 +08001680static void demo_prepare_framebuffers(struct demo *demo)
1681{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001682 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001683 attachments[1] = demo->depth.view;
1684
Chia-I Wuf973e322015-07-08 13:34:24 +08001685 const VkFramebufferCreateInfo fb_info = {
1686 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1687 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001688 .renderPass = demo->render_pass,
1689 .attachmentCount = 2,
1690 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001691 .width = demo->width,
1692 .height = demo->height,
1693 .layers = 1,
1694 };
1695 VkResult U_ASSERT_ONLY err;
1696 uint32_t i;
1697
Tony Barbourd8e504f2015-10-08 14:26:24 -06001698 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1699 assert(demo->framebuffers);
1700
1701 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001702 attachments[0] = demo->buffers[i].view;
Chia-I Wu63636062015-10-26 21:10:41 +08001703 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001704 assert(!err);
1705 }
1706}
1707
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001708static void demo_prepare(struct demo *demo)
1709{
Cody Northrop91e221b2015-07-09 18:08:32 -06001710 VkResult U_ASSERT_ONLY err;
1711
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001712 const VkCommandPoolCreateInfo cmd_pool_info = {
1713 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001714 .pNext = NULL,
1715 .queueFamilyIndex = demo->graphics_queue_node_index,
1716 .flags = 0,
1717 };
Chia-I Wu63636062015-10-26 21:10:41 +08001718 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001719 assert(!err);
1720
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001721 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001722 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001723 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001724 .commandPool = demo->cmd_pool,
1725 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001726 .bufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001728
1729 demo_prepare_buffers(demo);
1730 demo_prepare_depth(demo);
1731 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001732 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001734 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001735 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001736 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001737
Ian Elliotta0781972015-08-21 15:09:33 -06001738 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001739 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001740 assert(!err);
1741 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001742
Chia-I Wu63ea9262015-03-26 13:14:16 +08001743 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001744 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001745
Chia-I Wuf973e322015-07-08 13:34:24 +08001746 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001747
Ian Elliotta0781972015-08-21 15:09:33 -06001748 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001749 demo->current_buffer = i;
1750 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1751 }
1752
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001753 /*
1754 * Prepare functions above may generate pipeline commands
1755 * that need to be flushed before beginning the render loop.
1756 */
1757 demo_flush_init_cmd(demo);
1758
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001759 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001760 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001761}
1762
David Pinedo2bb7c932015-06-18 17:03:14 -06001763static void demo_cleanup(struct demo *demo)
1764{
1765 uint32_t i;
1766
1767 demo->prepared = false;
1768
Tony Barbourd8e504f2015-10-08 14:26:24 -06001769 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001770 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001771 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001772 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001773 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001774
Chia-I Wu63636062015-10-26 21:10:41 +08001775 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1776 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1777 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1778 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1779 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001780
1781 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001782 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1783 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1784 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1785 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001786 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001787 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001788
Chia-I Wu63636062015-10-26 21:10:41 +08001789 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1790 vkDestroyImage(demo->device, demo->depth.image, NULL);
1791 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001792
Chia-I Wu63636062015-10-26 21:10:41 +08001793 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1794 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001795
Ian Elliotta0781972015-08-21 15:09:33 -06001796 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001797 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001798 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001799 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001800 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001801
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001802 free(demo->queue_props);
1803
Chia-I Wu63636062015-10-26 21:10:41 +08001804 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1805 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001806 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001807 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001808 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001809 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001810 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001811
1812#ifndef _WIN32
1813 xcb_destroy_window(demo->connection, demo->window);
1814 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001815 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001816#endif // _WIN32
1817}
1818
Ian Elliottcf074d32015-10-16 18:02:43 -06001819static void demo_resize(struct demo *demo)
1820{
1821 uint32_t i;
1822
Mike Stroyanbb60b382015-10-28 09:46:57 -06001823 // Don't react to resize until after first initialization.
1824 if (!demo->prepared) {
1825 return;
1826 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001827 // In order to properly resize the window, we must re-create the swapchain
1828 // AND redo the command buffers, etc.
1829 //
1830 // First, perform part of the demo_cleanup() function:
1831 demo->prepared = false;
1832
1833 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001834 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001835 }
1836 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001837 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001838
Chia-I Wu63636062015-10-26 21:10:41 +08001839 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1840 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1841 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1842 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1843 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001844
1845 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001846 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1847 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1848 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1849 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001850 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001851
Chia-I Wu63636062015-10-26 21:10:41 +08001852 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1853 vkDestroyImage(demo->device, demo->depth.image, NULL);
1854 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001855
Chia-I Wu63636062015-10-26 21:10:41 +08001856 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1857 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001858
1859 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001860 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001861 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001862 }
Chia-I Wu63636062015-10-26 21:10:41 +08001863 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001864 free(demo->buffers);
1865
Ian Elliottcf074d32015-10-16 18:02:43 -06001866 // Second, re-perform the demo_prepare() function, which will re-create the
1867 // swapchain:
1868 demo_prepare(demo);
1869}
1870
David Pinedo2bb7c932015-06-18 17:03:14 -06001871// On MS-Windows, make this a global, so it's available to WndProc()
1872struct demo demo;
1873
Ian Elliott639ca472015-04-16 15:23:05 -06001874#ifdef _WIN32
1875static void demo_run(struct demo *demo)
1876{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001877 if (!demo->prepared)
1878 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001879 // Wait for work to finish before updating MVP.
1880 vkDeviceWaitIdle(demo->device);
1881 demo_update_data_buffer(demo);
1882
1883 demo_draw(demo);
1884
1885 // Wait for work to finish before updating MVP.
1886 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001887
David Pinedo2bb7c932015-06-18 17:03:14 -06001888 demo->curFrame++;
1889
1890 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1891 {
1892 demo->quit=true;
1893 demo_cleanup(demo);
1894 ExitProcess(0);
1895 }
1896
1897}
Ian Elliott639ca472015-04-16 15:23:05 -06001898
1899// MS-Windows event handling function:
1900LRESULT CALLBACK WndProc(HWND hWnd,
1901 UINT uMsg,
1902 WPARAM wParam,
1903 LPARAM lParam)
1904{
Ian Elliott639ca472015-04-16 15:23:05 -06001905 switch(uMsg)
1906 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001907 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001908 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001909 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001910 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001911 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001912 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001913 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001914 demo.width = lParam & 0xffff;
1915 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001916 demo_resize(&demo);
1917 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001918 default:
1919 break;
1920 }
1921 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1922}
1923
1924static void demo_create_window(struct demo *demo)
1925{
1926 WNDCLASSEX win_class;
1927
1928 // Initialize the window class structure:
1929 win_class.cbSize = sizeof(WNDCLASSEX);
1930 win_class.style = CS_HREDRAW | CS_VREDRAW;
1931 win_class.lpfnWndProc = WndProc;
1932 win_class.cbClsExtra = 0;
1933 win_class.cbWndExtra = 0;
1934 win_class.hInstance = demo->connection; // hInstance
1935 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1936 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1937 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1938 win_class.lpszMenuName = NULL;
1939 win_class.lpszClassName = demo->name;
1940 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1941 // Register window class:
1942 if (!RegisterClassEx(&win_class)) {
1943 // It didn't work, so try to give a useful error:
1944 printf("Unexpected error trying to start the application!\n");
1945 fflush(stdout);
1946 exit(1);
1947 }
1948 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001949 RECT wr = { 0, 0, demo->width, demo->height };
1950 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001951 demo->window = CreateWindowEx(0,
1952 demo->name, // class name
1953 demo->name, // app name
1954 WS_OVERLAPPEDWINDOW | // window style
1955 WS_VISIBLE |
1956 WS_SYSMENU,
1957 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001958 wr.right-wr.left, // width
1959 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001960 NULL, // handle to parent
1961 NULL, // handle to menu
1962 demo->connection, // hInstance
1963 NULL); // no extra parameters
1964 if (!demo->window) {
1965 // It didn't work, so try to give a useful error:
1966 printf("Cannot create a window in which to draw!\n");
1967 fflush(stdout);
1968 exit(1);
1969 }
1970}
1971#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001972static void demo_handle_event(struct demo *demo,
1973 const xcb_generic_event_t *event)
1974{
Piers Daniell735ee532015-02-23 16:23:13 -07001975 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001976 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001977 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001978 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001979 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001980 case XCB_CLIENT_MESSAGE:
1981 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1982 (*demo->atom_wm_delete_window).atom) {
1983 demo->quit = true;
1984 }
1985 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001986 case XCB_KEY_RELEASE:
1987 {
1988 const xcb_key_release_event_t *key =
1989 (const xcb_key_release_event_t *) event;
1990
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001991 switch (key->detail) {
1992 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001993 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001994 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001995 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001996 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001997 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001998 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001999 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002000 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002001 case 0x41:
2002 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002003 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002004 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002005 }
2006 break;
Ian Elliottcf074d32015-10-16 18:02:43 -06002007 case XCB_CONFIGURE_NOTIFY:
2008 {
2009 const xcb_configure_notify_event_t *cfg =
2010 (const xcb_configure_notify_event_t *) event;
2011 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2012 demo_resize(demo);
2013 }
2014 }
2015 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002016 default:
2017 break;
2018 }
2019}
2020
2021static void demo_run(struct demo *demo)
2022{
2023 xcb_flush(demo->connection);
2024
2025 while (!demo->quit) {
2026 xcb_generic_event_t *event;
2027
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002028 if (demo->pause) {
2029 event = xcb_wait_for_event(demo->connection);
2030 } else {
2031 event = xcb_poll_for_event(demo->connection);
2032 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002033 if (event) {
2034 demo_handle_event(demo, event);
2035 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002036 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002037
2038 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002039 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002040 demo_update_data_buffer(demo);
2041
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002042 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002043
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002044 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002045 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002046 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002047 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002048 demo->quit = true;
2049
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050 }
2051}
2052
2053static void demo_create_window(struct demo *demo)
2054{
2055 uint32_t value_mask, value_list[32];
2056
2057 demo->window = xcb_generate_id(demo->connection);
2058
2059 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2060 value_list[0] = demo->screen->black_pixel;
2061 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002062 XCB_EVENT_MASK_EXPOSURE |
2063 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002064
2065 xcb_create_window(demo->connection,
2066 XCB_COPY_FROM_PARENT,
2067 demo->window, demo->screen->root,
2068 0, 0, demo->width, demo->height, 0,
2069 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2070 demo->screen->root_visual,
2071 value_mask, value_list);
2072
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002073 /* Magic code that will send notification when window is destroyed */
2074 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2075 "WM_PROTOCOLS");
2076 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2077
2078 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2079 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2080
2081 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2082 demo->window, (*reply).atom, 4, 32, 1,
2083 &(*demo->atom_wm_delete_window).atom);
2084 free(reply);
2085
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002086 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002087
2088 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2089 const uint32_t coords[] = {100, 100};
2090 xcb_configure_window(demo->connection, demo->window,
2091 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002092}
Ian Elliott639ca472015-04-16 15:23:05 -06002093#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002094
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002095/*
2096 * Return 1 (true) if all layer names specified in check_names
2097 * can be found in given layer properties.
2098 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002099static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002100 uint32_t layer_count, VkLayerProperties *layers)
2101{
2102 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002103 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002104 for (uint32_t j = 0; j < layer_count; j++) {
2105 if (!strcmp(check_names[i], layers[j].layerName)) {
2106 found = 1;
2107 }
2108 }
2109 if (!found) {
2110 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2111 return 0;
2112 }
2113 }
2114 return 1;
2115}
2116
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002117static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002118{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002119 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002120 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002121 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002122 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002123 VkLayerProperties *instance_layers;
2124 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002125 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002126 uint32_t instance_layer_count = 0;
2127 uint32_t enabled_extension_count = 0;
2128 uint32_t enabled_layer_count = 0;
2129
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002130 char *instance_validation_layers[] = {
Jon Ashburnf453b372015-11-25 08:25:11 -07002131 "VK_LAYER_LUNARG_Threading",
2132 "VK_LAYER_LUNARG_MemTracker",
2133 "VK_LAYER_LUNARG_ObjectTracker",
2134 "VK_LAYER_LUNARG_DrawState",
2135 "VK_LAYER_LUNARG_ParamChecker",
Jon Ashburnf453b372015-11-25 08:25:11 -07002136 "VK_LAYER_LUNARG_Swapchain",
2137 "VK_LAYER_LUNARG_DeviceLimits",
2138 "VK_LAYER_LUNARG_Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002139 };
2140
2141 char *device_validation_layers[] = {
Jon Ashburnf453b372015-11-25 08:25:11 -07002142 "VK_LAYER_LUNARG_Threading",
2143 "VK_LAYER_LUNARG_MemTracker",
2144 "VK_LAYER_LUNARG_ObjectTracker",
2145 "VK_LAYER_LUNARG_DrawState",
2146 "VK_LAYER_LUNARG_ParamChecker",
Jon Ashburnf453b372015-11-25 08:25:11 -07002147 "VK_LAYER_LUNARG_Swapchain",
2148 "VK_LAYER_LUNARG_DeviceLimits",
2149 "VK_LAYER_LUNARG_Image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002150 };
2151
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002152 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002153 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002154 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002155 assert(!err);
2156
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002157 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002158 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002159 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002160
2161 if (demo->validate) {
2162 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2163 instance_layer_count, instance_layers);
2164 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002165 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002166 "required validation layer.\n\n"
2167 "Please look at the Getting Started guide for additional "
2168 "information.\n",
2169 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002170 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002171 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002172 }
2173
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002174 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002175 assert(!err);
2176
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002177 VkBool32 surfaceExtFound = 0;
2178 VkBool32 platformSurfaceExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002179 memset(extension_names, 0, sizeof(extension_names));
2180 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002181 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002182 assert(!err);
2183 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002184 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002185 surfaceExtFound = 1;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002186 extension_names[enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002187 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002188#ifdef _WIN32
2189 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2190 platformSurfaceExtFound = 1;
2191 extension_names[enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2192 }
2193#else // _WIN32
2194 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2195 platformSurfaceExtFound = 1;
2196 extension_names[enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2197 }
2198#endif // _WIN32
Courtney Goeltzenleuchter96403642015-11-25 13:40:37 -07002199 if (!strcmp(VK_EXT_LUNARG_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002200 if (demo->validate) {
Courtney Goeltzenleuchter96403642015-11-25 13:40:37 -07002201 extension_names[enabled_extension_count++] = VK_EXT_LUNARG_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002202 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002203 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002204 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002205 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002206 if (!surfaceExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002207 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002208 VK_KHR_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002209 "Vulkan installable client driver (ICD) installed?\nPlease "
2210 "look at the Getting Started guide for additional "
2211 "information.\n",
2212 "vkCreateInstance Failure");
2213 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002214 if (!platformSurfaceExtFound) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002215#ifdef _WIN32
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002216 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002217 VK_KHR_WIN32_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002218 "Vulkan installable client driver (ICD) installed?\nPlease "
2219 "look at the Getting Started guide for additional "
2220 "information.\n",
2221 "vkCreateInstance Failure");
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002222#else // _WIN32
2223 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2224 VK_KHR_XCB_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2225 "Vulkan installable client driver (ICD) installed?\nPlease "
2226 "look at the Getting Started guide for additional "
2227 "information.\n",
2228 "vkCreateInstance Failure");
2229#endif // _WIN32
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002230 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002231 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002232 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002233 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002234 .pApplicationName = APP_SHORT_NAME,
2235 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002236 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002237 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002238 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002239 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002240 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002241 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002242 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002243 .pApplicationInfo = &app,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002244 .enabledLayerNameCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002245 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002246 .enabledExtensionNameCount = enabled_extension_count,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002247 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002248 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002249
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002250 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002251
Chia-I Wu63636062015-10-26 21:10:41 +08002252 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002253 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2254 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002255 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002256 "additional information.\n",
2257 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002258 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002259 ERR_EXIT("Cannot find a specified extension library"
2260 ".\nMake sure your layers path is set appropriately\n",
2261 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002262 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002263 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2264 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002265 "the Getting Started guide for additional information.\n",
2266 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002267 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002268
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002269 free(instance_layers);
2270 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002271
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002272 /* Make initial call to query gpu_count, then second call for gpu info*/
2273 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2274 assert(!err && gpu_count > 0);
2275 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2276 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2277 assert(!err);
2278 /* For cube demo we just grab the first physical device */
2279 demo->gpu = physical_devices[0];
2280 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002281
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002282 /* Look for validation layers */
2283 validation_found = 0;
2284 enabled_layer_count = 0;
2285 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002286 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002287 assert(!err);
2288
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002289 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002290 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002291 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002292
2293 if (demo->validate) {
2294 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2295 device_layer_count, device_layers);
2296 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002297 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002298 "a required validation layer.\n\n"
2299 "Please look at the Getting Started guide for additional "
2300 "information.\n",
2301 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002302 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002303 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002304 }
2305
2306 uint32_t device_extension_count = 0;
2307 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002308 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002309 demo->gpu, NULL, &device_extension_count, NULL);
2310 assert(!err);
2311
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002312 VkBool32 swapchainExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002313 enabled_extension_count = 0;
2314 memset(extension_names, 0, sizeof(extension_names));
2315 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002316 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002317 demo->gpu, NULL, &device_extension_count, device_extensions);
2318 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002319
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002320 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002321 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002322 swapchainExtFound = 1;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002323 extension_names[enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002324 }
2325 assert(enabled_extension_count < 64);
2326 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002327 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002328 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002329 VK_KHR_SWAPCHAIN_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002330 "Vulkan installable client driver (ICD) installed?\nPlease "
2331 "look at the Getting Started guide for additional "
2332 "information.\n",
2333 "vkCreateInstance Failure");
2334 }
2335
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002336 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002337 demo->CreateDebugReportCallback = (PFN_vkCreateDebugReportCallbackLUNARG) vkGetInstanceProcAddr(demo->inst, "vkCreateDebugReportCallbackLUNARG");
2338 demo->DestroyDebugReportCallback = (PFN_vkDestroyDebugReportCallbackLUNARG) vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugReportCallbackLUNARG");
2339 if (!demo->CreateDebugReportCallback) {
2340 ERR_EXIT("GetProcAddr: Unable to find vkCreateDebugReportCallbackLUNARG\n",
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002341 "vkGetProcAddr Failure");
2342 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002343 if (!demo->DestroyDebugReportCallback) {
2344 ERR_EXIT("GetProcAddr: Unable to find vkDestroyDebugReportCallbackLUNARG\n",
Tony Barbourd70b42c2015-06-30 14:14:19 -06002345 "vkGetProcAddr Failure");
2346 }
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002347 demo->DebugReportMessage = (PFN_vkDebugReportMessageLUNARG) vkGetInstanceProcAddr(demo->inst, "vkDebugReportMessageLUNARG");
2348 if (!demo->DebugReportMessage) {
2349 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageLUNARG\n",
2350 "vkGetProcAddr Failure");
2351 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002352
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002353 PFN_vkDebugReportCallbackLUNARG callback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002354
2355 if (!demo->use_break) {
2356 callback = dbgFunc;
2357 } else {
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002358 callback = dbgFunc;
2359 // TODO add a break callback defined locally since there is no longer
2360 // one included in the loader
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002361 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002362 VkDebugReportCallbackCreateInfoLUNARG dbgCreateInfo;
2363 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_LUNARG;
2364 dbgCreateInfo.pNext = NULL;
2365 dbgCreateInfo.pfnCallback = callback;
2366 dbgCreateInfo.pUserData = NULL;
2367 dbgCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT | VK_DEBUG_REPORT_WARN_BIT;
2368 err = demo->CreateDebugReportCallback(
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002369 demo->inst,
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002370 &dbgCreateInfo,
2371 NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002372 &demo->msg_callback);
2373 switch (err) {
2374 case VK_SUCCESS:
2375 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002376 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002377 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2378 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002379 break;
2380 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002381 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2382 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002383 break;
2384 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002385 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002386 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002387
2388 /* Call with NULL data to get count */
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002389 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002390 assert(demo->queue_count >= 1);
2391
2392 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002393 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002394 // Find a queue that supports gfx
2395 uint32_t gfx_queue_idx = 0;
2396 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2397 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2398 break;
2399 }
2400 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002401 // Query fine-grained feature support for this device.
2402 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002403 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002404 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002405
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002406 float queue_priorities[1] = { 0.0 };
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002407 const VkDeviceQueueCreateInfo queue = {
2408 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2409 .pNext = NULL,
2410 .queueFamilyIndex = gfx_queue_idx,
Chia-I Wu8b497442015-11-06 06:42:02 +08002411 .queueCount = 1,
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002412 .pQueuePriorities = queue_priorities
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002413 };
2414
2415 VkDeviceCreateInfo device = {
2416 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2417 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002418 .queueCreateInfoCount = 1,
2419 .pQueueCreateInfos = &queue,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002420 .enabledLayerNameCount = enabled_layer_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002421 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002422 .enabledExtensionNameCount = enabled_extension_count,
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002423 .ppEnabledExtensionNames = (const char *const*) extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002424 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002425 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002426
Chia-I Wu63636062015-10-26 21:10:41 +08002427 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002428 assert(!err);
2429
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002430 free(device_layers);
2431
Ian Elliotta0781972015-08-21 15:09:33 -06002432 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
Ian Elliottf2ff8022015-11-23 12:48:15 -07002433 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2434 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2435 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002436 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
Ian Elliotta0781972015-08-21 15:09:33 -06002437 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2438 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2439 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2440 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002441}
2442
Tony Barbourcc69f452015-10-08 13:59:42 -06002443static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002444{
2445 VkResult err;
2446 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002447
Ian Elliottb08e0d92015-11-20 11:55:46 -07002448 // Create a WSI surface for the window:
Ian Elliottaaae5352015-07-06 14:27:58 -06002449#ifdef _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -07002450 err = vkCreateWin32SurfaceKHR(demo->inst, demo->connection, demo->window,
2451 NULL, &demo->surface);
2452
Ian Elliottaaae5352015-07-06 14:27:58 -06002453#else // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -07002454 err = vkCreateXcbSurfaceKHR(demo->inst, demo->connection, demo->window,
2455 NULL, &demo->surface);
Ian Elliottaaae5352015-07-06 14:27:58 -06002456#endif // _WIN32
2457
Tony Barbourcc69f452015-10-08 13:59:42 -06002458 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002459 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2460 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002461 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
Ian Elliottb08e0d92015-11-20 11:55:46 -07002462 demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002463 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002464 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002465
2466 // Search for a graphics and a present queue in the array of queue
2467 // families, try to find one that supports both
2468 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2469 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002470 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002471 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2472 if (graphicsQueueNodeIndex == UINT32_MAX) {
2473 graphicsQueueNodeIndex = i;
2474 }
2475
2476 if (supportsPresent[i] == VK_TRUE) {
2477 graphicsQueueNodeIndex = i;
2478 presentQueueNodeIndex = i;
2479 break;
2480 }
2481 }
2482 }
2483 if (presentQueueNodeIndex == UINT32_MAX) {
2484 // If didn't find a queue that supports both graphics and present, then
2485 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002486 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002487 if (supportsPresent[i] == VK_TRUE) {
2488 presentQueueNodeIndex = i;
2489 break;
2490 }
2491 }
2492 }
2493 free(supportsPresent);
2494
2495 // Generate error if could not find both a graphics and a present queue
2496 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2497 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002498 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002499 }
2500
2501 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002502 // synchronization, and appropriate tracking for QueueSubmit.
2503 // NOTE: While it is possible for an application to use a separate graphics
2504 // and a present queues, this demo program assumes it is only using
2505 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002506 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2507 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002508 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002509 }
2510
2511 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002512
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002513 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002514 0, &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002515
Ian Elliottaaae5352015-07-06 14:27:58 -06002516 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002517 uint32_t formatCount;
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002518 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002519 demo->surface,
2520 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002521 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002522 VkSurfaceFormatKHR *surfFormats =
2523 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002524 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002525 demo->surface,
2526 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002527 assert(!err);
2528 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2529 // the surface has no preferred format. Otherwise, at least one
2530 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002531 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2532 {
2533 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2534 }
2535 else
2536 {
2537 assert(formatCount >= 1);
2538 demo->format = surfFormats[0].format;
2539 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002540 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002541
David Pinedo2bb7c932015-06-18 17:03:14 -06002542 demo->quit = false;
2543 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002544
2545 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002546 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002547}
2548
2549static void demo_init_connection(struct demo *demo)
2550{
Ian Elliott639ca472015-04-16 15:23:05 -06002551#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002552 const xcb_setup_t *setup;
2553 xcb_screen_iterator_t iter;
2554 int scr;
2555
2556 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002557 if (demo->connection == NULL) {
2558 printf("Cannot find a compatible Vulkan installable client driver "
2559 "(ICD).\nExiting ...\n");
2560 fflush(stdout);
2561 exit(1);
2562 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002563
2564 setup = xcb_get_setup(demo->connection);
2565 iter = xcb_setup_roots_iterator(setup);
2566 while (scr-- > 0)
2567 xcb_screen_next(&iter);
2568
2569 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002570#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002571}
2572
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002573static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002574{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002575 vec3 eye = {0.0f, 3.0f, 5.0f};
2576 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002577 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002578
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002579 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002580 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002581
Piers Daniell735ee532015-02-23 16:23:13 -07002582 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002583 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002584 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002585 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002586 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002587 if (strcmp(argv[i], "--break") == 0) {
2588 demo->use_break = true;
2589 continue;
2590 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002591 if (strcmp(argv[i], "--validate") == 0) {
2592 demo->validate = true;
2593 continue;
2594 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002595 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002596 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002597 i < argc-1 &&
2598 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2599 demo->frameCount >= 0)
2600 {
2601 i++;
2602 continue;
2603 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002604
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002605 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002606 fflush(stderr);
2607 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002608 }
2609
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002610 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002611 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002612
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002613 demo->width = 500;
2614 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002615
2616 demo->spin_angle = 0.01f;
2617 demo->spin_increment = 0.01f;
2618 demo->pause = false;
2619
Piers Daniell735ee532015-02-23 16:23:13 -07002620 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002621 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2622 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002623}
2624
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002625
Ian Elliott639ca472015-04-16 15:23:05 -06002626#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002627extern int __getmainargs(
2628 int * _Argc,
2629 char *** _Argv,
2630 char *** _Env,
2631 int _DoWildCard,
2632 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002633
Ian Elliotta748eaf2015-04-28 15:50:36 -06002634int WINAPI WinMain(HINSTANCE hInstance,
2635 HINSTANCE hPrevInstance,
2636 LPSTR pCmdLine,
2637 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002638{
2639 MSG msg; // message
2640 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002641 int argc;
2642 char** argv;
2643 char** env;
2644 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002645
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002646 __getmainargs(&argc,&argv,&env,0,&new_mode);
2647
2648 demo_init(&demo, argc, argv);
2649 demo.connection = hInstance;
2650 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002651 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002652 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002653
2654 demo_prepare(&demo);
2655
2656 done = false; //initialize loop condition variable
2657 /* main message loop*/
2658 while(!done)
2659 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002660 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002661 if (msg.message == WM_QUIT) //check for a quit message
2662 {
2663 done = true; //if found, quit app
2664 }
2665 else
2666 {
2667 /* Translate and dispatch to event queue*/
2668 TranslateMessage(&msg);
2669 DispatchMessage(&msg);
2670 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002671 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002672 }
2673
2674 demo_cleanup(&demo);
2675
Tony Barbourf9921732015-04-22 11:36:22 -06002676 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002677}
2678#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002679int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002680{
2681 struct demo demo;
2682
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002683 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002684 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002685 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002686
2687 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002688 demo_run(&demo);
2689
2690 demo_cleanup(&demo);
2691
2692 return 0;
2693}
Ian Elliott639ca472015-04-16 15:23:05 -06002694#endif // _WIN32