blob: 47473717429af5b7a9853a49d59c935223211633 [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 Goeltzenleuchter0f060df2015-12-09 15:48:16 -070043#include <vulkan/vk_ext_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
Mark Youngcda9d842016-01-13 13:47:16 -0700258VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(
Tony Barbour155cce82015-07-03 10:33:54 -0600259 VkFlags msgFlags,
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700260 VkDebugReportObjectTypeEXT 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 Goeltzenleuchtera5a167a2016-01-18 17:42:08 -0700266 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600267{
268 char *message = (char *) malloc(strlen(pMsg)+100);
269
270 assert (message);
271
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700272 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600273 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700274 } else if (msgFlags & VK_DEBUG_REPORT_WARN_BIT_EXT) {
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,
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700304 VkDebugReportObjectTypeEXT objType,
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700305 uint64_t srcObject,
306 size_t location,
307 int32_t msgCode,
308 const char* pLayerPrefix,
309 const char* pMsg,
Courtney Goeltzenleuchtera5a167a2016-01-18 17:42:08 -0700310 void* pUserData)
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700311{
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
Tony Barbourda2bad52016-01-22 14:36:40 -0700352 uint32_t enabled_extension_count;
353 uint32_t enabled_layer_count;
354 char *extension_names[64];
355 char *device_validation_layers[64];
356
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600357 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600359 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360
Ian Elliotta0781972015-08-21 15:09:33 -0600361 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
Ian Elliottf4a4e442015-11-17 17:29:40 -0700362 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
363 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
364 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600365 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
366 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
367 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
368 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
369 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600370 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600371 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600372 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800374 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
376 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600377 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600378
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600379 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800380 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500381 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600382 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383 } depth;
384
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600385 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600386
387 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600388 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800389 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500390 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600391 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600392 } uniform_data;
393
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800394 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500395 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600396 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600397 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800398 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600399 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600401 mat4x4 projection_matrix;
402 mat4x4 view_matrix;
403 mat4x4 model_matrix;
404
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600405 float spin_angle;
406 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600407 bool pause;
408
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600409 VkShaderModule vert_shader_module;
410 VkShaderModule frag_shader_module;
411
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600412 VkDescriptorPool desc_pool;
413 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800414
Tony Barbourd8e504f2015-10-08 14:26:24 -0600415 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800416
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600417 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600418 int32_t curFrame;
419 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600420 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600421 bool use_break;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700422 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
423 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
424 VkDebugReportCallbackEXT msg_callback;
425 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600426
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600427 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600428 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600429};
430
Ian Elliottcf074d32015-10-16 18:02:43 -0600431// Forward declaration:
432static void demo_resize(struct demo *demo);
433
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600434static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex)
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600435{
436 // Search memtypes to find first index with those properties
437 for (uint32_t i = 0; i < 32; i++) {
438 if ((typeBits & 1) == 1) {
439 // Type is available, does it match user properties?
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600440 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600441 *typeIndex = i;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600442 return true;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600443 }
444 }
445 typeBits >>= 1;
446 }
447 // No memory types matched, return failure
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600448 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600449}
450
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451static void demo_flush_init_cmd(struct demo *demo)
452{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600453 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600456 return;
457
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600458 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600459 assert(!err);
460
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800461 const VkCommandBuffer cmd_bufs[] = { demo->cmd };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800462 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600463 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800464 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
465 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800466 .waitSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600467 .pWaitSemaphores = NULL,
Jon Ashburn244c6d02015-12-30 16:42:50 -0700468 .pWaitDstStageMask = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800469 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600470 .pCommandBuffers = cmd_bufs,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800471 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600472 .pSignalSemaphores = NULL
473 };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600474
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600475 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476 assert(!err);
477
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600478 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600479 assert(!err);
480
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600481 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600482 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600483}
484
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600485static void demo_set_image_layout(
486 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600487 VkImage image,
Cody Northrop0e951672015-09-14 13:48:12 -0600488 VkImageAspectFlags aspectMask,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600489 VkImageLayout old_image_layout,
490 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600492 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800495 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800496 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600497 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800498 .commandPool = demo->cmd_pool,
499 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700500 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600501 };
502
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800503 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600504 assert(!err);
505
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700506 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
507 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600508 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800509 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600510 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800511 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800512 .occlusionQueryEnable = VK_FALSE,
513 .queryFlags = 0,
514 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600515 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700516 VkCommandBufferBeginInfo cmd_buf_info = {
517 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
518 .pNext = NULL,
519 .flags = 0,
520 .pInheritanceInfo = &cmd_buf_hinfo,
521 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600522 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600523 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600524 }
525
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600526 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600527 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600528 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800529 .srcAccessMask = 0,
530 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600531 .oldLayout = old_image_layout,
532 .newLayout = new_image_layout,
533 .image = image,
Jeremy Hayes105ca502015-11-17 18:19:55 -0700534 .subresourceRange = { aspectMask, 0, 1, 0, 1 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600535 };
536
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800537 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600538 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800539 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600540 }
541
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700542 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Mark Lobodzinskid51e5a92015-12-03 15:42:32 -0700543 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700544 }
545
546 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Mark Lobodzinskid51e5a92015-12-03 15:42:32 -0700547 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700548 }
549
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600550 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600551 /* Make sure any Copy or CPU writes to image are flushed */
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700552 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553 }
554
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600555 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600556
Tony Barbour50bbca42015-06-29 16:20:35 -0600557 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
558 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600559
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700560 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600561}
562
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800563static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564{
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700565 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
566 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600567 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800568 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600569 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800570 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800571 .occlusionQueryEnable = VK_FALSE,
572 .queryFlags = 0,
573 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700574 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700575 const VkCommandBufferBeginInfo cmd_buf_info = {
576 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
577 .pNext = NULL,
578 .flags = 0,
579 .pInheritanceInfo = &cmd_buf_hinfo,
580 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800581 const VkClearValue clear_values[2] = {
Cody Northrop55745922015-08-25 15:26:38 -0600582 [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
583 [1] = { .depthStencil = { 1.0f, 0 } },
Chia-I Wua74c5b22015-07-07 11:50:03 +0800584 };
585 const VkRenderPassBeginInfo rp_begin = {
586 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
587 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800588 .renderPass = demo->render_pass,
589 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800590 .renderArea.offset.x = 0,
591 .renderArea.offset.y = 0,
592 .renderArea.extent.width = demo->width,
593 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600594 .clearValueCount = 2,
595 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700596 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800597 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600598
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600599 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600600 assert(!err);
601
Chia-I Wuf051d262015-10-27 19:25:11 +0800602 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800603
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600604 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600605 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600606 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600607 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600608
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600609 VkViewport viewport;
610 memset(&viewport, 0, sizeof(viewport));
611 viewport.height = (float) demo->height;
612 viewport.width = (float) demo->width;
613 viewport.minDepth = (float) 0.0f;
614 viewport.maxDepth = (float) 1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700615 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600616
617 VkRect2D scissor;
618 memset(&scissor, 0, sizeof(scissor));
619 scissor.extent.width = demo->width;
620 scissor.extent.height = demo->height;
621 scissor.offset.x = 0;
622 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700623 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600624
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600625 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800626 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600627
Tony Barbour15a4ef62015-10-21 10:14:48 -0600628 VkImageMemoryBarrier prePresentBarrier = {
629 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
630 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800631 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700632 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600633 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700634 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600635 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800636 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600637 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }
638 };
639
640 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
641 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Tony Barboure5af5392016-01-05 09:59:05 -0700642 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700643 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600644
645
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600646 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600647 assert(!err);
648}
649
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600650
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600651void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600653 mat4x4 MVP, Model, VP;
654 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600655 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600656 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600658 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600660 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600661 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700662 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600663 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600664
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200665 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 -0600666 assert(!err);
667
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600668 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600669
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600670 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600671}
672
673static void demo_draw(struct demo *demo)
674{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600675 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600676 VkSemaphore presentCompleteSemaphore;
677 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
678 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
679 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600680 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600681 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800682 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600683
Ian Elliottaaae5352015-07-06 14:27:58 -0600684 err = vkCreateSemaphore(demo->device,
685 &presentCompleteSemaphoreCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +0800686 NULL,
Ian Elliottaaae5352015-07-06 14:27:58 -0600687 &presentCompleteSemaphore);
688 assert(!err);
689
690 // Get the index of the next available swapchain image:
Ian Elliottcf074d32015-10-16 18:02:43 -0600691 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600692 UINT64_MAX,
693 presentCompleteSemaphore,
Mark Youngcda9d842016-01-13 13:47:16 -0700694 (VkFence)0,// TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600695 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600696 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
697 // demo->swapchain is out of date (e.g. the window was resized) and
698 // must be recreated:
699 demo_resize(demo);
700 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800701 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600702 return;
703 } else if (err == VK_SUBOPTIMAL_KHR) {
704 // demo->swapchain is not as optimal as it could be, but the platform's
705 // presentation engine will still present the image correctly.
706 } else {
707 assert(!err);
708 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600709
Tony Barbour15a4ef62015-10-21 10:14:48 -0600710 // Assume the command buffer has been run on current_buffer before so
711 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
712 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
713 VK_IMAGE_ASPECT_COLOR_BIT,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700714 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600715 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
716 demo_flush_init_cmd(demo);
717
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600718 // Wait for the present complete semaphore to be signaled to ensure
719 // that the image won't be rendered to until the presentation
720 // engine has fully released ownership to the application, and it is
721 // okay to render to the image.
722
Ian Elliottf4a4e442015-11-17 17:29:40 -0700723// FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
Jon Ashburn08ba3032015-12-31 12:32:16 -0700724 VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600725 VkSubmitInfo submit_info = {
Chia-I Wu4176d7b2015-10-26 20:37:06 +0800726 .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
727 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800728 .waitSemaphoreCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600729 .pWaitSemaphores = &presentCompleteSemaphore,
Jon Ashburn244c6d02015-12-30 16:42:50 -0700730 .pWaitDstStageMask = &pipe_stage_flags,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800731 .commandBufferCount = 1,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600732 .pCommandBuffers = &demo->buffers[demo->current_buffer].cmd,
Chia-I Wu3dfc1342015-10-26 20:48:51 +0800733 .signalSemaphoreCount = 0,
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600734 .pSignalSemaphores = NULL
735 };
736
737 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600738 assert(!err);
739
Ian Elliotta0781972015-08-21 15:09:33 -0600740 VkPresentInfoKHR present = {
741 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600742 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600743 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700744 .pSwapchains = &demo->swapchain,
745 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600746 };
747
748// TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600749 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600750 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
751 // demo->swapchain is out of date (e.g. the window was resized) and
752 // must be recreated:
753 demo_resize(demo);
754 } else if (err == VK_SUBOPTIMAL_KHR) {
755 // demo->swapchain is not as optimal as it could be, but the platform's
756 // presentation engine will still present the image correctly.
757 } else {
758 assert(!err);
759 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600760
Chia-I Wucbb564e2015-04-16 22:02:10 +0800761 err = vkQueueWaitIdle(demo->queue);
762 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600763
Chia-I Wu63636062015-10-26 21:10:41 +0800764 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600765}
766
767static void demo_prepare_buffers(struct demo *demo)
768{
Ian Elliottaaae5352015-07-06 14:27:58 -0600769 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600770 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600771
Ian Elliottb08e0d92015-11-20 11:55:46 -0700772 // Check the surface capabilities and formats
773 VkSurfaceCapabilitiesKHR surfCapabilities;
774 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu,
775 demo->surface,
776 &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600777 assert(!err);
778
Ian Elliott8528eff2015-08-07 15:56:59 -0600779 uint32_t presentModeCount;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700780 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu,
781 demo->surface,
Ian Elliott8528eff2015-08-07 15:56:59 -0600782 &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600783 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600784 VkPresentModeKHR *presentModes =
785 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600786 assert(presentModes);
Ian Elliottb08e0d92015-11-20 11:55:46 -0700787 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu,
788 demo->surface,
Ian Elliott8528eff2015-08-07 15:56:59 -0600789 &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600790 assert(!err);
791
Ian Elliotta0781972015-08-21 15:09:33 -0600792 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600793 // width and height are either both -1, or both not -1.
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700794 if (surfCapabilities.currentExtent.width == (uint32_t) -1)
Ian Elliottaaae5352015-07-06 14:27:58 -0600795 {
796 // If the surface size is undefined, the size is set to
797 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600798 swapchainExtent.width = demo->width;
799 swapchainExtent.height = demo->height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600800 }
801 else
802 {
803 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700804 swapchainExtent = surfCapabilities.currentExtent;
805 demo->width = surfCapabilities.currentExtent.width;
806 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600807 }
808
809 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600810 // tearing mode. If not, try IMMEDIATE which will usually be available,
811 // and is fastest (though it tears). If not, fall back to FIFO which is
812 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600813 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600814 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600815 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
816 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600817 break;
818 }
Ian Elliotta0781972015-08-21 15:09:33 -0600819 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
820 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
821 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600822 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600823 }
824
825 // Determine the number of VkImage's to use in the swap chain (we desire to
826 // own only 1 image at a time, besides the images being displayed and
827 // queued for display):
Ian Elliottb08e0d92015-11-20 11:55:46 -0700828 uint32_t desiredNumberOfSwapchainImages = surfCapabilities.minImageCount + 1;
829 if ((surfCapabilities.maxImageCount > 0) &&
830 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount))
Ian Elliottaaae5352015-07-06 14:27:58 -0600831 {
832 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700833 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600834 }
835
Tony Barbour9fd6d352015-09-16 15:01:32 -0600836 VkSurfaceTransformFlagsKHR preTransform;
Ian Elliotta7a731c2015-12-11 15:52:12 -0700837 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
838 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600839 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700840 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600841 }
842
Ian Elliottcf074d32015-10-16 18:02:43 -0600843 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600844 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800845 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700846 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600847 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800848 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600849 .imageColorSpace = demo->color_space,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800850 .imageExtent = {
Ian Elliotta0781972015-08-21 15:09:33 -0600851 .width = swapchainExtent.width,
852 .height = swapchainExtent.height,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600853 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700854 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600855 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700856 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700857 .imageArrayLayers = 1,
858 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
859 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600860 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600861 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600862 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600863 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600864 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600865 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600866
Ian Elliottb08e0d92015-11-20 11:55:46 -0700867 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800868 assert(!err);
869
Ian Elliottcf074d32015-10-16 18:02:43 -0600870 // If we just re-created an existing swapchain, we should destroy the old
871 // swapchain at this point.
872 // Note: destroying the swapchain also cleans up all its associated
873 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800874 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700875 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600876 }
877
878 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600879 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600880 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800881
Ian Elliotta0781972015-08-21 15:09:33 -0600882 VkImage* swapchainImages =
883 (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage));
884 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600885 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600886 &demo->swapchainImageCount,
887 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600888 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600889
Ian Elliotta0781972015-08-21 15:09:33 -0600890 demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600891 assert(demo->buffers);
892
Ian Elliotta0781972015-08-21 15:09:33 -0600893 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600894 VkImageViewCreateInfo color_image_view = {
895 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600896 .pNext = NULL,
897 .format = demo->format,
Chia-I Wub6d30c62015-10-27 19:00:15 +0800898 .components = {
Chia-I Wuf051d262015-10-27 19:25:11 +0800899 .r = VK_COMPONENT_SWIZZLE_R,
900 .g = VK_COMPONENT_SWIZZLE_G,
901 .b = VK_COMPONENT_SWIZZLE_B,
902 .a = VK_COMPONENT_SWIZZLE_A,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600903 },
904 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600905 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600906 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800907 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600908 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800909 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600910 },
911 .viewType = VK_IMAGE_VIEW_TYPE_2D,
912 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600913 };
914
Ian Elliotta0781972015-08-21 15:09:33 -0600915 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600916
Ian Elliottf4a4e442015-11-17 17:29:40 -0700917 // 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 -0600918 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image to that state
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600919 demo_set_image_layout(demo, demo->buffers[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -0600920 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600921 VK_IMAGE_LAYOUT_UNDEFINED,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700922 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600923
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600924 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600925
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600926 err = vkCreateImageView(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +0800927 &color_image_view, NULL, &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600928 assert(!err);
929 }
Mark Young04fd3d82016-01-25 14:43:50 -0700930
931 if (NULL != presentModes) {
932 free(presentModes);
933 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600934}
935
936static void demo_prepare_depth(struct demo *demo)
937{
Tony Barbour72304ef2015-04-16 15:59:00 -0600938 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600939 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600940 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600941 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600942 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600943 .format = depth_format,
944 .extent = { demo->width, demo->height, 1 },
945 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600946 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800947 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600948 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600949 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600950 .flags = 0,
951 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200952
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600953 VkImageViewCreateInfo view = {
954 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600955 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800956 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600957 .format = depth_format,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600958 .subresourceRange = {
Cody Northrop0e951672015-09-14 13:48:12 -0600959 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600960 .baseMipLevel = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800961 .levelCount = 1,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -0600962 .baseArrayLayer = 0,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800963 .layerCount = 1
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600964 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600965 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600966 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600967 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600968
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500969 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600970 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600971 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600972
973 demo->depth.format = depth_format;
974
975 /* create image */
Chia-I Wu63636062015-10-26 21:10:41 +0800976 err = vkCreateImage(demo->device, &image, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600977 &demo->depth.image);
978 assert(!err);
979
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -0600980 vkGetImageMemoryRequirements(demo->device,
Tony Barbour155cce82015-07-03 10:33:54 -0600981 demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600982 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600983
Chia-I Wuf48700b2015-11-10 16:21:09 +0800984 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200985 demo->depth.mem_alloc.pNext = NULL;
986 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
987 demo->depth.mem_alloc.memoryTypeIndex = 0;
988
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600989 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600990 mem_reqs.memoryTypeBits,
Tony Barbour8a9f62a2015-10-15 12:42:56 -0600991 0, /* No requirements */
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200992 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600993 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600994
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500995 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800996 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +0800997 NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500998 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600999
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001000 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001001 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001002 demo->depth.mem, 0);
1003 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001005 demo_set_image_layout(demo, demo->depth.image,
Cody Northrop0e951672015-09-14 13:48:12 -06001006 VK_IMAGE_ASPECT_DEPTH_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001007 VK_IMAGE_LAYOUT_UNDEFINED,
1008 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001009
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001010 /* create image view */
1011 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001012 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001013 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001014}
1015
Tony Barbour1a86d032015-09-21 15:17:33 -06001016/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001018 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001019 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001020{
Tony Barbour1a86d032015-09-21 15:17:33 -06001021 FILE *fPtr = fopen(filename,"rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001022 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001023
Tony Barbour1a86d032015-09-21 15:17:33 -06001024 if (!fPtr)
1025 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001026
Tony Barbour1a86d032015-09-21 15:17:33 -06001027 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001028 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1029 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001030 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001031 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001032
Tony Barbour1a86d032015-09-21 15:17:33 -06001033 do {
1034 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001035 if (cPtr == NULL) {
1036 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001037 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001038 }
Tony Barbour1a86d032015-09-21 15:17:33 -06001039 } while ( !strncmp(header, "#", 1) );
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001040
Tony Barbour1a86d032015-09-21 15:17:33 -06001041 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001042 if (rgba_data == NULL) {
1043 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001044 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001045 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001046 tmp = fgets(header, 256, fPtr); // Format
1047 (void) tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001048 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1049 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001050 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001051 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001052
Tony Barbour1a86d032015-09-21 15:17:33 -06001053 for(int y = 0; y < *height; y++)
1054 {
1055 uint8_t *rowPtr = rgba_data;
1056 for(int x = 0; x < *width; x++)
1057 {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001058 size_t s = fread(rowPtr, 3, 1, fPtr);
1059 (void) s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001060 rowPtr[3] = 255; /* Alpha of 1 */
1061 rowPtr += 4;
1062 }
1063 rgba_data += layout->rowPitch;
1064 }
1065 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001066 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001067}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001068
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069static void demo_prepare_texture_image(struct demo *demo,
1070 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001071 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001072 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001073 VkImageUsageFlags usage,
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001074 VkFlags required_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075{
Mike Stroyan8b89e072015-06-15 14:21:03 -06001076 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001077 int32_t tex_width;
1078 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001079 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001080 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001081
David Pinedo30bd71d2015-04-23 08:16:57 -06001082 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
1083 {
1084 printf("Failed to load textures\n");
1085 fflush(stdout);
1086 exit(1);
1087 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001088
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001089 tex_obj->tex_width = tex_width;
1090 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001091
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001092 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001093 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001094 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001095 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001096 .format = tex_format,
1097 .extent = { tex_width, tex_height, 1 },
1098 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001099 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001100 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001101 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001102 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001103 .flags = 0,
1104 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001105
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001106 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001107
Chia-I Wu63636062015-10-26 21:10:41 +08001108 err = vkCreateImage(demo->device, &image_create_info, NULL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001109 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001110 assert(!err);
1111
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001112 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001113
Chia-I Wuf48700b2015-11-10 16:21:09 +08001114 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001115 tex_obj->mem_alloc.pNext = NULL;
1116 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1117 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001119 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1120 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001121
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001122 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001123 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001124 &(tex_obj->mem));
1125 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001126
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001127 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -06001128 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001129 tex_obj->mem, 0);
1130 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001131
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001132 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001133 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001134 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001135 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001136 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001137 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001138 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001139 void *data;
1140
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001141 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001142
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001143 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001144 assert(!err);
1145
1146 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1147 fprintf(stderr, "Error loading texture: %s\n", filename);
1148 }
1149
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001150 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001151 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001152
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001153 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001154 demo_set_image_layout(demo, tex_obj->image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001155 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001156 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001157 tex_obj->imageLayout);
1158 /* 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 -07001159}
1160
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001161static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162{
1163 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001164 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1165 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001166}
1167
1168static void demo_prepare_textures(struct demo *demo)
1169{
Tony Barbour72304ef2015-04-16 15:59:00 -06001170 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001171 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001172 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001173
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001174 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001175
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001176 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001177 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001178
FslNopper6a7e50e2015-05-06 21:42:01 +02001179 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001180 /* Device can texture using linear textures */
1181 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001182 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001183 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001184 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001185 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001186
1187 memset(&staging_texture, 0, sizeof(staging_texture));
1188 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001189 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001190
1191 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001192 VK_IMAGE_TILING_OPTIMAL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001193 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
Chia-I Wuc42afd72015-10-27 18:53:22 +08001194 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001196 demo_set_image_layout(demo, staging_texture.image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001197 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001198 staging_texture.imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001199 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001200
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001201 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001202 VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001203 demo->textures[i].imageLayout,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001204 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001205
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001206 VkImageCopy copy_region = {
Tony Barbour466f0012015-11-02 11:47:51 -07001207 .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208 .srcOffset = { 0, 0, 0 },
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001209 .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
1210 .dstOffset = { 0, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001211 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1212 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001213 vkCmdCopyImage(demo->cmd,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001214 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1215 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001216 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001218 demo_set_image_layout(demo, demo->textures[i].image,
Tony Barbourc99f2942015-10-12 11:07:58 -06001219 VK_IMAGE_ASPECT_COLOR_BIT,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001220 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001221 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001222
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001223 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001224
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001225 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001226 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001227 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1228 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001229 }
1230
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001231 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001232 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001233 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001234 .magFilter = VK_FILTER_NEAREST,
1235 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001236 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001237 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1238 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1239 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001240 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001241 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001242 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001243 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001244 .minLod = 0.0f,
1245 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001246 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001247 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001248 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001249
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001250 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001251 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001253 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001254 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001255 .format = tex_format,
Chia-I Wub6d30c62015-10-27 19:00:15 +08001256 .components = { VK_COMPONENT_SWIZZLE_R,
Chia-I Wuf051d262015-10-27 19:25:11 +08001257 VK_COMPONENT_SWIZZLE_G,
1258 VK_COMPONENT_SWIZZLE_B,
1259 VK_COMPONENT_SWIZZLE_A, },
Cody Northrop0e951672015-09-14 13:48:12 -06001260 .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001261 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001262 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001263
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001264 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001265 err = vkCreateSampler(demo->device, &sampler, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001266 &demo->textures[i].sampler);
1267 assert(!err);
1268
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001269 /* create image view */
1270 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001271 err = vkCreateImageView(demo->device, &view, NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001272 &demo->textures[i].view);
1273 assert(!err);
1274 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001275}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001276
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001277void demo_prepare_cube_data_buffer(struct demo *demo)
1278{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001279 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001280 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001281 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001282 int i;
1283 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001284 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001285 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001286 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001287
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001288 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001289 mat4x4_mul(MVP, VP, demo->model_matrix);
1290 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001291// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001292
1293 for (i=0; i<12*3; i++) {
1294 data.position[i][0] = g_vertex_buffer_data[i*3];
1295 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1296 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1297 data.position[i][3] = 1.0f;
1298 data.attr[i][0] = g_uv_buffer_data[2*i];
1299 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1300 data.attr[i][2] = 0;
1301 data.attr[i][3] = 0;
1302 }
1303
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001304 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001305 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001306 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001307 buf_info.size = sizeof(data);
Chia-I Wu63636062015-10-26 21:10:41 +08001308 err = vkCreateBuffer(demo->device, &buf_info, NULL,
1309 &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001310 assert(!err);
1311
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001312 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001313
Chia-I Wuf48700b2015-11-10 16:21:09 +08001314 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001315 demo->uniform_data.mem_alloc.pNext = NULL;
1316 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1317 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1318
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001319 pass = memory_type_from_properties(demo,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001320 mem_reqs.memoryTypeBits,
1321 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001322 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001323 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001324
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001325 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc,
Chia-I Wu63636062015-10-26 21:10:41 +08001326 NULL, &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001327 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001328
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001329 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 -05001330 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001331
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001332 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001333
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001334 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001335
Tony Barbour155cce82015-07-03 10:33:54 -06001336 err = vkBindBufferMemory(demo->device,
1337 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001338 demo->uniform_data.mem, 0);
1339 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001340
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001341 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1342 demo->uniform_data.buffer_info.offset = 0;
1343 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001344}
1345
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001346static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001347{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001348 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001349 [0] = {
Chia-I Wued577562015-10-31 00:31:16 +08001350 .binding = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001351 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001352 .descriptorCount = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001353 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001354 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001355 },
1356 [1] = {
Chia-I Wued577562015-10-31 00:31:16 +08001357 .binding = 1,
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001358 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu8b497442015-11-06 06:42:02 +08001359 .descriptorCount = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001360 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001361 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001362 },
1363 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001364 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001365 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001366 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001367 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001368 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001369 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001370 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001372 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001373 &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001374 assert(!err);
1375
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001376 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1377 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1378 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001379 .setLayoutCount = 1,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001380 .pSetLayouts = &demo->desc_layout,
1381 };
1382
1383 err = vkCreatePipelineLayout(demo->device,
1384 &pPipelineLayoutCreateInfo,
Chia-I Wu63636062015-10-26 21:10:41 +08001385 NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001386 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001387 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388}
1389
Chia-I Wuf973e322015-07-08 13:34:24 +08001390static void demo_prepare_render_pass(struct demo *demo)
1391{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001392 const VkAttachmentDescription attachments[2] = {
1393 [0] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001394 .format = demo->format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001395 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001396 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1397 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1398 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1399 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1400 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1401 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1402 },
1403 [1] = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001404 .format = demo->depth.format,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001405 .samples = VK_SAMPLE_COUNT_1_BIT,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001406 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1407 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1408 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1409 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1410 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1411 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1412 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001413 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001414 const VkAttachmentReference color_reference = {
1415 .attachment = 0,
1416 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1417 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001418 const VkAttachmentReference depth_reference = {
1419 .attachment = 1,
1420 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1421 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001422 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001423 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1424 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001425 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001426 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001427 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001428 .pColorAttachments = &color_reference,
1429 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001430 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001431 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001432 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001433 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001434 const VkRenderPassCreateInfo rp_info = {
1435 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1436 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001437 .attachmentCount = 2,
1438 .pAttachments = attachments,
1439 .subpassCount = 1,
1440 .pSubpasses = &subpass,
1441 .dependencyCount = 0,
1442 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001443 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001444 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001445
Chia-I Wu63636062015-10-26 21:10:41 +08001446 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001447 assert(!err);
1448}
1449
Chia-I Wu46176f12015-10-31 00:31:16 +08001450static VkShaderModule demo_prepare_shader_module(struct demo* demo,
Chia-I Wu46176f12015-10-31 00:31:16 +08001451 const void* code,
1452 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001453{
Chia-I Wu46176f12015-10-31 00:31:16 +08001454 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001455 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001456 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001457
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001458 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1459 moduleCreateInfo.pNext = NULL;
1460
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001461 moduleCreateInfo.codeSize = size;
1462 moduleCreateInfo.pCode = code;
1463 moduleCreateInfo.flags = 0;
1464 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1465 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001466
1467 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468}
1469
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001470char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001471{
1472 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001473 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001474 void *shader_code;
1475
1476 FILE *fp = fopen(filename, "rb");
1477 if (!fp) return NULL;
1478
1479 fseek(fp, 0L, SEEK_END);
1480 size = ftell(fp);
1481
1482 fseek(fp, 0L, SEEK_SET);
1483
1484 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001485 retval = fread(shader_code, size, 1, fp);
1486 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001487
1488 *psize = size;
1489
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001490 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001491 return shader_code;
1492}
1493
Chia-I Wu46176f12015-10-31 00:31:16 +08001494static VkShaderModule demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001495{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001496 void *vertShaderCode;
1497 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001498
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001499 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1500
Tony Barbourf331eb32015-11-06 10:10:37 -07001501 demo->vert_shader_module = demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001502
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001503 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001504
1505 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001506}
1507
Chia-I Wu46176f12015-10-31 00:31:16 +08001508static VkShaderModule demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001509{
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001510 void *fragShaderCode;
1511 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001512
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001513 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001514
Tony Barbourf331eb32015-11-06 10:10:37 -07001515 demo->frag_shader_module = demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001516
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001517 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001518
1519 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520}
1521
1522static void demo_prepare_pipeline(struct demo *demo)
1523{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001524 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001525 VkPipelineCacheCreateInfo pipelineCache;
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001526 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001527 VkPipelineInputAssemblyStateCreateInfo ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001528 VkPipelineRasterizationStateCreateInfo rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001529 VkPipelineColorBlendStateCreateInfo cb;
1530 VkPipelineDepthStencilStateCreateInfo ds;
1531 VkPipelineViewportStateCreateInfo vp;
1532 VkPipelineMultisampleStateCreateInfo ms;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001533 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
Piers Daniellba839d12015-09-29 13:01:09 -06001534 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001535 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001536
Piers Daniellba839d12015-09-29 13:01:09 -06001537 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1538 memset(&dynamicState, 0, sizeof dynamicState);
1539 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1540 dynamicState.pDynamicStates = dynamicStateEnables;
1541
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001542 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001543 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001544 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001545
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001546 memset(&vi, 0, sizeof(vi));
1547 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1548
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001549 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001550 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001551 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001552
1553 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001554 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1555 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001556 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001557 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001558 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001559 rs.rasterizerDiscardEnable = VK_FALSE;
1560 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561
1562 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001563 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1564 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001565 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001566 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001567 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001568 cb.attachmentCount = 1;
1569 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570
Tony Barbour29645d02015-01-16 14:27:35 -07001571 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001572 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001573 vp.viewportCount = 1;
Piers Daniellba839d12015-09-29 13:01:09 -06001574 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
1575 vp.scissorCount = 1;
1576 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001577
1578 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001579 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001580 ds.depthTestEnable = VK_TRUE;
1581 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001582 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001583 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001584 ds.back.failOp = VK_STENCIL_OP_KEEP;
1585 ds.back.passOp = VK_STENCIL_OP_KEEP;
1586 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001587 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001588 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001589
Tony Barbour29645d02015-01-16 14:27:35 -07001590 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001591 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001592 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001593 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001594
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001595 // Two stages: vs and fs
1596 pipeline.stageCount = 2;
1597 VkPipelineShaderStageCreateInfo shaderStages[2];
1598 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1599
1600 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001601 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
1602 shaderStages[0].module = demo_prepare_vs(demo);
1603 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001604
1605 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Chia-I Wu46176f12015-10-31 00:31:16 +08001606 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
1607 shaderStages[1].module = demo_prepare_fs(demo);
1608 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001609
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001610 memset(&pipelineCache, 0, sizeof(pipelineCache));
1611 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001612
Chia-I Wu63636062015-10-26 21:10:41 +08001613 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001614 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001615
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001616 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001617 pipeline.pInputAssemblyState = &ia;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001618 pipeline.pRasterizationState = &rs;
Tony Barbour194bf282015-07-10 15:29:03 -06001619 pipeline.pColorBlendState = &cb;
1620 pipeline.pMultisampleState = &ms;
1621 pipeline.pViewportState = &vp;
1622 pipeline.pDepthStencilState = &ds;
1623 pipeline.pStages = shaderStages;
Tony Barbour13ed3222015-08-06 14:32:54 -06001624 pipeline.renderPass = demo->render_pass;
Piers Daniellba839d12015-09-29 13:01:09 -06001625 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001626
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001627 pipeline.renderPass = demo->render_pass;
1628
Chia-I Wu63636062015-10-26 21:10:41 +08001629 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache,
1630 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631 assert(!err);
1632
Chia-I Wu63636062015-10-26 21:10:41 +08001633 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1634 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001635}
1636
Chia-I Wu63ea9262015-03-26 13:14:16 +08001637static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001638{
Chia-I Wuf051d262015-10-27 19:25:11 +08001639 const VkDescriptorPoolSize type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001640 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001641 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001642 .descriptorCount = 1,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001643 },
1644 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001645 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001646 .descriptorCount = DEMO_TEXTURE_COUNT,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001647 },
1648 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001649 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001650 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001651 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001652 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001653 .poolSizeCount = 2,
1654 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001655 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001656 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001657
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001658 err = vkCreateDescriptorPool(demo->device,
Chia-I Wu63636062015-10-26 21:10:41 +08001659 &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001660 assert(!err);
1661}
1662
1663static void demo_prepare_descriptor_set(struct demo *demo)
1664{
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001665 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001666 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001667 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001668 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001669
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001670 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001671 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001672 .pNext = NULL,
1673 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001674 .descriptorSetCount = 1,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001675 .pSetLayouts = &demo->desc_layout
1676 };
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001677 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001678 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001679
Chia-I Wuae721ba2015-05-25 16:27:55 +08001680 memset(&tex_descs, 0, sizeof(tex_descs));
1681 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001682 tex_descs[i].sampler = demo->textures[i].sampler;
1683 tex_descs[i].imageView = demo->textures[i].view;
1684 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001685 }
1686
1687 memset(&writes, 0, sizeof(writes));
1688
1689 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001690 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001691 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001692 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001693 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001694
1695 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001696 writes[1].dstSet = demo->desc_set;
1697 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001698 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001699 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001700 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001701
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001702 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001703}
1704
Chia-I Wuf973e322015-07-08 13:34:24 +08001705static void demo_prepare_framebuffers(struct demo *demo)
1706{
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001707 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001708 attachments[1] = demo->depth.view;
1709
Chia-I Wuf973e322015-07-08 13:34:24 +08001710 const VkFramebufferCreateInfo fb_info = {
1711 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1712 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001713 .renderPass = demo->render_pass,
1714 .attachmentCount = 2,
1715 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001716 .width = demo->width,
1717 .height = demo->height,
1718 .layers = 1,
1719 };
1720 VkResult U_ASSERT_ONLY err;
1721 uint32_t i;
1722
Tony Barbourd8e504f2015-10-08 14:26:24 -06001723 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * sizeof(VkFramebuffer));
1724 assert(demo->framebuffers);
1725
1726 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001727 attachments[0] = demo->buffers[i].view;
Chia-I Wu63636062015-10-26 21:10:41 +08001728 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001729 assert(!err);
1730 }
1731}
1732
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733static void demo_prepare(struct demo *demo)
1734{
Cody Northrop91e221b2015-07-09 18:08:32 -06001735 VkResult U_ASSERT_ONLY err;
1736
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001737 const VkCommandPoolCreateInfo cmd_pool_info = {
1738 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001739 .pNext = NULL,
1740 .queueFamilyIndex = demo->graphics_queue_node_index,
1741 .flags = 0,
1742 };
Chia-I Wu63636062015-10-26 21:10:41 +08001743 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001744 assert(!err);
1745
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001746 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001747 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001748 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001749 .commandPool = demo->cmd_pool,
1750 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001751 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753
1754 demo_prepare_buffers(demo);
1755 demo_prepare_depth(demo);
1756 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001757 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001759 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001760 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001761 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001762
Ian Elliotta0781972015-08-21 15:09:33 -06001763 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001764 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001765 assert(!err);
1766 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001767
Chia-I Wu63ea9262015-03-26 13:14:16 +08001768 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001769 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001770
Chia-I Wuf973e322015-07-08 13:34:24 +08001771 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001772
Ian Elliotta0781972015-08-21 15:09:33 -06001773 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001774 demo->current_buffer = i;
1775 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1776 }
1777
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001778 /*
1779 * Prepare functions above may generate pipeline commands
1780 * that need to be flushed before beginning the render loop.
1781 */
1782 demo_flush_init_cmd(demo);
1783
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001784 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001785 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001786}
1787
David Pinedo2bb7c932015-06-18 17:03:14 -06001788static void demo_cleanup(struct demo *demo)
1789{
1790 uint32_t i;
1791
1792 demo->prepared = false;
1793
Tony Barbourd8e504f2015-10-08 14:26:24 -06001794 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001795 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001796 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001797 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001798 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001799
Chia-I Wu63636062015-10-26 21:10:41 +08001800 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1801 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1802 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1803 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1804 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001805
1806 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001807 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1808 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1809 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1810 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001811 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001812 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001813
Chia-I Wu63636062015-10-26 21:10:41 +08001814 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1815 vkDestroyImage(demo->device, demo->depth.image, NULL);
1816 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001817
Chia-I Wu63636062015-10-26 21:10:41 +08001818 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1819 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001820
Ian Elliotta0781972015-08-21 15:09:33 -06001821 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001822 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001823 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001824 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001825 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001826
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001827 free(demo->queue_props);
1828
Chia-I Wu63636062015-10-26 21:10:41 +08001829 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1830 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001831 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001832 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001833 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001834 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001835 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001836
1837#ifndef _WIN32
1838 xcb_destroy_window(demo->connection, demo->window);
1839 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001840 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001841#endif // _WIN32
1842}
1843
Ian Elliottcf074d32015-10-16 18:02:43 -06001844static void demo_resize(struct demo *demo)
1845{
1846 uint32_t i;
1847
Mike Stroyanbb60b382015-10-28 09:46:57 -06001848 // Don't react to resize until after first initialization.
1849 if (!demo->prepared) {
1850 return;
1851 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001852 // In order to properly resize the window, we must re-create the swapchain
1853 // AND redo the command buffers, etc.
1854 //
1855 // First, perform part of the demo_cleanup() function:
1856 demo->prepared = false;
1857
1858 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001859 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001860 }
1861 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001862 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001863
Chia-I Wu63636062015-10-26 21:10:41 +08001864 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1865 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1866 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1867 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1868 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001869
1870 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001871 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1872 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1873 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1874 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001875 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001876
Chia-I Wu63636062015-10-26 21:10:41 +08001877 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1878 vkDestroyImage(demo->device, demo->depth.image, NULL);
1879 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001880
Chia-I Wu63636062015-10-26 21:10:41 +08001881 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1882 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001883
1884 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001885 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Ian Elliott55234c42015-10-27 11:06:33 -06001886 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001887 }
Chia-I Wu63636062015-10-26 21:10:41 +08001888 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001889 free(demo->buffers);
1890
Ian Elliottcf074d32015-10-16 18:02:43 -06001891 // Second, re-perform the demo_prepare() function, which will re-create the
1892 // swapchain:
1893 demo_prepare(demo);
1894}
1895
David Pinedo2bb7c932015-06-18 17:03:14 -06001896// On MS-Windows, make this a global, so it's available to WndProc()
1897struct demo demo;
1898
Ian Elliott639ca472015-04-16 15:23:05 -06001899#ifdef _WIN32
1900static void demo_run(struct demo *demo)
1901{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001902 if (!demo->prepared)
1903 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001904 // Wait for work to finish before updating MVP.
1905 vkDeviceWaitIdle(demo->device);
1906 demo_update_data_buffer(demo);
1907
1908 demo_draw(demo);
1909
1910 // Wait for work to finish before updating MVP.
1911 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001912
David Pinedo2bb7c932015-06-18 17:03:14 -06001913 demo->curFrame++;
1914
1915 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1916 {
1917 demo->quit=true;
1918 demo_cleanup(demo);
1919 ExitProcess(0);
1920 }
1921
1922}
Ian Elliott639ca472015-04-16 15:23:05 -06001923
1924// MS-Windows event handling function:
1925LRESULT CALLBACK WndProc(HWND hWnd,
1926 UINT uMsg,
1927 WPARAM wParam,
1928 LPARAM lParam)
1929{
Ian Elliott639ca472015-04-16 15:23:05 -06001930 switch(uMsg)
1931 {
Ian Elliottaaae5352015-07-06 14:27:58 -06001932 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001933 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001934 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001935 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001936 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001937 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001938 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001939 demo.width = lParam & 0xffff;
1940 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001941 demo_resize(&demo);
1942 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001943 default:
1944 break;
1945 }
1946 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1947}
1948
1949static void demo_create_window(struct demo *demo)
1950{
1951 WNDCLASSEX win_class;
1952
1953 // Initialize the window class structure:
1954 win_class.cbSize = sizeof(WNDCLASSEX);
1955 win_class.style = CS_HREDRAW | CS_VREDRAW;
1956 win_class.lpfnWndProc = WndProc;
1957 win_class.cbClsExtra = 0;
1958 win_class.cbWndExtra = 0;
1959 win_class.hInstance = demo->connection; // hInstance
1960 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1961 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1962 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1963 win_class.lpszMenuName = NULL;
1964 win_class.lpszClassName = demo->name;
1965 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1966 // Register window class:
1967 if (!RegisterClassEx(&win_class)) {
1968 // It didn't work, so try to give a useful error:
1969 printf("Unexpected error trying to start the application!\n");
1970 fflush(stdout);
1971 exit(1);
1972 }
1973 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001974 RECT wr = { 0, 0, demo->width, demo->height };
1975 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001976 demo->window = CreateWindowEx(0,
1977 demo->name, // class name
1978 demo->name, // app name
1979 WS_OVERLAPPEDWINDOW | // window style
1980 WS_VISIBLE |
1981 WS_SYSMENU,
1982 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001983 wr.right-wr.left, // width
1984 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001985 NULL, // handle to parent
1986 NULL, // handle to menu
1987 demo->connection, // hInstance
1988 NULL); // no extra parameters
1989 if (!demo->window) {
1990 // It didn't work, so try to give a useful error:
1991 printf("Cannot create a window in which to draw!\n");
1992 fflush(stdout);
1993 exit(1);
1994 }
1995}
1996#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001997static void demo_handle_event(struct demo *demo,
1998 const xcb_generic_event_t *event)
1999{
Piers Daniell735ee532015-02-23 16:23:13 -07002000 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002001 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002002 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002003 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002004 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002005 case XCB_CLIENT_MESSAGE:
2006 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
2007 (*demo->atom_wm_delete_window).atom) {
2008 demo->quit = true;
2009 }
2010 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002011 case XCB_KEY_RELEASE:
2012 {
2013 const xcb_key_release_event_t *key =
2014 (const xcb_key_release_event_t *) event;
2015
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002016 switch (key->detail) {
2017 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002018 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002019 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002020 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002021 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06002022 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002023 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002024 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002025 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002026 case 0x41:
2027 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07002028 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002029 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030 }
2031 break;
Ian Elliottcf074d32015-10-16 18:02:43 -06002032 case XCB_CONFIGURE_NOTIFY:
2033 {
2034 const xcb_configure_notify_event_t *cfg =
2035 (const xcb_configure_notify_event_t *) event;
2036 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2037 demo_resize(demo);
2038 }
2039 }
2040 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002041 default:
2042 break;
2043 }
2044}
2045
2046static void demo_run(struct demo *demo)
2047{
2048 xcb_flush(demo->connection);
2049
2050 while (!demo->quit) {
2051 xcb_generic_event_t *event;
2052
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002053 if (demo->pause) {
2054 event = xcb_wait_for_event(demo->connection);
2055 } else {
2056 event = xcb_poll_for_event(demo->connection);
2057 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002058 if (event) {
2059 demo_handle_event(demo, event);
2060 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002061 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002062
2063 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002064 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002065 demo_update_data_buffer(demo);
2066
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002067 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002068
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002069 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002070 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002071 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002072 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002073 demo->quit = true;
2074
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002075 }
2076}
2077
2078static void demo_create_window(struct demo *demo)
2079{
2080 uint32_t value_mask, value_list[32];
2081
2082 demo->window = xcb_generate_id(demo->connection);
2083
2084 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2085 value_list[0] = demo->screen->black_pixel;
2086 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002087 XCB_EVENT_MASK_EXPOSURE |
2088 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002089
2090 xcb_create_window(demo->connection,
2091 XCB_COPY_FROM_PARENT,
2092 demo->window, demo->screen->root,
2093 0, 0, demo->width, demo->height, 0,
2094 XCB_WINDOW_CLASS_INPUT_OUTPUT,
2095 demo->screen->root_visual,
2096 value_mask, value_list);
2097
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002098 /* Magic code that will send notification when window is destroyed */
2099 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
2100 "WM_PROTOCOLS");
2101 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2102
2103 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2104 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2105
2106 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
2107 demo->window, (*reply).atom, 4, 32, 1,
2108 &(*demo->atom_wm_delete_window).atom);
2109 free(reply);
2110
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002111 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002112
2113 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
2114 const uint32_t coords[] = {100, 100};
2115 xcb_configure_window(demo->connection, demo->window,
2116 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002117}
Ian Elliott639ca472015-04-16 15:23:05 -06002118#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002119
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002120/*
2121 * Return 1 (true) if all layer names specified in check_names
2122 * can be found in given layer properties.
2123 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002124static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002125 uint32_t layer_count, VkLayerProperties *layers)
2126{
2127 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002128 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002129 for (uint32_t j = 0; j < layer_count; j++) {
2130 if (!strcmp(check_names[i], layers[j].layerName)) {
2131 found = 1;
2132 }
2133 }
2134 if (!found) {
2135 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2136 return 0;
2137 }
2138 }
2139 return 1;
2140}
2141
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002142static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002143{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002144 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002145 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002146 VkExtensionProperties *instance_extensions;
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002147 VkPhysicalDevice *physical_devices;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002148 VkLayerProperties *instance_layers;
2149 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002150 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002151 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002152 uint32_t device_validation_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002153 uint32_t enabled_extension_count = 0;
2154 uint32_t enabled_layer_count = 0;
2155
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002156 char *instance_validation_layers[] = {
Mark Lobodzinski7ce1eb52015-12-30 08:16:12 -07002157 "VK_LAYER_LUNARG_threading",
2158 "VK_LAYER_LUNARG_mem_tracker",
2159 "VK_LAYER_LUNARG_object_tracker",
2160 "VK_LAYER_LUNARG_draw_state",
2161 "VK_LAYER_LUNARG_param_checker",
2162 "VK_LAYER_LUNARG_swapchain",
2163 "VK_LAYER_LUNARG_device_limits",
2164 "VK_LAYER_LUNARG_image",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002165 };
2166
Tony Barbourda2bad52016-01-22 14:36:40 -07002167 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_threading";
2168 demo->device_validation_layers[1] = "VK_LAYER_LUNARG_mem_tracker";
2169 demo->device_validation_layers[2] = "VK_LAYER_LUNARG_object_tracker",
2170 demo->device_validation_layers[3] = "VK_LAYER_LUNARG_draw_state",
2171 demo->device_validation_layers[4] = "VK_LAYER_LUNARG_param_checker",
2172 demo->device_validation_layers[5] = "VK_LAYER_LUNARG_swapchain",
2173 demo->device_validation_layers[6] = "VK_LAYER_LUNARG_device_limits",
2174 demo->device_validation_layers[7] = "VK_LAYER_LUNARG_image",
2175 device_validation_layer_count = 8;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002176
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002177 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002178 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002179 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002180 assert(!err);
2181
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002182 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002183 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002184 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002185
2186 if (demo->validate) {
2187 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2188 instance_layer_count, instance_layers);
2189 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002190 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002191 "required validation layer.\n\n"
2192 "Please look at the Getting Started guide for additional "
2193 "information.\n",
2194 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002195 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002196 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002197 }
2198
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002199 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002200 assert(!err);
2201
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002202 VkBool32 surfaceExtFound = 0;
2203 VkBool32 platformSurfaceExtFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002204 memset(extension_names, 0, sizeof(extension_names));
2205 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002206 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002207 assert(!err);
2208 for (uint32_t i = 0; i < instance_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002209 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002210 surfaceExtFound = 1;
Ian Elliottf4a4e442015-11-17 17:29:40 -07002211 extension_names[enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002212 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002213#ifdef _WIN32
2214 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2215 platformSurfaceExtFound = 1;
2216 extension_names[enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2217 }
2218#else // _WIN32
2219 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2220 platformSurfaceExtFound = 1;
2221 extension_names[enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2222 }
2223#endif // _WIN32
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002224 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002225 if (demo->validate) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002226 extension_names[enabled_extension_count++] = VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002227 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002228 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002229 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002230 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002231 if (!surfaceExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002232 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002233 VK_KHR_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002234 "Vulkan installable client driver (ICD) installed?\nPlease "
2235 "look at the Getting Started guide for additional "
2236 "information.\n",
2237 "vkCreateInstance Failure");
2238 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002239 if (!platformSurfaceExtFound) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002240#ifdef _WIN32
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002241 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002242 VK_KHR_WIN32_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002243 "Vulkan installable client driver (ICD) installed?\nPlease "
2244 "look at the Getting Started guide for additional "
2245 "information.\n",
2246 "vkCreateInstance Failure");
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002247#else // _WIN32
2248 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
2249 VK_KHR_XCB_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
2250 "Vulkan installable client driver (ICD) installed?\nPlease "
2251 "look at the Getting Started guide for additional "
2252 "information.\n",
2253 "vkCreateInstance Failure");
2254#endif // _WIN32
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002255 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002256 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002257 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002258 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002259 .pApplicationName = APP_SHORT_NAME,
2260 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002261 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002262 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002263 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002264 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002265 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002266 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002267 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002268 .pApplicationInfo = &app,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002269 .enabledLayerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002270 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002271 .enabledExtensionCount = enabled_extension_count,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002272 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002273 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002274
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002275 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002276
Chia-I Wu63636062015-10-26 21:10:41 +08002277 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002278 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2279 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002280 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002281 "additional information.\n",
2282 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002283 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002284 ERR_EXIT("Cannot find a specified extension library"
2285 ".\nMake sure your layers path is set appropriately\n",
2286 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002287 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002288 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2289 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002290 "the Getting Started guide for additional information.\n",
2291 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002292 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002293
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002294 free(instance_layers);
2295 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002296
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002297 /* Make initial call to query gpu_count, then second call for gpu info*/
2298 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2299 assert(!err && gpu_count > 0);
2300 physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2301 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2302 assert(!err);
2303 /* For cube demo we just grab the first physical device */
2304 demo->gpu = physical_devices[0];
2305 free(physical_devices);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002306
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002307 /* Look for validation layers */
2308 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002309 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002310 uint32_t device_layer_count = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002311 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002312 assert(!err);
2313
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002314 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002315 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002316 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002317
2318 if (demo->validate) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002319 validation_found = demo_check_layers(device_validation_layer_count, demo->device_validation_layers,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002320 device_layer_count, device_layers);
2321 if (!validation_found) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002322 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002323 "a required validation layer.\n\n"
2324 "Please look at the Getting Started guide for additional "
2325 "information.\n",
2326 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002327 }
Tony Barbourda2bad52016-01-22 14:36:40 -07002328 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002329 }
2330
2331 uint32_t device_extension_count = 0;
2332 VkExtensionProperties *device_extensions = NULL;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002333 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002334 demo->gpu, NULL, &device_extension_count, NULL);
2335 assert(!err);
2336
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002337 VkBool32 swapchainExtFound = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002338 demo->enabled_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002339 memset(extension_names, 0, sizeof(extension_names));
2340 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002341 err = vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002342 demo->gpu, NULL, &device_extension_count, device_extensions);
2343 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002344
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002345 for (uint32_t i = 0; i < device_extension_count; i++) {
Ian Elliottf4a4e442015-11-17 17:29:40 -07002346 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Tony Barbourcc69f452015-10-08 13:59:42 -06002347 swapchainExtFound = 1;
Tony Barbourda2bad52016-01-22 14:36:40 -07002348 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002349 }
Tony Barbourda2bad52016-01-22 14:36:40 -07002350 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002351 }
Tony Barbourcc69f452015-10-08 13:59:42 -06002352 if (!swapchainExtFound) {
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002353 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the "
Ian Elliottf4a4e442015-11-17 17:29:40 -07002354 VK_KHR_SWAPCHAIN_EXTENSION_NAME" extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002355 "Vulkan installable client driver (ICD) installed?\nPlease "
2356 "look at the Getting Started guide for additional "
2357 "information.\n",
2358 "vkCreateInstance Failure");
2359 }
2360
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002361 if (demo->validate) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002362 demo->CreateDebugReportCallback = (PFN_vkCreateDebugReportCallbackEXT) vkGetInstanceProcAddr(demo->inst, "vkCreateDebugReportCallbackEXT");
2363 demo->DestroyDebugReportCallback = (PFN_vkDestroyDebugReportCallbackEXT) vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002364 if (!demo->CreateDebugReportCallback) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002365 ERR_EXIT("GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002366 "vkGetProcAddr Failure");
2367 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002368 if (!demo->DestroyDebugReportCallback) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002369 ERR_EXIT("GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
Tony Barbourd70b42c2015-06-30 14:14:19 -06002370 "vkGetProcAddr Failure");
2371 }
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002372 demo->DebugReportMessage = (PFN_vkDebugReportMessageEXT) vkGetInstanceProcAddr(demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002373 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002374 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002375 "vkGetProcAddr Failure");
2376 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002377
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002378 PFN_vkDebugReportCallbackEXT callback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002379
2380 if (!demo->use_break) {
2381 callback = dbgFunc;
2382 } else {
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002383 callback = dbgFunc;
2384 // TODO add a break callback defined locally since there is no longer
2385 // one included in the loader
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002386 }
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002387 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
2388 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002389 dbgCreateInfo.pNext = NULL;
2390 dbgCreateInfo.pfnCallback = callback;
2391 dbgCreateInfo.pUserData = NULL;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002392 dbgCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002393 err = demo->CreateDebugReportCallback(
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002394 demo->inst,
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002395 &dbgCreateInfo,
2396 NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002397 &demo->msg_callback);
2398 switch (err) {
2399 case VK_SUCCESS:
2400 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002401 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002402 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2403 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002404 break;
2405 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002406 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2407 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002408 break;
2409 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002410 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002411 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002412
2413 /* Call with NULL data to get count */
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002414 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002415 assert(demo->queue_count >= 1);
2416
2417 demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties));
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002418 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002419 // Find a queue that supports gfx
2420 uint32_t gfx_queue_idx = 0;
2421 for (gfx_queue_idx = 0; gfx_queue_idx<demo->queue_count; gfx_queue_idx++) {
2422 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2423 break;
2424 }
2425 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002426 // Query fine-grained feature support for this device.
2427 // If app has specific feature requirements it should check supported features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002428 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002429 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002430
Tony Barbourda2bad52016-01-22 14:36:40 -07002431 free(device_layers);
2432
2433 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2434 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2435 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2436 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2437 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2438}
2439
2440static void demo_create_device(struct demo *demo)
2441{
2442 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002443 float queue_priorities[1] = { 0.0 };
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002444 const VkDeviceQueueCreateInfo queue = {
2445 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2446 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002447 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002448 .queueCount = 1,
Courtney Goeltzenleuchter2aff13f2015-10-23 10:37:02 -06002449 .pQueuePriorities = queue_priorities
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002450 };
2451
2452 VkDeviceCreateInfo device = {
2453 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2454 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002455 .queueCreateInfoCount = 1,
2456 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002457 .enabledLayerCount = demo->enabled_layer_count,
2458 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? demo->device_validation_layers : NULL),
2459 .enabledExtensionCount = demo->enabled_extension_count,
2460 .ppEnabledExtensionNames = (const char *const*) demo->extension_names,
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002461 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002462 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002463
Chia-I Wu63636062015-10-26 21:10:41 +08002464 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002465 assert(!err);
2466
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002467}
2468
Tony Barbourcc69f452015-10-08 13:59:42 -06002469static void demo_init_vk_swapchain(struct demo *demo)
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002470{
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002471 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002472 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002473
Ian Elliottb08e0d92015-11-20 11:55:46 -07002474 // Create a WSI surface for the window:
Ian Elliottaaae5352015-07-06 14:27:58 -06002475#ifdef _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002476 VkWin32SurfaceCreateInfoKHR createInfo;
2477 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2478 createInfo.pNext = NULL;
2479 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002480 createInfo.hinstance = demo->connection;
2481 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002482
2483 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo,
Ian Elliottb08e0d92015-11-20 11:55:46 -07002484 NULL, &demo->surface);
2485
Ian Elliottaaae5352015-07-06 14:27:58 -06002486#else // _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002487 VkXcbSurfaceCreateInfoKHR createInfo;
2488 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2489 createInfo.pNext = NULL;
2490 createInfo.flags = 0;
2491 createInfo.connection = demo->connection;
2492 createInfo.window = demo->window;
2493
2494 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo,
Ian Elliottb08e0d92015-11-20 11:55:46 -07002495 NULL, &demo->surface);
Ian Elliottaaae5352015-07-06 14:27:58 -06002496#endif // _WIN32
2497
Tony Barbourcc69f452015-10-08 13:59:42 -06002498 // Iterate over each queue to learn whether it supports presenting:
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002499 VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
2500 for (i = 0; i < demo->queue_count; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -06002501 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i,
Ian Elliottb08e0d92015-11-20 11:55:46 -07002502 demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002503 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002504 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002505
2506 // Search for a graphics and a present queue in the array of queue
2507 // families, try to find one that supports both
2508 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
2509 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002510 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002511 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2512 if (graphicsQueueNodeIndex == UINT32_MAX) {
2513 graphicsQueueNodeIndex = i;
2514 }
2515
2516 if (supportsPresent[i] == VK_TRUE) {
2517 graphicsQueueNodeIndex = i;
2518 presentQueueNodeIndex = i;
2519 break;
2520 }
2521 }
2522 }
2523 if (presentQueueNodeIndex == UINT32_MAX) {
2524 // If didn't find a queue that supports both graphics and present, then
2525 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002526 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002527 if (supportsPresent[i] == VK_TRUE) {
2528 presentQueueNodeIndex = i;
2529 break;
2530 }
2531 }
2532 }
2533 free(supportsPresent);
2534
2535 // Generate error if could not find both a graphics and a present queue
2536 if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) {
2537 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002538 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002539 }
2540
2541 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002542 // synchronization, and appropriate tracking for QueueSubmit.
2543 // NOTE: While it is possible for an application to use a separate graphics
2544 // and a present queues, this demo program assumes it is only using
2545 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002546 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2547 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002548 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002549 }
2550
2551 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002552
Tony Barbourda2bad52016-01-22 14:36:40 -07002553 demo_create_device(demo);
2554
2555 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2556 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2557 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2558 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2559 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2560
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002561 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002562 0, &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002563
Ian Elliottaaae5352015-07-06 14:27:58 -06002564 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002565 uint32_t formatCount;
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002566 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002567 demo->surface,
2568 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002569 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002570 VkSurfaceFormatKHR *surfFormats =
2571 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002572 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002573 demo->surface,
2574 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002575 assert(!err);
2576 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2577 // the surface has no preferred format. Otherwise, at least one
2578 // supported format will be returned.
Ian Elliottaaae5352015-07-06 14:27:58 -06002579 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED)
2580 {
2581 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
2582 }
2583 else
2584 {
2585 assert(formatCount >= 1);
2586 demo->format = surfFormats[0].format;
2587 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002588 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002589
David Pinedo2bb7c932015-06-18 17:03:14 -06002590 demo->quit = false;
2591 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002592
2593 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002594 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002595}
2596
2597static void demo_init_connection(struct demo *demo)
2598{
Ian Elliott639ca472015-04-16 15:23:05 -06002599#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002600 const xcb_setup_t *setup;
2601 xcb_screen_iterator_t iter;
2602 int scr;
2603
2604 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002605 if (demo->connection == NULL) {
2606 printf("Cannot find a compatible Vulkan installable client driver "
2607 "(ICD).\nExiting ...\n");
2608 fflush(stdout);
2609 exit(1);
2610 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002611
2612 setup = xcb_get_setup(demo->connection);
2613 iter = xcb_setup_roots_iterator(setup);
2614 while (scr-- > 0)
2615 xcb_screen_next(&iter);
2616
2617 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002618#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002619}
2620
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002621static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002622{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002623 vec3 eye = {0.0f, 3.0f, 5.0f};
2624 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002625 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002626
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002627 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002628 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002629
Piers Daniell735ee532015-02-23 16:23:13 -07002630 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002631 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002632 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002633 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002634 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002635 if (strcmp(argv[i], "--break") == 0) {
2636 demo->use_break = true;
2637 continue;
2638 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002639 if (strcmp(argv[i], "--validate") == 0) {
2640 demo->validate = true;
2641 continue;
2642 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002643 if (strcmp(argv[i], "--c") == 0 &&
Tony Barbour1a86d032015-09-21 15:17:33 -06002644 demo->frameCount == INT32_MAX &&
David Pinedo2bb7c932015-06-18 17:03:14 -06002645 i < argc-1 &&
2646 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2647 demo->frameCount >= 0)
2648 {
2649 i++;
2650 continue;
2651 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002652
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002653 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002654 fflush(stderr);
2655 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002656 }
2657
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002658 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002659 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002660
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002661 demo->width = 500;
2662 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002663
2664 demo->spin_angle = 0.01f;
2665 demo->spin_increment = 0.01f;
2666 demo->pause = false;
2667
Piers Daniell735ee532015-02-23 16:23:13 -07002668 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002669 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2670 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002671}
2672
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002673
Ian Elliott639ca472015-04-16 15:23:05 -06002674#ifdef _WIN32
Mark Young418b9d12016-01-06 14:26:04 -07002675// Include header required for parsing the command line options.
2676#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002677
Ian Elliotta748eaf2015-04-28 15:50:36 -06002678int WINAPI WinMain(HINSTANCE hInstance,
2679 HINSTANCE hPrevInstance,
2680 LPSTR pCmdLine,
2681 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002682{
2683 MSG msg; // message
Mark Young418b9d12016-01-06 14:26:04 -07002684 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002685 int argc;
2686 char** argv;
Ian Elliott639ca472015-04-16 15:23:05 -06002687
Mark Young418b9d12016-01-06 14:26:04 -07002688 // Use the CommandLine functions to get the command line arguments. Unfortunately, Microsoft outputs
2689 // this information as wide characters for Unicode, and we simply want the Ascii version to be compatible
2690 // with the non-Windows side. So, we have to convert the information to Ascii character strings.
2691 LPWSTR* commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
2692 if (NULL == commandLineArgs)
2693 {
2694 argc = 0;
2695 }
2696
2697 if (argc > 0)
2698 {
2699 argv = (char**)malloc(sizeof(char*) * argc);
2700 if (argv == NULL)
2701 {
2702 argc = 0;
2703 }
2704 else
2705 {
2706 for (int iii = 0; iii < argc; iii++)
2707 {
2708 size_t wideCharLen = wcslen(commandLineArgs[iii]);
2709 size_t numConverted = 0;
2710
2711 argv[iii] = (char*)malloc(sizeof(char) * (wideCharLen + 1));
2712 if (argv[iii] != NULL)
2713 {
2714 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
2715 }
2716 }
2717 }
2718 }
2719 else
2720 {
2721 argv = NULL;
2722 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002723
2724 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07002725
2726 // Free up the items we had to allocate for the command line arguments.
2727 if (argc > 0 && argv != NULL)
2728 {
2729 for (int iii = 0; iii < argc; iii++)
2730 {
2731 if (argv[iii] != NULL)
2732 {
2733 free(argv[iii]);
2734 }
2735 }
2736 free(argv);
2737 }
2738
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002739 demo.connection = hInstance;
2740 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002741 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002742 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002743
2744 demo_prepare(&demo);
2745
2746 done = false; //initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07002747
2748 // main message loop
Ian Elliott639ca472015-04-16 15:23:05 -06002749 while(!done)
2750 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002751 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002752 if (msg.message == WM_QUIT) //check for a quit message
2753 {
2754 done = true; //if found, quit app
2755 }
2756 else
2757 {
2758 /* Translate and dispatch to event queue*/
2759 TranslateMessage(&msg);
2760 DispatchMessage(&msg);
2761 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002762 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002763 }
2764
2765 demo_cleanup(&demo);
2766
Tony Barbourf9921732015-04-22 11:36:22 -06002767 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002768}
2769#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002770int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002771{
2772 struct demo demo;
2773
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002774 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002775 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002776 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002777
2778 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002779 demo_run(&demo);
2780
2781 demo_cleanup(&demo);
2782
2783 return 0;
2784}
Ian Elliott639ca472015-04-16 15:23:05 -06002785#endif // _WIN32