blob: 02e75a5ab263358c144b24be26d185cf53a9df77 [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
Karl Schultze4dc75c2016-02-02 15:37:51 -07002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
Ian Elliotta748eaf2015-04-28 15:50:36 -06005 *
Karl Schultze4dc75c2016-02-02 15:37:51 -07006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
Ian Elliotta748eaf2015-04-28 15:50:36 -060012 *
Karl Schultze4dc75c2016-02-02 15:37:51 -070013 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
Ian Elliotta748eaf2015-04-28 15:50:36 -060015 *
Karl Schultze4dc75c2016-02-02 15:37:51 -070016 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Ian Elliotta748eaf2015-04-28 15:50:36 -060017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultze4dc75c2016-02-02 15:37:51 -070018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060024 *
25 * Author: Chia-I Wu <olv@lunarg.com>
26 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
27 * Author: Ian Elliott <ian@LunarG.com>
28 * Author: Jon Ashburn <jon@lunarg.com>
Ian Elliotta748eaf2015-04-28 15:50:36 -060029 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070030
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060031#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060032#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <stdbool.h>
36#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070037#include <signal.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060038
Ian Elliott639ca472015-04-16 15:23:05 -060039#ifdef _WIN32
40#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060041#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060042#endif // _WIN32
43
David Pinedoc5193c12015-11-06 12:54:48 -070044#include <vulkan/vulkan.h>
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -070045#include <vulkan/vk_ext_debug_report.h>
Ian Elliottb08e0d92015-11-20 11:55:46 -070046
David Pinedo541526f2015-11-24 09:00:24 -070047#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060048#include "linmath.h"
49
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060050#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060051#define APP_SHORT_NAME "cube"
52#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060053
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060054#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
55
Tony Barbourfdc2d352015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Ian Elliott07264132015-04-28 11:35:02 -060062#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -070063#define ERR_EXIT(err_msg, err_class) \
64 do { \
65 MessageBox(NULL, err_msg, err_class, MB_OK); \
66 exit(1); \
67 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060068
Karl Schultze4dc75c2016-02-02 15:37:51 -070069#else // _WIN32
Ian Elliott07264132015-04-28 11:35:02 -060070
Karl Schultze4dc75c2016-02-02 15:37:51 -070071#define ERR_EXIT(err_msg, err_class) \
72 do { \
73 printf(err_msg); \
74 fflush(stdout); \
75 exit(1); \
76 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060077#endif // _WIN32
78
Karl Schultze4dc75c2016-02-02 15:37:51 -070079#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
80 { \
81 demo->fp##entrypoint = \
82 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
83 if (demo->fp##entrypoint == NULL) { \
84 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
85 "vkGetInstanceProcAddr Failure"); \
86 } \
87 }
Ian Elliottaaae5352015-07-06 14:27:58 -060088
Jon Ashburn7d8342c2015-10-08 15:58:23 -060089static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
90
Karl Schultze4dc75c2016-02-02 15:37:51 -070091#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
92 { \
93 if (!g_gdpa) \
94 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
95 demo->inst, "vkGetDeviceProcAddr"); \
96 demo->fp##entrypoint = \
97 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
98 if (demo->fp##entrypoint == NULL) { \
99 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
100 "vkGetDeviceProcAddr Failure"); \
101 } \
102 }
Ian Elliott673898b2015-06-22 15:07:49 -0600103
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600104/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700105 * structure to track all objects related to a texture.
106 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600107struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600108 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700109
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600110 VkImage image;
111 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600112
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800113 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500114 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600115 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700116 int32_t tex_width, tex_height;
117};
118
Karl Schultze4dc75c2016-02-02 15:37:51 -0700119static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600120
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600121struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600122 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700123 float mvp[4][4];
124 float position[12 * 3][4];
125 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600126};
127
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600128struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600129 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700130 float mvp[4][4];
131 float position[12 * 3][4];
132 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600133};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600134
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600135//--------------------------------------------------------------------------------------
136// Mesh and VertexFormat Data
137//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700138// clang-format off
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600139struct Vertex
140{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600141 float posX, posY, posZ, posW; // Position data
142 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600143};
144
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600145struct VertexPosTex
146{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600147 float posX, posY, posZ, posW; // Position data
148 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600149};
150
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600151#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600152#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600153
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600154static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800155 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 -1.0f,-1.0f, 1.0f,
157 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 -1.0f, 1.0f,-1.0f,
160 -1.0f,-1.0f,-1.0f,
161
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 1.0f, 1.0f,-1.0f,
164 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f,-1.0f,-1.0f,
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,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600171 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600174 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 -1.0f, 1.0f, 1.0f,
178 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600181 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800183 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 1.0f, 1.0f, 1.0f,
185 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800186 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 1.0f,-1.0f,-1.0f,
188 1.0f, 1.0f,-1.0f,
189
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800190 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600191 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600192 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800193 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600194 1.0f,-1.0f, 1.0f,
195 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600196};
197
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600198static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800199 0.0f, 0.0f, // -X side
200 1.0f, 0.0f,
201 1.0f, 1.0f,
202 1.0f, 1.0f,
203 0.0f, 1.0f,
204 0.0f, 0.0f,
205
206 1.0f, 0.0f, // -Z side
207 0.0f, 1.0f,
208 0.0f, 0.0f,
209 1.0f, 0.0f,
210 1.0f, 1.0f,
211 0.0f, 1.0f,
212
213 1.0f, 1.0f, // -Y side
214 1.0f, 0.0f,
215 0.0f, 0.0f,
216 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 0.0f, 0.0f,
218 0.0f, 1.0f,
219
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600221 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800222 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600223 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600226
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 1.0f, 1.0f, // +X side
228 0.0f, 1.0f,
229 0.0f, 0.0f,
230 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600231 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600232 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 0.0f, 1.0f, // +Z side
235 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600237 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800238 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600239 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600240};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700241// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600242
Karl Schultze4dc75c2016-02-02 15:37:51 -0700243void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600244 int i;
245
246 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700247 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600248 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
249 }
250 printf("\n");
251 fflush(stdout);
252}
253
Karl Schultze4dc75c2016-02-02 15:37:51 -0700254void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600255 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700256 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600257 printf("\n");
258 fflush(stdout);
259}
260
Karl Schultze4dc75c2016-02-02 15:37:51 -0700261VKAPI_ATTR VkBool32 VKAPI_CALL
262dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
263 uint64_t srcObject, size_t location, int32_t msgCode,
264 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
265 char *message = (char *)malloc(strlen(pMsg) + 100);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600266
Karl Schultze4dc75c2016-02-02 15:37:51 -0700267 assert(message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600268
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700269 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700270 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
271 pMsg);
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700272 } else if (msgFlags & VK_DEBUG_REPORT_WARN_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700273 // We know that we're submitting queues without fences, ignore this
274 // warning
275 if (strstr(pMsg,
276 "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 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700279 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
280 pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600281 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600282 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600283 }
284
285#ifdef _WIN32
286 MessageBox(NULL, message, "Alert", MB_OK);
287#else
Karl Schultze4dc75c2016-02-02 15:37:51 -0700288 printf("%s\n", message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600289 fflush(stdout);
290#endif
291 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600292
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600293 /*
294 * false indicates that layer should not bail-out of an
295 * API call that had validation failures. This may mean that the
296 * app dies inside the driver due to invalid parameter(s).
297 * That's what would happen without validation layers, so we'll
298 * keep that behavior here.
299 */
300 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600301}
302
Karl Schultze4dc75c2016-02-02 15:37:51 -0700303VkBool32 BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
304 uint64_t srcObject, size_t location, int32_t msgCode,
305 const char *pLayerPrefix, const char *pMsg,
306 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700307#ifndef WIN32
308 raise(SIGTRAP);
309#else
310 DebugBreak();
311#endif
312
313 return false;
314}
315
Ian Elliotta0781972015-08-21 15:09:33 -0600316typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600317 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800318 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600319 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600320} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600321
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600323#ifdef _WIN32
324#define APP_NAME_STR_LEN 80
325 HINSTANCE connection; // hInstance - Windows Instance
326 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700327 HWND window; // hWnd - window handle
328#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329 xcb_connection_t *connection;
330 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800331 xcb_window_t window;
332 xcb_intern_atom_reply_t *atom_wm_delete_window;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700333#endif // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -0700334 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600335 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700336 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600337
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600338 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600339 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600340 VkDevice device;
341 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700342 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600343 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600344 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600345 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600346
Tony Barbourda2bad52016-01-22 14:36:40 -0700347 uint32_t enabled_extension_count;
348 uint32_t enabled_layer_count;
349 char *extension_names[64];
350 char *device_validation_layers[64];
351
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600352 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600353 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600354 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355
Karl Schultze4dc75c2016-02-02 15:37:51 -0700356 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
357 fpGetPhysicalDeviceSurfaceSupportKHR;
358 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
359 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
360 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
361 fpGetPhysicalDeviceSurfaceFormatsKHR;
362 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
363 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600364 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
365 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
366 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
367 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
368 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600369 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600370 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600371 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800373 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
375 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600377
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600378 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800379 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500380 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600381 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600382 } depth;
383
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600384 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600385
386 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600387 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800388 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500389 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600390 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600391 } uniform_data;
392
Karl Schultze4dc75c2016-02-02 15:37:51 -0700393 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500394 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600395 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600396 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800397 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600398 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600399
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600400 mat4x4 projection_matrix;
401 mat4x4 view_matrix;
402 mat4x4 model_matrix;
403
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600404 float spin_angle;
405 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600406 bool pause;
407
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600408 VkShaderModule vert_shader_module;
409 VkShaderModule frag_shader_module;
410
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600411 VkDescriptorPool desc_pool;
412 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800413
Tony Barbourd8e504f2015-10-08 14:26:24 -0600414 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800415
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600416 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600417 int32_t curFrame;
418 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600419 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600420 bool use_break;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700421 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
422 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
423 VkDebugReportCallbackEXT msg_callback;
424 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600425
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600426 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600427 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600428};
429
Ian Elliottcf074d32015-10-16 18:02:43 -0600430// Forward declaration:
431static void demo_resize(struct demo *demo);
432
Karl Schultze4dc75c2016-02-02 15:37:51 -0700433static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
434 VkFlags requirements_mask,
435 uint32_t *typeIndex) {
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?
440 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
441 requirements_mask) == requirements_mask) {
442 *typeIndex = i;
443 return true;
444 }
445 }
446 typeBits >>= 1;
447 }
448 // No memory types matched, return failure
449 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600450}
451
Karl Schultze4dc75c2016-02-02 15:37:51 -0700452static void demo_flush_init_cmd(struct demo *demo) {
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
Karl Schultze4dc75c2016-02-02 15:37:51 -0700461 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800462 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700463 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
464 .pNext = NULL,
465 .waitSemaphoreCount = 0,
466 .pWaitSemaphores = NULL,
467 .pWaitDstStageMask = NULL,
468 .commandBufferCount = 1,
469 .pCommandBuffers = cmd_bufs,
470 .signalSemaphoreCount = 0,
471 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600472
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600473 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600474 assert(!err);
475
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600476 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477 assert(!err);
478
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600479 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600481}
482
Karl Schultze4dc75c2016-02-02 15:37:51 -0700483static void demo_set_image_layout(struct demo *demo, VkImage image,
484 VkImageAspectFlags aspectMask,
485 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700486 VkImageLayout new_image_layout,
487 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600488 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600489
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600490 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800491 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800492 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600493 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800494 .commandPool = demo->cmd_pool,
495 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700496 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600497 };
498
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800499 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600500 assert(!err);
501
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700502 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
503 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600504 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800505 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600506 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800507 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800508 .occlusionQueryEnable = VK_FALSE,
509 .queryFlags = 0,
510 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600511 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700512 VkCommandBufferBeginInfo cmd_buf_info = {
513 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
514 .pNext = NULL,
515 .flags = 0,
516 .pInheritanceInfo = &cmd_buf_hinfo,
517 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600518 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600519 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600520 }
521
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600522 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600523 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600524 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700525 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800526 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600527 .oldLayout = old_image_layout,
528 .newLayout = new_image_layout,
529 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700530 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600531
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800532 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600533 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800534 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600535 }
536
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700537 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700538 image_memory_barrier.dstAccessMask =
539 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700540 }
541
542 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700543 image_memory_barrier.dstAccessMask =
544 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700545 }
546
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600547 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600548 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700549 image_memory_barrier.dstAccessMask =
550 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600551 }
552
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600553 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600554
Tony Barbour50bbca42015-06-29 16:20:35 -0600555 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
556 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600557
Karl Schultze4dc75c2016-02-02 15:37:51 -0700558 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
559 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600560}
561
Karl Schultze4dc75c2016-02-02 15:37:51 -0700562static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700563 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
564 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600565 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800566 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600567 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800568 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800569 .occlusionQueryEnable = VK_FALSE,
570 .queryFlags = 0,
571 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700572 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700573 const VkCommandBufferBeginInfo cmd_buf_info = {
574 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
575 .pNext = NULL,
576 .flags = 0,
577 .pInheritanceInfo = &cmd_buf_hinfo,
578 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800579 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700580 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
581 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800582 };
583 const VkRenderPassBeginInfo rp_begin = {
584 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
585 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800586 .renderPass = demo->render_pass,
587 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800588 .renderArea.offset.x = 0,
589 .renderArea.offset.y = 0,
590 .renderArea.extent.width = demo->width,
591 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600592 .clearValueCount = 2,
593 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700594 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800595 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600597 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600598 assert(!err);
599
Chia-I Wuf051d262015-10-27 19:25:11 +0800600 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800601
Karl Schultze4dc75c2016-02-02 15:37:51 -0700602 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
603 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
604 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
605 NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600606
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600607 VkViewport viewport;
608 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700609 viewport.height = (float)demo->height;
610 viewport.width = (float)demo->width;
611 viewport.minDepth = (float)0.0f;
612 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700613 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600614
615 VkRect2D scissor;
616 memset(&scissor, 0, sizeof(scissor));
617 scissor.extent.width = demo->width;
618 scissor.extent.height = demo->height;
619 scissor.offset.x = 0;
620 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700621 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600622
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600623 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800624 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600625
Tony Barbour15a4ef62015-10-21 10:14:48 -0600626 VkImageMemoryBarrier prePresentBarrier = {
627 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
628 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800629 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700630 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600631 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700632 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600633 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800634 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700635 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600636
637 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
638 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700639 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
640 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
641 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600642
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600643 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600644 assert(!err);
645}
646
Karl Schultze4dc75c2016-02-02 15:37:51 -0700647void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600648 mat4x4 MVP, Model, VP;
649 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600650 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600651 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600653 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600654
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600655 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600656 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700657 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
658 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600659 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600660
Karl Schultze4dc75c2016-02-02 15:37:51 -0700661 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
662 demo->uniform_data.mem_alloc.allocationSize, 0,
663 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600664 assert(!err);
665
Karl Schultze4dc75c2016-02-02 15:37:51 -0700666 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600667
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600668 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600669}
670
Karl Schultze4dc75c2016-02-02 15:37:51 -0700671static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600672 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600673 VkSemaphore presentCompleteSemaphore;
674 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
675 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
676 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600677 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600678 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800679 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600680
Karl Schultze4dc75c2016-02-02 15:37:51 -0700681 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
682 NULL, &presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600683 assert(!err);
684
685 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700686 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Ian Elliottaaae5352015-07-06 14:27:58 -0600687 presentCompleteSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700688 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600689 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600690 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
691 // demo->swapchain is out of date (e.g. the window was resized) and
692 // must be recreated:
693 demo_resize(demo);
694 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800695 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600696 return;
697 } else if (err == VK_SUBOPTIMAL_KHR) {
698 // demo->swapchain is not as optimal as it could be, but the platform's
699 // presentation engine will still present the image correctly.
700 } else {
701 assert(!err);
702 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600703
Tony Barbour15a4ef62015-10-21 10:14:48 -0600704 // Assume the command buffer has been run on current_buffer before so
705 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
706 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700707 VK_IMAGE_ASPECT_COLOR_BIT,
708 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700709 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
710 0);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600711 demo_flush_init_cmd(demo);
712
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600713 // Wait for the present complete semaphore to be signaled to ensure
714 // that the image won't be rendered to until the presentation
715 // engine has fully released ownership to the application, and it is
716 // okay to render to the image.
717
Karl Schultze4dc75c2016-02-02 15:37:51 -0700718 // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
719 VkPipelineStageFlags pipe_stage_flags =
720 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
721 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
722 .pNext = NULL,
723 .waitSemaphoreCount = 1,
724 .pWaitSemaphores = &presentCompleteSemaphore,
725 .pWaitDstStageMask = &pipe_stage_flags,
726 .commandBufferCount = 1,
727 .pCommandBuffers =
728 &demo->buffers[demo->current_buffer].cmd,
729 .signalSemaphoreCount = 0,
730 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600731
732 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733 assert(!err);
734
Ian Elliotta0781972015-08-21 15:09:33 -0600735 VkPresentInfoKHR present = {
736 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600737 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600738 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700739 .pSwapchains = &demo->swapchain,
740 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600741 };
742
Karl Schultze4dc75c2016-02-02 15:37:51 -0700743 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600744 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600745 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
746 // demo->swapchain is out of date (e.g. the window was resized) and
747 // must be recreated:
748 demo_resize(demo);
749 } else if (err == VK_SUBOPTIMAL_KHR) {
750 // demo->swapchain is not as optimal as it could be, but the platform's
751 // presentation engine will still present the image correctly.
752 } else {
753 assert(!err);
754 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600755
Chia-I Wucbb564e2015-04-16 22:02:10 +0800756 err = vkQueueWaitIdle(demo->queue);
757 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600758
Chia-I Wu63636062015-10-26 21:10:41 +0800759 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600760}
761
Karl Schultze4dc75c2016-02-02 15:37:51 -0700762static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600763 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600764 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600765
Ian Elliottb08e0d92015-11-20 11:55:46 -0700766 // Check the surface capabilities and formats
767 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700768 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
769 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600770 assert(!err);
771
Ian Elliott8528eff2015-08-07 15:56:59 -0600772 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700773 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
774 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600775 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600776 VkPresentModeKHR *presentModes =
777 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600778 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700779 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
780 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600781 assert(!err);
782
Ian Elliotta0781972015-08-21 15:09:33 -0600783 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600784 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700785 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600786 // If the surface size is undefined, the size is set to
787 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600788 swapchainExtent.width = demo->width;
789 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700790 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600791 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700792 swapchainExtent = surfCapabilities.currentExtent;
793 demo->width = surfCapabilities.currentExtent.width;
794 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600795 }
796
797 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600798 // tearing mode. If not, try IMMEDIATE which will usually be available,
799 // and is fastest (though it tears). If not, fall back to FIFO which is
800 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600801 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600802 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600803 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
804 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600805 break;
806 }
Ian Elliotta0781972015-08-21 15:09:33 -0600807 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
808 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
809 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600810 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600811 }
812
813 // Determine the number of VkImage's to use in the swap chain (we desire to
814 // own only 1 image at a time, besides the images being displayed and
815 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700816 uint32_t desiredNumberOfSwapchainImages =
817 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700818 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700819 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600820 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700821 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600822 }
823
Tony Barbour9fd6d352015-09-16 15:01:32 -0600824 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700825 if (surfCapabilities.supportedTransforms &
826 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700827 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600828 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700829 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600830 }
831
Ian Elliottcf074d32015-10-16 18:02:43 -0600832 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600833 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800834 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700835 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600836 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800837 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600838 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700839 .imageExtent =
840 {
841 .width = swapchainExtent.width, .height = swapchainExtent.height,
842 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700843 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600844 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700845 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700846 .imageArrayLayers = 1,
847 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
848 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600849 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600850 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600851 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600852 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600853 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600854 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600855
Karl Schultze4dc75c2016-02-02 15:37:51 -0700856 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
857 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800858 assert(!err);
859
Ian Elliottcf074d32015-10-16 18:02:43 -0600860 // If we just re-created an existing swapchain, we should destroy the old
861 // swapchain at this point.
862 // Note: destroying the swapchain also cleans up all its associated
863 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800864 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700865 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600866 }
867
868 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600869 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600870 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800871
Karl Schultze4dc75c2016-02-02 15:37:51 -0700872 VkImage *swapchainImages =
873 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600874 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600875 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600876 &demo->swapchainImageCount,
877 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600878 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600879
Karl Schultze4dc75c2016-02-02 15:37:51 -0700880 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
881 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600882 assert(demo->buffers);
883
Ian Elliotta0781972015-08-21 15:09:33 -0600884 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600885 VkImageViewCreateInfo color_image_view = {
886 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600887 .pNext = NULL,
888 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700889 .components =
890 {
891 .r = VK_COMPONENT_SWIZZLE_R,
892 .g = VK_COMPONENT_SWIZZLE_G,
893 .b = VK_COMPONENT_SWIZZLE_B,
894 .a = VK_COMPONENT_SWIZZLE_A,
895 },
896 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
897 .baseMipLevel = 0,
898 .levelCount = 1,
899 .baseArrayLayer = 0,
900 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600901 .viewType = VK_IMAGE_VIEW_TYPE_2D,
902 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600903 };
904
Ian Elliotta0781972015-08-21 15:09:33 -0600905 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600906
Karl Schultze4dc75c2016-02-02 15:37:51 -0700907 // Render loop will expect image to have been used before and in
908 // VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
909 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image
910 // to that state
911 demo_set_image_layout(
912 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700913 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
914 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600915
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600916 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600917
Karl Schultze4dc75c2016-02-02 15:37:51 -0700918 err = vkCreateImageView(demo->device, &color_image_view, NULL,
919 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600920 assert(!err);
921 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700922
Mark Young04fd3d82016-01-25 14:43:50 -0700923 if (NULL != presentModes) {
924 free(presentModes);
925 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600926}
927
Karl Schultze4dc75c2016-02-02 15:37:51 -0700928static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600929 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600930 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600931 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600932 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600933 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600934 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700935 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600936 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600937 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800938 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600939 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600940 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600941 .flags = 0,
942 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200943
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600944 VkImageViewCreateInfo view = {
945 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600946 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800947 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600948 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700949 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
950 .baseMipLevel = 0,
951 .levelCount = 1,
952 .baseArrayLayer = 0,
953 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600954 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600955 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600956 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600957
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500958 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600959 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600960 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600961
962 demo->depth.format = depth_format;
963
964 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700965 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600966 assert(!err);
967
Karl Schultze4dc75c2016-02-02 15:37:51 -0700968 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600969 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600970
Chia-I Wuf48700b2015-11-10 16:21:09 +0800971 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200972 demo->depth.mem_alloc.pNext = NULL;
973 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
974 demo->depth.mem_alloc.memoryTypeIndex = 0;
975
Karl Schultze4dc75c2016-02-02 15:37:51 -0700976 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
977 0, /* No requirements */
978 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600979 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600980
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500981 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700982 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
983 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500984 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600985
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500986 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700987 err =
988 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500989 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990
Karl Schultze4dc75c2016-02-02 15:37:51 -0700991 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
992 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700993 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
994 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600995
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600996 /* create image view */
997 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800998 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600999 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001000}
1001
Tony Barbour1a86d032015-09-21 15:17:33 -06001002/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001003bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001004 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
1005 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001006 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001007
Tony Barbour1a86d032015-09-21 15:17:33 -06001008 if (!fPtr)
1009 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001010
Tony Barbour1a86d032015-09-21 15:17:33 -06001011 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001012 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1013 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001014 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001015 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001016
Tony Barbour1a86d032015-09-21 15:17:33 -06001017 do {
1018 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001019 if (cPtr == NULL) {
1020 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001021 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001022 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001023 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001024
Tony Barbour1a86d032015-09-21 15:17:33 -06001025 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001026 if (rgba_data == NULL) {
1027 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001028 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001029 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001030 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001031 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001032 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1033 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001034 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001035 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001036
Karl Schultze4dc75c2016-02-02 15:37:51 -07001037 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001038 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001039 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001040 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001041 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001042 rowPtr[3] = 255; /* Alpha of 1 */
1043 rowPtr += 4;
1044 }
1045 rgba_data += layout->rowPitch;
1046 }
1047 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001048 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001049}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001050
Karl Schultze4dc75c2016-02-02 15:37:51 -07001051static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001052 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001053 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001054 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001055 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001056 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001057 int32_t tex_width;
1058 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001059 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001060 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061
Karl Schultze4dc75c2016-02-02 15:37:51 -07001062 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
David Pinedo30bd71d2015-04-23 08:16:57 -06001063 printf("Failed to load textures\n");
1064 fflush(stdout);
1065 exit(1);
1066 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001067
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001068 tex_obj->tex_width = tex_width;
1069 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001070
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001071 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001072 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001074 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001075 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001076 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001077 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001078 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001079 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001081 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001083 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001084 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001086 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001087
Karl Schultze4dc75c2016-02-02 15:37:51 -07001088 err =
1089 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001090 assert(!err);
1091
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001092 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001093
Chia-I Wuf48700b2015-11-10 16:21:09 +08001094 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001095 tex_obj->mem_alloc.pNext = NULL;
1096 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1097 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001098
Karl Schultze4dc75c2016-02-02 15:37:51 -07001099 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1100 required_props,
1101 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001102 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001103
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001104 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001105 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001106 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001107 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001108
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001109 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001110 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001111 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001112
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001113 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001114 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001115 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001116 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001117 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001119 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001120 void *data;
1121
Karl Schultze4dc75c2016-02-02 15:37:51 -07001122 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1123 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001124
Karl Schultze4dc75c2016-02-02 15:37:51 -07001125 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1126 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001127 assert(!err);
1128
1129 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1130 fprintf(stderr, "Error loading texture: %s\n", filename);
1131 }
1132
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001133 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001134 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001135
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001136 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001137 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001138 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1139 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001140 /* setting the image layout does not reference the actual memory so no need
1141 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001142}
1143
Karl Schultze4dc75c2016-02-02 15:37:51 -07001144static void demo_destroy_texture_image(struct demo *demo,
1145 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001146 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001147 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1148 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001149}
1150
Karl Schultze4dc75c2016-02-02 15:37:51 -07001151static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001152 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001153 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001154 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001155
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001156 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001157
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001158 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001159 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001160
Karl Schultze4dc75c2016-02-02 15:37:51 -07001161 if ((props.linearTilingFeatures &
1162 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1163 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001164 /* Device can texture using linear textures */
1165 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Karl Schultze4dc75c2016-02-02 15:37:51 -07001166 VK_IMAGE_TILING_LINEAR,
1167 VK_IMAGE_USAGE_SAMPLED_BIT,
1168 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
1169 } else if (props.optimalTilingFeatures &
1170 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001171 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001172 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001173
1174 memset(&staging_texture, 0, sizeof(staging_texture));
1175 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001176 VK_IMAGE_TILING_LINEAR,
1177 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1178 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001179
Karl Schultze4dc75c2016-02-02 15:37:51 -07001180 demo_prepare_texture_image(
1181 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1182 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1183 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001184
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001185 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001186 VK_IMAGE_ASPECT_COLOR_BIT,
1187 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001188 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1189 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001190
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001191 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001192 VK_IMAGE_ASPECT_COLOR_BIT,
1193 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001194 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1195 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001196
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001197 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001198 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1199 .srcOffset = {0, 0, 0},
1200 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1201 .dstOffset = {0, 0, 0},
1202 .extent = {staging_texture.tex_width,
1203 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001204 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001205 vkCmdCopyImage(
1206 demo->cmd, staging_texture.image,
1207 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1208 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001209
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001210 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001211 VK_IMAGE_ASPECT_COLOR_BIT,
1212 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001213 demo->textures[i].imageLayout,
1214 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001215
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001216 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001218 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001219 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001220 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1221 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001222 }
1223
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001224 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001225 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001226 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001227 .magFilter = VK_FILTER_NEAREST,
1228 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001229 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001230 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1231 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1232 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001233 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001234 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001235 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001236 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001237 .minLod = 0.0f,
1238 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001239 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001240 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001242
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001243 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001244 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001245 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001246 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001247 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001248 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001249 .components =
1250 {
1251 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1252 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1253 },
1254 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001255 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001256 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001257
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001258 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001259 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001260 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001261 assert(!err);
1262
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001263 /* create image view */
1264 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001265 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001266 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001267 assert(!err);
1268 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001269}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001270
Karl Schultze4dc75c2016-02-02 15:37:51 -07001271void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001272 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001273 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001274 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001275 int i;
1276 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001277 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001278 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001279 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001280
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001281 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001282 mat4x4_mul(MVP, VP, demo->model_matrix);
1283 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001284 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001285
Karl Schultze4dc75c2016-02-02 15:37:51 -07001286 for (i = 0; i < 12 * 3; i++) {
1287 data.position[i][0] = g_vertex_buffer_data[i * 3];
1288 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1289 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001290 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001291 data.attr[i][0] = g_uv_buffer_data[2 * i];
1292 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001293 data.attr[i][2] = 0;
1294 data.attr[i][3] = 0;
1295 }
1296
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001297 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001298 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001299 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001300 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001301 err =
1302 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001303 assert(!err);
1304
Karl Schultze4dc75c2016-02-02 15:37:51 -07001305 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1306 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001307
Chia-I Wuf48700b2015-11-10 16:21:09 +08001308 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001309 demo->uniform_data.mem_alloc.pNext = NULL;
1310 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1311 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1312
Karl Schultze4dc75c2016-02-02 15:37:51 -07001313 pass = memory_type_from_properties(
1314 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1315 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001316 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001317
Karl Schultze4dc75c2016-02-02 15:37:51 -07001318 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1319 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001320 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001321
Karl Schultze4dc75c2016-02-02 15:37:51 -07001322 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1323 demo->uniform_data.mem_alloc.allocationSize, 0,
1324 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001325 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001326
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001327 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001328
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001329 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001330
Karl Schultze4dc75c2016-02-02 15:37:51 -07001331 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1332 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001333 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001334
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001335 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1336 demo->uniform_data.buffer_info.offset = 0;
1337 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001338}
1339
Karl Schultze4dc75c2016-02-02 15:37:51 -07001340static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001341 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001342 [0] =
1343 {
1344 .binding = 0,
1345 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1346 .descriptorCount = 1,
1347 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1348 .pImmutableSamplers = NULL,
1349 },
1350 [1] =
1351 {
1352 .binding = 1,
1353 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1354 .descriptorCount = DEMO_TEXTURE_COUNT,
1355 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1356 .pImmutableSamplers = NULL,
1357 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001358 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001359 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001360 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001361 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001362 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001363 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001364 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001365 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001366
Karl Schultze4dc75c2016-02-02 15:37:51 -07001367 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1368 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001369 assert(!err);
1370
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001371 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001372 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1373 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001374 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001375 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001376 };
1377
Karl Schultze4dc75c2016-02-02 15:37:51 -07001378 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001379 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001381}
1382
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001384 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001385 [0] =
1386 {
1387 .format = demo->format,
1388 .samples = VK_SAMPLE_COUNT_1_BIT,
1389 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1390 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1391 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1392 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1393 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1394 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1395 },
1396 [1] =
1397 {
1398 .format = demo->depth.format,
1399 .samples = VK_SAMPLE_COUNT_1_BIT,
1400 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1401 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1402 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1403 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1404 .initialLayout =
1405 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1406 .finalLayout =
1407 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1408 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001409 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001410 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001411 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001412 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001413 const VkAttachmentReference depth_reference = {
1414 .attachment = 1,
1415 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1416 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001417 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001418 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1419 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001420 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001421 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001422 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001423 .pColorAttachments = &color_reference,
1424 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001425 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001426 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001427 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001428 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001429 const VkRenderPassCreateInfo rp_info = {
1430 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1431 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001432 .attachmentCount = 2,
1433 .pAttachments = attachments,
1434 .subpassCount = 1,
1435 .pSubpasses = &subpass,
1436 .dependencyCount = 0,
1437 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001438 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001439 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001440
Chia-I Wu63636062015-10-26 21:10:41 +08001441 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001442 assert(!err);
1443}
1444
Karl Schultze4dc75c2016-02-02 15:37:51 -07001445static VkShaderModule
1446demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001447 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001448 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001449 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001450
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001451 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1452 moduleCreateInfo.pNext = NULL;
1453
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001454 moduleCreateInfo.codeSize = size;
1455 moduleCreateInfo.pCode = code;
1456 moduleCreateInfo.flags = 0;
1457 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1458 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001459
1460 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001461}
1462
Karl Schultze4dc75c2016-02-02 15:37:51 -07001463char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001464 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001465 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001466 void *shader_code;
1467
1468 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001469 if (!fp)
1470 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001471
1472 fseek(fp, 0L, SEEK_END);
1473 size = ftell(fp);
1474
1475 fseek(fp, 0L, SEEK_SET);
1476
1477 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001478 retval = fread(shader_code, size, 1, fp);
1479 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001480
1481 *psize = size;
1482
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001483 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001484 return shader_code;
1485}
1486
Karl Schultze4dc75c2016-02-02 15:37:51 -07001487static VkShaderModule demo_prepare_vs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001488 void *vertShaderCode;
1489 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001490
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001491 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1492
Karl Schultze4dc75c2016-02-02 15:37:51 -07001493 demo->vert_shader_module =
1494 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001495
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001496 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001497
1498 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001499}
1500
Karl Schultze4dc75c2016-02-02 15:37:51 -07001501static VkShaderModule demo_prepare_fs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001502 void *fragShaderCode;
1503 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001504
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001505 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001506
Karl Schultze4dc75c2016-02-02 15:37:51 -07001507 demo->frag_shader_module =
1508 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001509
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001510 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001511
1512 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001513}
1514
Karl Schultze4dc75c2016-02-02 15:37:51 -07001515static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001516 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001517 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001518 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001519 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001520 VkPipelineRasterizationStateCreateInfo rs;
1521 VkPipelineColorBlendStateCreateInfo cb;
1522 VkPipelineDepthStencilStateCreateInfo ds;
1523 VkPipelineViewportStateCreateInfo vp;
1524 VkPipelineMultisampleStateCreateInfo ms;
1525 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1526 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001527 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001528
Piers Daniellba839d12015-09-29 13:01:09 -06001529 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1530 memset(&dynamicState, 0, sizeof dynamicState);
1531 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1532 dynamicState.pDynamicStates = dynamicStateEnables;
1533
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001534 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001535 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001536 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001538 memset(&vi, 0, sizeof(vi));
1539 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1540
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001541 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001542 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001543 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001544
1545 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001546 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1547 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001548 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001549 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001550 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001551 rs.rasterizerDiscardEnable = VK_FALSE;
1552 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001553
1554 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001555 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1556 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001557 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001558 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001559 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001560 cb.attachmentCount = 1;
1561 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001562
Tony Barbour29645d02015-01-16 14:27:35 -07001563 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001564 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001565 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001566 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1567 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001568 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001569 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1570 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001571
1572 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001573 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001574 ds.depthTestEnable = VK_TRUE;
1575 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001576 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001577 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001578 ds.back.failOp = VK_STENCIL_OP_KEEP;
1579 ds.back.passOp = VK_STENCIL_OP_KEEP;
1580 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001581 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001582 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001583
Tony Barbour29645d02015-01-16 14:27:35 -07001584 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001585 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001586 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001587 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001588
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001589 // Two stages: vs and fs
1590 pipeline.stageCount = 2;
1591 VkPipelineShaderStageCreateInfo shaderStages[2];
1592 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1593
Karl Schultze4dc75c2016-02-02 15:37:51 -07001594 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1595 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001596 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001597 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001598
Karl Schultze4dc75c2016-02-02 15:37:51 -07001599 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1600 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001601 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001602 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001603
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001604 memset(&pipelineCache, 0, sizeof(pipelineCache));
1605 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001606
Karl Schultze4dc75c2016-02-02 15:37:51 -07001607 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1608 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001609 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001610
Karl Schultze4dc75c2016-02-02 15:37:51 -07001611 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001612 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001613 pipeline.pRasterizationState = &rs;
1614 pipeline.pColorBlendState = &cb;
1615 pipeline.pMultisampleState = &ms;
1616 pipeline.pViewportState = &vp;
1617 pipeline.pDepthStencilState = &ds;
1618 pipeline.pStages = shaderStages;
1619 pipeline.renderPass = demo->render_pass;
1620 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001621
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001622 pipeline.renderPass = demo->render_pass;
1623
Karl Schultze4dc75c2016-02-02 15:37:51 -07001624 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1625 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001626 assert(!err);
1627
Chia-I Wu63636062015-10-26 21:10:41 +08001628 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1629 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001630}
1631
Karl Schultze4dc75c2016-02-02 15:37:51 -07001632static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001633 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001634 [0] =
1635 {
1636 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1637 .descriptorCount = 1,
1638 },
1639 [1] =
1640 {
1641 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1642 .descriptorCount = DEMO_TEXTURE_COUNT,
1643 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001644 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001645 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001646 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001647 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001648 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001649 .poolSizeCount = 2,
1650 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001651 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001652 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001653
Karl Schultze4dc75c2016-02-02 15:37:51 -07001654 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1655 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001656 assert(!err);
1657}
1658
Karl Schultze4dc75c2016-02-02 15:37:51 -07001659static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001660 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001661 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001662 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001663 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001664
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001665 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001666 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001667 .pNext = NULL,
1668 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001669 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001670 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001671 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001672 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001673
Chia-I Wuae721ba2015-05-25 16:27:55 +08001674 memset(&tex_descs, 0, sizeof(tex_descs));
1675 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001676 tex_descs[i].sampler = demo->textures[i].sampler;
1677 tex_descs[i].imageView = demo->textures[i].view;
1678 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001679 }
1680
1681 memset(&writes, 0, sizeof(writes));
1682
1683 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001684 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001685 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001686 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001687 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001688
1689 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001690 writes[1].dstSet = demo->desc_set;
1691 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001692 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001693 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001694 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001695
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001696 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001697}
1698
Karl Schultze4dc75c2016-02-02 15:37:51 -07001699static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001700 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001701 attachments[1] = demo->depth.view;
1702
Chia-I Wuf973e322015-07-08 13:34:24 +08001703 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001704 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1705 .pNext = NULL,
1706 .renderPass = demo->render_pass,
1707 .attachmentCount = 2,
1708 .pAttachments = attachments,
1709 .width = demo->width,
1710 .height = demo->height,
1711 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001712 };
1713 VkResult U_ASSERT_ONLY err;
1714 uint32_t i;
1715
Karl Schultze4dc75c2016-02-02 15:37:51 -07001716 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1717 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001718 assert(demo->framebuffers);
1719
1720 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001721 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001722 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1723 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001724 assert(!err);
1725 }
1726}
1727
Karl Schultze4dc75c2016-02-02 15:37:51 -07001728static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001729 VkResult U_ASSERT_ONLY err;
1730
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001731 const VkCommandPoolCreateInfo cmd_pool_info = {
1732 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001733 .pNext = NULL,
1734 .queueFamilyIndex = demo->graphics_queue_node_index,
1735 .flags = 0,
1736 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001737 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1738 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001739 assert(!err);
1740
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001741 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001742 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001743 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001744 .commandPool = demo->cmd_pool,
1745 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001746 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001748
1749 demo_prepare_buffers(demo);
1750 demo_prepare_depth(demo);
1751 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001752 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001754 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001755 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001756 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001757
Ian Elliotta0781972015-08-21 15:09:33 -06001758 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001759 err =
1760 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001761 assert(!err);
1762 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001763
Chia-I Wu63ea9262015-03-26 13:14:16 +08001764 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001765 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001766
Chia-I Wuf973e322015-07-08 13:34:24 +08001767 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001768
Ian Elliotta0781972015-08-21 15:09:33 -06001769 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001770 demo->current_buffer = i;
1771 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1772 }
1773
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001774 /*
1775 * Prepare functions above may generate pipeline commands
1776 * that need to be flushed before beginning the render loop.
1777 */
1778 demo_flush_init_cmd(demo);
1779
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001780 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001781 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001782}
1783
Karl Schultze4dc75c2016-02-02 15:37:51 -07001784static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001785 uint32_t i;
1786
1787 demo->prepared = false;
1788
Tony Barbourd8e504f2015-10-08 14:26:24 -06001789 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001790 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001791 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001792 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001793 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001794
Chia-I Wu63636062015-10-26 21:10:41 +08001795 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1796 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1797 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1798 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1799 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001800
1801 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001802 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1803 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1804 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1805 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001806 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001807 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001808
Chia-I Wu63636062015-10-26 21:10:41 +08001809 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1810 vkDestroyImage(demo->device, demo->depth.image, NULL);
1811 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001812
Chia-I Wu63636062015-10-26 21:10:41 +08001813 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1814 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001815
Ian Elliotta0781972015-08-21 15:09:33 -06001816 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001817 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001818 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1819 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001820 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001821 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001822
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001823 free(demo->queue_props);
1824
Chia-I Wu63636062015-10-26 21:10:41 +08001825 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1826 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001827 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001828 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001829 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001830 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001831 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001832
1833#ifndef _WIN32
1834 xcb_destroy_window(demo->connection, demo->window);
1835 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001836 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001837#endif // _WIN32
1838}
1839
Karl Schultze4dc75c2016-02-02 15:37:51 -07001840static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001841 uint32_t i;
1842
Mike Stroyanbb60b382015-10-28 09:46:57 -06001843 // Don't react to resize until after first initialization.
1844 if (!demo->prepared) {
1845 return;
1846 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001847 // In order to properly resize the window, we must re-create the swapchain
1848 // AND redo the command buffers, etc.
1849 //
1850 // First, perform part of the demo_cleanup() function:
1851 demo->prepared = false;
1852
1853 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001854 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001855 }
1856 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001857 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001858
Chia-I Wu63636062015-10-26 21:10:41 +08001859 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1860 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1861 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1862 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1863 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001864
1865 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001866 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1867 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1868 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1869 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001870 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001871
Chia-I Wu63636062015-10-26 21:10:41 +08001872 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1873 vkDestroyImage(demo->device, demo->depth.image, NULL);
1874 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001875
Chia-I Wu63636062015-10-26 21:10:41 +08001876 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1877 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001878
1879 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001880 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001881 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1882 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001883 }
Chia-I Wu63636062015-10-26 21:10:41 +08001884 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001885 free(demo->buffers);
1886
Ian Elliottcf074d32015-10-16 18:02:43 -06001887 // Second, re-perform the demo_prepare() function, which will re-create the
1888 // swapchain:
1889 demo_prepare(demo);
1890}
1891
David Pinedo2bb7c932015-06-18 17:03:14 -06001892// On MS-Windows, make this a global, so it's available to WndProc()
1893struct demo demo;
1894
Ian Elliott639ca472015-04-16 15:23:05 -06001895#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07001896static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001897 if (!demo->prepared)
1898 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001899 // Wait for work to finish before updating MVP.
1900 vkDeviceWaitIdle(demo->device);
1901 demo_update_data_buffer(demo);
1902
1903 demo_draw(demo);
1904
1905 // Wait for work to finish before updating MVP.
1906 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001907
David Pinedo2bb7c932015-06-18 17:03:14 -06001908 demo->curFrame++;
1909
Karl Schultze4dc75c2016-02-02 15:37:51 -07001910 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
1911 demo->quit = true;
David Pinedo2bb7c932015-06-18 17:03:14 -06001912 demo_cleanup(demo);
1913 ExitProcess(0);
1914 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001915}
Ian Elliott639ca472015-04-16 15:23:05 -06001916
1917// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001918LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
1919 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001920 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001921 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001922 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001923 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001924 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001925 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001926 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001927 demo.width = lParam & 0xffff;
1928 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001929 demo_resize(&demo);
1930 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001931 default:
1932 break;
1933 }
1934 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1935}
1936
Karl Schultze4dc75c2016-02-02 15:37:51 -07001937static void demo_create_window(struct demo *demo) {
1938 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06001939
1940 // Initialize the window class structure:
1941 win_class.cbSize = sizeof(WNDCLASSEX);
1942 win_class.style = CS_HREDRAW | CS_VREDRAW;
1943 win_class.lpfnWndProc = WndProc;
1944 win_class.cbClsExtra = 0;
1945 win_class.cbWndExtra = 0;
1946 win_class.hInstance = demo->connection; // hInstance
1947 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1948 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1949 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1950 win_class.lpszMenuName = NULL;
1951 win_class.lpszClassName = demo->name;
1952 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1953 // Register window class:
1954 if (!RegisterClassEx(&win_class)) {
1955 // It didn't work, so try to give a useful error:
1956 printf("Unexpected error trying to start the application!\n");
1957 fflush(stdout);
1958 exit(1);
1959 }
1960 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001961 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06001962 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001963 demo->window = CreateWindowEx(0,
1964 demo->name, // class name
1965 demo->name, // app name
1966 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07001967 WS_VISIBLE | WS_SYSMENU,
1968 100, 100, // x/y coords
1969 wr.right - wr.left, // width
1970 wr.bottom - wr.top, // height
1971 NULL, // handle to parent
1972 NULL, // handle to menu
1973 demo->connection, // hInstance
1974 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06001975 if (!demo->window) {
1976 // It didn't work, so try to give a useful error:
1977 printf("Cannot create a window in which to draw!\n");
1978 fflush(stdout);
1979 exit(1);
1980 }
1981}
1982#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001983static void demo_handle_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001984 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07001985 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001986 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001988 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001989 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001990 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001991 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
1992 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001993 demo->quit = true;
1994 }
1995 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001996 case XCB_KEY_RELEASE: {
1997 const xcb_key_release_event_t *key =
1998 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001999
Karl Schultze4dc75c2016-02-02 15:37:51 -07002000 switch (key->detail) {
2001 case 0x9: // Escape
2002 demo->quit = true;
2003 break;
2004 case 0x71: // left arrow key
2005 demo->spin_angle += demo->spin_increment;
2006 break;
2007 case 0x72: // right arrow key
2008 demo->spin_angle -= demo->spin_increment;
2009 break;
2010 case 0x41:
2011 demo->pause = !demo->pause;
2012 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002013 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002014 } break;
2015 case XCB_CONFIGURE_NOTIFY: {
2016 const xcb_configure_notify_event_t *cfg =
2017 (const xcb_configure_notify_event_t *)event;
2018 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002019 demo->width = cfg->width;
2020 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002021 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002022 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002023 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002024 default:
2025 break;
2026 }
2027}
2028
Karl Schultze4dc75c2016-02-02 15:37:51 -07002029static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030 xcb_flush(demo->connection);
2031
2032 while (!demo->quit) {
2033 xcb_generic_event_t *event;
2034
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002035 if (demo->pause) {
2036 event = xcb_wait_for_event(demo->connection);
2037 } else {
2038 event = xcb_poll_for_event(demo->connection);
2039 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002040 if (event) {
2041 demo_handle_event(demo, event);
2042 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002043 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002044
2045 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002046 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002047 demo_update_data_buffer(demo);
2048
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002049 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002050
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002051 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002052 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002053 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002054 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002055 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002056 }
2057}
2058
Karl Schultze4dc75c2016-02-02 15:37:51 -07002059static void demo_create_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002060 uint32_t value_mask, value_list[32];
2061
2062 demo->window = xcb_generate_id(demo->connection);
2063
2064 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2065 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002066 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002067 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002068
Karl Schultze4dc75c2016-02-02 15:37:51 -07002069 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->window,
2070 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2071 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2072 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002073
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002074 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002075 xcb_intern_atom_cookie_t cookie =
2076 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2077 xcb_intern_atom_reply_t *reply =
2078 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002079
Karl Schultze4dc75c2016-02-02 15:37:51 -07002080 xcb_intern_atom_cookie_t cookie2 =
2081 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2082 demo->atom_wm_delete_window =
2083 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002084
Karl Schultze4dc75c2016-02-02 15:37:51 -07002085 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->window,
2086 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002087 &(*demo->atom_wm_delete_window).atom);
2088 free(reply);
2089
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002090 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002091
Karl Schultze4dc75c2016-02-02 15:37:51 -07002092 // Force the x/y coordinates to 100,100 results are identical in consecutive
2093 // runs
2094 const uint32_t coords[] = {100, 100};
David Pinedo2bb7c932015-06-18 17:03:14 -06002095 xcb_configure_window(demo->connection, demo->window,
2096 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002097}
Ian Elliott639ca472015-04-16 15:23:05 -06002098#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002099
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002100/*
2101 * Return 1 (true) if all layer names specified in check_names
2102 * can be found in given layer properties.
2103 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002104static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002105 uint32_t layer_count,
2106 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002107 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002108 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002109 for (uint32_t j = 0; j < layer_count; j++) {
2110 if (!strcmp(check_names[i], layers[j].layerName)) {
2111 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002112 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002113 }
2114 }
2115 if (!found) {
2116 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2117 return 0;
2118 }
2119 }
2120 return 1;
2121}
2122
Karl Schultze4dc75c2016-02-02 15:37:51 -07002123static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002124 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002125 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002126 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002127 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002128 uint32_t device_validation_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002129 uint32_t enabled_extension_count = 0;
2130 uint32_t enabled_layer_count = 0;
2131
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002132 char *instance_validation_layers[] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002133 "VK_LAYER_LUNARG_threading", "VK_LAYER_LUNARG_mem_tracker",
2134 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_draw_state",
2135 "VK_LAYER_LUNARG_param_checker", "VK_LAYER_LUNARG_swapchain",
2136 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_image",
Dustin Gravescfe5ade2016-01-26 16:30:22 -07002137 "VK_LAYER_GOOGLE_unique_objects",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002138 };
2139
Tony Barbourda2bad52016-01-22 14:36:40 -07002140 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_threading";
2141 demo->device_validation_layers[1] = "VK_LAYER_LUNARG_mem_tracker";
Dustin Gravescfe5ade2016-01-26 16:30:22 -07002142 demo->device_validation_layers[2] = "VK_LAYER_LUNARG_object_tracker";
2143 demo->device_validation_layers[3] = "VK_LAYER_LUNARG_draw_state";
2144 demo->device_validation_layers[4] = "VK_LAYER_LUNARG_param_checker";
2145 demo->device_validation_layers[5] = "VK_LAYER_LUNARG_swapchain";
2146 demo->device_validation_layers[6] = "VK_LAYER_LUNARG_device_limits";
2147 demo->device_validation_layers[7] = "VK_LAYER_LUNARG_image";
2148 demo->device_validation_layers[8] = "VK_LAYER_GOOGLE_unique_objects";
2149 device_validation_layer_count = 9;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002150
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002151 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002152 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002153 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002154 assert(!err);
2155
Dustin Graves7c287352016-01-27 13:48:06 -07002156 if (instance_layer_count > 0) {
2157 VkLayerProperties *instance_layers =
2158 malloc(sizeof(VkLayerProperties) * instance_layer_count);
2159 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2160 instance_layers);
2161 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002162
Dustin Graves7c287352016-01-27 13:48:06 -07002163 if (demo->validate) {
2164 validation_found = demo_check_layers(
2165 ARRAY_SIZE(instance_validation_layers),
2166 instance_validation_layers, instance_layer_count,
2167 instance_layers);
2168
2169 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002170 }
Dustin Graves7c287352016-01-27 13:48:06 -07002171
2172 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002173 }
2174
Dustin Graves7c287352016-01-27 13:48:06 -07002175 if (demo->validate && !validation_found) {
2176 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
2177 "required validation layer.\n\n"
2178 "Please look at the Getting Started guide for additional "
2179 "information.\n",
2180 "vkCreateInstance Failure");
2181 }
2182
2183 /* Look for instance extensions */
2184 VkBool32 surfaceExtFound = 0;
2185 VkBool32 platformSurfaceExtFound = 0;
2186 memset(extension_names, 0, sizeof(extension_names));
2187
Karl Schultze4dc75c2016-02-02 15:37:51 -07002188 err = vkEnumerateInstanceExtensionProperties(
2189 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002190 assert(!err);
2191
Dustin Graves7c287352016-01-27 13:48:06 -07002192 if (instance_extension_count > 0) {
2193 VkExtensionProperties *instance_extensions =
2194 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2195 err = vkEnumerateInstanceExtensionProperties(
2196 NULL, &instance_extension_count, instance_extensions);
2197 assert(!err);
2198 for (uint32_t i = 0; i < instance_extension_count; i++) {
2199 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2200 instance_extensions[i].extensionName)) {
2201 surfaceExtFound = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002202 extension_names[enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002203 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002204 }
Dustin Graves7c287352016-01-27 13:48:06 -07002205#ifdef _WIN32
2206 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2207 instance_extensions[i].extensionName)) {
2208 platformSurfaceExtFound = 1;
2209 extension_names[enabled_extension_count++] =
2210 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2211 }
2212#else // _WIN32
2213 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2214 instance_extensions[i].extensionName)) {
2215 platformSurfaceExtFound = 1;
2216 extension_names[enabled_extension_count++] =
2217 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2218 }
2219#endif // _WIN32
2220 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2221 instance_extensions[i].extensionName)) {
2222 if (demo->validate) {
2223 extension_names[enabled_extension_count++] =
2224 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2225 }
2226 }
2227 assert(enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002228 }
Dustin Graves7c287352016-01-27 13:48:06 -07002229
2230 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002231 }
Dustin Graves7c287352016-01-27 13:48:06 -07002232
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002233 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002234 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2235 "the " VK_KHR_SURFACE_EXTENSION_NAME
2236 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002237 "Vulkan installable client driver (ICD) installed?\nPlease "
2238 "look at the Getting Started guide for additional "
2239 "information.\n",
2240 "vkCreateInstance Failure");
2241 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002242 if (!platformSurfaceExtFound) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002243#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002244 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2245 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2246 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002247 "Vulkan installable client driver (ICD) installed?\nPlease "
2248 "look at the Getting Started guide for additional "
2249 "information.\n",
2250 "vkCreateInstance Failure");
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002251#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002252 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2253 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2254 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002255 "Vulkan installable client driver (ICD) installed?\nPlease "
2256 "look at the Getting Started guide for additional "
2257 "information.\n",
2258 "vkCreateInstance Failure");
2259#endif // _WIN32
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002260 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002261 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002262 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002263 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002264 .pApplicationName = APP_SHORT_NAME,
2265 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002266 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002267 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002268 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002269 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002270 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002271 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002272 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002273 .pApplicationInfo = &app,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002274 .enabledLayerCount = enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002275 .ppEnabledLayerNames =
2276 (const char *const *)((demo->validate) ? instance_validation_layers
2277 : NULL),
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002278 .enabledExtensionCount = enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002279 .ppEnabledExtensionNames = (const char *const *)extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002280 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002281
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002282 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002283
Chia-I Wu63636062015-10-26 21:10:41 +08002284 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002285 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2286 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002287 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002288 "additional information.\n",
2289 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002290 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002291 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002292 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002293 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002294 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002295 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2296 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002297 "the Getting Started guide for additional information.\n",
2298 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002299 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002300
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002301 /* Make initial call to query gpu_count, then second call for gpu info*/
2302 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2303 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002304
2305 if (gpu_count > 0) {
2306 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2307 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2308 assert(!err);
2309 /* For cube demo we just grab the first physical device */
2310 demo->gpu = physical_devices[0];
2311 free(physical_devices);
2312 } else {
2313 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2314 "Do you have a compatible Vulkan installable client driver (ICD) "
2315 "installed?\nPlease look at the Getting Started guide for "
2316 "additional information.\n",
2317 "vkEnumeratePhysicalDevices Failure");
2318 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002319
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002320 /* Look for validation layers */
2321 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002322 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002323 uint32_t device_layer_count = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002324 err =
2325 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002326 assert(!err);
2327
Dustin Graves7c287352016-01-27 13:48:06 -07002328 if (device_layer_count > 0) {
2329 VkLayerProperties *device_layers =
2330 malloc(sizeof(VkLayerProperties) * device_layer_count);
2331 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2332 device_layers);
2333 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002334
Dustin Graves7c287352016-01-27 13:48:06 -07002335 if (demo->validate) {
2336 validation_found = demo_check_layers(device_validation_layer_count,
2337 demo->device_validation_layers,
2338 device_layer_count,
2339 device_layers);
2340 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002341 }
Dustin Graves7c287352016-01-27 13:48:06 -07002342
2343 free(device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002344 }
2345
Dustin Graves7c287352016-01-27 13:48:06 -07002346 if (demo->validate && !validation_found) {
2347 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
2348 "a required validation layer.\n\n"
2349 "Please look at the Getting Started guide for additional "
2350 "information.\n",
2351 "vkCreateDevice Failure");
2352 }
2353
2354 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002355 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002356 VkBool32 swapchainExtFound = 0;
2357 demo->enabled_extension_count = 0;
2358 memset(extension_names, 0, sizeof(extension_names));
2359
Karl Schultze4dc75c2016-02-02 15:37:51 -07002360 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2361 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002362 assert(!err);
2363
Dustin Graves7c287352016-01-27 13:48:06 -07002364 if (device_extension_count > 0) {
2365 VkExtensionProperties *device_extensions =
2366 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2367 err = vkEnumerateDeviceExtensionProperties(
2368 demo->gpu, NULL, &device_extension_count, device_extensions);
2369 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002370
Dustin Graves7c287352016-01-27 13:48:06 -07002371 for (uint32_t i = 0; i < device_extension_count; i++) {
2372 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2373 device_extensions[i].extensionName)) {
2374 swapchainExtFound = 1;
2375 demo->extension_names[demo->enabled_extension_count++] =
2376 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2377 }
2378 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002379 }
Dustin Graves7c287352016-01-27 13:48:06 -07002380
2381 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002382 }
Dustin Graves7c287352016-01-27 13:48:06 -07002383
Tony Barbourcc69f452015-10-08 13:59:42 -06002384 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002385 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2386 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2387 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002388 "Vulkan installable client driver (ICD) installed?\nPlease "
2389 "look at the Getting Started guide for additional "
2390 "information.\n",
2391 "vkCreateInstance Failure");
2392 }
2393
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002394 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002395 demo->CreateDebugReportCallback =
2396 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2397 demo->inst, "vkCreateDebugReportCallbackEXT");
2398 demo->DestroyDebugReportCallback =
2399 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2400 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002401 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002402 ERR_EXIT(
2403 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2404 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002405 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002406 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002407 ERR_EXIT(
2408 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2409 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002410 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002411 demo->DebugReportMessage =
2412 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2413 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002414 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002415 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002416 "vkGetProcAddr Failure");
2417 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002418
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002419 PFN_vkDebugReportCallbackEXT callback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002420
2421 if (!demo->use_break) {
2422 callback = dbgFunc;
2423 } else {
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002424 callback = dbgFunc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002425 // TODO add a break callback defined locally since there is no
2426 // longer
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002427 // one included in the loader
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002428 }
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002429 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
2430 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002431 dbgCreateInfo.pNext = NULL;
2432 dbgCreateInfo.pfnCallback = callback;
2433 dbgCreateInfo.pUserData = NULL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002434 dbgCreateInfo.flags =
2435 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT;
2436 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2437 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002438 switch (err) {
2439 case VK_SUCCESS:
2440 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002441 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002442 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2443 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002444 break;
2445 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002446 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2447 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002448 break;
2449 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002450 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002451 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002452
2453 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002454 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2455 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002456 assert(demo->queue_count >= 1);
2457
Karl Schultze4dc75c2016-02-02 15:37:51 -07002458 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2459 demo->queue_count * sizeof(VkQueueFamilyProperties));
2460 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2461 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002462 // Find a queue that supports gfx
2463 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002464 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2465 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002466 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2467 break;
2468 }
2469 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002470 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002471 // If app has specific feature requirements it should check supported
2472 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002473 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002474 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002475
Tony Barbourda2bad52016-01-22 14:36:40 -07002476 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2477 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2478 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2479 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2480 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2481}
2482
Karl Schultze4dc75c2016-02-02 15:37:51 -07002483static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002484 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002485 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002486 const VkDeviceQueueCreateInfo queue = {
2487 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2488 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002489 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002490 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002491 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002492
2493 VkDeviceCreateInfo device = {
2494 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2495 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002496 .queueCreateInfoCount = 1,
2497 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002498 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002499 .ppEnabledLayerNames =
2500 (const char *const *)((demo->validate)
2501 ? demo->device_validation_layers
2502 : NULL),
Tony Barbourda2bad52016-01-22 14:36:40 -07002503 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002504 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2505 .pEnabledFeatures =
2506 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002507 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002508
Chia-I Wu63636062015-10-26 21:10:41 +08002509 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002510 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002511}
2512
Karl Schultze4dc75c2016-02-02 15:37:51 -07002513static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002514 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002515 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002516
Karl Schultze4dc75c2016-02-02 15:37:51 -07002517// Create a WSI surface for the window:
Ian Elliottaaae5352015-07-06 14:27:58 -06002518#ifdef _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002519 VkWin32SurfaceCreateInfoKHR createInfo;
2520 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2521 createInfo.pNext = NULL;
2522 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002523 createInfo.hinstance = demo->connection;
2524 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002525
Karl Schultze4dc75c2016-02-02 15:37:51 -07002526 err =
2527 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002528
Ian Elliottaaae5352015-07-06 14:27:58 -06002529#else // _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002530 VkXcbSurfaceCreateInfoKHR createInfo;
2531 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2532 createInfo.pNext = NULL;
2533 createInfo.flags = 0;
2534 createInfo.connection = demo->connection;
2535 createInfo.window = demo->window;
2536
Karl Schultze4dc75c2016-02-02 15:37:51 -07002537 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottaaae5352015-07-06 14:27:58 -06002538#endif // _WIN32
2539
Tony Barbourcc69f452015-10-08 13:59:42 -06002540 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002541 VkBool32 *supportsPresent =
2542 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002543 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002544 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002545 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002546 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002547
2548 // Search for a graphics and a present queue in the array of queue
2549 // families, try to find one that supports both
2550 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002551 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002552 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002553 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2554 if (graphicsQueueNodeIndex == UINT32_MAX) {
2555 graphicsQueueNodeIndex = i;
2556 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002557
Ian Elliottaaae5352015-07-06 14:27:58 -06002558 if (supportsPresent[i] == VK_TRUE) {
2559 graphicsQueueNodeIndex = i;
2560 presentQueueNodeIndex = i;
2561 break;
2562 }
2563 }
2564 }
2565 if (presentQueueNodeIndex == UINT32_MAX) {
2566 // If didn't find a queue that supports both graphics and present, then
2567 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002568 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002569 if (supportsPresent[i] == VK_TRUE) {
2570 presentQueueNodeIndex = i;
2571 break;
2572 }
2573 }
2574 }
2575 free(supportsPresent);
2576
2577 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002578 if (graphicsQueueNodeIndex == UINT32_MAX ||
2579 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002580 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002581 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002582 }
2583
2584 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002585 // synchronization, and appropriate tracking for QueueSubmit.
2586 // NOTE: While it is possible for an application to use a separate graphics
2587 // and a present queues, this demo program assumes it is only using
2588 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002589 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2590 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002591 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002592 }
2593
2594 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002595
Tony Barbourda2bad52016-01-22 14:36:40 -07002596 demo_create_device(demo);
2597
2598 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2599 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2600 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2601 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2602 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2603
Karl Schultze4dc75c2016-02-02 15:37:51 -07002604 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2605 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002606
Ian Elliottaaae5352015-07-06 14:27:58 -06002607 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002608 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002609 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002610 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002611 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002612 VkSurfaceFormatKHR *surfFormats =
2613 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002614 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002615 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002616 assert(!err);
2617 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2618 // the surface has no preferred format. Otherwise, at least one
2619 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002620 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002621 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002622 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002623 assert(formatCount >= 1);
2624 demo->format = surfFormats[0].format;
2625 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002626 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002627
David Pinedo2bb7c932015-06-18 17:03:14 -06002628 demo->quit = false;
2629 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002630
2631 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002632 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002633}
2634
Karl Schultze4dc75c2016-02-02 15:37:51 -07002635static void demo_init_connection(struct demo *demo) {
Ian Elliott639ca472015-04-16 15:23:05 -06002636#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002637 const xcb_setup_t *setup;
2638 xcb_screen_iterator_t iter;
2639 int scr;
2640
2641 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002642 if (demo->connection == NULL) {
2643 printf("Cannot find a compatible Vulkan installable client driver "
2644 "(ICD).\nExiting ...\n");
2645 fflush(stdout);
2646 exit(1);
2647 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002648
2649 setup = xcb_get_setup(demo->connection);
2650 iter = xcb_setup_roots_iterator(setup);
2651 while (scr-- > 0)
2652 xcb_screen_next(&iter);
2653
2654 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002655#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002656}
2657
Karl Schultze4dc75c2016-02-02 15:37:51 -07002658static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002659 vec3 eye = {0.0f, 3.0f, 5.0f};
2660 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002661 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002662
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002663 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002664 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002665
Piers Daniell735ee532015-02-23 16:23:13 -07002666 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002667 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002668 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002669 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002670 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002671 if (strcmp(argv[i], "--break") == 0) {
2672 demo->use_break = true;
2673 continue;
2674 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002675 if (strcmp(argv[i], "--validate") == 0) {
2676 demo->validate = true;
2677 continue;
2678 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002679 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2680 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2681 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002682 i++;
2683 continue;
2684 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002685
Karl Schultze4dc75c2016-02-02 15:37:51 -07002686 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
2687 "[--c <framecount>]\n",
2688 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002689 fflush(stderr);
2690 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002691 }
2692
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002693 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002694 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002695
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002696 demo->width = 500;
2697 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002698
2699 demo->spin_angle = 0.01f;
2700 demo->spin_increment = 0.01f;
2701 demo->pause = false;
2702
Karl Schultze4dc75c2016-02-02 15:37:51 -07002703 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
2704 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002705 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2706 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002707}
2708
Ian Elliott639ca472015-04-16 15:23:05 -06002709#ifdef _WIN32
Mark Young418b9d12016-01-06 14:26:04 -07002710// Include header required for parsing the command line options.
2711#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002712
Karl Schultze4dc75c2016-02-02 15:37:51 -07002713int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
2714 int nCmdShow) {
2715 MSG msg; // message
2716 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002717 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002718 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06002719
Karl Schultze4dc75c2016-02-02 15:37:51 -07002720 // Use the CommandLine functions to get the command line arguments.
2721 // Unfortunately, Microsoft outputs
2722 // this information as wide characters for Unicode, and we simply want the
2723 // Ascii version to be compatible
2724 // with the non-Windows side. So, we have to convert the information to
2725 // Ascii character strings.
2726 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
2727 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07002728 argc = 0;
2729 }
2730
Karl Schultze4dc75c2016-02-02 15:37:51 -07002731 if (argc > 0) {
2732 argv = (char **)malloc(sizeof(char *) * argc);
2733 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002734 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002735 } else {
2736 for (int iii = 0; iii < argc; iii++) {
2737 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07002738 size_t numConverted = 0;
2739
Karl Schultze4dc75c2016-02-02 15:37:51 -07002740 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
2741 if (argv[iii] != NULL) {
2742 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
2743 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07002744 }
2745 }
2746 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002747 } else {
Mark Young418b9d12016-01-06 14:26:04 -07002748 argv = NULL;
2749 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002750
2751 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07002752
2753 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002754 if (argc > 0 && argv != NULL) {
2755 for (int iii = 0; iii < argc; iii++) {
2756 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002757 free(argv[iii]);
2758 }
2759 }
2760 free(argv);
2761 }
2762
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002763 demo.connection = hInstance;
2764 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002765 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002766 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002767
2768 demo_prepare(&demo);
2769
Karl Schultze4dc75c2016-02-02 15:37:51 -07002770 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07002771
2772 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07002773 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002774 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002775 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06002776 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002777 done = true; // if found, quit app
2778 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06002779 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07002780 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06002781 DispatchMessage(&msg);
2782 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002783 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002784 }
2785
2786 demo_cleanup(&demo);
2787
Karl Schultze4dc75c2016-02-02 15:37:51 -07002788 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002789}
2790#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002791int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002792 struct demo demo;
2793
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002794 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002795 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002796 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002797
2798 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002799 demo_run(&demo);
2800
2801 demo_cleanup(&demo);
2802
2803 return 0;
2804}
Ian Elliott639ca472015-04-16 15:23:05 -06002805#endif // _WIN32