blob: 51dae383cc3ecc9bbed40c26abcba679129ed48b [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,
486 VkImageLayout new_image_layout) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600487 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800490 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800491 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800493 .commandPool = demo->cmd_pool,
494 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700495 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496 };
497
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800498 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600499 assert(!err);
500
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700501 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
502 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600503 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800504 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600505 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800506 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800507 .occlusionQueryEnable = VK_FALSE,
508 .queryFlags = 0,
509 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600510 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700511 VkCommandBufferBeginInfo cmd_buf_info = {
512 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
513 .pNext = NULL,
514 .flags = 0,
515 .pInheritanceInfo = &cmd_buf_hinfo,
516 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600517 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600518 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600519 }
520
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600521 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600522 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600523 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800524 .srcAccessMask = 0,
525 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600526 .oldLayout = old_image_layout,
527 .newLayout = new_image_layout,
528 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700529 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800531 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600532 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800533 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600534 }
535
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700536 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700537 image_memory_barrier.dstAccessMask =
538 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700539 }
540
541 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700542 image_memory_barrier.dstAccessMask =
543 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700544 }
545
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600546 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700548 image_memory_barrier.dstAccessMask =
549 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600550 }
551
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600552 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553
Tony Barbour50bbca42015-06-29 16:20:35 -0600554 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
555 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600556
Karl Schultze4dc75c2016-02-02 15:37:51 -0700557 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
558 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600559}
560
Karl Schultze4dc75c2016-02-02 15:37:51 -0700561static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700562 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
563 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600564 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800565 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600566 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800567 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800568 .occlusionQueryEnable = VK_FALSE,
569 .queryFlags = 0,
570 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700571 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700572 const VkCommandBufferBeginInfo cmd_buf_info = {
573 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
574 .pNext = NULL,
575 .flags = 0,
576 .pInheritanceInfo = &cmd_buf_hinfo,
577 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800578 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700579 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
580 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800581 };
582 const VkRenderPassBeginInfo rp_begin = {
583 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
584 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800585 .renderPass = demo->render_pass,
586 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800587 .renderArea.offset.x = 0,
588 .renderArea.offset.y = 0,
589 .renderArea.extent.width = demo->width,
590 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600591 .clearValueCount = 2,
592 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700593 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800594 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600595
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600596 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600597 assert(!err);
598
Chia-I Wuf051d262015-10-27 19:25:11 +0800599 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800600
Karl Schultze4dc75c2016-02-02 15:37:51 -0700601 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
602 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
603 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
604 NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600605
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600606 VkViewport viewport;
607 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700608 viewport.height = (float)demo->height;
609 viewport.width = (float)demo->width;
610 viewport.minDepth = (float)0.0f;
611 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700612 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600613
614 VkRect2D scissor;
615 memset(&scissor, 0, sizeof(scissor));
616 scissor.extent.width = demo->width;
617 scissor.extent.height = demo->height;
618 scissor.offset.x = 0;
619 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700620 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600621
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600622 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800623 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624
Tony Barbour15a4ef62015-10-21 10:14:48 -0600625 VkImageMemoryBarrier prePresentBarrier = {
626 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
627 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800628 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700629 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600630 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700631 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600632 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800633 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700634 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600635
636 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
637 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700638 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
639 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
640 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600641
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600642 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643 assert(!err);
644}
645
Karl Schultze4dc75c2016-02-02 15:37:51 -0700646void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600647 mat4x4 MVP, Model, VP;
648 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600649 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600650 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600651
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600652 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600654 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600655 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700656 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
657 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600658 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600659
Karl Schultze4dc75c2016-02-02 15:37:51 -0700660 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
661 demo->uniform_data.mem_alloc.allocationSize, 0,
662 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600663 assert(!err);
664
Karl Schultze4dc75c2016-02-02 15:37:51 -0700665 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600666
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600667 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600668}
669
Karl Schultze4dc75c2016-02-02 15:37:51 -0700670static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600671 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600672 VkSemaphore presentCompleteSemaphore;
673 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
674 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
675 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600676 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600677 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800678 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600679
Karl Schultze4dc75c2016-02-02 15:37:51 -0700680 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
681 NULL, &presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600682 assert(!err);
683
684 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700685 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Ian Elliottaaae5352015-07-06 14:27:58 -0600686 presentCompleteSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700687 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600688 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600689 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
690 // demo->swapchain is out of date (e.g. the window was resized) and
691 // must be recreated:
692 demo_resize(demo);
693 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800694 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600695 return;
696 } else if (err == VK_SUBOPTIMAL_KHR) {
697 // demo->swapchain is not as optimal as it could be, but the platform's
698 // presentation engine will still present the image correctly.
699 } else {
700 assert(!err);
701 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600702
Tony Barbour15a4ef62015-10-21 10:14:48 -0600703 // Assume the command buffer has been run on current_buffer before so
704 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
705 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700706 VK_IMAGE_ASPECT_COLOR_BIT,
707 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
708 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600709 demo_flush_init_cmd(demo);
710
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600711 // Wait for the present complete semaphore to be signaled to ensure
712 // that the image won't be rendered to until the presentation
713 // engine has fully released ownership to the application, and it is
714 // okay to render to the image.
715
Karl Schultze4dc75c2016-02-02 15:37:51 -0700716 // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
717 VkPipelineStageFlags pipe_stage_flags =
718 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
719 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
720 .pNext = NULL,
721 .waitSemaphoreCount = 1,
722 .pWaitSemaphores = &presentCompleteSemaphore,
723 .pWaitDstStageMask = &pipe_stage_flags,
724 .commandBufferCount = 1,
725 .pCommandBuffers =
726 &demo->buffers[demo->current_buffer].cmd,
727 .signalSemaphoreCount = 0,
728 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600729
730 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600731 assert(!err);
732
Ian Elliotta0781972015-08-21 15:09:33 -0600733 VkPresentInfoKHR present = {
734 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600735 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600736 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700737 .pSwapchains = &demo->swapchain,
738 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600739 };
740
Karl Schultze4dc75c2016-02-02 15:37:51 -0700741 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600742 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600743 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
744 // demo->swapchain is out of date (e.g. the window was resized) and
745 // must be recreated:
746 demo_resize(demo);
747 } else if (err == VK_SUBOPTIMAL_KHR) {
748 // demo->swapchain is not as optimal as it could be, but the platform's
749 // presentation engine will still present the image correctly.
750 } else {
751 assert(!err);
752 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600753
Chia-I Wucbb564e2015-04-16 22:02:10 +0800754 err = vkQueueWaitIdle(demo->queue);
755 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600756
Chia-I Wu63636062015-10-26 21:10:41 +0800757 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600758}
759
Karl Schultze4dc75c2016-02-02 15:37:51 -0700760static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600761 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600762 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600763
Ian Elliottb08e0d92015-11-20 11:55:46 -0700764 // Check the surface capabilities and formats
765 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700766 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
767 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600768 assert(!err);
769
Ian Elliott8528eff2015-08-07 15:56:59 -0600770 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700771 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
772 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600773 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600774 VkPresentModeKHR *presentModes =
775 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600776 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700777 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
778 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600779 assert(!err);
780
Ian Elliotta0781972015-08-21 15:09:33 -0600781 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600782 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700783 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600784 // If the surface size is undefined, the size is set to
785 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600786 swapchainExtent.width = demo->width;
787 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700788 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600789 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700790 swapchainExtent = surfCapabilities.currentExtent;
791 demo->width = surfCapabilities.currentExtent.width;
792 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600793 }
794
795 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600796 // tearing mode. If not, try IMMEDIATE which will usually be available,
797 // and is fastest (though it tears). If not, fall back to FIFO which is
798 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600799 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600800 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600801 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
802 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600803 break;
804 }
Ian Elliotta0781972015-08-21 15:09:33 -0600805 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
806 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
807 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600808 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600809 }
810
811 // Determine the number of VkImage's to use in the swap chain (we desire to
812 // own only 1 image at a time, besides the images being displayed and
813 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700814 uint32_t desiredNumberOfSwapchainImages =
815 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700816 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700817 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600818 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700819 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600820 }
821
Tony Barbour9fd6d352015-09-16 15:01:32 -0600822 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700823 if (surfCapabilities.supportedTransforms &
824 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700825 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600826 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700827 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600828 }
829
Ian Elliottcf074d32015-10-16 18:02:43 -0600830 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600831 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800832 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700833 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600834 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800835 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600836 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700837 .imageExtent =
838 {
839 .width = swapchainExtent.width, .height = swapchainExtent.height,
840 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700841 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600842 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700843 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700844 .imageArrayLayers = 1,
845 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
846 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600847 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600848 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600849 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600850 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600851 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600852 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600853
Karl Schultze4dc75c2016-02-02 15:37:51 -0700854 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
855 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800856 assert(!err);
857
Ian Elliottcf074d32015-10-16 18:02:43 -0600858 // If we just re-created an existing swapchain, we should destroy the old
859 // swapchain at this point.
860 // Note: destroying the swapchain also cleans up all its associated
861 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800862 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700863 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600864 }
865
866 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600867 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600868 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800869
Karl Schultze4dc75c2016-02-02 15:37:51 -0700870 VkImage *swapchainImages =
871 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600872 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600873 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600874 &demo->swapchainImageCount,
875 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600876 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600877
Karl Schultze4dc75c2016-02-02 15:37:51 -0700878 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
879 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600880 assert(demo->buffers);
881
Ian Elliotta0781972015-08-21 15:09:33 -0600882 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600883 VkImageViewCreateInfo color_image_view = {
884 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600885 .pNext = NULL,
886 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700887 .components =
888 {
889 .r = VK_COMPONENT_SWIZZLE_R,
890 .g = VK_COMPONENT_SWIZZLE_G,
891 .b = VK_COMPONENT_SWIZZLE_B,
892 .a = VK_COMPONENT_SWIZZLE_A,
893 },
894 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
895 .baseMipLevel = 0,
896 .levelCount = 1,
897 .baseArrayLayer = 0,
898 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600899 .viewType = VK_IMAGE_VIEW_TYPE_2D,
900 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600901 };
902
Ian Elliotta0781972015-08-21 15:09:33 -0600903 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600904
Karl Schultze4dc75c2016-02-02 15:37:51 -0700905 // Render loop will expect image to have been used before and in
906 // VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
907 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image
908 // to that state
909 demo_set_image_layout(
910 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
911 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600912
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600913 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600914
Karl Schultze4dc75c2016-02-02 15:37:51 -0700915 err = vkCreateImageView(demo->device, &color_image_view, NULL,
916 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600917 assert(!err);
918 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700919
Mark Young04fd3d82016-01-25 14:43:50 -0700920 if (NULL != presentModes) {
921 free(presentModes);
922 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600923}
924
Karl Schultze4dc75c2016-02-02 15:37:51 -0700925static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600926 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600927 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600928 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600929 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600930 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600931 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700932 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600933 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600934 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800935 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600936 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600937 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600938 .flags = 0,
939 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200940
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600941 VkImageViewCreateInfo view = {
942 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600943 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800944 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600945 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700946 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
947 .baseMipLevel = 0,
948 .levelCount = 1,
949 .baseArrayLayer = 0,
950 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600951 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600952 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600953 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600954
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500955 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600956 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600957 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600958
959 demo->depth.format = depth_format;
960
961 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700962 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600963 assert(!err);
964
Karl Schultze4dc75c2016-02-02 15:37:51 -0700965 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600966 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600967
Chia-I Wuf48700b2015-11-10 16:21:09 +0800968 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200969 demo->depth.mem_alloc.pNext = NULL;
970 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
971 demo->depth.mem_alloc.memoryTypeIndex = 0;
972
Karl Schultze4dc75c2016-02-02 15:37:51 -0700973 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
974 0, /* No requirements */
975 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600976 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600977
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500978 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700979 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
980 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500981 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600982
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500983 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700984 err =
985 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500986 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600987
Karl Schultze4dc75c2016-02-02 15:37:51 -0700988 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
989 VK_IMAGE_LAYOUT_UNDEFINED,
990 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600991
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600992 /* create image view */
993 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800994 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600995 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600996}
997
Tony Barbour1a86d032015-09-21 15:17:33 -0600998/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700999bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001000 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
1001 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001002 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001003
Tony Barbour1a86d032015-09-21 15:17:33 -06001004 if (!fPtr)
1005 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001006
Tony Barbour1a86d032015-09-21 15:17:33 -06001007 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001008 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1009 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001010 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001011 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001012
Tony Barbour1a86d032015-09-21 15:17:33 -06001013 do {
1014 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001015 if (cPtr == NULL) {
1016 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001017 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001018 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001019 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001020
Tony Barbour1a86d032015-09-21 15:17:33 -06001021 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001022 if (rgba_data == NULL) {
1023 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001024 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001025 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001026 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001027 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001028 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1029 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001030 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001031 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001032
Karl Schultze4dc75c2016-02-02 15:37:51 -07001033 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001034 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001035 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001036 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001037 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001038 rowPtr[3] = 255; /* Alpha of 1 */
1039 rowPtr += 4;
1040 }
1041 rgba_data += layout->rowPitch;
1042 }
1043 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001044 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001045}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001046
Karl Schultze4dc75c2016-02-02 15:37:51 -07001047static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001048 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001049 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001050 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001051 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001052 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001053 int32_t tex_width;
1054 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001055 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001056 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001057
Karl Schultze4dc75c2016-02-02 15:37:51 -07001058 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
David Pinedo30bd71d2015-04-23 08:16:57 -06001059 printf("Failed to load textures\n");
1060 fflush(stdout);
1061 exit(1);
1062 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001063
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001064 tex_obj->tex_width = tex_width;
1065 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001067 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001068 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001070 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001072 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001074 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001075 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001076 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001077 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078 .flags = 0,
1079 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001081 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082
Karl Schultze4dc75c2016-02-02 15:37:51 -07001083 err =
1084 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085 assert(!err);
1086
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001087 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001088
Chia-I Wuf48700b2015-11-10 16:21:09 +08001089 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001090 tex_obj->mem_alloc.pNext = NULL;
1091 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1092 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001093
Karl Schultze4dc75c2016-02-02 15:37:51 -07001094 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1095 required_props,
1096 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001097 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001098
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001099 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001100 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001101 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001102 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001103
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001104 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001105 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001106 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001107
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001108 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001109 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001110 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001112 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001113 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001114 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115 void *data;
1116
Karl Schultze4dc75c2016-02-02 15:37:51 -07001117 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1118 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119
Karl Schultze4dc75c2016-02-02 15:37:51 -07001120 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1121 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001122 assert(!err);
1123
1124 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1125 fprintf(stderr, "Error loading texture: %s\n", filename);
1126 }
1127
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001128 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001130
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001131 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001132 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
1133 VK_IMAGE_LAYOUT_UNDEFINED, tex_obj->imageLayout);
1134 /* setting the image layout does not reference the actual memory so no need
1135 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001136}
1137
Karl Schultze4dc75c2016-02-02 15:37:51 -07001138static void demo_destroy_texture_image(struct demo *demo,
1139 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001140 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001141 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1142 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001143}
1144
Karl Schultze4dc75c2016-02-02 15:37:51 -07001145static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001146 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001147 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001148 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001149
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001150 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001151
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001152 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001153 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001154
Karl Schultze4dc75c2016-02-02 15:37:51 -07001155 if ((props.linearTilingFeatures &
1156 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1157 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001158 /* Device can texture using linear textures */
1159 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Karl Schultze4dc75c2016-02-02 15:37:51 -07001160 VK_IMAGE_TILING_LINEAR,
1161 VK_IMAGE_USAGE_SAMPLED_BIT,
1162 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
1163 } else if (props.optimalTilingFeatures &
1164 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001165 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001166 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001167
1168 memset(&staging_texture, 0, sizeof(staging_texture));
1169 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001170 VK_IMAGE_TILING_LINEAR,
1171 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1172 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001173
Karl Schultze4dc75c2016-02-02 15:37:51 -07001174 demo_prepare_texture_image(
1175 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1176 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1177 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001178
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001179 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001180 VK_IMAGE_ASPECT_COLOR_BIT,
1181 staging_texture.imageLayout,
1182 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001183
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001184 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001185 VK_IMAGE_ASPECT_COLOR_BIT,
1186 demo->textures[i].imageLayout,
1187 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001188
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001189 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001190 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1191 .srcOffset = {0, 0, 0},
1192 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1193 .dstOffset = {0, 0, 0},
1194 .extent = {staging_texture.tex_width,
1195 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001196 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001197 vkCmdCopyImage(
1198 demo->cmd, staging_texture.image,
1199 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1200 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001201
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001202 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001203 VK_IMAGE_ASPECT_COLOR_BIT,
1204 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1205 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001206
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001207 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001209 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001210 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001211 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1212 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001213 }
1214
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001215 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001216 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001217 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001218 .magFilter = VK_FILTER_NEAREST,
1219 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001220 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001221 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1222 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1223 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001224 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001225 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001226 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001227 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001228 .minLod = 0.0f,
1229 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001230 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001231 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001233
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001234 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001235 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001236 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001237 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001238 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001239 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001240 .components =
1241 {
1242 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1243 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1244 },
1245 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001246 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001248
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001249 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001250 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001251 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252 assert(!err);
1253
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001254 /* create image view */
1255 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001256 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001257 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001258 assert(!err);
1259 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001260}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001261
Karl Schultze4dc75c2016-02-02 15:37:51 -07001262void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001263 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001264 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001265 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001266 int i;
1267 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001268 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001269 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001270 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001271
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001272 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001273 mat4x4_mul(MVP, VP, demo->model_matrix);
1274 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001275 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001276
Karl Schultze4dc75c2016-02-02 15:37:51 -07001277 for (i = 0; i < 12 * 3; i++) {
1278 data.position[i][0] = g_vertex_buffer_data[i * 3];
1279 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1280 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001281 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001282 data.attr[i][0] = g_uv_buffer_data[2 * i];
1283 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001284 data.attr[i][2] = 0;
1285 data.attr[i][3] = 0;
1286 }
1287
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001288 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001289 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001290 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001291 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001292 err =
1293 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001294 assert(!err);
1295
Karl Schultze4dc75c2016-02-02 15:37:51 -07001296 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1297 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001298
Chia-I Wuf48700b2015-11-10 16:21:09 +08001299 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001300 demo->uniform_data.mem_alloc.pNext = NULL;
1301 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1302 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1303
Karl Schultze4dc75c2016-02-02 15:37:51 -07001304 pass = memory_type_from_properties(
1305 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1306 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001307 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001308
Karl Schultze4dc75c2016-02-02 15:37:51 -07001309 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1310 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001311 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001312
Karl Schultze4dc75c2016-02-02 15:37:51 -07001313 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1314 demo->uniform_data.mem_alloc.allocationSize, 0,
1315 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001316 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001317
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001318 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001319
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001320 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001321
Karl Schultze4dc75c2016-02-02 15:37:51 -07001322 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1323 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001324 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001325
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001326 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1327 demo->uniform_data.buffer_info.offset = 0;
1328 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001329}
1330
Karl Schultze4dc75c2016-02-02 15:37:51 -07001331static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001332 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001333 [0] =
1334 {
1335 .binding = 0,
1336 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1337 .descriptorCount = 1,
1338 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1339 .pImmutableSamplers = NULL,
1340 },
1341 [1] =
1342 {
1343 .binding = 1,
1344 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1345 .descriptorCount = DEMO_TEXTURE_COUNT,
1346 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1347 .pImmutableSamplers = NULL,
1348 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001349 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001350 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001351 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001352 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001353 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001354 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001355 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001356 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001357
Karl Schultze4dc75c2016-02-02 15:37:51 -07001358 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1359 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001360 assert(!err);
1361
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001362 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001363 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1364 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001365 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001367 };
1368
Karl Schultze4dc75c2016-02-02 15:37:51 -07001369 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001370 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001372}
1373
Karl Schultze4dc75c2016-02-02 15:37:51 -07001374static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001375 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001376 [0] =
1377 {
1378 .format = demo->format,
1379 .samples = VK_SAMPLE_COUNT_1_BIT,
1380 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1381 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1382 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1383 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1384 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1385 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1386 },
1387 [1] =
1388 {
1389 .format = demo->depth.format,
1390 .samples = VK_SAMPLE_COUNT_1_BIT,
1391 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1392 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1393 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1394 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1395 .initialLayout =
1396 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1397 .finalLayout =
1398 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1399 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001400 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001401 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001402 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001403 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001404 const VkAttachmentReference depth_reference = {
1405 .attachment = 1,
1406 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1407 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001408 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001409 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1410 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001411 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001412 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001413 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001414 .pColorAttachments = &color_reference,
1415 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001416 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001417 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001418 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001419 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001420 const VkRenderPassCreateInfo rp_info = {
1421 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1422 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001423 .attachmentCount = 2,
1424 .pAttachments = attachments,
1425 .subpassCount = 1,
1426 .pSubpasses = &subpass,
1427 .dependencyCount = 0,
1428 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001429 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001430 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001431
Chia-I Wu63636062015-10-26 21:10:41 +08001432 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001433 assert(!err);
1434}
1435
Karl Schultze4dc75c2016-02-02 15:37:51 -07001436static VkShaderModule
1437demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001438 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001439 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001440 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001441
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001442 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1443 moduleCreateInfo.pNext = NULL;
1444
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001445 moduleCreateInfo.codeSize = size;
1446 moduleCreateInfo.pCode = code;
1447 moduleCreateInfo.flags = 0;
1448 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1449 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001450
1451 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001452}
1453
Karl Schultze4dc75c2016-02-02 15:37:51 -07001454char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001455 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001456 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001457 void *shader_code;
1458
1459 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001460 if (!fp)
1461 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001462
1463 fseek(fp, 0L, SEEK_END);
1464 size = ftell(fp);
1465
1466 fseek(fp, 0L, SEEK_SET);
1467
1468 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001469 retval = fread(shader_code, size, 1, fp);
1470 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001471
1472 *psize = size;
1473
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001474 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001475 return shader_code;
1476}
1477
Karl Schultze4dc75c2016-02-02 15:37:51 -07001478static VkShaderModule demo_prepare_vs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001479 void *vertShaderCode;
1480 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001481
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001482 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1483
Karl Schultze4dc75c2016-02-02 15:37:51 -07001484 demo->vert_shader_module =
1485 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001486
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001487 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001488
1489 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001490}
1491
Karl Schultze4dc75c2016-02-02 15:37:51 -07001492static VkShaderModule demo_prepare_fs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001493 void *fragShaderCode;
1494 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001495
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001496 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001497
Karl Schultze4dc75c2016-02-02 15:37:51 -07001498 demo->frag_shader_module =
1499 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001500
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001501 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001502
1503 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001504}
1505
Karl Schultze4dc75c2016-02-02 15:37:51 -07001506static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001507 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001508 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001509 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001510 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001511 VkPipelineRasterizationStateCreateInfo rs;
1512 VkPipelineColorBlendStateCreateInfo cb;
1513 VkPipelineDepthStencilStateCreateInfo ds;
1514 VkPipelineViewportStateCreateInfo vp;
1515 VkPipelineMultisampleStateCreateInfo ms;
1516 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1517 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001518 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001519
Piers Daniellba839d12015-09-29 13:01:09 -06001520 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1521 memset(&dynamicState, 0, sizeof dynamicState);
1522 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1523 dynamicState.pDynamicStates = dynamicStateEnables;
1524
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001525 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001526 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001527 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001528
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001529 memset(&vi, 0, sizeof(vi));
1530 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1531
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001533 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001534 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001535
1536 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001537 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1538 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001539 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001540 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001541 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001542 rs.rasterizerDiscardEnable = VK_FALSE;
1543 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001544
1545 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001546 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1547 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001548 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001549 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001550 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001551 cb.attachmentCount = 1;
1552 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001553
Tony Barbour29645d02015-01-16 14:27:35 -07001554 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001555 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001556 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001557 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1558 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001559 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001560 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1561 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001562
1563 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001564 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001565 ds.depthTestEnable = VK_TRUE;
1566 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001567 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001568 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001569 ds.back.failOp = VK_STENCIL_OP_KEEP;
1570 ds.back.passOp = VK_STENCIL_OP_KEEP;
1571 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001572 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001573 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001574
Tony Barbour29645d02015-01-16 14:27:35 -07001575 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001576 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001577 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001578 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001579
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001580 // Two stages: vs and fs
1581 pipeline.stageCount = 2;
1582 VkPipelineShaderStageCreateInfo shaderStages[2];
1583 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1584
Karl Schultze4dc75c2016-02-02 15:37:51 -07001585 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1586 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001587 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001588 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001589
Karl Schultze4dc75c2016-02-02 15:37:51 -07001590 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1591 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001592 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001593 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001594
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001595 memset(&pipelineCache, 0, sizeof(pipelineCache));
1596 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001597
Karl Schultze4dc75c2016-02-02 15:37:51 -07001598 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1599 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001600 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001601
Karl Schultze4dc75c2016-02-02 15:37:51 -07001602 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001603 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001604 pipeline.pRasterizationState = &rs;
1605 pipeline.pColorBlendState = &cb;
1606 pipeline.pMultisampleState = &ms;
1607 pipeline.pViewportState = &vp;
1608 pipeline.pDepthStencilState = &ds;
1609 pipeline.pStages = shaderStages;
1610 pipeline.renderPass = demo->render_pass;
1611 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001612
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001613 pipeline.renderPass = demo->render_pass;
1614
Karl Schultze4dc75c2016-02-02 15:37:51 -07001615 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1616 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001617 assert(!err);
1618
Chia-I Wu63636062015-10-26 21:10:41 +08001619 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1620 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001621}
1622
Karl Schultze4dc75c2016-02-02 15:37:51 -07001623static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001624 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001625 [0] =
1626 {
1627 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1628 .descriptorCount = 1,
1629 },
1630 [1] =
1631 {
1632 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1633 .descriptorCount = DEMO_TEXTURE_COUNT,
1634 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001635 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001636 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001637 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001638 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001639 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001640 .poolSizeCount = 2,
1641 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001642 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001643 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001644
Karl Schultze4dc75c2016-02-02 15:37:51 -07001645 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1646 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001647 assert(!err);
1648}
1649
Karl Schultze4dc75c2016-02-02 15:37:51 -07001650static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001651 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001652 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001653 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001654 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001655
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001656 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001657 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001658 .pNext = NULL,
1659 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001660 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001661 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001662 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001663 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001664
Chia-I Wuae721ba2015-05-25 16:27:55 +08001665 memset(&tex_descs, 0, sizeof(tex_descs));
1666 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001667 tex_descs[i].sampler = demo->textures[i].sampler;
1668 tex_descs[i].imageView = demo->textures[i].view;
1669 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001670 }
1671
1672 memset(&writes, 0, sizeof(writes));
1673
1674 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001675 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001676 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001677 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001678 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001679
1680 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001681 writes[1].dstSet = demo->desc_set;
1682 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001683 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001684 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001685 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001686
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001687 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001688}
1689
Karl Schultze4dc75c2016-02-02 15:37:51 -07001690static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001691 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001692 attachments[1] = demo->depth.view;
1693
Chia-I Wuf973e322015-07-08 13:34:24 +08001694 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001695 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1696 .pNext = NULL,
1697 .renderPass = demo->render_pass,
1698 .attachmentCount = 2,
1699 .pAttachments = attachments,
1700 .width = demo->width,
1701 .height = demo->height,
1702 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001703 };
1704 VkResult U_ASSERT_ONLY err;
1705 uint32_t i;
1706
Karl Schultze4dc75c2016-02-02 15:37:51 -07001707 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1708 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001709 assert(demo->framebuffers);
1710
1711 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001712 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001713 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1714 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001715 assert(!err);
1716 }
1717}
1718
Karl Schultze4dc75c2016-02-02 15:37:51 -07001719static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001720 VkResult U_ASSERT_ONLY err;
1721
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001722 const VkCommandPoolCreateInfo cmd_pool_info = {
1723 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001724 .pNext = NULL,
1725 .queueFamilyIndex = demo->graphics_queue_node_index,
1726 .flags = 0,
1727 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001728 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1729 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001730 assert(!err);
1731
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001732 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001733 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001735 .commandPool = demo->cmd_pool,
1736 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001737 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001738 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001739
1740 demo_prepare_buffers(demo);
1741 demo_prepare_depth(demo);
1742 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001743 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001745 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001746 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001748
Ian Elliotta0781972015-08-21 15:09:33 -06001749 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001750 err =
1751 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001752 assert(!err);
1753 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001754
Chia-I Wu63ea9262015-03-26 13:14:16 +08001755 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001756 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001757
Chia-I Wuf973e322015-07-08 13:34:24 +08001758 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001759
Ian Elliotta0781972015-08-21 15:09:33 -06001760 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001761 demo->current_buffer = i;
1762 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1763 }
1764
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001765 /*
1766 * Prepare functions above may generate pipeline commands
1767 * that need to be flushed before beginning the render loop.
1768 */
1769 demo_flush_init_cmd(demo);
1770
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001771 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001772 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001773}
1774
Karl Schultze4dc75c2016-02-02 15:37:51 -07001775static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001776 uint32_t i;
1777
1778 demo->prepared = false;
1779
Tony Barbourd8e504f2015-10-08 14:26:24 -06001780 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001781 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001782 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001783 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001784 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001785
Chia-I Wu63636062015-10-26 21:10:41 +08001786 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1787 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1788 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1789 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1790 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001791
1792 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001793 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1794 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1795 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1796 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001797 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001798 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001799
Chia-I Wu63636062015-10-26 21:10:41 +08001800 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1801 vkDestroyImage(demo->device, demo->depth.image, NULL);
1802 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001803
Chia-I Wu63636062015-10-26 21:10:41 +08001804 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1805 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001806
Ian Elliotta0781972015-08-21 15:09:33 -06001807 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001808 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001809 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1810 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001811 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001812 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001813
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001814 free(demo->queue_props);
1815
Chia-I Wu63636062015-10-26 21:10:41 +08001816 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1817 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001818 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001819 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001820 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001821 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001822 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001823
1824#ifndef _WIN32
1825 xcb_destroy_window(demo->connection, demo->window);
1826 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001827 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001828#endif // _WIN32
1829}
1830
Karl Schultze4dc75c2016-02-02 15:37:51 -07001831static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001832 uint32_t i;
1833
Mike Stroyanbb60b382015-10-28 09:46:57 -06001834 // Don't react to resize until after first initialization.
1835 if (!demo->prepared) {
1836 return;
1837 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001838 // In order to properly resize the window, we must re-create the swapchain
1839 // AND redo the command buffers, etc.
1840 //
1841 // First, perform part of the demo_cleanup() function:
1842 demo->prepared = false;
1843
1844 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001845 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001846 }
1847 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001848 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001849
Chia-I Wu63636062015-10-26 21:10:41 +08001850 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1851 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1852 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1853 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1854 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001855
1856 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001857 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1858 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1859 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1860 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001861 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001862
Chia-I Wu63636062015-10-26 21:10:41 +08001863 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1864 vkDestroyImage(demo->device, demo->depth.image, NULL);
1865 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001866
Chia-I Wu63636062015-10-26 21:10:41 +08001867 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1868 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001869
1870 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001871 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001872 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1873 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001874 }
Chia-I Wu63636062015-10-26 21:10:41 +08001875 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001876 free(demo->buffers);
1877
Ian Elliottcf074d32015-10-16 18:02:43 -06001878 // Second, re-perform the demo_prepare() function, which will re-create the
1879 // swapchain:
1880 demo_prepare(demo);
1881}
1882
David Pinedo2bb7c932015-06-18 17:03:14 -06001883// On MS-Windows, make this a global, so it's available to WndProc()
1884struct demo demo;
1885
Ian Elliott639ca472015-04-16 15:23:05 -06001886#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07001887static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001888 if (!demo->prepared)
1889 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001890 // Wait for work to finish before updating MVP.
1891 vkDeviceWaitIdle(demo->device);
1892 demo_update_data_buffer(demo);
1893
1894 demo_draw(demo);
1895
1896 // Wait for work to finish before updating MVP.
1897 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001898
David Pinedo2bb7c932015-06-18 17:03:14 -06001899 demo->curFrame++;
1900
Karl Schultze4dc75c2016-02-02 15:37:51 -07001901 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
1902 demo->quit = true;
David Pinedo2bb7c932015-06-18 17:03:14 -06001903 demo_cleanup(demo);
1904 ExitProcess(0);
1905 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001906}
Ian Elliott639ca472015-04-16 15:23:05 -06001907
1908// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001909LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
1910 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001911 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001912 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001913 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001914 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001915 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001916 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001917 case WM_SIZE:
Mike Stroyanbb60b382015-10-28 09:46:57 -06001918 demo.width = lParam & 0xffff;
1919 demo.height = lParam & 0xffff0000 >> 16;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001920 demo_resize(&demo);
1921 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001922 default:
1923 break;
1924 }
1925 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1926}
1927
Karl Schultze4dc75c2016-02-02 15:37:51 -07001928static void demo_create_window(struct demo *demo) {
1929 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06001930
1931 // Initialize the window class structure:
1932 win_class.cbSize = sizeof(WNDCLASSEX);
1933 win_class.style = CS_HREDRAW | CS_VREDRAW;
1934 win_class.lpfnWndProc = WndProc;
1935 win_class.cbClsExtra = 0;
1936 win_class.cbWndExtra = 0;
1937 win_class.hInstance = demo->connection; // hInstance
1938 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1939 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1940 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1941 win_class.lpszMenuName = NULL;
1942 win_class.lpszClassName = demo->name;
1943 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1944 // Register window class:
1945 if (!RegisterClassEx(&win_class)) {
1946 // It didn't work, so try to give a useful error:
1947 printf("Unexpected error trying to start the application!\n");
1948 fflush(stdout);
1949 exit(1);
1950 }
1951 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001952 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06001953 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001954 demo->window = CreateWindowEx(0,
1955 demo->name, // class name
1956 demo->name, // app name
1957 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07001958 WS_VISIBLE | WS_SYSMENU,
1959 100, 100, // x/y coords
1960 wr.right - wr.left, // width
1961 wr.bottom - wr.top, // height
1962 NULL, // handle to parent
1963 NULL, // handle to menu
1964 demo->connection, // hInstance
1965 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06001966 if (!demo->window) {
1967 // It didn't work, so try to give a useful error:
1968 printf("Cannot create a window in which to draw!\n");
1969 fflush(stdout);
1970 exit(1);
1971 }
1972}
1973#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001974static void demo_handle_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001975 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07001976 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001977 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001978 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001979 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001980 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001981 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001982 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
1983 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001984 demo->quit = true;
1985 }
1986 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001987 case XCB_KEY_RELEASE: {
1988 const xcb_key_release_event_t *key =
1989 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001990
Karl Schultze4dc75c2016-02-02 15:37:51 -07001991 switch (key->detail) {
1992 case 0x9: // Escape
1993 demo->quit = true;
1994 break;
1995 case 0x71: // left arrow key
1996 demo->spin_angle += demo->spin_increment;
1997 break;
1998 case 0x72: // right arrow key
1999 demo->spin_angle -= demo->spin_increment;
2000 break;
2001 case 0x41:
2002 demo->pause = !demo->pause;
2003 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002004 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002005 } break;
2006 case XCB_CONFIGURE_NOTIFY: {
2007 const xcb_configure_notify_event_t *cfg =
2008 (const xcb_configure_notify_event_t *)event;
2009 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002010 demo->width = cfg->width;
2011 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002012 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002013 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002014 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002015 default:
2016 break;
2017 }
2018}
2019
Karl Schultze4dc75c2016-02-02 15:37:51 -07002020static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002021 xcb_flush(demo->connection);
2022
2023 while (!demo->quit) {
2024 xcb_generic_event_t *event;
2025
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002026 if (demo->pause) {
2027 event = xcb_wait_for_event(demo->connection);
2028 } else {
2029 event = xcb_poll_for_event(demo->connection);
2030 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002031 if (event) {
2032 demo_handle_event(demo, event);
2033 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002034 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002035
2036 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002037 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002038 demo_update_data_buffer(demo);
2039
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002040 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002041
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002042 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002043 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002044 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002045 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002046 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002047 }
2048}
2049
Karl Schultze4dc75c2016-02-02 15:37:51 -07002050static void demo_create_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002051 uint32_t value_mask, value_list[32];
2052
2053 demo->window = xcb_generate_id(demo->connection);
2054
2055 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2056 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002057 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002058 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002059
Karl Schultze4dc75c2016-02-02 15:37:51 -07002060 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->window,
2061 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2062 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2063 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002064
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002065 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002066 xcb_intern_atom_cookie_t cookie =
2067 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2068 xcb_intern_atom_reply_t *reply =
2069 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002070
Karl Schultze4dc75c2016-02-02 15:37:51 -07002071 xcb_intern_atom_cookie_t cookie2 =
2072 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2073 demo->atom_wm_delete_window =
2074 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002075
Karl Schultze4dc75c2016-02-02 15:37:51 -07002076 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->window,
2077 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002078 &(*demo->atom_wm_delete_window).atom);
2079 free(reply);
2080
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002081 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002082
Karl Schultze4dc75c2016-02-02 15:37:51 -07002083 // Force the x/y coordinates to 100,100 results are identical in consecutive
2084 // runs
2085 const uint32_t coords[] = {100, 100};
David Pinedo2bb7c932015-06-18 17:03:14 -06002086 xcb_configure_window(demo->connection, demo->window,
2087 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002088}
Ian Elliott639ca472015-04-16 15:23:05 -06002089#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002090
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002091/*
2092 * Return 1 (true) if all layer names specified in check_names
2093 * can be found in given layer properties.
2094 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002095static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002096 uint32_t layer_count,
2097 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002098 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002099 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002100 for (uint32_t j = 0; j < layer_count; j++) {
2101 if (!strcmp(check_names[i], layers[j].layerName)) {
2102 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002103 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002104 }
2105 }
2106 if (!found) {
2107 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2108 return 0;
2109 }
2110 }
2111 return 1;
2112}
2113
Karl Schultze4dc75c2016-02-02 15:37:51 -07002114static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002115 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002116 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002117 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002118 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002119 uint32_t device_validation_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002120 uint32_t enabled_extension_count = 0;
2121 uint32_t enabled_layer_count = 0;
2122
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002123 char *instance_validation_layers[] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002124 "VK_LAYER_LUNARG_threading", "VK_LAYER_LUNARG_mem_tracker",
2125 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_draw_state",
2126 "VK_LAYER_LUNARG_param_checker", "VK_LAYER_LUNARG_swapchain",
2127 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_image",
Dustin Gravescfe5ade2016-01-26 16:30:22 -07002128 "VK_LAYER_GOOGLE_unique_objects",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002129 };
2130
Tony Barbourda2bad52016-01-22 14:36:40 -07002131 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_threading";
2132 demo->device_validation_layers[1] = "VK_LAYER_LUNARG_mem_tracker";
Dustin Gravescfe5ade2016-01-26 16:30:22 -07002133 demo->device_validation_layers[2] = "VK_LAYER_LUNARG_object_tracker";
2134 demo->device_validation_layers[3] = "VK_LAYER_LUNARG_draw_state";
2135 demo->device_validation_layers[4] = "VK_LAYER_LUNARG_param_checker";
2136 demo->device_validation_layers[5] = "VK_LAYER_LUNARG_swapchain";
2137 demo->device_validation_layers[6] = "VK_LAYER_LUNARG_device_limits";
2138 demo->device_validation_layers[7] = "VK_LAYER_LUNARG_image";
2139 demo->device_validation_layers[8] = "VK_LAYER_GOOGLE_unique_objects";
2140 device_validation_layer_count = 9;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002141
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002142 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002143 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002144 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002145 assert(!err);
2146
Dustin Graves7c287352016-01-27 13:48:06 -07002147 if (instance_layer_count > 0) {
2148 VkLayerProperties *instance_layers =
2149 malloc(sizeof(VkLayerProperties) * instance_layer_count);
2150 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2151 instance_layers);
2152 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002153
Dustin Graves7c287352016-01-27 13:48:06 -07002154 if (demo->validate) {
2155 validation_found = demo_check_layers(
2156 ARRAY_SIZE(instance_validation_layers),
2157 instance_validation_layers, instance_layer_count,
2158 instance_layers);
2159
2160 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002161 }
Dustin Graves7c287352016-01-27 13:48:06 -07002162
2163 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002164 }
2165
Dustin Graves7c287352016-01-27 13:48:06 -07002166 if (demo->validate && !validation_found) {
2167 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
2168 "required validation layer.\n\n"
2169 "Please look at the Getting Started guide for additional "
2170 "information.\n",
2171 "vkCreateInstance Failure");
2172 }
2173
2174 /* Look for instance extensions */
2175 VkBool32 surfaceExtFound = 0;
2176 VkBool32 platformSurfaceExtFound = 0;
2177 memset(extension_names, 0, sizeof(extension_names));
2178
Karl Schultze4dc75c2016-02-02 15:37:51 -07002179 err = vkEnumerateInstanceExtensionProperties(
2180 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002181 assert(!err);
2182
Dustin Graves7c287352016-01-27 13:48:06 -07002183 if (instance_extension_count > 0) {
2184 VkExtensionProperties *instance_extensions =
2185 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2186 err = vkEnumerateInstanceExtensionProperties(
2187 NULL, &instance_extension_count, instance_extensions);
2188 assert(!err);
2189 for (uint32_t i = 0; i < instance_extension_count; i++) {
2190 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2191 instance_extensions[i].extensionName)) {
2192 surfaceExtFound = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002193 extension_names[enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002194 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002195 }
Dustin Graves7c287352016-01-27 13:48:06 -07002196#ifdef _WIN32
2197 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2198 instance_extensions[i].extensionName)) {
2199 platformSurfaceExtFound = 1;
2200 extension_names[enabled_extension_count++] =
2201 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2202 }
2203#else // _WIN32
2204 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2205 instance_extensions[i].extensionName)) {
2206 platformSurfaceExtFound = 1;
2207 extension_names[enabled_extension_count++] =
2208 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2209 }
2210#endif // _WIN32
2211 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2212 instance_extensions[i].extensionName)) {
2213 if (demo->validate) {
2214 extension_names[enabled_extension_count++] =
2215 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2216 }
2217 }
2218 assert(enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002219 }
Dustin Graves7c287352016-01-27 13:48:06 -07002220
2221 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002222 }
Dustin Graves7c287352016-01-27 13:48:06 -07002223
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002224 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002225 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2226 "the " VK_KHR_SURFACE_EXTENSION_NAME
2227 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002228 "Vulkan installable client driver (ICD) installed?\nPlease "
2229 "look at the Getting Started guide for additional "
2230 "information.\n",
2231 "vkCreateInstance Failure");
2232 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002233 if (!platformSurfaceExtFound) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002234#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002235 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2236 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2237 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002238 "Vulkan installable client driver (ICD) installed?\nPlease "
2239 "look at the Getting Started guide for additional "
2240 "information.\n",
2241 "vkCreateInstance Failure");
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002242#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002243 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2244 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2245 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002246 "Vulkan installable client driver (ICD) installed?\nPlease "
2247 "look at the Getting Started guide for additional "
2248 "information.\n",
2249 "vkCreateInstance Failure");
2250#endif // _WIN32
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002251 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002252 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002253 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002254 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002255 .pApplicationName = APP_SHORT_NAME,
2256 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002257 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002258 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002259 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002260 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002261 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002262 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002263 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002264 .pApplicationInfo = &app,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002265 .enabledLayerCount = enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002266 .ppEnabledLayerNames =
2267 (const char *const *)((demo->validate) ? instance_validation_layers
2268 : NULL),
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002269 .enabledExtensionCount = enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002270 .ppEnabledExtensionNames = (const char *const *)extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002271 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002272
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002273 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002274
Chia-I Wu63636062015-10-26 21:10:41 +08002275 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002276 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2277 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002278 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002279 "additional information.\n",
2280 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002281 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002282 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002283 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002284 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002285 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002286 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2287 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002288 "the Getting Started guide for additional information.\n",
2289 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002290 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002291
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002292 /* Make initial call to query gpu_count, then second call for gpu info*/
2293 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2294 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002295
2296 if (gpu_count > 0) {
2297 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2298 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2299 assert(!err);
2300 /* For cube demo we just grab the first physical device */
2301 demo->gpu = physical_devices[0];
2302 free(physical_devices);
2303 } else {
2304 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2305 "Do you have a compatible Vulkan installable client driver (ICD) "
2306 "installed?\nPlease look at the Getting Started guide for "
2307 "additional information.\n",
2308 "vkEnumeratePhysicalDevices Failure");
2309 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002310
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002311 /* Look for validation layers */
2312 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002313 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002314 uint32_t device_layer_count = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002315 err =
2316 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002317 assert(!err);
2318
Dustin Graves7c287352016-01-27 13:48:06 -07002319 if (device_layer_count > 0) {
2320 VkLayerProperties *device_layers =
2321 malloc(sizeof(VkLayerProperties) * device_layer_count);
2322 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2323 device_layers);
2324 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002325
Dustin Graves7c287352016-01-27 13:48:06 -07002326 if (demo->validate) {
2327 validation_found = demo_check_layers(device_validation_layer_count,
2328 demo->device_validation_layers,
2329 device_layer_count,
2330 device_layers);
2331 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002332 }
Dustin Graves7c287352016-01-27 13:48:06 -07002333
2334 free(device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002335 }
2336
Dustin Graves7c287352016-01-27 13:48:06 -07002337 if (demo->validate && !validation_found) {
2338 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
2339 "a required validation layer.\n\n"
2340 "Please look at the Getting Started guide for additional "
2341 "information.\n",
2342 "vkCreateDevice Failure");
2343 }
2344
2345 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002346 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002347 VkBool32 swapchainExtFound = 0;
2348 demo->enabled_extension_count = 0;
2349 memset(extension_names, 0, sizeof(extension_names));
2350
Karl Schultze4dc75c2016-02-02 15:37:51 -07002351 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2352 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002353 assert(!err);
2354
Dustin Graves7c287352016-01-27 13:48:06 -07002355 if (device_extension_count > 0) {
2356 VkExtensionProperties *device_extensions =
2357 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2358 err = vkEnumerateDeviceExtensionProperties(
2359 demo->gpu, NULL, &device_extension_count, device_extensions);
2360 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002361
Dustin Graves7c287352016-01-27 13:48:06 -07002362 for (uint32_t i = 0; i < device_extension_count; i++) {
2363 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2364 device_extensions[i].extensionName)) {
2365 swapchainExtFound = 1;
2366 demo->extension_names[demo->enabled_extension_count++] =
2367 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2368 }
2369 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002370 }
Dustin Graves7c287352016-01-27 13:48:06 -07002371
2372 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002373 }
Dustin Graves7c287352016-01-27 13:48:06 -07002374
Tony Barbourcc69f452015-10-08 13:59:42 -06002375 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002376 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2377 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2378 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002379 "Vulkan installable client driver (ICD) installed?\nPlease "
2380 "look at the Getting Started guide for additional "
2381 "information.\n",
2382 "vkCreateInstance Failure");
2383 }
2384
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002385 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002386 demo->CreateDebugReportCallback =
2387 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2388 demo->inst, "vkCreateDebugReportCallbackEXT");
2389 demo->DestroyDebugReportCallback =
2390 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2391 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002392 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002393 ERR_EXIT(
2394 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2395 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002396 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002397 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002398 ERR_EXIT(
2399 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2400 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002401 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002402 demo->DebugReportMessage =
2403 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2404 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002405 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002406 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002407 "vkGetProcAddr Failure");
2408 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002409
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002410 PFN_vkDebugReportCallbackEXT callback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002411
2412 if (!demo->use_break) {
2413 callback = dbgFunc;
2414 } else {
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002415 callback = dbgFunc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002416 // TODO add a break callback defined locally since there is no
2417 // longer
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002418 // one included in the loader
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002419 }
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002420 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
2421 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002422 dbgCreateInfo.pNext = NULL;
2423 dbgCreateInfo.pfnCallback = callback;
2424 dbgCreateInfo.pUserData = NULL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002425 dbgCreateInfo.flags =
2426 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARN_BIT_EXT;
2427 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2428 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002429 switch (err) {
2430 case VK_SUCCESS:
2431 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002432 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002433 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2434 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002435 break;
2436 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002437 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2438 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002439 break;
2440 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002441 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002442 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002443
2444 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002445 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2446 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002447 assert(demo->queue_count >= 1);
2448
Karl Schultze4dc75c2016-02-02 15:37:51 -07002449 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2450 demo->queue_count * sizeof(VkQueueFamilyProperties));
2451 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2452 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002453 // Find a queue that supports gfx
2454 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002455 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2456 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002457 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2458 break;
2459 }
2460 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002461 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002462 // If app has specific feature requirements it should check supported
2463 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002464 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002465 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002466
Tony Barbourda2bad52016-01-22 14:36:40 -07002467 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2468 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2469 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2470 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2471 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2472}
2473
Karl Schultze4dc75c2016-02-02 15:37:51 -07002474static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002475 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002476 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002477 const VkDeviceQueueCreateInfo queue = {
2478 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2479 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002480 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002481 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002482 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002483
2484 VkDeviceCreateInfo device = {
2485 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2486 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002487 .queueCreateInfoCount = 1,
2488 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002489 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002490 .ppEnabledLayerNames =
2491 (const char *const *)((demo->validate)
2492 ? demo->device_validation_layers
2493 : NULL),
Tony Barbourda2bad52016-01-22 14:36:40 -07002494 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002495 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2496 .pEnabledFeatures =
2497 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002498 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002499
Chia-I Wu63636062015-10-26 21:10:41 +08002500 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002501 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002502}
2503
Karl Schultze4dc75c2016-02-02 15:37:51 -07002504static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002505 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002506 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002507
Karl Schultze4dc75c2016-02-02 15:37:51 -07002508// Create a WSI surface for the window:
Ian Elliottaaae5352015-07-06 14:27:58 -06002509#ifdef _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002510 VkWin32SurfaceCreateInfoKHR createInfo;
2511 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2512 createInfo.pNext = NULL;
2513 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002514 createInfo.hinstance = demo->connection;
2515 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002516
Karl Schultze4dc75c2016-02-02 15:37:51 -07002517 err =
2518 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002519
Ian Elliottaaae5352015-07-06 14:27:58 -06002520#else // _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002521 VkXcbSurfaceCreateInfoKHR createInfo;
2522 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2523 createInfo.pNext = NULL;
2524 createInfo.flags = 0;
2525 createInfo.connection = demo->connection;
2526 createInfo.window = demo->window;
2527
Karl Schultze4dc75c2016-02-02 15:37:51 -07002528 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottaaae5352015-07-06 14:27:58 -06002529#endif // _WIN32
2530
Tony Barbourcc69f452015-10-08 13:59:42 -06002531 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002532 VkBool32 *supportsPresent =
2533 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002534 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002535 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002536 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002537 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002538
2539 // Search for a graphics and a present queue in the array of queue
2540 // families, try to find one that supports both
2541 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002542 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002543 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002544 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2545 if (graphicsQueueNodeIndex == UINT32_MAX) {
2546 graphicsQueueNodeIndex = i;
2547 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002548
Ian Elliottaaae5352015-07-06 14:27:58 -06002549 if (supportsPresent[i] == VK_TRUE) {
2550 graphicsQueueNodeIndex = i;
2551 presentQueueNodeIndex = i;
2552 break;
2553 }
2554 }
2555 }
2556 if (presentQueueNodeIndex == UINT32_MAX) {
2557 // If didn't find a queue that supports both graphics and present, then
2558 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002559 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002560 if (supportsPresent[i] == VK_TRUE) {
2561 presentQueueNodeIndex = i;
2562 break;
2563 }
2564 }
2565 }
2566 free(supportsPresent);
2567
2568 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002569 if (graphicsQueueNodeIndex == UINT32_MAX ||
2570 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002571 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002572 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002573 }
2574
2575 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002576 // synchronization, and appropriate tracking for QueueSubmit.
2577 // NOTE: While it is possible for an application to use a separate graphics
2578 // and a present queues, this demo program assumes it is only using
2579 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002580 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2581 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002582 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002583 }
2584
2585 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002586
Tony Barbourda2bad52016-01-22 14:36:40 -07002587 demo_create_device(demo);
2588
2589 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2590 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2591 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2592 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2593 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2594
Karl Schultze4dc75c2016-02-02 15:37:51 -07002595 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2596 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002597
Ian Elliottaaae5352015-07-06 14:27:58 -06002598 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002599 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002600 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002601 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002602 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002603 VkSurfaceFormatKHR *surfFormats =
2604 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002605 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002606 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002607 assert(!err);
2608 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2609 // the surface has no preferred format. Otherwise, at least one
2610 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002611 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002612 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002613 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002614 assert(formatCount >= 1);
2615 demo->format = surfFormats[0].format;
2616 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002617 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002618
David Pinedo2bb7c932015-06-18 17:03:14 -06002619 demo->quit = false;
2620 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002621
2622 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002623 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002624}
2625
Karl Schultze4dc75c2016-02-02 15:37:51 -07002626static void demo_init_connection(struct demo *demo) {
Ian Elliott639ca472015-04-16 15:23:05 -06002627#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002628 const xcb_setup_t *setup;
2629 xcb_screen_iterator_t iter;
2630 int scr;
2631
2632 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002633 if (demo->connection == NULL) {
2634 printf("Cannot find a compatible Vulkan installable client driver "
2635 "(ICD).\nExiting ...\n");
2636 fflush(stdout);
2637 exit(1);
2638 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002639
2640 setup = xcb_get_setup(demo->connection);
2641 iter = xcb_setup_roots_iterator(setup);
2642 while (scr-- > 0)
2643 xcb_screen_next(&iter);
2644
2645 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002646#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002647}
2648
Karl Schultze4dc75c2016-02-02 15:37:51 -07002649static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002650 vec3 eye = {0.0f, 3.0f, 5.0f};
2651 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002652 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002653
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002654 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002655 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002656
Piers Daniell735ee532015-02-23 16:23:13 -07002657 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002658 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002659 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002660 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002661 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002662 if (strcmp(argv[i], "--break") == 0) {
2663 demo->use_break = true;
2664 continue;
2665 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002666 if (strcmp(argv[i], "--validate") == 0) {
2667 demo->validate = true;
2668 continue;
2669 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002670 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2671 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2672 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002673 i++;
2674 continue;
2675 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002676
Karl Schultze4dc75c2016-02-02 15:37:51 -07002677 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
2678 "[--c <framecount>]\n",
2679 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002680 fflush(stderr);
2681 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002682 }
2683
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002684 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002685 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002686
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002687 demo->width = 500;
2688 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002689
2690 demo->spin_angle = 0.01f;
2691 demo->spin_increment = 0.01f;
2692 demo->pause = false;
2693
Karl Schultze4dc75c2016-02-02 15:37:51 -07002694 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
2695 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002696 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2697 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002698}
2699
Ian Elliott639ca472015-04-16 15:23:05 -06002700#ifdef _WIN32
Mark Young418b9d12016-01-06 14:26:04 -07002701// Include header required for parsing the command line options.
2702#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002703
Karl Schultze4dc75c2016-02-02 15:37:51 -07002704int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
2705 int nCmdShow) {
2706 MSG msg; // message
2707 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002708 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002709 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06002710
Karl Schultze4dc75c2016-02-02 15:37:51 -07002711 // Use the CommandLine functions to get the command line arguments.
2712 // Unfortunately, Microsoft outputs
2713 // this information as wide characters for Unicode, and we simply want the
2714 // Ascii version to be compatible
2715 // with the non-Windows side. So, we have to convert the information to
2716 // Ascii character strings.
2717 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
2718 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07002719 argc = 0;
2720 }
2721
Karl Schultze4dc75c2016-02-02 15:37:51 -07002722 if (argc > 0) {
2723 argv = (char **)malloc(sizeof(char *) * argc);
2724 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002725 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002726 } else {
2727 for (int iii = 0; iii < argc; iii++) {
2728 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07002729 size_t numConverted = 0;
2730
Karl Schultze4dc75c2016-02-02 15:37:51 -07002731 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
2732 if (argv[iii] != NULL) {
2733 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
2734 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07002735 }
2736 }
2737 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002738 } else {
Mark Young418b9d12016-01-06 14:26:04 -07002739 argv = NULL;
2740 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002741
2742 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07002743
2744 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002745 if (argc > 0 && argv != NULL) {
2746 for (int iii = 0; iii < argc; iii++) {
2747 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002748 free(argv[iii]);
2749 }
2750 }
2751 free(argv);
2752 }
2753
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002754 demo.connection = hInstance;
2755 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002756 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002757 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002758
2759 demo_prepare(&demo);
2760
Karl Schultze4dc75c2016-02-02 15:37:51 -07002761 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07002762
2763 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07002764 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002765 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002766 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06002767 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002768 done = true; // if found, quit app
2769 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06002770 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07002771 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06002772 DispatchMessage(&msg);
2773 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002774 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002775 }
2776
2777 demo_cleanup(&demo);
2778
Karl Schultze4dc75c2016-02-02 15:37:51 -07002779 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002780}
2781#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002782int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002783 struct demo demo;
2784
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002785 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002786 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002787 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002788
2789 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002790 demo_run(&demo);
2791
2792 demo_cleanup(&demo);
2793
2794 return 0;
2795}
Ian Elliott639ca472015-04-16 15:23:05 -06002796#endif // _WIN32