blob: ec66133a8053571fe83eaadc9050f33eebea2ca0 [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>
Ian Elliottb08e0d92015-11-20 11:55:46 -070045
David Pinedo541526f2015-11-24 09:00:24 -070046#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047#include "linmath.h"
48
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060049#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbourfdc2d352015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliott07264132015-04-28 11:35:02 -060061#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -070062#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060067
Karl Schultze4dc75c2016-02-02 15:37:51 -070068#else // _WIN32
Ian Elliott07264132015-04-28 11:35:02 -060069
Karl Schultze4dc75c2016-02-02 15:37:51 -070070#define ERR_EXIT(err_msg, err_class) \
71 do { \
72 printf(err_msg); \
73 fflush(stdout); \
74 exit(1); \
75 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060076#endif // _WIN32
77
Karl Schultze4dc75c2016-02-02 15:37:51 -070078#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
79 { \
80 demo->fp##entrypoint = \
81 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
82 if (demo->fp##entrypoint == NULL) { \
83 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
84 "vkGetInstanceProcAddr Failure"); \
85 } \
86 }
Ian Elliottaaae5352015-07-06 14:27:58 -060087
Jon Ashburn7d8342c2015-10-08 15:58:23 -060088static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
89
Karl Schultze4dc75c2016-02-02 15:37:51 -070090#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
91 { \
92 if (!g_gdpa) \
93 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
94 demo->inst, "vkGetDeviceProcAddr"); \
95 demo->fp##entrypoint = \
96 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
97 if (demo->fp##entrypoint == NULL) { \
98 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
99 "vkGetDeviceProcAddr Failure"); \
100 } \
101 }
Ian Elliott673898b2015-06-22 15:07:49 -0600102
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600103/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700104 * structure to track all objects related to a texture.
105 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600106struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600107 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700108
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600109 VkImage image;
110 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600111
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800112 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500113 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600114 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700115 int32_t tex_width, tex_height;
116};
117
Karl Schultze4dc75c2016-02-02 15:37:51 -0700118static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600119
Karl Schultz0218c002016-03-22 17:06:13 -0600120static int validation_error = 0;
121
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600122struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600123 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700124 float mvp[4][4];
125 float position[12 * 3][4];
126 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127};
128
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600129struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600130 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700131 float mvp[4][4];
132 float position[12 * 3][4];
133 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600134};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600135
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600136//--------------------------------------------------------------------------------------
137// Mesh and VertexFormat Data
138//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700139// clang-format off
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600140struct Vertex
141{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600142 float posX, posY, posZ, posW; // Position data
143 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600144};
145
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146struct VertexPosTex
147{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600148 float posX, posY, posZ, posW; // Position data
149 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600150};
151
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600152#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600153#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600154
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600155static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800156 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 -1.0f,-1.0f, 1.0f,
158 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800159 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160 -1.0f, 1.0f,-1.0f,
161 -1.0f,-1.0f,-1.0f,
162
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 1.0f, 1.0f,-1.0f,
165 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800166 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600168 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600172 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800173 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600175 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 -1.0f, 1.0f, 1.0f,
179 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800180 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600182 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f, 1.0f, 1.0f,
186 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 1.0f,-1.0f,-1.0f,
189 1.0f, 1.0f,-1.0f,
190
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600193 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800194 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600195 1.0f,-1.0f, 1.0f,
196 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600197};
198
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600199static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800200 0.0f, 0.0f, // -X side
201 1.0f, 0.0f,
202 1.0f, 1.0f,
203 1.0f, 1.0f,
204 0.0f, 1.0f,
205 0.0f, 0.0f,
206
207 1.0f, 0.0f, // -Z side
208 0.0f, 1.0f,
209 0.0f, 0.0f,
210 1.0f, 0.0f,
211 1.0f, 1.0f,
212 0.0f, 1.0f,
213
214 1.0f, 1.0f, // -Y side
215 1.0f, 0.0f,
216 0.0f, 0.0f,
217 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600218 0.0f, 0.0f,
219 0.0f, 1.0f,
220
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800221 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600222 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600224 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600226 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800228 1.0f, 1.0f, // +X side
229 0.0f, 1.0f,
230 0.0f, 0.0f,
231 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600233 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600234
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 0.0f, 1.0f, // +Z side
236 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600237 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600238 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800239 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600240 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600241};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700242// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600243
Karl Schultze4dc75c2016-02-02 15:37:51 -0700244void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600245 int i;
246
247 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700248 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600249 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
250 }
251 printf("\n");
252 fflush(stdout);
253}
254
Karl Schultze4dc75c2016-02-02 15:37:51 -0700255void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600256 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700257 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600258 printf("\n");
259 fflush(stdout);
260}
261
Karl Schultze4dc75c2016-02-02 15:37:51 -0700262VKAPI_ATTR VkBool32 VKAPI_CALL
263dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
264 uint64_t srcObject, size_t location, int32_t msgCode,
265 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
266 char *message = (char *)malloc(strlen(pMsg) + 100);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600267
Karl Schultze4dc75c2016-02-02 15:37:51 -0700268 assert(message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600269
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700270 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700271 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
272 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600273 validation_error = 1;
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -0700274 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700275 // We know that we're submitting queues without fences, ignore this
276 // warning
277 if (strstr(pMsg,
278 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600279 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600280 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700281 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
282 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600283 validation_error = 1;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600284 } else {
Karl Schultz0218c002016-03-22 17:06:13 -0600285 validation_error = 1;
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600286 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600287 }
288
289#ifdef _WIN32
290 MessageBox(NULL, message, "Alert", MB_OK);
291#else
Karl Schultze4dc75c2016-02-02 15:37:51 -0700292 printf("%s\n", message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600293 fflush(stdout);
294#endif
295 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600296
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600297 /*
298 * false indicates that layer should not bail-out of an
299 * API call that had validation failures. This may mean that the
300 * app dies inside the driver due to invalid parameter(s).
301 * That's what would happen without validation layers, so we'll
302 * keep that behavior here.
303 */
304 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600305}
306
Karl Schultz0c3c1512016-03-25 15:35:18 -0600307VKAPI_ATTR VkBool32 VKAPI_CALL
308BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
309 uint64_t srcObject, size_t location, int32_t msgCode,
310 const char *pLayerPrefix, const char *pMsg,
311 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700312#ifndef WIN32
313 raise(SIGTRAP);
314#else
315 DebugBreak();
316#endif
317
318 return false;
319}
320
Ian Elliotta0781972015-08-21 15:09:33 -0600321typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600322 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800323 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600324 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600325} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600326
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600328#ifdef _WIN32
329#define APP_NAME_STR_LEN 80
330 HINSTANCE connection; // hInstance - Windows Instance
331 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700332 HWND window; // hWnd - window handle
333#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334 xcb_connection_t *connection;
335 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800336 xcb_window_t window;
337 xcb_intern_atom_reply_t *atom_wm_delete_window;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700338#endif // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -0700339 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600340 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700341 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600342
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600343 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600344 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600345 VkDevice device;
346 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700347 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600348 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600349 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600350 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
Tony Barbourda2bad52016-01-22 14:36:40 -0700352 uint32_t enabled_extension_count;
353 uint32_t enabled_layer_count;
354 char *extension_names[64];
355 char *device_validation_layers[64];
356
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600357 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600359 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360
Karl Schultze4dc75c2016-02-02 15:37:51 -0700361 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
362 fpGetPhysicalDeviceSurfaceSupportKHR;
363 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
364 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
365 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
366 fpGetPhysicalDeviceSurfaceFormatsKHR;
367 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
368 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600369 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
370 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
371 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
372 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
373 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600374 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600375 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600376 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600377
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800378 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379
380 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600381 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600382
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600383 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800384 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500385 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600386 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600387 } depth;
388
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600389 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390
391 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600392 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800393 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500394 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600395 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600396 } uniform_data;
397
Karl Schultze4dc75c2016-02-02 15:37:51 -0700398 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500399 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600400 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600401 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800402 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600403 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600405 mat4x4 projection_matrix;
406 mat4x4 view_matrix;
407 mat4x4 model_matrix;
408
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600409 float spin_angle;
410 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600411 bool pause;
412
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600413 VkShaderModule vert_shader_module;
414 VkShaderModule frag_shader_module;
415
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600416 VkDescriptorPool desc_pool;
417 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800418
Tony Barbourd8e504f2015-10-08 14:26:24 -0600419 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800420
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600421 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600422 int32_t curFrame;
423 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600424 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600425 bool use_break;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700426 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
427 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
428 VkDebugReportCallbackEXT msg_callback;
429 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600430
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600431 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600432 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600433};
434
Ian Elliottcf074d32015-10-16 18:02:43 -0600435// Forward declaration:
436static void demo_resize(struct demo *demo);
437
Karl Schultze4dc75c2016-02-02 15:37:51 -0700438static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
439 VkFlags requirements_mask,
440 uint32_t *typeIndex) {
441 // Search memtypes to find first index with those properties
442 for (uint32_t i = 0; i < 32; i++) {
443 if ((typeBits & 1) == 1) {
444 // Type is available, does it match user properties?
445 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
446 requirements_mask) == requirements_mask) {
447 *typeIndex = i;
448 return true;
449 }
450 }
451 typeBits >>= 1;
452 }
453 // No memory types matched, return failure
454 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600455}
456
Karl Schultze4dc75c2016-02-02 15:37:51 -0700457static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600458 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600459
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600460 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461 return;
462
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600463 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600464 assert(!err);
465
Karl Schultze4dc75c2016-02-02 15:37:51 -0700466 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800467 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700468 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
469 .pNext = NULL,
470 .waitSemaphoreCount = 0,
471 .pWaitSemaphores = NULL,
472 .pWaitDstStageMask = NULL,
473 .commandBufferCount = 1,
474 .pCommandBuffers = cmd_bufs,
475 .signalSemaphoreCount = 0,
476 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600477
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600478 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600479 assert(!err);
480
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600481 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600482 assert(!err);
483
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600484 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600485 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600486}
487
Karl Schultze4dc75c2016-02-02 15:37:51 -0700488static void demo_set_image_layout(struct demo *demo, VkImage image,
489 VkImageAspectFlags aspectMask,
490 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700491 VkImageLayout new_image_layout,
492 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600493 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600494
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600495 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800496 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800497 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800499 .commandPool = demo->cmd_pool,
500 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700501 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502 };
503
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800504 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600505 assert(!err);
506
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700507 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
508 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600509 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800510 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600511 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800512 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800513 .occlusionQueryEnable = VK_FALSE,
514 .queryFlags = 0,
515 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600516 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700517 VkCommandBufferBeginInfo cmd_buf_info = {
518 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
519 .pNext = NULL,
520 .flags = 0,
521 .pInheritanceInfo = &cmd_buf_hinfo,
522 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600523 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600524 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600525 }
526
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600527 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600528 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700530 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800531 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600532 .oldLayout = old_image_layout,
533 .newLayout = new_image_layout,
534 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700535 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600536
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800537 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600538 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800539 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600540 }
541
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700542 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700543 image_memory_barrier.dstAccessMask =
544 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700545 }
546
547 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700548 image_memory_barrier.dstAccessMask =
549 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700550 }
551
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600552 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700554 image_memory_barrier.dstAccessMask =
555 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600556 }
557
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600558 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600559
Tony Barbour50bbca42015-06-29 16:20:35 -0600560 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
561 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600562
Karl Schultze4dc75c2016-02-02 15:37:51 -0700563 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
564 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600565}
566
Karl Schultze4dc75c2016-02-02 15:37:51 -0700567static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700568 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
569 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600570 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800571 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600572 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800573 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800574 .occlusionQueryEnable = VK_FALSE,
575 .queryFlags = 0,
576 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700577 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700578 const VkCommandBufferBeginInfo cmd_buf_info = {
579 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
580 .pNext = NULL,
581 .flags = 0,
582 .pInheritanceInfo = &cmd_buf_hinfo,
583 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800584 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700585 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
586 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800587 };
588 const VkRenderPassBeginInfo rp_begin = {
589 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
590 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800591 .renderPass = demo->render_pass,
592 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800593 .renderArea.offset.x = 0,
594 .renderArea.offset.y = 0,
595 .renderArea.extent.width = demo->width,
596 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600597 .clearValueCount = 2,
598 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700599 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800600 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600601
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600602 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600603 assert(!err);
604
Chia-I Wuf051d262015-10-27 19:25:11 +0800605 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800606
Karl Schultze4dc75c2016-02-02 15:37:51 -0700607 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
608 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
609 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
610 NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600611
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600612 VkViewport viewport;
613 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700614 viewport.height = (float)demo->height;
615 viewport.width = (float)demo->width;
616 viewport.minDepth = (float)0.0f;
617 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700618 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600619
620 VkRect2D scissor;
621 memset(&scissor, 0, sizeof(scissor));
622 scissor.extent.width = demo->width;
623 scissor.extent.height = demo->height;
624 scissor.offset.x = 0;
625 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700626 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600627
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600628 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800629 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600630
Tony Barbour15a4ef62015-10-21 10:14:48 -0600631 VkImageMemoryBarrier prePresentBarrier = {
632 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
633 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800634 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700635 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600636 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700637 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600638 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800639 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700640 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600641
642 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
643 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700644 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
645 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
646 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600647
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600648 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600649 assert(!err);
650}
651
Karl Schultze4dc75c2016-02-02 15:37:51 -0700652void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600653 mat4x4 MVP, Model, VP;
654 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600655 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600656 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600658 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600660 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600661 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700662 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
663 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600664 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600665
Karl Schultze4dc75c2016-02-02 15:37:51 -0700666 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
667 demo->uniform_data.mem_alloc.allocationSize, 0,
668 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600669 assert(!err);
670
Karl Schultze4dc75c2016-02-02 15:37:51 -0700671 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600672
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600673 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674}
675
Karl Schultze4dc75c2016-02-02 15:37:51 -0700676static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600677 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600678 VkSemaphore presentCompleteSemaphore;
679 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
680 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
681 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600682 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600683 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800684 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600685
Karl Schultze4dc75c2016-02-02 15:37:51 -0700686 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
687 NULL, &presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600688 assert(!err);
689
690 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700691 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Ian Elliottaaae5352015-07-06 14:27:58 -0600692 presentCompleteSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700693 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600694 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600695 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
696 // demo->swapchain is out of date (e.g. the window was resized) and
697 // must be recreated:
698 demo_resize(demo);
699 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800700 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600701 return;
702 } else if (err == VK_SUBOPTIMAL_KHR) {
703 // demo->swapchain is not as optimal as it could be, but the platform's
704 // presentation engine will still present the image correctly.
705 } else {
706 assert(!err);
707 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600708
Tony Barbour15a4ef62015-10-21 10:14:48 -0600709 // Assume the command buffer has been run on current_buffer before so
710 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
711 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700712 VK_IMAGE_ASPECT_COLOR_BIT,
713 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700714 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
715 0);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600716 demo_flush_init_cmd(demo);
717
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600718 // Wait for the present complete semaphore to be signaled to ensure
719 // that the image won't be rendered to until the presentation
720 // engine has fully released ownership to the application, and it is
721 // okay to render to the image.
722
Karl Schultze4dc75c2016-02-02 15:37:51 -0700723 // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
724 VkPipelineStageFlags pipe_stage_flags =
725 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
726 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
727 .pNext = NULL,
728 .waitSemaphoreCount = 1,
729 .pWaitSemaphores = &presentCompleteSemaphore,
730 .pWaitDstStageMask = &pipe_stage_flags,
731 .commandBufferCount = 1,
732 .pCommandBuffers =
733 &demo->buffers[demo->current_buffer].cmd,
734 .signalSemaphoreCount = 0,
735 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600736
737 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600738 assert(!err);
739
Ian Elliotta0781972015-08-21 15:09:33 -0600740 VkPresentInfoKHR present = {
741 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600742 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600743 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700744 .pSwapchains = &demo->swapchain,
745 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600746 };
747
Karl Schultze4dc75c2016-02-02 15:37:51 -0700748 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600749 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600750 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
751 // demo->swapchain is out of date (e.g. the window was resized) and
752 // must be recreated:
753 demo_resize(demo);
754 } else if (err == VK_SUBOPTIMAL_KHR) {
755 // demo->swapchain is not as optimal as it could be, but the platform's
756 // presentation engine will still present the image correctly.
757 } else {
758 assert(!err);
759 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600760
Chia-I Wucbb564e2015-04-16 22:02:10 +0800761 err = vkQueueWaitIdle(demo->queue);
762 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600763
Chia-I Wu63636062015-10-26 21:10:41 +0800764 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600765}
766
Karl Schultze4dc75c2016-02-02 15:37:51 -0700767static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600768 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600769 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600770
Ian Elliottb08e0d92015-11-20 11:55:46 -0700771 // Check the surface capabilities and formats
772 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700773 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
774 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600775 assert(!err);
776
Ian Elliott8528eff2015-08-07 15:56:59 -0600777 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700778 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
779 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600780 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600781 VkPresentModeKHR *presentModes =
782 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600783 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700784 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
785 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600786 assert(!err);
787
Ian Elliotta0781972015-08-21 15:09:33 -0600788 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600789 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700790 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600791 // If the surface size is undefined, the size is set to
792 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600793 swapchainExtent.width = demo->width;
794 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700795 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600796 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700797 swapchainExtent = surfCapabilities.currentExtent;
798 demo->width = surfCapabilities.currentExtent.width;
799 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600800 }
801
802 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600803 // tearing mode. If not, try IMMEDIATE which will usually be available,
804 // and is fastest (though it tears). If not, fall back to FIFO which is
805 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600806 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600807 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600808 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
809 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600810 break;
811 }
Ian Elliotta0781972015-08-21 15:09:33 -0600812 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
813 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
814 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600815 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600816 }
817
818 // Determine the number of VkImage's to use in the swap chain (we desire to
819 // own only 1 image at a time, besides the images being displayed and
820 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700821 uint32_t desiredNumberOfSwapchainImages =
822 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700823 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700824 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600825 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700826 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600827 }
828
Tony Barbour9fd6d352015-09-16 15:01:32 -0600829 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700830 if (surfCapabilities.supportedTransforms &
831 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700832 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600833 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700834 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600835 }
836
Ian Elliottcf074d32015-10-16 18:02:43 -0600837 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600838 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800839 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700840 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600841 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800842 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600843 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700844 .imageExtent =
845 {
846 .width = swapchainExtent.width, .height = swapchainExtent.height,
847 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700848 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600849 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700850 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700851 .imageArrayLayers = 1,
852 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
853 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600854 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600855 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600856 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600857 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600858 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600859 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600860
Karl Schultze4dc75c2016-02-02 15:37:51 -0700861 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
862 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800863 assert(!err);
864
Ian Elliottcf074d32015-10-16 18:02:43 -0600865 // If we just re-created an existing swapchain, we should destroy the old
866 // swapchain at this point.
867 // Note: destroying the swapchain also cleans up all its associated
868 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800869 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700870 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600871 }
872
873 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600874 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600875 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800876
Karl Schultze4dc75c2016-02-02 15:37:51 -0700877 VkImage *swapchainImages =
878 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600879 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600880 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600881 &demo->swapchainImageCount,
882 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600883 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600884
Karl Schultze4dc75c2016-02-02 15:37:51 -0700885 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
886 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600887 assert(demo->buffers);
888
Ian Elliotta0781972015-08-21 15:09:33 -0600889 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600890 VkImageViewCreateInfo color_image_view = {
891 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600892 .pNext = NULL,
893 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700894 .components =
895 {
896 .r = VK_COMPONENT_SWIZZLE_R,
897 .g = VK_COMPONENT_SWIZZLE_G,
898 .b = VK_COMPONENT_SWIZZLE_B,
899 .a = VK_COMPONENT_SWIZZLE_A,
900 },
901 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
902 .baseMipLevel = 0,
903 .levelCount = 1,
904 .baseArrayLayer = 0,
905 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600906 .viewType = VK_IMAGE_VIEW_TYPE_2D,
907 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600908 };
909
Ian Elliotta0781972015-08-21 15:09:33 -0600910 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600911
Karl Schultze4dc75c2016-02-02 15:37:51 -0700912 // Render loop will expect image to have been used before and in
913 // VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
914 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image
915 // to that state
916 demo_set_image_layout(
917 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700918 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
919 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600920
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600921 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600922
Karl Schultze4dc75c2016-02-02 15:37:51 -0700923 err = vkCreateImageView(demo->device, &color_image_view, NULL,
924 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600925 assert(!err);
926 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700927
Mark Young04fd3d82016-01-25 14:43:50 -0700928 if (NULL != presentModes) {
929 free(presentModes);
930 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600931}
932
Karl Schultze4dc75c2016-02-02 15:37:51 -0700933static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600934 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600935 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600936 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600937 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600938 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600939 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700940 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600941 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600942 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800943 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600944 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600945 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600946 .flags = 0,
947 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200948
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600949 VkImageViewCreateInfo view = {
950 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600951 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800952 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600953 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700954 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
955 .baseMipLevel = 0,
956 .levelCount = 1,
957 .baseArrayLayer = 0,
958 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600959 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600960 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600961 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600962
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500963 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600964 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600965 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600966
967 demo->depth.format = depth_format;
968
969 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700970 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600971 assert(!err);
972
Karl Schultze4dc75c2016-02-02 15:37:51 -0700973 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600974 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600975
Chia-I Wuf48700b2015-11-10 16:21:09 +0800976 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200977 demo->depth.mem_alloc.pNext = NULL;
978 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
979 demo->depth.mem_alloc.memoryTypeIndex = 0;
980
Karl Schultze4dc75c2016-02-02 15:37:51 -0700981 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
982 0, /* No requirements */
983 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600984 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600985
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500986 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700987 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
988 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500989 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600990
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500991 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700992 err =
993 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500994 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600995
Karl Schultze4dc75c2016-02-02 15:37:51 -0700996 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
997 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700998 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
999 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001000
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001001 /* create image view */
1002 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001003 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001005}
1006
Tony Barbour1a86d032015-09-21 15:17:33 -06001007/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001008bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001009 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
1010 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001011 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001012
Tony Barbour1a86d032015-09-21 15:17:33 -06001013 if (!fPtr)
1014 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001015
Tony Barbour1a86d032015-09-21 15:17:33 -06001016 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001017 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1018 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001019 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001020 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001021
Tony Barbour1a86d032015-09-21 15:17:33 -06001022 do {
1023 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001024 if (cPtr == NULL) {
1025 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001026 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001027 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001028 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001029
Tony Barbour1a86d032015-09-21 15:17:33 -06001030 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001031 if (rgba_data == NULL) {
1032 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001033 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001034 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001035 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001036 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001037 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1038 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001039 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001040 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001041
Karl Schultze4dc75c2016-02-02 15:37:51 -07001042 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001043 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001044 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001045 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001046 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001047 rowPtr[3] = 255; /* Alpha of 1 */
1048 rowPtr += 4;
1049 }
1050 rgba_data += layout->rowPitch;
1051 }
1052 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001053 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001054}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001055
Karl Schultze4dc75c2016-02-02 15:37:51 -07001056static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001057 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001058 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001059 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001060 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001061 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001062 int32_t tex_width;
1063 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001064 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001065 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066
Karl Schultze4dc75c2016-02-02 15:37:51 -07001067 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
David Pinedo30bd71d2015-04-23 08:16:57 -06001068 printf("Failed to load textures\n");
1069 fflush(stdout);
1070 exit(1);
1071 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001072
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001073 tex_obj->tex_width = tex_width;
1074 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001075
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001076 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001077 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001079 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001081 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001083 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001084 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001086 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001087 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001088 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001089 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001090
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001091 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001092
Karl Schultze4dc75c2016-02-02 15:37:51 -07001093 err =
1094 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001095 assert(!err);
1096
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001097 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001098
Chia-I Wuf48700b2015-11-10 16:21:09 +08001099 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001100 tex_obj->mem_alloc.pNext = NULL;
1101 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1102 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001103
Karl Schultze4dc75c2016-02-02 15:37:51 -07001104 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1105 required_props,
1106 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001107 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001108
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001109 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001110 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001111 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001112 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001113
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001114 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001115 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001116 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001118 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001119 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001120 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001121 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001122 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001123 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001124 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125 void *data;
1126
Karl Schultze4dc75c2016-02-02 15:37:51 -07001127 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1128 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001129
Karl Schultze4dc75c2016-02-02 15:37:51 -07001130 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1131 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001132 assert(!err);
1133
1134 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1135 fprintf(stderr, "Error loading texture: %s\n", filename);
1136 }
1137
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001138 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001139 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001140
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001141 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001142 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001143 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1144 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001145 /* setting the image layout does not reference the actual memory so no need
1146 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001147}
1148
Karl Schultze4dc75c2016-02-02 15:37:51 -07001149static void demo_destroy_texture_image(struct demo *demo,
1150 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001151 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001152 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1153 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001154}
1155
Karl Schultze4dc75c2016-02-02 15:37:51 -07001156static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001157 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001158 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001159 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001160
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001161 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001163 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001164 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001165
Karl Schultze4dc75c2016-02-02 15:37:51 -07001166 if ((props.linearTilingFeatures &
1167 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1168 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001169 /* Device can texture using linear textures */
1170 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Karl Schultze4dc75c2016-02-02 15:37:51 -07001171 VK_IMAGE_TILING_LINEAR,
1172 VK_IMAGE_USAGE_SAMPLED_BIT,
1173 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
1174 } else if (props.optimalTilingFeatures &
1175 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001176 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001177 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001178
1179 memset(&staging_texture, 0, sizeof(staging_texture));
1180 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001181 VK_IMAGE_TILING_LINEAR,
1182 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1183 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001184
Karl Schultze4dc75c2016-02-02 15:37:51 -07001185 demo_prepare_texture_image(
1186 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1187 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1188 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001189
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001190 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001191 VK_IMAGE_ASPECT_COLOR_BIT,
1192 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001193 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1194 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001196 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001197 VK_IMAGE_ASPECT_COLOR_BIT,
1198 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001199 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1200 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001201
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001202 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001203 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1204 .srcOffset = {0, 0, 0},
1205 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1206 .dstOffset = {0, 0, 0},
1207 .extent = {staging_texture.tex_width,
1208 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001209 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001210 vkCmdCopyImage(
1211 demo->cmd, staging_texture.image,
1212 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1213 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001214
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001215 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001216 VK_IMAGE_ASPECT_COLOR_BIT,
1217 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001218 demo->textures[i].imageLayout,
1219 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001220
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001221 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001222
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001223 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001224 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001225 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1226 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001227 }
1228
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001229 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001230 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001231 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001232 .magFilter = VK_FILTER_NEAREST,
1233 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001234 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001235 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1236 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1237 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001239 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001240 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001241 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001242 .minLod = 0.0f,
1243 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001244 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001245 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001246 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001247
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001248 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001249 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001250 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001251 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001252 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001253 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001254 .components =
1255 {
1256 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1257 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1258 },
1259 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001260 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001261 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001262
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001263 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001264 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001265 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001266 assert(!err);
1267
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001268 /* create image view */
1269 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001270 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001271 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001272 assert(!err);
1273 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001274}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001275
Karl Schultze4dc75c2016-02-02 15:37:51 -07001276void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001277 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001278 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001279 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001280 int i;
1281 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001282 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001283 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001284 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001285
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001286 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001287 mat4x4_mul(MVP, VP, demo->model_matrix);
1288 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001289 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001290
Karl Schultze4dc75c2016-02-02 15:37:51 -07001291 for (i = 0; i < 12 * 3; i++) {
1292 data.position[i][0] = g_vertex_buffer_data[i * 3];
1293 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1294 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001295 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001296 data.attr[i][0] = g_uv_buffer_data[2 * i];
1297 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001298 data.attr[i][2] = 0;
1299 data.attr[i][3] = 0;
1300 }
1301
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001302 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001303 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001304 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001305 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001306 err =
1307 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001308 assert(!err);
1309
Karl Schultze4dc75c2016-02-02 15:37:51 -07001310 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1311 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001312
Chia-I Wuf48700b2015-11-10 16:21:09 +08001313 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001314 demo->uniform_data.mem_alloc.pNext = NULL;
1315 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1316 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1317
Karl Schultze4dc75c2016-02-02 15:37:51 -07001318 pass = memory_type_from_properties(
1319 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1320 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001321 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001322
Karl Schultze4dc75c2016-02-02 15:37:51 -07001323 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1324 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001325 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001326
Karl Schultze4dc75c2016-02-02 15:37:51 -07001327 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1328 demo->uniform_data.mem_alloc.allocationSize, 0,
1329 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001330 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001331
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001332 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001333
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001334 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001335
Karl Schultze4dc75c2016-02-02 15:37:51 -07001336 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1337 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001338 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001339
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001340 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1341 demo->uniform_data.buffer_info.offset = 0;
1342 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001343}
1344
Karl Schultze4dc75c2016-02-02 15:37:51 -07001345static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001346 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001347 [0] =
1348 {
1349 .binding = 0,
1350 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1351 .descriptorCount = 1,
1352 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1353 .pImmutableSamplers = NULL,
1354 },
1355 [1] =
1356 {
1357 .binding = 1,
1358 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1359 .descriptorCount = DEMO_TEXTURE_COUNT,
1360 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1361 .pImmutableSamplers = NULL,
1362 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001363 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001364 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001365 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001366 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001367 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001368 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001369 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001370 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371
Karl Schultze4dc75c2016-02-02 15:37:51 -07001372 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1373 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001374 assert(!err);
1375
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001376 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001377 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1378 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001379 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001380 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001381 };
1382
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001384 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001385 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001386}
1387
Karl Schultze4dc75c2016-02-02 15:37:51 -07001388static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001389 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001390 [0] =
1391 {
1392 .format = demo->format,
1393 .samples = VK_SAMPLE_COUNT_1_BIT,
1394 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1395 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1396 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1397 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1398 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1399 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1400 },
1401 [1] =
1402 {
1403 .format = demo->depth.format,
1404 .samples = VK_SAMPLE_COUNT_1_BIT,
1405 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1406 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1407 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1408 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1409 .initialLayout =
1410 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1411 .finalLayout =
1412 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1413 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001414 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001415 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001416 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001417 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001418 const VkAttachmentReference depth_reference = {
1419 .attachment = 1,
1420 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1421 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001422 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001423 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1424 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001425 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001426 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001427 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001428 .pColorAttachments = &color_reference,
1429 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001430 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001431 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001432 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001433 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001434 const VkRenderPassCreateInfo rp_info = {
1435 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1436 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001437 .attachmentCount = 2,
1438 .pAttachments = attachments,
1439 .subpassCount = 1,
1440 .pSubpasses = &subpass,
1441 .dependencyCount = 0,
1442 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001443 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001444 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001445
Chia-I Wu63636062015-10-26 21:10:41 +08001446 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001447 assert(!err);
1448}
1449
Karl Schultze4dc75c2016-02-02 15:37:51 -07001450static VkShaderModule
1451demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001452 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001453 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001454 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001455
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001456 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1457 moduleCreateInfo.pNext = NULL;
1458
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001459 moduleCreateInfo.codeSize = size;
1460 moduleCreateInfo.pCode = code;
1461 moduleCreateInfo.flags = 0;
1462 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1463 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001464
1465 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001466}
1467
Karl Schultze4dc75c2016-02-02 15:37:51 -07001468char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001469 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001470 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001471 void *shader_code;
1472
1473 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001474 if (!fp)
1475 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001476
1477 fseek(fp, 0L, SEEK_END);
1478 size = ftell(fp);
1479
1480 fseek(fp, 0L, SEEK_SET);
1481
1482 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001483 retval = fread(shader_code, size, 1, fp);
1484 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001485
1486 *psize = size;
1487
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001488 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001489 return shader_code;
1490}
1491
Karl Schultze4dc75c2016-02-02 15:37:51 -07001492static VkShaderModule demo_prepare_vs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001493 void *vertShaderCode;
1494 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001495
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001496 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1497
Karl Schultze4dc75c2016-02-02 15:37:51 -07001498 demo->vert_shader_module =
1499 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001500
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001501 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001502
1503 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001504}
1505
Karl Schultze4dc75c2016-02-02 15:37:51 -07001506static VkShaderModule demo_prepare_fs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001507 void *fragShaderCode;
1508 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001509
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001510 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001511
Karl Schultze4dc75c2016-02-02 15:37:51 -07001512 demo->frag_shader_module =
1513 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001514
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001515 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001516
1517 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001518}
1519
Karl Schultze4dc75c2016-02-02 15:37:51 -07001520static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001521 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001522 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001523 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001524 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001525 VkPipelineRasterizationStateCreateInfo rs;
1526 VkPipelineColorBlendStateCreateInfo cb;
1527 VkPipelineDepthStencilStateCreateInfo ds;
1528 VkPipelineViewportStateCreateInfo vp;
1529 VkPipelineMultisampleStateCreateInfo ms;
1530 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1531 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001532 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001533
Piers Daniellba839d12015-09-29 13:01:09 -06001534 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1535 memset(&dynamicState, 0, sizeof dynamicState);
1536 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1537 dynamicState.pDynamicStates = dynamicStateEnables;
1538
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001539 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001540 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001541 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001542
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001543 memset(&vi, 0, sizeof(vi));
1544 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1545
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001546 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001547 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001548 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001549
1550 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001551 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1552 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001553 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001554 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001555 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001556 rs.rasterizerDiscardEnable = VK_FALSE;
1557 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001558
1559 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001560 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1561 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001562 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001563 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001564 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001565 cb.attachmentCount = 1;
1566 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567
Tony Barbour29645d02015-01-16 14:27:35 -07001568 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001569 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001570 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001571 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1572 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001573 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001574 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1575 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001576
1577 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001578 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001579 ds.depthTestEnable = VK_TRUE;
1580 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001581 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001582 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001583 ds.back.failOp = VK_STENCIL_OP_KEEP;
1584 ds.back.passOp = VK_STENCIL_OP_KEEP;
1585 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001586 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001587 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001588
Tony Barbour29645d02015-01-16 14:27:35 -07001589 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001590 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001591 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001592 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001593
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001594 // Two stages: vs and fs
1595 pipeline.stageCount = 2;
1596 VkPipelineShaderStageCreateInfo shaderStages[2];
1597 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1598
Karl Schultze4dc75c2016-02-02 15:37:51 -07001599 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1600 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001601 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001602 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001603
Karl Schultze4dc75c2016-02-02 15:37:51 -07001604 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1605 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001606 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001607 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001608
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001609 memset(&pipelineCache, 0, sizeof(pipelineCache));
1610 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001611
Karl Schultze4dc75c2016-02-02 15:37:51 -07001612 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1613 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001614 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001615
Karl Schultze4dc75c2016-02-02 15:37:51 -07001616 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001617 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001618 pipeline.pRasterizationState = &rs;
1619 pipeline.pColorBlendState = &cb;
1620 pipeline.pMultisampleState = &ms;
1621 pipeline.pViewportState = &vp;
1622 pipeline.pDepthStencilState = &ds;
1623 pipeline.pStages = shaderStages;
1624 pipeline.renderPass = demo->render_pass;
1625 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001626
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001627 pipeline.renderPass = demo->render_pass;
1628
Karl Schultze4dc75c2016-02-02 15:37:51 -07001629 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1630 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631 assert(!err);
1632
Chia-I Wu63636062015-10-26 21:10:41 +08001633 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1634 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001635}
1636
Karl Schultze4dc75c2016-02-02 15:37:51 -07001637static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001638 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001639 [0] =
1640 {
1641 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1642 .descriptorCount = 1,
1643 },
1644 [1] =
1645 {
1646 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1647 .descriptorCount = DEMO_TEXTURE_COUNT,
1648 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001649 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001650 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001651 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001652 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001653 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001654 .poolSizeCount = 2,
1655 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001656 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001657 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001658
Karl Schultze4dc75c2016-02-02 15:37:51 -07001659 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1660 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001661 assert(!err);
1662}
1663
Karl Schultze4dc75c2016-02-02 15:37:51 -07001664static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001665 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001666 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001667 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001668 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001669
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001670 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001671 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001672 .pNext = NULL,
1673 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001674 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001675 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001676 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001677 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001678
Chia-I Wuae721ba2015-05-25 16:27:55 +08001679 memset(&tex_descs, 0, sizeof(tex_descs));
1680 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001681 tex_descs[i].sampler = demo->textures[i].sampler;
1682 tex_descs[i].imageView = demo->textures[i].view;
1683 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001684 }
1685
1686 memset(&writes, 0, sizeof(writes));
1687
1688 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001689 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001690 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001691 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001692 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001693
1694 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001695 writes[1].dstSet = demo->desc_set;
1696 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001697 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001698 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001699 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001700
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001701 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001702}
1703
Karl Schultze4dc75c2016-02-02 15:37:51 -07001704static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001705 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001706 attachments[1] = demo->depth.view;
1707
Chia-I Wuf973e322015-07-08 13:34:24 +08001708 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001709 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1710 .pNext = NULL,
1711 .renderPass = demo->render_pass,
1712 .attachmentCount = 2,
1713 .pAttachments = attachments,
1714 .width = demo->width,
1715 .height = demo->height,
1716 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001717 };
1718 VkResult U_ASSERT_ONLY err;
1719 uint32_t i;
1720
Karl Schultze4dc75c2016-02-02 15:37:51 -07001721 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1722 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001723 assert(demo->framebuffers);
1724
1725 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001726 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001727 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1728 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001729 assert(!err);
1730 }
1731}
1732
Karl Schultze4dc75c2016-02-02 15:37:51 -07001733static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001734 VkResult U_ASSERT_ONLY err;
1735
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001736 const VkCommandPoolCreateInfo cmd_pool_info = {
1737 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001738 .pNext = NULL,
1739 .queueFamilyIndex = demo->graphics_queue_node_index,
1740 .flags = 0,
1741 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001742 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1743 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001744 assert(!err);
1745
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001746 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001747 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001748 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001749 .commandPool = demo->cmd_pool,
1750 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001751 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753
1754 demo_prepare_buffers(demo);
1755 demo_prepare_depth(demo);
1756 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001757 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001759 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001760 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001761 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001762
Ian Elliotta0781972015-08-21 15:09:33 -06001763 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001764 err =
1765 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001766 assert(!err);
1767 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001768
Chia-I Wu63ea9262015-03-26 13:14:16 +08001769 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001770 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001771
Chia-I Wuf973e322015-07-08 13:34:24 +08001772 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001773
Ian Elliotta0781972015-08-21 15:09:33 -06001774 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001775 demo->current_buffer = i;
1776 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1777 }
1778
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001779 /*
1780 * Prepare functions above may generate pipeline commands
1781 * that need to be flushed before beginning the render loop.
1782 */
1783 demo_flush_init_cmd(demo);
1784
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001785 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001786 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001787}
1788
Karl Schultze4dc75c2016-02-02 15:37:51 -07001789static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001790 uint32_t i;
1791
1792 demo->prepared = false;
1793
Tony Barbourd8e504f2015-10-08 14:26:24 -06001794 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001795 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001796 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001797 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001798 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001799
Chia-I Wu63636062015-10-26 21:10:41 +08001800 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1801 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1802 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1803 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1804 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001805
1806 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001807 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1808 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1809 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1810 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001811 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001812 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001813
Chia-I Wu63636062015-10-26 21:10:41 +08001814 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1815 vkDestroyImage(demo->device, demo->depth.image, NULL);
1816 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001817
Chia-I Wu63636062015-10-26 21:10:41 +08001818 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1819 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001820
Ian Elliotta0781972015-08-21 15:09:33 -06001821 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001822 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001823 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1824 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001825 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001826 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001827
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001828 free(demo->queue_props);
1829
Chia-I Wu63636062015-10-26 21:10:41 +08001830 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1831 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001832 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001833 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001834 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001835 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001836 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001837
1838#ifndef _WIN32
1839 xcb_destroy_window(demo->connection, demo->window);
1840 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001841 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001842#endif // _WIN32
1843}
1844
Karl Schultze4dc75c2016-02-02 15:37:51 -07001845static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001846 uint32_t i;
1847
Mike Stroyanbb60b382015-10-28 09:46:57 -06001848 // Don't react to resize until after first initialization.
1849 if (!demo->prepared) {
1850 return;
1851 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001852 // In order to properly resize the window, we must re-create the swapchain
1853 // AND redo the command buffers, etc.
1854 //
1855 // First, perform part of the demo_cleanup() function:
1856 demo->prepared = false;
1857
1858 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001859 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001860 }
1861 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001862 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001863
Chia-I Wu63636062015-10-26 21:10:41 +08001864 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1865 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1866 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1867 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1868 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001869
1870 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001871 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1872 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1873 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1874 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001875 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001876
Chia-I Wu63636062015-10-26 21:10:41 +08001877 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1878 vkDestroyImage(demo->device, demo->depth.image, NULL);
1879 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001880
Chia-I Wu63636062015-10-26 21:10:41 +08001881 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1882 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001883
1884 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001885 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001886 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1887 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001888 }
Chia-I Wu63636062015-10-26 21:10:41 +08001889 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001890 free(demo->buffers);
1891
Ian Elliottcf074d32015-10-16 18:02:43 -06001892 // Second, re-perform the demo_prepare() function, which will re-create the
1893 // swapchain:
1894 demo_prepare(demo);
1895}
1896
David Pinedo2bb7c932015-06-18 17:03:14 -06001897// On MS-Windows, make this a global, so it's available to WndProc()
1898struct demo demo;
1899
Ian Elliott639ca472015-04-16 15:23:05 -06001900#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07001901static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001902 if (!demo->prepared)
1903 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001904 // Wait for work to finish before updating MVP.
1905 vkDeviceWaitIdle(demo->device);
1906 demo_update_data_buffer(demo);
1907
1908 demo_draw(demo);
1909
1910 // Wait for work to finish before updating MVP.
1911 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001912
David Pinedo2bb7c932015-06-18 17:03:14 -06001913 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001914 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06001915 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06001916 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001917}
Ian Elliott639ca472015-04-16 15:23:05 -06001918
1919// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001920LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
1921 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001922 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06001923 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001924 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001925 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001926 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001927 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001928 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07001929 // Resize the application to the new window size, except when
1930 // it was minimized. Vulkan doesn't support images or swapchains
1931 // with width=0 and height=0.
1932 if (wParam != SIZE_MINIMIZED) {
1933 demo.width = lParam & 0xffff;
1934 demo.height = lParam & 0xffff0000 >> 16;
1935 demo_resize(&demo);
1936 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06001937 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001938 default:
1939 break;
1940 }
1941 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1942}
1943
Karl Schultze4dc75c2016-02-02 15:37:51 -07001944static void demo_create_window(struct demo *demo) {
1945 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06001946
1947 // Initialize the window class structure:
1948 win_class.cbSize = sizeof(WNDCLASSEX);
1949 win_class.style = CS_HREDRAW | CS_VREDRAW;
1950 win_class.lpfnWndProc = WndProc;
1951 win_class.cbClsExtra = 0;
1952 win_class.cbWndExtra = 0;
1953 win_class.hInstance = demo->connection; // hInstance
1954 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1955 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1956 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1957 win_class.lpszMenuName = NULL;
1958 win_class.lpszClassName = demo->name;
1959 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1960 // Register window class:
1961 if (!RegisterClassEx(&win_class)) {
1962 // It didn't work, so try to give a useful error:
1963 printf("Unexpected error trying to start the application!\n");
1964 fflush(stdout);
1965 exit(1);
1966 }
1967 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001968 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06001969 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001970 demo->window = CreateWindowEx(0,
1971 demo->name, // class name
1972 demo->name, // app name
1973 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07001974 WS_VISIBLE | WS_SYSMENU,
1975 100, 100, // x/y coords
1976 wr.right - wr.left, // width
1977 wr.bottom - wr.top, // height
1978 NULL, // handle to parent
1979 NULL, // handle to menu
1980 demo->connection, // hInstance
1981 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06001982 if (!demo->window) {
1983 // It didn't work, so try to give a useful error:
1984 printf("Cannot create a window in which to draw!\n");
1985 fflush(stdout);
1986 exit(1);
1987 }
1988}
1989#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001990static void demo_handle_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001991 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07001992 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001993 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001994 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001995 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001996 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001997 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001998 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
1999 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002000 demo->quit = true;
2001 }
2002 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002003 case XCB_KEY_RELEASE: {
2004 const xcb_key_release_event_t *key =
2005 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002006
Karl Schultze4dc75c2016-02-02 15:37:51 -07002007 switch (key->detail) {
2008 case 0x9: // Escape
2009 demo->quit = true;
2010 break;
2011 case 0x71: // left arrow key
2012 demo->spin_angle += demo->spin_increment;
2013 break;
2014 case 0x72: // right arrow key
2015 demo->spin_angle -= demo->spin_increment;
2016 break;
2017 case 0x41:
2018 demo->pause = !demo->pause;
2019 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002020 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002021 } break;
2022 case XCB_CONFIGURE_NOTIFY: {
2023 const xcb_configure_notify_event_t *cfg =
2024 (const xcb_configure_notify_event_t *)event;
2025 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002026 demo->width = cfg->width;
2027 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002028 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002029 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002030 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002031 default:
2032 break;
2033 }
2034}
2035
Karl Schultze4dc75c2016-02-02 15:37:51 -07002036static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002037 xcb_flush(demo->connection);
2038
2039 while (!demo->quit) {
2040 xcb_generic_event_t *event;
2041
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002042 if (demo->pause) {
2043 event = xcb_wait_for_event(demo->connection);
2044 } else {
2045 event = xcb_poll_for_event(demo->connection);
2046 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002047 if (event) {
2048 demo_handle_event(demo, event);
2049 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002050 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002051
2052 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002053 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002054 demo_update_data_buffer(demo);
2055
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002056 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002057
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002058 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002059 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002060 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002061 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002062 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002063 }
2064}
2065
Karl Schultze4dc75c2016-02-02 15:37:51 -07002066static void demo_create_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002067 uint32_t value_mask, value_list[32];
2068
2069 demo->window = xcb_generate_id(demo->connection);
2070
2071 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2072 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002073 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002074 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002075
Karl Schultze4dc75c2016-02-02 15:37:51 -07002076 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->window,
2077 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2078 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2079 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002080
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002081 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002082 xcb_intern_atom_cookie_t cookie =
2083 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2084 xcb_intern_atom_reply_t *reply =
2085 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002086
Karl Schultze4dc75c2016-02-02 15:37:51 -07002087 xcb_intern_atom_cookie_t cookie2 =
2088 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2089 demo->atom_wm_delete_window =
2090 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002091
Karl Schultze4dc75c2016-02-02 15:37:51 -07002092 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->window,
2093 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002094 &(*demo->atom_wm_delete_window).atom);
2095 free(reply);
2096
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002097 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002098
Karl Schultze4dc75c2016-02-02 15:37:51 -07002099 // Force the x/y coordinates to 100,100 results are identical in consecutive
2100 // runs
2101 const uint32_t coords[] = {100, 100};
David Pinedo2bb7c932015-06-18 17:03:14 -06002102 xcb_configure_window(demo->connection, demo->window,
2103 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002104}
Ian Elliott639ca472015-04-16 15:23:05 -06002105#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002106
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002107/*
2108 * Return 1 (true) if all layer names specified in check_names
2109 * can be found in given layer properties.
2110 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002111static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002112 uint32_t layer_count,
2113 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002114 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002115 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002116 for (uint32_t j = 0; j < layer_count; j++) {
2117 if (!strcmp(check_names[i], layers[j].layerName)) {
2118 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002119 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002120 }
2121 }
2122 if (!found) {
2123 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2124 return 0;
2125 }
2126 }
2127 return 1;
2128}
2129
Karl Schultze4dc75c2016-02-02 15:37:51 -07002130static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002131 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002132 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002133 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002134 uint32_t device_validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002135 char **instance_validation_layers = NULL;
2136 demo->enabled_extension_count = 0;
2137 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002138
Karl Schultz7c50ad62016-04-01 12:07:03 -06002139 char *instance_validation_layers_alt1[] = {
2140 "VK_LAYER_LUNARG_standard_validation"
2141 };
2142
2143 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskiaaab5c02016-03-17 15:08:18 -06002144 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
Karl Schultz635272d2016-03-07 17:54:07 -07002145 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker",
Tobin Ehlis1398c712016-03-09 16:12:48 -07002146 "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation",
Karl Schultz7c50ad62016-04-01 12:07:03 -06002147 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002148 };
2149
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002150 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002151 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002152 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002153
Karl Schultz7c50ad62016-04-01 12:07:03 -06002154 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002155 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002156
Karl Schultz7c50ad62016-04-01 12:07:03 -06002157 instance_validation_layers = instance_validation_layers_alt1;
2158 if (instance_layer_count > 0) {
2159 VkLayerProperties *instance_layers =
2160 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2161 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2162 instance_layers);
2163 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002164
Karl Schultz7c50ad62016-04-01 12:07:03 -06002165
2166 validation_found = demo_check_layers(
2167 ARRAY_SIZE(instance_validation_layers_alt1),
2168 instance_validation_layers, instance_layer_count,
2169 instance_layers);
2170 if (validation_found) {
2171 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
2172 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2173 device_validation_layer_count = 1;
2174 } else {
2175 // use alternative set of validation layers
2176 instance_validation_layers = instance_validation_layers_alt2;
2177 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2178 validation_found = demo_check_layers(
2179 ARRAY_SIZE(instance_validation_layers_alt2),
2180 instance_validation_layers, instance_layer_count,
2181 instance_layers);
2182 device_validation_layer_count =
2183 ARRAY_SIZE(instance_validation_layers_alt2);
2184 for (uint32_t i = 0; i < device_validation_layer_count; i++) {
2185 demo->device_validation_layers[i] =
2186 instance_validation_layers[i];
2187 }
2188 }
2189 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002190 }
Dustin Graves7c287352016-01-27 13:48:06 -07002191
Karl Schultz7c50ad62016-04-01 12:07:03 -06002192 if (!validation_found) {
2193 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
2194 "required validation layer.\n\n"
2195 "Please look at the Getting Started guide for additional "
2196 "information.\n",
2197 "vkCreateInstance Failure");
2198 }
Dustin Graves7c287352016-01-27 13:48:06 -07002199 }
2200
2201 /* Look for instance extensions */
2202 VkBool32 surfaceExtFound = 0;
2203 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002204 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002205
Karl Schultze4dc75c2016-02-02 15:37:51 -07002206 err = vkEnumerateInstanceExtensionProperties(
2207 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002208 assert(!err);
2209
Dustin Graves7c287352016-01-27 13:48:06 -07002210 if (instance_extension_count > 0) {
2211 VkExtensionProperties *instance_extensions =
2212 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2213 err = vkEnumerateInstanceExtensionProperties(
2214 NULL, &instance_extension_count, instance_extensions);
2215 assert(!err);
2216 for (uint32_t i = 0; i < instance_extension_count; i++) {
2217 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2218 instance_extensions[i].extensionName)) {
2219 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002220 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002221 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002222 }
Dustin Graves7c287352016-01-27 13:48:06 -07002223#ifdef _WIN32
2224 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2225 instance_extensions[i].extensionName)) {
2226 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002227 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002228 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2229 }
2230#else // _WIN32
2231 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2232 instance_extensions[i].extensionName)) {
2233 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002234 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002235 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2236 }
2237#endif // _WIN32
2238 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2239 instance_extensions[i].extensionName)) {
2240 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002241 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002242 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2243 }
2244 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002245 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002246 }
Dustin Graves7c287352016-01-27 13:48:06 -07002247
2248 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002249 }
Dustin Graves7c287352016-01-27 13:48:06 -07002250
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002251 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002252 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2253 "the " VK_KHR_SURFACE_EXTENSION_NAME
2254 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002255 "Vulkan installable client driver (ICD) installed?\nPlease "
2256 "look at the Getting Started guide for additional "
2257 "information.\n",
2258 "vkCreateInstance Failure");
2259 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002260 if (!platformSurfaceExtFound) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002261#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002262 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2263 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2264 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002265 "Vulkan installable client driver (ICD) installed?\nPlease "
2266 "look at the Getting Started guide for additional "
2267 "information.\n",
2268 "vkCreateInstance Failure");
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002269#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002270 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2271 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2272 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002273 "Vulkan installable client driver (ICD) installed?\nPlease "
2274 "look at the Getting Started guide for additional "
2275 "information.\n",
2276 "vkCreateInstance Failure");
2277#endif // _WIN32
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002278 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002279 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002280 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002281 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002282 .pApplicationName = APP_SHORT_NAME,
2283 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002284 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002285 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002286 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002287 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002288 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002289 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002290 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002291 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002292 .enabledLayerCount = demo->enabled_layer_count,
2293 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2294 .enabledExtensionCount = demo->enabled_extension_count,
2295 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002296 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002297
2298 /*
2299 * This is info for a temp callback to use during CreateInstance.
2300 * After the instance is created, we use the instance-based
2301 * function to register the final callback.
2302 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002303 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002304 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002305 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2306 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002307 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002308 dbgCreateInfo.pUserData = NULL;
2309 dbgCreateInfo.flags =
2310 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2311 inst_info.pNext = &dbgCreateInfo;
2312 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002313
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002314 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002315
Chia-I Wu63636062015-10-26 21:10:41 +08002316 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002317 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2318 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002319 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002320 "additional information.\n",
2321 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002322 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002323 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002324 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002325 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002326 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002327 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2328 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002329 "the Getting Started guide for additional information.\n",
2330 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002331 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002332
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002333 /* Make initial call to query gpu_count, then second call for gpu info*/
2334 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2335 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002336
2337 if (gpu_count > 0) {
2338 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2339 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2340 assert(!err);
2341 /* For cube demo we just grab the first physical device */
2342 demo->gpu = physical_devices[0];
2343 free(physical_devices);
2344 } else {
2345 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2346 "Do you have a compatible Vulkan installable client driver (ICD) "
2347 "installed?\nPlease look at the Getting Started guide for "
2348 "additional information.\n",
2349 "vkEnumeratePhysicalDevices Failure");
2350 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002351
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002352 /* Look for validation layers */
2353 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002354 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002355 uint32_t device_layer_count = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002356 err =
2357 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002358 assert(!err);
2359
Dustin Graves7c287352016-01-27 13:48:06 -07002360 if (device_layer_count > 0) {
2361 VkLayerProperties *device_layers =
2362 malloc(sizeof(VkLayerProperties) * device_layer_count);
2363 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2364 device_layers);
2365 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002366
Dustin Graves7c287352016-01-27 13:48:06 -07002367 if (demo->validate) {
2368 validation_found = demo_check_layers(device_validation_layer_count,
2369 demo->device_validation_layers,
2370 device_layer_count,
2371 device_layers);
2372 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002373 }
Dustin Graves7c287352016-01-27 13:48:06 -07002374
2375 free(device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002376 }
2377
Dustin Graves7c287352016-01-27 13:48:06 -07002378 if (demo->validate && !validation_found) {
2379 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
2380 "a required validation layer.\n\n"
2381 "Please look at the Getting Started guide for additional "
2382 "information.\n",
2383 "vkCreateDevice Failure");
2384 }
2385
2386 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002387 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002388 VkBool32 swapchainExtFound = 0;
2389 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002390 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002391
Karl Schultze4dc75c2016-02-02 15:37:51 -07002392 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2393 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002394 assert(!err);
2395
Dustin Graves7c287352016-01-27 13:48:06 -07002396 if (device_extension_count > 0) {
2397 VkExtensionProperties *device_extensions =
2398 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2399 err = vkEnumerateDeviceExtensionProperties(
2400 demo->gpu, NULL, &device_extension_count, device_extensions);
2401 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002402
Dustin Graves7c287352016-01-27 13:48:06 -07002403 for (uint32_t i = 0; i < device_extension_count; i++) {
2404 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2405 device_extensions[i].extensionName)) {
2406 swapchainExtFound = 1;
2407 demo->extension_names[demo->enabled_extension_count++] =
2408 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2409 }
2410 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002411 }
Dustin Graves7c287352016-01-27 13:48:06 -07002412
2413 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002414 }
Dustin Graves7c287352016-01-27 13:48:06 -07002415
Tony Barbourcc69f452015-10-08 13:59:42 -06002416 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002417 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2418 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2419 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002420 "Vulkan installable client driver (ICD) installed?\nPlease "
2421 "look at the Getting Started guide for additional "
2422 "information.\n",
2423 "vkCreateInstance Failure");
2424 }
2425
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002426 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002427 demo->CreateDebugReportCallback =
2428 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2429 demo->inst, "vkCreateDebugReportCallbackEXT");
2430 demo->DestroyDebugReportCallback =
2431 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2432 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002433 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002434 ERR_EXIT(
2435 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2436 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002437 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002438 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002439 ERR_EXIT(
2440 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2441 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002442 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002443 demo->DebugReportMessage =
2444 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2445 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002446 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002447 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002448 "vkGetProcAddr Failure");
2449 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002450
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002451 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002452 PFN_vkDebugReportCallbackEXT callback;
2453 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002454 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002455 dbgCreateInfo.pNext = NULL;
2456 dbgCreateInfo.pfnCallback = callback;
2457 dbgCreateInfo.pUserData = NULL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002458 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002459 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002460 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2461 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002462 switch (err) {
2463 case VK_SUCCESS:
2464 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002465 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002466 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2467 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002468 break;
2469 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002470 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2471 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002472 break;
2473 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002474 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002475 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002476
2477 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002478 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2479 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002480 assert(demo->queue_count >= 1);
2481
Karl Schultze4dc75c2016-02-02 15:37:51 -07002482 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2483 demo->queue_count * sizeof(VkQueueFamilyProperties));
2484 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2485 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002486 // Find a queue that supports gfx
2487 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002488 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2489 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002490 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2491 break;
2492 }
2493 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002494 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002495 // If app has specific feature requirements it should check supported
2496 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002497 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002498 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002499
Tony Barbourda2bad52016-01-22 14:36:40 -07002500 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2501 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2502 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2503 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2504 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2505}
2506
Karl Schultze4dc75c2016-02-02 15:37:51 -07002507static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002508 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002509 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002510 const VkDeviceQueueCreateInfo queue = {
2511 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2512 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002513 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002514 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002515 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002516
2517 VkDeviceCreateInfo device = {
2518 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2519 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002520 .queueCreateInfoCount = 1,
2521 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002522 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002523 .ppEnabledLayerNames =
2524 (const char *const *)((demo->validate)
2525 ? demo->device_validation_layers
2526 : NULL),
Tony Barbourda2bad52016-01-22 14:36:40 -07002527 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002528 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2529 .pEnabledFeatures =
2530 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002531 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002532
Chia-I Wu63636062015-10-26 21:10:41 +08002533 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002534 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002535}
2536
Karl Schultze4dc75c2016-02-02 15:37:51 -07002537static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002538 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002539 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002540
Karl Schultze4dc75c2016-02-02 15:37:51 -07002541// Create a WSI surface for the window:
Ian Elliottaaae5352015-07-06 14:27:58 -06002542#ifdef _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002543 VkWin32SurfaceCreateInfoKHR createInfo;
2544 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2545 createInfo.pNext = NULL;
2546 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002547 createInfo.hinstance = demo->connection;
2548 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002549
Karl Schultze4dc75c2016-02-02 15:37:51 -07002550 err =
2551 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002552
Ian Elliottaaae5352015-07-06 14:27:58 -06002553#else // _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002554 VkXcbSurfaceCreateInfoKHR createInfo;
2555 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2556 createInfo.pNext = NULL;
2557 createInfo.flags = 0;
2558 createInfo.connection = demo->connection;
2559 createInfo.window = demo->window;
2560
Karl Schultze4dc75c2016-02-02 15:37:51 -07002561 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottaaae5352015-07-06 14:27:58 -06002562#endif // _WIN32
2563
Tony Barbourcc69f452015-10-08 13:59:42 -06002564 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002565 VkBool32 *supportsPresent =
2566 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002567 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002568 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002569 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002570 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002571
2572 // Search for a graphics and a present queue in the array of queue
2573 // families, try to find one that supports both
2574 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002575 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002576 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002577 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2578 if (graphicsQueueNodeIndex == UINT32_MAX) {
2579 graphicsQueueNodeIndex = i;
2580 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002581
Ian Elliottaaae5352015-07-06 14:27:58 -06002582 if (supportsPresent[i] == VK_TRUE) {
2583 graphicsQueueNodeIndex = i;
2584 presentQueueNodeIndex = i;
2585 break;
2586 }
2587 }
2588 }
2589 if (presentQueueNodeIndex == UINT32_MAX) {
2590 // If didn't find a queue that supports both graphics and present, then
2591 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002592 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002593 if (supportsPresent[i] == VK_TRUE) {
2594 presentQueueNodeIndex = i;
2595 break;
2596 }
2597 }
2598 }
2599 free(supportsPresent);
2600
2601 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002602 if (graphicsQueueNodeIndex == UINT32_MAX ||
2603 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002604 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002605 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002606 }
2607
2608 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002609 // synchronization, and appropriate tracking for QueueSubmit.
2610 // NOTE: While it is possible for an application to use a separate graphics
2611 // and a present queues, this demo program assumes it is only using
2612 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002613 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2614 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002615 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002616 }
2617
2618 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002619
Tony Barbourda2bad52016-01-22 14:36:40 -07002620 demo_create_device(demo);
2621
2622 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2623 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2624 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2625 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2626 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2627
Karl Schultze4dc75c2016-02-02 15:37:51 -07002628 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2629 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002630
Ian Elliottaaae5352015-07-06 14:27:58 -06002631 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002632 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002633 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002634 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002635 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002636 VkSurfaceFormatKHR *surfFormats =
2637 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002638 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002639 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002640 assert(!err);
2641 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2642 // the surface has no preferred format. Otherwise, at least one
2643 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002644 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002645 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002646 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002647 assert(formatCount >= 1);
2648 demo->format = surfFormats[0].format;
2649 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002650 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002651
David Pinedo2bb7c932015-06-18 17:03:14 -06002652 demo->quit = false;
2653 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002654
2655 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002656 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002657}
2658
Karl Schultze4dc75c2016-02-02 15:37:51 -07002659static void demo_init_connection(struct demo *demo) {
Ian Elliott639ca472015-04-16 15:23:05 -06002660#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002661 const xcb_setup_t *setup;
2662 xcb_screen_iterator_t iter;
2663 int scr;
2664
2665 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002666 if (demo->connection == NULL) {
2667 printf("Cannot find a compatible Vulkan installable client driver "
2668 "(ICD).\nExiting ...\n");
2669 fflush(stdout);
2670 exit(1);
2671 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002672
2673 setup = xcb_get_setup(demo->connection);
2674 iter = xcb_setup_roots_iterator(setup);
2675 while (scr-- > 0)
2676 xcb_screen_next(&iter);
2677
2678 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002679#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002680}
2681
Karl Schultze4dc75c2016-02-02 15:37:51 -07002682static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002683 vec3 eye = {0.0f, 3.0f, 5.0f};
2684 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002685 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002686
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002687 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002688 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002689
Piers Daniell735ee532015-02-23 16:23:13 -07002690 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002691 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002692 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002693 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002694 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002695 if (strcmp(argv[i], "--break") == 0) {
2696 demo->use_break = true;
2697 continue;
2698 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002699 if (strcmp(argv[i], "--validate") == 0) {
2700 demo->validate = true;
2701 continue;
2702 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002703 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2704 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2705 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002706 i++;
2707 continue;
2708 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002709
Karl Schultze4dc75c2016-02-02 15:37:51 -07002710 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
2711 "[--c <framecount>]\n",
2712 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002713 fflush(stderr);
2714 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002715 }
2716
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002717 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002718 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002719
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002720 demo->width = 500;
2721 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002722
2723 demo->spin_angle = 0.01f;
2724 demo->spin_increment = 0.01f;
2725 demo->pause = false;
2726
Karl Schultze4dc75c2016-02-02 15:37:51 -07002727 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
2728 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002729 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2730 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002731}
2732
Ian Elliott639ca472015-04-16 15:23:05 -06002733#ifdef _WIN32
Mark Young418b9d12016-01-06 14:26:04 -07002734// Include header required for parsing the command line options.
2735#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002736
Karl Schultze4dc75c2016-02-02 15:37:51 -07002737int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
2738 int nCmdShow) {
2739 MSG msg; // message
2740 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002741 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002742 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06002743
Karl Schultze4dc75c2016-02-02 15:37:51 -07002744 // Use the CommandLine functions to get the command line arguments.
2745 // Unfortunately, Microsoft outputs
2746 // this information as wide characters for Unicode, and we simply want the
2747 // Ascii version to be compatible
2748 // with the non-Windows side. So, we have to convert the information to
2749 // Ascii character strings.
2750 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
2751 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07002752 argc = 0;
2753 }
2754
Karl Schultze4dc75c2016-02-02 15:37:51 -07002755 if (argc > 0) {
2756 argv = (char **)malloc(sizeof(char *) * argc);
2757 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002758 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002759 } else {
2760 for (int iii = 0; iii < argc; iii++) {
2761 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07002762 size_t numConverted = 0;
2763
Karl Schultze4dc75c2016-02-02 15:37:51 -07002764 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
2765 if (argv[iii] != NULL) {
2766 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
2767 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07002768 }
2769 }
2770 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002771 } else {
Mark Young418b9d12016-01-06 14:26:04 -07002772 argv = NULL;
2773 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002774
2775 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07002776
2777 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002778 if (argc > 0 && argv != NULL) {
2779 for (int iii = 0; iii < argc; iii++) {
2780 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002781 free(argv[iii]);
2782 }
2783 }
2784 free(argv);
2785 }
2786
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002787 demo.connection = hInstance;
2788 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002789 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002790 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002791
2792 demo_prepare(&demo);
2793
Karl Schultze4dc75c2016-02-02 15:37:51 -07002794 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07002795
2796 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07002797 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002798 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002799 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06002800 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002801 done = true; // if found, quit app
2802 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06002803 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07002804 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06002805 DispatchMessage(&msg);
2806 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002807 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002808 }
2809
2810 demo_cleanup(&demo);
2811
Karl Schultze4dc75c2016-02-02 15:37:51 -07002812 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002813}
2814#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002815int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002816 struct demo demo;
2817
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002818 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002819 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002820 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002821
2822 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002823 demo_run(&demo);
2824
2825 demo_cleanup(&demo);
2826
Karl Schultz0218c002016-03-22 17:06:13 -06002827 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002828}
Ian Elliott639ca472015-04-16 15:23:05 -06002829#endif // _WIN32