blob: 1a5e054947f87638a52b5215578aa94d349ee539 [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 *
Jon Ashburn7b44e362016-04-19 11:30:31 -06006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
Ian Elliotta748eaf2015-04-28 15:50:36 -06009 *
Jon Ashburn7b44e362016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Ian Elliotta748eaf2015-04-28 15:50:36 -060011 *
Jon Ashburn7b44e362016-04-19 11:30:31 -060012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060017 *
18 * Author: Chia-I Wu <olv@lunarg.com>
19 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20 * Author: Ian Elliott <ian@LunarG.com>
21 * Author: Jon Ashburn <jon@lunarg.com>
Ian Elliotta748eaf2015-04-28 15:50:36 -060022 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070023
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070030#include <signal.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060031
Ian Elliott639ca472015-04-16 15:23:05 -060032#ifdef _WIN32
33#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060034#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060035#endif // _WIN32
36
David Pinedoc5193c12015-11-06 12:54:48 -070037#include <vulkan/vulkan.h>
Ian Elliottb08e0d92015-11-20 11:55:46 -070038
David Pinedo541526f2015-11-24 09:00:24 -070039#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060040#include "linmath.h"
41
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060042#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060043#define APP_SHORT_NAME "cube"
44#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060045
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060046#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
47
Tony Barbourfdc2d352015-04-22 09:02:32 -060048#if defined(NDEBUG) && defined(__GNUC__)
49#define U_ASSERT_ONLY __attribute__((unused))
50#else
51#define U_ASSERT_ONLY
52#endif
53
Ian Elliott07264132015-04-28 11:35:02 -060054#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -070055#define ERR_EXIT(err_msg, err_class) \
56 do { \
57 MessageBox(NULL, err_msg, err_class, MB_OK); \
58 exit(1); \
59 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060060
Karl Schultze4dc75c2016-02-02 15:37:51 -070061#else // _WIN32
Ian Elliott07264132015-04-28 11:35:02 -060062
Karl Schultze4dc75c2016-02-02 15:37:51 -070063#define ERR_EXIT(err_msg, err_class) \
64 do { \
65 printf(err_msg); \
66 fflush(stdout); \
67 exit(1); \
68 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060069#endif // _WIN32
70
Karl Schultze4dc75c2016-02-02 15:37:51 -070071#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
72 { \
73 demo->fp##entrypoint = \
74 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
75 if (demo->fp##entrypoint == NULL) { \
76 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
77 "vkGetInstanceProcAddr Failure"); \
78 } \
79 }
Ian Elliottaaae5352015-07-06 14:27:58 -060080
Jon Ashburn7d8342c2015-10-08 15:58:23 -060081static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
82
Karl Schultze4dc75c2016-02-02 15:37:51 -070083#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
84 { \
85 if (!g_gdpa) \
86 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
87 demo->inst, "vkGetDeviceProcAddr"); \
88 demo->fp##entrypoint = \
89 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
90 if (demo->fp##entrypoint == NULL) { \
91 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
92 "vkGetDeviceProcAddr Failure"); \
93 } \
94 }
Ian Elliott673898b2015-06-22 15:07:49 -060095
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060096/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070097 * structure to track all objects related to a texture.
98 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060099struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600100 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700101
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600102 VkImage image;
103 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600104
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800105 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500106 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600107 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700108 int32_t tex_width, tex_height;
109};
110
Karl Schultze4dc75c2016-02-02 15:37:51 -0700111static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600112
Karl Schultz0218c002016-03-22 17:06:13 -0600113static int validation_error = 0;
114
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600115struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600116 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700117 float mvp[4][4];
118 float position[12 * 3][4];
119 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600120};
121
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600122struct vktexcube_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 attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600128
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600129//--------------------------------------------------------------------------------------
130// Mesh and VertexFormat Data
131//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700132// clang-format off
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600133struct Vertex
134{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600135 float posX, posY, posZ, posW; // Position data
136 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600137};
138
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600139struct VertexPosTex
140{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600141 float posX, posY, posZ, posW; // Position data
142 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600143};
144
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600145#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600146#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600147
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600148static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800149 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600150 -1.0f,-1.0f, 1.0f,
151 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800152 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600153 -1.0f, 1.0f,-1.0f,
154 -1.0f,-1.0f,-1.0f,
155
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800156 -1.0f,-1.0f,-1.0f, // -Z 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,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600161 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600165 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,
172 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, // +X 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,
182 1.0f, 1.0f,-1.0f,
183
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800184 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 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,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600190};
191
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600192static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800193 0.0f, 0.0f, // -X side
194 1.0f, 0.0f,
195 1.0f, 1.0f,
196 1.0f, 1.0f,
197 0.0f, 1.0f,
198 0.0f, 0.0f,
199
200 1.0f, 0.0f, // -Z side
201 0.0f, 1.0f,
202 0.0f, 0.0f,
203 1.0f, 0.0f,
204 1.0f, 1.0f,
205 0.0f, 1.0f,
206
207 1.0f, 1.0f, // -Y side
208 1.0f, 0.0f,
209 0.0f, 0.0f,
210 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600211 0.0f, 0.0f,
212 0.0f, 1.0f,
213
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800214 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600215 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600217 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600218 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600219 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800221 1.0f, 1.0f, // +X side
222 0.0f, 1.0f,
223 0.0f, 0.0f,
224 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600226 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800228 0.0f, 1.0f, // +Z side
229 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600230 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600231 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800232 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600234};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700235// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600236
Karl Schultze4dc75c2016-02-02 15:37:51 -0700237void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600238 int i;
239
240 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700241 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600242 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
243 }
244 printf("\n");
245 fflush(stdout);
246}
247
Karl Schultze4dc75c2016-02-02 15:37:51 -0700248void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600249 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700250 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600251 printf("\n");
252 fflush(stdout);
253}
254
Karl Schultze4dc75c2016-02-02 15:37:51 -0700255VKAPI_ATTR VkBool32 VKAPI_CALL
256dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
257 uint64_t srcObject, size_t location, int32_t msgCode,
258 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
259 char *message = (char *)malloc(strlen(pMsg) + 100);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600260
Karl Schultze4dc75c2016-02-02 15:37:51 -0700261 assert(message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600262
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700263 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700264 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
265 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600266 validation_error = 1;
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -0700267 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700268 // We know that we're submitting queues without fences, ignore this
269 // warning
270 if (strstr(pMsg,
271 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600272 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600273 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700274 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
275 pMsg);
Karl Schultz0218c002016-03-22 17:06:13 -0600276 validation_error = 1;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600277 } else {
Karl Schultz0218c002016-03-22 17:06:13 -0600278 validation_error = 1;
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600279 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600280 }
281
282#ifdef _WIN32
283 MessageBox(NULL, message, "Alert", MB_OK);
284#else
Karl Schultze4dc75c2016-02-02 15:37:51 -0700285 printf("%s\n", message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600286 fflush(stdout);
287#endif
288 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600289
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600290 /*
291 * false indicates that layer should not bail-out of an
292 * API call that had validation failures. This may mean that the
293 * app dies inside the driver due to invalid parameter(s).
294 * That's what would happen without validation layers, so we'll
295 * keep that behavior here.
296 */
297 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600298}
299
Karl Schultz0c3c1512016-03-25 15:35:18 -0600300VKAPI_ATTR VkBool32 VKAPI_CALL
301BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
302 uint64_t srcObject, size_t location, int32_t msgCode,
303 const char *pLayerPrefix, const char *pMsg,
304 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700305#ifndef WIN32
306 raise(SIGTRAP);
307#else
308 DebugBreak();
309#endif
310
311 return false;
312}
313
Ian Elliotta0781972015-08-21 15:09:33 -0600314typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600315 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800316 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600317 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600318} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600319
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600320struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600321#ifdef _WIN32
322#define APP_NAME_STR_LEN 80
323 HINSTANCE connection; // hInstance - Windows Instance
324 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700325 HWND window; // hWnd - window handle
326#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327 xcb_connection_t *connection;
328 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800329 xcb_window_t window;
330 xcb_intern_atom_reply_t *atom_wm_delete_window;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700331#endif // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -0700332 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600333 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700334 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600336 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600337 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600338 VkDevice device;
339 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700340 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600341 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600342 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600343 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344
Tony Barbourda2bad52016-01-22 14:36:40 -0700345 uint32_t enabled_extension_count;
346 uint32_t enabled_layer_count;
347 char *extension_names[64];
348 char *device_validation_layers[64];
349
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600350 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600351 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600352 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600353
Karl Schultze4dc75c2016-02-02 15:37:51 -0700354 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
355 fpGetPhysicalDeviceSurfaceSupportKHR;
356 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
357 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
358 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
359 fpGetPhysicalDeviceSurfaceFormatsKHR;
360 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
361 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600362 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
363 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
364 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
365 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
366 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600367 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600368 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600369 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800371 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372
373 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600374 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800377 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500378 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600379 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380 } depth;
381
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600382 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383
384 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600385 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800386 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500387 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600388 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600389 } uniform_data;
390
Karl Schultze4dc75c2016-02-02 15:37:51 -0700391 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500392 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600393 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600394 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800395 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600396 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600397
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600398 mat4x4 projection_matrix;
399 mat4x4 view_matrix;
400 mat4x4 model_matrix;
401
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600402 float spin_angle;
403 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600404 bool pause;
405
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600406 VkShaderModule vert_shader_module;
407 VkShaderModule frag_shader_module;
408
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600409 VkDescriptorPool desc_pool;
410 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800411
Tony Barbourd8e504f2015-10-08 14:26:24 -0600412 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800413
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600414 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600415 int32_t curFrame;
416 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600417 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600418 bool use_break;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700419 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
420 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
421 VkDebugReportCallbackEXT msg_callback;
422 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600423
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600424 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600425 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600426};
427
Ian Elliottcf074d32015-10-16 18:02:43 -0600428// Forward declaration:
429static void demo_resize(struct demo *demo);
430
Karl Schultze4dc75c2016-02-02 15:37:51 -0700431static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
432 VkFlags requirements_mask,
433 uint32_t *typeIndex) {
434 // Search memtypes to find first index with those properties
435 for (uint32_t i = 0; i < 32; i++) {
436 if ((typeBits & 1) == 1) {
437 // Type is available, does it match user properties?
438 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
439 requirements_mask) == requirements_mask) {
440 *typeIndex = i;
441 return true;
442 }
443 }
444 typeBits >>= 1;
445 }
446 // No memory types matched, return failure
447 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600448}
449
Karl Schultze4dc75c2016-02-02 15:37:51 -0700450static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600451 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600452
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600453 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454 return;
455
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600456 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600457 assert(!err);
458
Karl Schultze4dc75c2016-02-02 15:37:51 -0700459 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800460 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700461 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
462 .pNext = NULL,
463 .waitSemaphoreCount = 0,
464 .pWaitSemaphores = NULL,
465 .pWaitDstStageMask = NULL,
466 .commandBufferCount = 1,
467 .pCommandBuffers = cmd_bufs,
468 .signalSemaphoreCount = 0,
469 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600470
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600471 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600472 assert(!err);
473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600474 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600475 assert(!err);
476
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600477 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600478 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600479}
480
Karl Schultze4dc75c2016-02-02 15:37:51 -0700481static void demo_set_image_layout(struct demo *demo, VkImage image,
482 VkImageAspectFlags aspectMask,
483 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700484 VkImageLayout new_image_layout,
485 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600486 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600487
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600488 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800489 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800490 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600491 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800492 .commandPool = demo->cmd_pool,
493 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700494 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600495 };
496
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800497 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600498 assert(!err);
499
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700500 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
501 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600502 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800503 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600504 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800505 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800506 .occlusionQueryEnable = VK_FALSE,
507 .queryFlags = 0,
508 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600509 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700510 VkCommandBufferBeginInfo cmd_buf_info = {
511 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
512 .pNext = NULL,
513 .flags = 0,
514 .pInheritanceInfo = &cmd_buf_hinfo,
515 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600516 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600517 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600518 }
519
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600520 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600521 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600522 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700523 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800524 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600525 .oldLayout = old_image_layout,
526 .newLayout = new_image_layout,
527 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700528 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600529
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800530 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600531 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800532 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600533 }
534
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700535 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700536 image_memory_barrier.dstAccessMask =
537 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700538 }
539
540 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700541 image_memory_barrier.dstAccessMask =
542 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700543 }
544
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600545 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600546 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700547 image_memory_barrier.dstAccessMask =
548 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600549 }
550
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600551 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600552
Tony Barbour50bbca42015-06-29 16:20:35 -0600553 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
554 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600555
Karl Schultze4dc75c2016-02-02 15:37:51 -0700556 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
557 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600558}
559
Karl Schultze4dc75c2016-02-02 15:37:51 -0700560static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700561 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
562 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600563 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800564 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600565 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800566 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800567 .occlusionQueryEnable = VK_FALSE,
568 .queryFlags = 0,
569 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700570 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700571 const VkCommandBufferBeginInfo cmd_buf_info = {
572 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
573 .pNext = NULL,
574 .flags = 0,
575 .pInheritanceInfo = &cmd_buf_hinfo,
576 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800577 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700578 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
579 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800580 };
581 const VkRenderPassBeginInfo rp_begin = {
582 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
583 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800584 .renderPass = demo->render_pass,
585 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800586 .renderArea.offset.x = 0,
587 .renderArea.offset.y = 0,
588 .renderArea.extent.width = demo->width,
589 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600590 .clearValueCount = 2,
591 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700592 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800593 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600594
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600595 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596 assert(!err);
597
Chia-I Wuf051d262015-10-27 19:25:11 +0800598 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800599
Karl Schultze4dc75c2016-02-02 15:37:51 -0700600 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
601 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
602 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
603 NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600604
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600605 VkViewport viewport;
606 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700607 viewport.height = (float)demo->height;
608 viewport.width = (float)demo->width;
609 viewport.minDepth = (float)0.0f;
610 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700611 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600612
613 VkRect2D scissor;
614 memset(&scissor, 0, sizeof(scissor));
615 scissor.extent.width = demo->width;
616 scissor.extent.height = demo->height;
617 scissor.offset.x = 0;
618 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700619 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600620
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600621 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800622 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600623
Tony Barbour15a4ef62015-10-21 10:14:48 -0600624 VkImageMemoryBarrier prePresentBarrier = {
625 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
626 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800627 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700628 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600629 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700630 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600631 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800632 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700633 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600634
635 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
636 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700637 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
638 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
639 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600640
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600641 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600642 assert(!err);
643}
644
Karl Schultze4dc75c2016-02-02 15:37:51 -0700645void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600646 mat4x4 MVP, Model, VP;
647 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600648 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600649 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600650
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600651 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600653 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600654 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700655 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
656 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600657 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600658
Karl Schultze4dc75c2016-02-02 15:37:51 -0700659 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
660 demo->uniform_data.mem_alloc.allocationSize, 0,
661 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662 assert(!err);
663
Karl Schultze4dc75c2016-02-02 15:37:51 -0700664 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600665
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600666 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600667}
668
Karl Schultze4dc75c2016-02-02 15:37:51 -0700669static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600670 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600671 VkSemaphore presentCompleteSemaphore;
672 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
673 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
674 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600675 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600676 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800677 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600678
Karl Schultze4dc75c2016-02-02 15:37:51 -0700679 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
680 NULL, &presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600681 assert(!err);
682
683 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700684 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Ian Elliottaaae5352015-07-06 14:27:58 -0600685 presentCompleteSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700686 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600687 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600688 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
689 // demo->swapchain is out of date (e.g. the window was resized) and
690 // must be recreated:
691 demo_resize(demo);
692 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800693 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600694 return;
695 } else if (err == VK_SUBOPTIMAL_KHR) {
696 // demo->swapchain is not as optimal as it could be, but the platform's
697 // presentation engine will still present the image correctly.
698 } else {
699 assert(!err);
700 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600701
Tony Barbour15a4ef62015-10-21 10:14:48 -0600702 // Assume the command buffer has been run on current_buffer before so
703 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
704 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700705 VK_IMAGE_ASPECT_COLOR_BIT,
706 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700707 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
708 0);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600709 demo_flush_init_cmd(demo);
710
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600711 // Wait for the present complete semaphore to be signaled to ensure
712 // that the image won't be rendered to until the presentation
713 // engine has fully released ownership to the application, and it is
714 // okay to render to the image.
715
Karl Schultze4dc75c2016-02-02 15:37:51 -0700716 // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
717 VkPipelineStageFlags pipe_stage_flags =
718 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
719 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
720 .pNext = NULL,
721 .waitSemaphoreCount = 1,
722 .pWaitSemaphores = &presentCompleteSemaphore,
723 .pWaitDstStageMask = &pipe_stage_flags,
724 .commandBufferCount = 1,
725 .pCommandBuffers =
726 &demo->buffers[demo->current_buffer].cmd,
727 .signalSemaphoreCount = 0,
728 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600729
730 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600731 assert(!err);
732
Ian Elliotta0781972015-08-21 15:09:33 -0600733 VkPresentInfoKHR present = {
734 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600735 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600736 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700737 .pSwapchains = &demo->swapchain,
738 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600739 };
740
Karl Schultze4dc75c2016-02-02 15:37:51 -0700741 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600742 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600743 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
744 // demo->swapchain is out of date (e.g. the window was resized) and
745 // must be recreated:
746 demo_resize(demo);
747 } else if (err == VK_SUBOPTIMAL_KHR) {
748 // demo->swapchain is not as optimal as it could be, but the platform's
749 // presentation engine will still present the image correctly.
750 } else {
751 assert(!err);
752 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600753
Chia-I Wucbb564e2015-04-16 22:02:10 +0800754 err = vkQueueWaitIdle(demo->queue);
755 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600756
Chia-I Wu63636062015-10-26 21:10:41 +0800757 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600758}
759
Karl Schultze4dc75c2016-02-02 15:37:51 -0700760static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600761 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600762 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600763
Ian Elliottb08e0d92015-11-20 11:55:46 -0700764 // Check the surface capabilities and formats
765 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700766 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
767 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600768 assert(!err);
769
Ian Elliott8528eff2015-08-07 15:56:59 -0600770 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700771 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
772 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600773 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600774 VkPresentModeKHR *presentModes =
775 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600776 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700777 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
778 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600779 assert(!err);
780
Ian Elliotta0781972015-08-21 15:09:33 -0600781 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600782 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700783 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600784 // If the surface size is undefined, the size is set to
785 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600786 swapchainExtent.width = demo->width;
787 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700788 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600789 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700790 swapchainExtent = surfCapabilities.currentExtent;
791 demo->width = surfCapabilities.currentExtent.width;
792 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600793 }
794
795 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600796 // tearing mode. If not, try IMMEDIATE which will usually be available,
797 // and is fastest (though it tears). If not, fall back to FIFO which is
798 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600799 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600800 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600801 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
802 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600803 break;
804 }
Ian Elliotta0781972015-08-21 15:09:33 -0600805 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
806 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
807 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600808 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600809 }
810
811 // Determine the number of VkImage's to use in the swap chain (we desire to
812 // own only 1 image at a time, besides the images being displayed and
813 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700814 uint32_t desiredNumberOfSwapchainImages =
815 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700816 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700817 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600818 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700819 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600820 }
821
Tony Barbour9fd6d352015-09-16 15:01:32 -0600822 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700823 if (surfCapabilities.supportedTransforms &
824 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700825 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600826 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700827 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600828 }
829
Ian Elliottcf074d32015-10-16 18:02:43 -0600830 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600831 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800832 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700833 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600834 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800835 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600836 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700837 .imageExtent =
838 {
839 .width = swapchainExtent.width, .height = swapchainExtent.height,
840 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700841 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600842 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700843 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700844 .imageArrayLayers = 1,
845 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
846 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600847 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600848 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600849 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600850 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600851 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600852 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600853
Karl Schultze4dc75c2016-02-02 15:37:51 -0700854 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
855 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800856 assert(!err);
857
Ian Elliottcf074d32015-10-16 18:02:43 -0600858 // If we just re-created an existing swapchain, we should destroy the old
859 // swapchain at this point.
860 // Note: destroying the swapchain also cleans up all its associated
861 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800862 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700863 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600864 }
865
866 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600867 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600868 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800869
Karl Schultze4dc75c2016-02-02 15:37:51 -0700870 VkImage *swapchainImages =
871 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600872 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600873 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600874 &demo->swapchainImageCount,
875 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600876 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600877
Karl Schultze4dc75c2016-02-02 15:37:51 -0700878 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
879 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600880 assert(demo->buffers);
881
Ian Elliotta0781972015-08-21 15:09:33 -0600882 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600883 VkImageViewCreateInfo color_image_view = {
884 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600885 .pNext = NULL,
886 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700887 .components =
888 {
889 .r = VK_COMPONENT_SWIZZLE_R,
890 .g = VK_COMPONENT_SWIZZLE_G,
891 .b = VK_COMPONENT_SWIZZLE_B,
892 .a = VK_COMPONENT_SWIZZLE_A,
893 },
894 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
895 .baseMipLevel = 0,
896 .levelCount = 1,
897 .baseArrayLayer = 0,
898 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600899 .viewType = VK_IMAGE_VIEW_TYPE_2D,
900 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600901 };
902
Ian Elliotta0781972015-08-21 15:09:33 -0600903 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600904
Karl Schultze4dc75c2016-02-02 15:37:51 -0700905 // Render loop will expect image to have been used before and in
906 // VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
907 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image
908 // to that state
909 demo_set_image_layout(
910 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700911 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
912 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600913
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600914 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600915
Karl Schultze4dc75c2016-02-02 15:37:51 -0700916 err = vkCreateImageView(demo->device, &color_image_view, NULL,
917 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600918 assert(!err);
919 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700920
Mark Young04fd3d82016-01-25 14:43:50 -0700921 if (NULL != presentModes) {
922 free(presentModes);
923 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600924}
925
Karl Schultze4dc75c2016-02-02 15:37:51 -0700926static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600927 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600928 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600929 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600930 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600931 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600932 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700933 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600934 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600935 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800936 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600937 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600938 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600939 .flags = 0,
940 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200941
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600942 VkImageViewCreateInfo view = {
943 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600944 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800945 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600946 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700947 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
948 .baseMipLevel = 0,
949 .levelCount = 1,
950 .baseArrayLayer = 0,
951 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600952 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600953 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600954 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600955
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500956 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600957 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600958 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600959
960 demo->depth.format = depth_format;
961
962 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700963 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600964 assert(!err);
965
Karl Schultze4dc75c2016-02-02 15:37:51 -0700966 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600967 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600968
Chia-I Wuf48700b2015-11-10 16:21:09 +0800969 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200970 demo->depth.mem_alloc.pNext = NULL;
971 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
972 demo->depth.mem_alloc.memoryTypeIndex = 0;
973
Karl Schultze4dc75c2016-02-02 15:37:51 -0700974 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
975 0, /* No requirements */
976 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600977 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500979 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700980 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
981 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500982 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600983
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500984 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700985 err =
986 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500987 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600988
Karl Schultze4dc75c2016-02-02 15:37:51 -0700989 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
990 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700991 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
992 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600993
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600994 /* create image view */
995 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800996 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600997 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600998}
999
Tony Barbour1a86d032015-09-21 15:17:33 -06001000/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001001bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001002 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
1003 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001004 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001005
Tony Barbour1a86d032015-09-21 15:17:33 -06001006 if (!fPtr)
1007 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001008
Tony Barbour1a86d032015-09-21 15:17:33 -06001009 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001010 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1011 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001012 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001013 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001014
Tony Barbour1a86d032015-09-21 15:17:33 -06001015 do {
1016 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001017 if (cPtr == NULL) {
1018 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001019 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001020 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001021 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001022
Tony Barbour1a86d032015-09-21 15:17:33 -06001023 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001024 if (rgba_data == NULL) {
1025 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001026 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001027 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001028 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001029 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001030 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1031 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001032 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001033 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001034
Karl Schultze4dc75c2016-02-02 15:37:51 -07001035 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001036 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001037 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001038 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001039 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001040 rowPtr[3] = 255; /* Alpha of 1 */
1041 rowPtr += 4;
1042 }
1043 rgba_data += layout->rowPitch;
1044 }
1045 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001046 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001047}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001048
Karl Schultze4dc75c2016-02-02 15:37:51 -07001049static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001050 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001051 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001052 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001053 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001054 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001055 int32_t tex_width;
1056 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001057 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001058 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001059
Karl Schultze4dc75c2016-02-02 15:37:51 -07001060 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
David Pinedo30bd71d2015-04-23 08:16:57 -06001061 printf("Failed to load textures\n");
1062 fflush(stdout);
1063 exit(1);
1064 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001065
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001066 tex_obj->tex_width = tex_width;
1067 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001068
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001069 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001070 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001072 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001074 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001075 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001076 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001077 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001078 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001079 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001081 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001082 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001083
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001084 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085
Karl Schultze4dc75c2016-02-02 15:37:51 -07001086 err =
1087 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001088 assert(!err);
1089
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001090 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001091
Chia-I Wuf48700b2015-11-10 16:21:09 +08001092 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001093 tex_obj->mem_alloc.pNext = NULL;
1094 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1095 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001096
Karl Schultze4dc75c2016-02-02 15:37:51 -07001097 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1098 required_props,
1099 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001100 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001101
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001102 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001103 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001104 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001105 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001106
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001107 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001108 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001109 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001110
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001111 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001112 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001113 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001114 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001115 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001116 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001117 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001118 void *data;
1119
Karl Schultze4dc75c2016-02-02 15:37:51 -07001120 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1121 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001122
Karl Schultze4dc75c2016-02-02 15:37:51 -07001123 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1124 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001125 assert(!err);
1126
1127 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1128 fprintf(stderr, "Error loading texture: %s\n", filename);
1129 }
1130
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001131 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001132 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001133
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001134 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001135 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001136 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1137 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001138 /* setting the image layout does not reference the actual memory so no need
1139 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001140}
1141
Karl Schultze4dc75c2016-02-02 15:37:51 -07001142static void demo_destroy_texture_image(struct demo *demo,
1143 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001144 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001145 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1146 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001147}
1148
Karl Schultze4dc75c2016-02-02 15:37:51 -07001149static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001150 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001151 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001152 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001153
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001154 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001155
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001156 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001157 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001158
Karl Schultze4dc75c2016-02-02 15:37:51 -07001159 if ((props.linearTilingFeatures &
1160 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1161 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001162 /* Device can texture using linear textures */
1163 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Karl Schultze4dc75c2016-02-02 15:37:51 -07001164 VK_IMAGE_TILING_LINEAR,
1165 VK_IMAGE_USAGE_SAMPLED_BIT,
1166 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
1167 } else if (props.optimalTilingFeatures &
1168 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001169 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001170 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001171
1172 memset(&staging_texture, 0, sizeof(staging_texture));
1173 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001174 VK_IMAGE_TILING_LINEAR,
1175 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1176 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001177
Karl Schultze4dc75c2016-02-02 15:37:51 -07001178 demo_prepare_texture_image(
1179 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1180 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1181 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001182
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001183 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001184 VK_IMAGE_ASPECT_COLOR_BIT,
1185 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001186 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1187 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001188
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001189 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001190 VK_IMAGE_ASPECT_COLOR_BIT,
1191 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001192 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1193 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001194
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001195 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001196 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1197 .srcOffset = {0, 0, 0},
1198 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1199 .dstOffset = {0, 0, 0},
1200 .extent = {staging_texture.tex_width,
1201 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001202 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001203 vkCmdCopyImage(
1204 demo->cmd, staging_texture.image,
1205 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1206 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001207
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001208 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001209 VK_IMAGE_ASPECT_COLOR_BIT,
1210 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001211 demo->textures[i].imageLayout,
1212 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001213
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001214 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001215
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001216 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001217 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001218 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1219 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001220 }
1221
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001222 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001223 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001224 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001225 .magFilter = VK_FILTER_NEAREST,
1226 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001227 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001228 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1229 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1230 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001231 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001232 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001233 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001234 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001235 .minLod = 0.0f,
1236 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001237 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001238 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001239 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001240
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001241 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001242 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001243 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001244 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001245 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001246 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001247 .components =
1248 {
1249 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1250 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1251 },
1252 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001253 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001254 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001255
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001256 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001257 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001258 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001259 assert(!err);
1260
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001261 /* create image view */
1262 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001263 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001264 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001265 assert(!err);
1266 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001267}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001268
Karl Schultze4dc75c2016-02-02 15:37:51 -07001269void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001270 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001271 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001272 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001273 int i;
1274 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001275 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001276 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001277 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001278
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001279 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001280 mat4x4_mul(MVP, VP, demo->model_matrix);
1281 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001282 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001283
Karl Schultze4dc75c2016-02-02 15:37:51 -07001284 for (i = 0; i < 12 * 3; i++) {
1285 data.position[i][0] = g_vertex_buffer_data[i * 3];
1286 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1287 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001288 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001289 data.attr[i][0] = g_uv_buffer_data[2 * i];
1290 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001291 data.attr[i][2] = 0;
1292 data.attr[i][3] = 0;
1293 }
1294
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001295 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001296 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001297 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001298 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001299 err =
1300 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001301 assert(!err);
1302
Karl Schultze4dc75c2016-02-02 15:37:51 -07001303 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1304 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001305
Chia-I Wuf48700b2015-11-10 16:21:09 +08001306 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001307 demo->uniform_data.mem_alloc.pNext = NULL;
1308 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1309 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1310
Karl Schultze4dc75c2016-02-02 15:37:51 -07001311 pass = memory_type_from_properties(
1312 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1313 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001314 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001315
Karl Schultze4dc75c2016-02-02 15:37:51 -07001316 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1317 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001318 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001319
Karl Schultze4dc75c2016-02-02 15:37:51 -07001320 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1321 demo->uniform_data.mem_alloc.allocationSize, 0,
1322 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001323 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001324
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001325 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001326
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001327 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001328
Karl Schultze4dc75c2016-02-02 15:37:51 -07001329 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1330 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001331 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001332
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001333 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1334 demo->uniform_data.buffer_info.offset = 0;
1335 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001336}
1337
Karl Schultze4dc75c2016-02-02 15:37:51 -07001338static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001339 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001340 [0] =
1341 {
1342 .binding = 0,
1343 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1344 .descriptorCount = 1,
1345 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1346 .pImmutableSamplers = NULL,
1347 },
1348 [1] =
1349 {
1350 .binding = 1,
1351 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1352 .descriptorCount = DEMO_TEXTURE_COUNT,
1353 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1354 .pImmutableSamplers = NULL,
1355 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001356 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001357 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001358 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001359 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001360 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001361 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001362 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001363 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001364
Karl Schultze4dc75c2016-02-02 15:37:51 -07001365 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1366 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001367 assert(!err);
1368
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001369 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001370 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1371 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001372 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001373 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001374 };
1375
Karl Schultze4dc75c2016-02-02 15:37:51 -07001376 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001377 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001378 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001379}
1380
Karl Schultze4dc75c2016-02-02 15:37:51 -07001381static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001382 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383 [0] =
1384 {
1385 .format = demo->format,
1386 .samples = VK_SAMPLE_COUNT_1_BIT,
1387 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1388 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1389 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1390 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1391 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1392 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1393 },
1394 [1] =
1395 {
1396 .format = demo->depth.format,
1397 .samples = VK_SAMPLE_COUNT_1_BIT,
1398 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1399 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1400 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1401 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1402 .initialLayout =
1403 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1404 .finalLayout =
1405 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1406 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001407 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001408 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001409 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001410 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001411 const VkAttachmentReference depth_reference = {
1412 .attachment = 1,
1413 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1414 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001415 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001416 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1417 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001418 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001419 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001420 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001421 .pColorAttachments = &color_reference,
1422 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001423 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001424 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001425 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001426 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001427 const VkRenderPassCreateInfo rp_info = {
1428 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1429 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001430 .attachmentCount = 2,
1431 .pAttachments = attachments,
1432 .subpassCount = 1,
1433 .pSubpasses = &subpass,
1434 .dependencyCount = 0,
1435 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001436 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001437 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001438
Chia-I Wu63636062015-10-26 21:10:41 +08001439 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001440 assert(!err);
1441}
1442
Karl Schultze4dc75c2016-02-02 15:37:51 -07001443static VkShaderModule
1444demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001445 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001446 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001447 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001448
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001449 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1450 moduleCreateInfo.pNext = NULL;
1451
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001452 moduleCreateInfo.codeSize = size;
1453 moduleCreateInfo.pCode = code;
1454 moduleCreateInfo.flags = 0;
1455 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1456 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001457
1458 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001459}
1460
Karl Schultze4dc75c2016-02-02 15:37:51 -07001461char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001462 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001463 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001464 void *shader_code;
1465
1466 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001467 if (!fp)
1468 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001469
1470 fseek(fp, 0L, SEEK_END);
1471 size = ftell(fp);
1472
1473 fseek(fp, 0L, SEEK_SET);
1474
1475 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001476 retval = fread(shader_code, size, 1, fp);
1477 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001478
1479 *psize = size;
1480
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001481 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001482 return shader_code;
1483}
1484
Karl Schultze4dc75c2016-02-02 15:37:51 -07001485static VkShaderModule demo_prepare_vs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001486 void *vertShaderCode;
1487 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001488
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001489 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1490
Karl Schultze4dc75c2016-02-02 15:37:51 -07001491 demo->vert_shader_module =
1492 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001493
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001494 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001495
1496 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001497}
1498
Karl Schultze4dc75c2016-02-02 15:37:51 -07001499static VkShaderModule demo_prepare_fs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001500 void *fragShaderCode;
1501 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001502
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001503 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001504
Karl Schultze4dc75c2016-02-02 15:37:51 -07001505 demo->frag_shader_module =
1506 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001507
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001508 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001509
1510 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001511}
1512
Karl Schultze4dc75c2016-02-02 15:37:51 -07001513static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001514 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001515 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001516 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001517 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001518 VkPipelineRasterizationStateCreateInfo rs;
1519 VkPipelineColorBlendStateCreateInfo cb;
1520 VkPipelineDepthStencilStateCreateInfo ds;
1521 VkPipelineViewportStateCreateInfo vp;
1522 VkPipelineMultisampleStateCreateInfo ms;
1523 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1524 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001525 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001526
Piers Daniellba839d12015-09-29 13:01:09 -06001527 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1528 memset(&dynamicState, 0, sizeof dynamicState);
1529 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1530 dynamicState.pDynamicStates = dynamicStateEnables;
1531
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001533 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001534 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001535
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001536 memset(&vi, 0, sizeof(vi));
1537 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1538
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001539 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001540 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001541 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001542
1543 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001544 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1545 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001546 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001547 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001548 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001549 rs.rasterizerDiscardEnable = VK_FALSE;
1550 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001551
1552 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001553 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1554 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001555 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001556 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001557 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001558 cb.attachmentCount = 1;
1559 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001560
Tony Barbour29645d02015-01-16 14:27:35 -07001561 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001562 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001563 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001564 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1565 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001566 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001567 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1568 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001569
1570 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001571 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001572 ds.depthTestEnable = VK_TRUE;
1573 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001574 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001575 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001576 ds.back.failOp = VK_STENCIL_OP_KEEP;
1577 ds.back.passOp = VK_STENCIL_OP_KEEP;
1578 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001579 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001580 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001581
Tony Barbour29645d02015-01-16 14:27:35 -07001582 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001583 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001584 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001585 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001586
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001587 // Two stages: vs and fs
1588 pipeline.stageCount = 2;
1589 VkPipelineShaderStageCreateInfo shaderStages[2];
1590 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1591
Karl Schultze4dc75c2016-02-02 15:37:51 -07001592 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1593 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001594 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001595 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001596
Karl Schultze4dc75c2016-02-02 15:37:51 -07001597 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1598 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001599 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001600 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001601
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001602 memset(&pipelineCache, 0, sizeof(pipelineCache));
1603 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001604
Karl Schultze4dc75c2016-02-02 15:37:51 -07001605 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1606 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001607 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001608
Karl Schultze4dc75c2016-02-02 15:37:51 -07001609 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001610 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001611 pipeline.pRasterizationState = &rs;
1612 pipeline.pColorBlendState = &cb;
1613 pipeline.pMultisampleState = &ms;
1614 pipeline.pViewportState = &vp;
1615 pipeline.pDepthStencilState = &ds;
1616 pipeline.pStages = shaderStages;
1617 pipeline.renderPass = demo->render_pass;
1618 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001619
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001620 pipeline.renderPass = demo->render_pass;
1621
Karl Schultze4dc75c2016-02-02 15:37:51 -07001622 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1623 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001624 assert(!err);
1625
Chia-I Wu63636062015-10-26 21:10:41 +08001626 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1627 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001628}
1629
Karl Schultze4dc75c2016-02-02 15:37:51 -07001630static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001631 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001632 [0] =
1633 {
1634 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1635 .descriptorCount = 1,
1636 },
1637 [1] =
1638 {
1639 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1640 .descriptorCount = DEMO_TEXTURE_COUNT,
1641 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001642 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001643 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001644 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001645 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001646 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001647 .poolSizeCount = 2,
1648 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001649 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001650 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001651
Karl Schultze4dc75c2016-02-02 15:37:51 -07001652 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1653 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001654 assert(!err);
1655}
1656
Karl Schultze4dc75c2016-02-02 15:37:51 -07001657static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001658 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001659 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001660 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001661 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001662
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001663 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001664 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001665 .pNext = NULL,
1666 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001667 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001668 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001669 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001670 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001671
Chia-I Wuae721ba2015-05-25 16:27:55 +08001672 memset(&tex_descs, 0, sizeof(tex_descs));
1673 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001674 tex_descs[i].sampler = demo->textures[i].sampler;
1675 tex_descs[i].imageView = demo->textures[i].view;
1676 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001677 }
1678
1679 memset(&writes, 0, sizeof(writes));
1680
1681 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001682 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001683 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001684 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001685 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001686
1687 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001688 writes[1].dstSet = demo->desc_set;
1689 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001690 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001691 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001692 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001693
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001694 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001695}
1696
Karl Schultze4dc75c2016-02-02 15:37:51 -07001697static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001698 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001699 attachments[1] = demo->depth.view;
1700
Chia-I Wuf973e322015-07-08 13:34:24 +08001701 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001702 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1703 .pNext = NULL,
1704 .renderPass = demo->render_pass,
1705 .attachmentCount = 2,
1706 .pAttachments = attachments,
1707 .width = demo->width,
1708 .height = demo->height,
1709 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001710 };
1711 VkResult U_ASSERT_ONLY err;
1712 uint32_t i;
1713
Karl Schultze4dc75c2016-02-02 15:37:51 -07001714 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1715 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001716 assert(demo->framebuffers);
1717
1718 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001719 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001720 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1721 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001722 assert(!err);
1723 }
1724}
1725
Karl Schultze4dc75c2016-02-02 15:37:51 -07001726static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001727 VkResult U_ASSERT_ONLY err;
1728
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001729 const VkCommandPoolCreateInfo cmd_pool_info = {
1730 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001731 .pNext = NULL,
1732 .queueFamilyIndex = demo->graphics_queue_node_index,
1733 .flags = 0,
1734 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001735 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1736 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001737 assert(!err);
1738
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001739 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001740 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001741 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001742 .commandPool = demo->cmd_pool,
1743 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001744 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001746
1747 demo_prepare_buffers(demo);
1748 demo_prepare_depth(demo);
1749 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001750 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001752 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001753 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001754 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001755
Ian Elliotta0781972015-08-21 15:09:33 -06001756 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001757 err =
1758 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001759 assert(!err);
1760 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001761
Chia-I Wu63ea9262015-03-26 13:14:16 +08001762 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001763 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001764
Chia-I Wuf973e322015-07-08 13:34:24 +08001765 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001766
Ian Elliotta0781972015-08-21 15:09:33 -06001767 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001768 demo->current_buffer = i;
1769 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1770 }
1771
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001772 /*
1773 * Prepare functions above may generate pipeline commands
1774 * that need to be flushed before beginning the render loop.
1775 */
1776 demo_flush_init_cmd(demo);
1777
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001778 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001779 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001780}
1781
Karl Schultze4dc75c2016-02-02 15:37:51 -07001782static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001783 uint32_t i;
1784
1785 demo->prepared = false;
1786
Tony Barbourd8e504f2015-10-08 14:26:24 -06001787 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001788 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001789 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001790 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001791 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001792
Chia-I Wu63636062015-10-26 21:10:41 +08001793 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1794 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1795 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1796 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1797 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001798
1799 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001800 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1801 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1802 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1803 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001804 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001805 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001806
Chia-I Wu63636062015-10-26 21:10:41 +08001807 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1808 vkDestroyImage(demo->device, demo->depth.image, NULL);
1809 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001810
Chia-I Wu63636062015-10-26 21:10:41 +08001811 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1812 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001813
Ian Elliotta0781972015-08-21 15:09:33 -06001814 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001815 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001816 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1817 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001818 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001819 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001820
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001821 free(demo->queue_props);
1822
Chia-I Wu63636062015-10-26 21:10:41 +08001823 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1824 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001825 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001826 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001827 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001828 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001829 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001830
1831#ifndef _WIN32
1832 xcb_destroy_window(demo->connection, demo->window);
1833 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001834 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001835#endif // _WIN32
1836}
1837
Karl Schultze4dc75c2016-02-02 15:37:51 -07001838static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001839 uint32_t i;
1840
Mike Stroyanbb60b382015-10-28 09:46:57 -06001841 // Don't react to resize until after first initialization.
1842 if (!demo->prepared) {
1843 return;
1844 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001845 // In order to properly resize the window, we must re-create the swapchain
1846 // AND redo the command buffers, etc.
1847 //
1848 // First, perform part of the demo_cleanup() function:
1849 demo->prepared = false;
1850
1851 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001852 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001853 }
1854 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001855 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001856
Chia-I Wu63636062015-10-26 21:10:41 +08001857 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1858 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1859 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1860 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1861 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001862
1863 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001864 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1865 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1866 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1867 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001868 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001869
Chia-I Wu63636062015-10-26 21:10:41 +08001870 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1871 vkDestroyImage(demo->device, demo->depth.image, NULL);
1872 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001873
Chia-I Wu63636062015-10-26 21:10:41 +08001874 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1875 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001876
1877 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001878 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001879 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1880 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001881 }
Chia-I Wu63636062015-10-26 21:10:41 +08001882 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001883 free(demo->buffers);
1884
Ian Elliottcf074d32015-10-16 18:02:43 -06001885 // Second, re-perform the demo_prepare() function, which will re-create the
1886 // swapchain:
1887 demo_prepare(demo);
1888}
1889
David Pinedo2bb7c932015-06-18 17:03:14 -06001890// On MS-Windows, make this a global, so it's available to WndProc()
1891struct demo demo;
1892
Ian Elliott639ca472015-04-16 15:23:05 -06001893#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07001894static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001895 if (!demo->prepared)
1896 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001897 // Wait for work to finish before updating MVP.
1898 vkDeviceWaitIdle(demo->device);
1899 demo_update_data_buffer(demo);
1900
1901 demo_draw(demo);
1902
1903 // Wait for work to finish before updating MVP.
1904 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001905
David Pinedo2bb7c932015-06-18 17:03:14 -06001906 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001907 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06001908 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06001909 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001910}
Ian Elliott639ca472015-04-16 15:23:05 -06001911
1912// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001913LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
1914 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001915 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06001916 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001917 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001918 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001919 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001920 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001921 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07001922 // Resize the application to the new window size, except when
1923 // it was minimized. Vulkan doesn't support images or swapchains
1924 // with width=0 and height=0.
1925 if (wParam != SIZE_MINIMIZED) {
1926 demo.width = lParam & 0xffff;
1927 demo.height = lParam & 0xffff0000 >> 16;
1928 demo_resize(&demo);
1929 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06001930 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001931 default:
1932 break;
1933 }
1934 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1935}
1936
Karl Schultze4dc75c2016-02-02 15:37:51 -07001937static void demo_create_window(struct demo *demo) {
1938 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06001939
1940 // Initialize the window class structure:
1941 win_class.cbSize = sizeof(WNDCLASSEX);
1942 win_class.style = CS_HREDRAW | CS_VREDRAW;
1943 win_class.lpfnWndProc = WndProc;
1944 win_class.cbClsExtra = 0;
1945 win_class.cbWndExtra = 0;
1946 win_class.hInstance = demo->connection; // hInstance
1947 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1948 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1949 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1950 win_class.lpszMenuName = NULL;
1951 win_class.lpszClassName = demo->name;
1952 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1953 // Register window class:
1954 if (!RegisterClassEx(&win_class)) {
1955 // It didn't work, so try to give a useful error:
1956 printf("Unexpected error trying to start the application!\n");
1957 fflush(stdout);
1958 exit(1);
1959 }
1960 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001961 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06001962 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001963 demo->window = CreateWindowEx(0,
1964 demo->name, // class name
1965 demo->name, // app name
1966 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07001967 WS_VISIBLE | WS_SYSMENU,
1968 100, 100, // x/y coords
1969 wr.right - wr.left, // width
1970 wr.bottom - wr.top, // height
1971 NULL, // handle to parent
1972 NULL, // handle to menu
1973 demo->connection, // hInstance
1974 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06001975 if (!demo->window) {
1976 // It didn't work, so try to give a useful error:
1977 printf("Cannot create a window in which to draw!\n");
1978 fflush(stdout);
1979 exit(1);
1980 }
1981}
1982#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001983static void demo_handle_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001984 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07001985 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001986 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001988 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001989 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001990 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001991 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
1992 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001993 demo->quit = true;
1994 }
1995 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001996 case XCB_KEY_RELEASE: {
1997 const xcb_key_release_event_t *key =
1998 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001999
Karl Schultze4dc75c2016-02-02 15:37:51 -07002000 switch (key->detail) {
2001 case 0x9: // Escape
2002 demo->quit = true;
2003 break;
2004 case 0x71: // left arrow key
2005 demo->spin_angle += demo->spin_increment;
2006 break;
2007 case 0x72: // right arrow key
2008 demo->spin_angle -= demo->spin_increment;
2009 break;
2010 case 0x41:
2011 demo->pause = !demo->pause;
2012 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002013 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002014 } break;
2015 case XCB_CONFIGURE_NOTIFY: {
2016 const xcb_configure_notify_event_t *cfg =
2017 (const xcb_configure_notify_event_t *)event;
2018 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002019 demo->width = cfg->width;
2020 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002021 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002022 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002023 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002024 default:
2025 break;
2026 }
2027}
2028
Karl Schultze4dc75c2016-02-02 15:37:51 -07002029static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030 xcb_flush(demo->connection);
2031
2032 while (!demo->quit) {
2033 xcb_generic_event_t *event;
2034
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002035 if (demo->pause) {
2036 event = xcb_wait_for_event(demo->connection);
2037 } else {
2038 event = xcb_poll_for_event(demo->connection);
2039 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002040 if (event) {
2041 demo_handle_event(demo, event);
2042 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002043 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002044
2045 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002046 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002047 demo_update_data_buffer(demo);
2048
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002049 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002050
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002051 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002052 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002053 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002054 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002055 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002056 }
2057}
2058
Karl Schultze4dc75c2016-02-02 15:37:51 -07002059static void demo_create_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002060 uint32_t value_mask, value_list[32];
2061
2062 demo->window = xcb_generate_id(demo->connection);
2063
2064 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2065 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002066 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002067 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002068
Karl Schultze4dc75c2016-02-02 15:37:51 -07002069 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->window,
2070 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2071 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2072 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002073
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002074 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002075 xcb_intern_atom_cookie_t cookie =
2076 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2077 xcb_intern_atom_reply_t *reply =
2078 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002079
Karl Schultze4dc75c2016-02-02 15:37:51 -07002080 xcb_intern_atom_cookie_t cookie2 =
2081 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2082 demo->atom_wm_delete_window =
2083 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002084
Karl Schultze4dc75c2016-02-02 15:37:51 -07002085 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->window,
2086 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002087 &(*demo->atom_wm_delete_window).atom);
2088 free(reply);
2089
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002090 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002091
Karl Schultze4dc75c2016-02-02 15:37:51 -07002092 // Force the x/y coordinates to 100,100 results are identical in consecutive
2093 // runs
2094 const uint32_t coords[] = {100, 100};
David Pinedo2bb7c932015-06-18 17:03:14 -06002095 xcb_configure_window(demo->connection, demo->window,
2096 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002097}
Ian Elliott639ca472015-04-16 15:23:05 -06002098#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002099
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002100/*
2101 * Return 1 (true) if all layer names specified in check_names
2102 * can be found in given layer properties.
2103 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002104static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002105 uint32_t layer_count,
2106 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002107 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002108 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002109 for (uint32_t j = 0; j < layer_count; j++) {
2110 if (!strcmp(check_names[i], layers[j].layerName)) {
2111 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002112 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002113 }
2114 }
2115 if (!found) {
2116 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2117 return 0;
2118 }
2119 }
2120 return 1;
2121}
2122
Karl Schultze4dc75c2016-02-02 15:37:51 -07002123static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002124 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002125 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002126 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002127 uint32_t device_validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002128 char **instance_validation_layers = NULL;
2129 demo->enabled_extension_count = 0;
2130 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002131
Karl Schultz7c50ad62016-04-01 12:07:03 -06002132 char *instance_validation_layers_alt1[] = {
2133 "VK_LAYER_LUNARG_standard_validation"
2134 };
2135
2136 char *instance_validation_layers_alt2[] = {
Mark Lobodzinskiaaab5c02016-03-17 15:08:18 -06002137 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
Karl Schultz635272d2016-03-07 17:54:07 -07002138 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker",
Tobin Ehlis1398c712016-03-09 16:12:48 -07002139 "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation",
Karl Schultz7c50ad62016-04-01 12:07:03 -06002140 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002141 };
2142
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002143 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002144 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002145 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002146
Karl Schultz7c50ad62016-04-01 12:07:03 -06002147 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002148 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002149
Karl Schultz7c50ad62016-04-01 12:07:03 -06002150 instance_validation_layers = instance_validation_layers_alt1;
2151 if (instance_layer_count > 0) {
2152 VkLayerProperties *instance_layers =
2153 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2154 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2155 instance_layers);
2156 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002157
Karl Schultz7c50ad62016-04-01 12:07:03 -06002158
2159 validation_found = demo_check_layers(
2160 ARRAY_SIZE(instance_validation_layers_alt1),
2161 instance_validation_layers, instance_layer_count,
2162 instance_layers);
2163 if (validation_found) {
2164 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
2165 demo->device_validation_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2166 device_validation_layer_count = 1;
2167 } else {
2168 // use alternative set of validation layers
2169 instance_validation_layers = instance_validation_layers_alt2;
2170 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2171 validation_found = demo_check_layers(
2172 ARRAY_SIZE(instance_validation_layers_alt2),
2173 instance_validation_layers, instance_layer_count,
2174 instance_layers);
2175 device_validation_layer_count =
2176 ARRAY_SIZE(instance_validation_layers_alt2);
2177 for (uint32_t i = 0; i < device_validation_layer_count; i++) {
2178 demo->device_validation_layers[i] =
2179 instance_validation_layers[i];
2180 }
2181 }
2182 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002183 }
Dustin Graves7c287352016-01-27 13:48:06 -07002184
Karl Schultz7c50ad62016-04-01 12:07:03 -06002185 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002186 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06002187 "required validation layer.\n\n"
2188 "Please look at the Getting Started guide for additional "
2189 "information.\n",
2190 "vkCreateInstance Failure");
2191 }
Dustin Graves7c287352016-01-27 13:48:06 -07002192 }
2193
2194 /* Look for instance extensions */
2195 VkBool32 surfaceExtFound = 0;
2196 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002197 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002198
Karl Schultze4dc75c2016-02-02 15:37:51 -07002199 err = vkEnumerateInstanceExtensionProperties(
2200 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002201 assert(!err);
2202
Dustin Graves7c287352016-01-27 13:48:06 -07002203 if (instance_extension_count > 0) {
2204 VkExtensionProperties *instance_extensions =
2205 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2206 err = vkEnumerateInstanceExtensionProperties(
2207 NULL, &instance_extension_count, instance_extensions);
2208 assert(!err);
2209 for (uint32_t i = 0; i < instance_extension_count; i++) {
2210 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2211 instance_extensions[i].extensionName)) {
2212 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002213 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002214 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002215 }
Dustin Graves7c287352016-01-27 13:48:06 -07002216#ifdef _WIN32
2217 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2218 instance_extensions[i].extensionName)) {
2219 platformSurfaceExtFound = 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_WIN32_SURFACE_EXTENSION_NAME;
2222 }
2223#else // _WIN32
2224 if (!strcmp(VK_KHR_XCB_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_XCB_SURFACE_EXTENSION_NAME;
2229 }
2230#endif // _WIN32
2231 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2232 instance_extensions[i].extensionName)) {
2233 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002234 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002235 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2236 }
2237 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002238 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002239 }
Dustin Graves7c287352016-01-27 13:48:06 -07002240
2241 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002242 }
Dustin Graves7c287352016-01-27 13:48:06 -07002243
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002244 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002245 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2246 "the " VK_KHR_SURFACE_EXTENSION_NAME
2247 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002248 "Vulkan installable client driver (ICD) installed?\nPlease "
2249 "look at the Getting Started guide for additional "
2250 "information.\n",
2251 "vkCreateInstance Failure");
2252 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002253 if (!platformSurfaceExtFound) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002254#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002255 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2256 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2257 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002258 "Vulkan installable client driver (ICD) installed?\nPlease "
2259 "look at the Getting Started guide for additional "
2260 "information.\n",
2261 "vkCreateInstance Failure");
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002262#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002263 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2264 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2265 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002266 "Vulkan installable client driver (ICD) installed?\nPlease "
2267 "look at the Getting Started guide for additional "
2268 "information.\n",
2269 "vkCreateInstance Failure");
2270#endif // _WIN32
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002271 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002272 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002273 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002274 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002275 .pApplicationName = APP_SHORT_NAME,
2276 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002277 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002278 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002279 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002280 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002281 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002282 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002283 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002284 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002285 .enabledLayerCount = demo->enabled_layer_count,
2286 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2287 .enabledExtensionCount = demo->enabled_extension_count,
2288 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002289 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002290
2291 /*
2292 * This is info for a temp callback to use during CreateInstance.
2293 * After the instance is created, we use the instance-based
2294 * function to register the final callback.
2295 */
Ian Elliott5959bbd2016-03-25 09:07:19 -06002296 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002297 if (demo->validate) {
Ian Elliott5959bbd2016-03-25 09:07:19 -06002298 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2299 dbgCreateInfo.pNext = NULL;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002300 dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002301 dbgCreateInfo.pUserData = NULL;
2302 dbgCreateInfo.flags =
2303 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
2304 inst_info.pNext = &dbgCreateInfo;
2305 }
Ian Elliott097d9f32015-04-28 11:35:02 -06002306
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002307 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002308
Chia-I Wu63636062015-10-26 21:10:41 +08002309 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002310 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2311 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002312 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002313 "additional information.\n",
2314 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002315 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002316 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002317 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002318 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002319 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002320 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2321 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002322 "the Getting Started guide for additional information.\n",
2323 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002324 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002325
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002326 /* Make initial call to query gpu_count, then second call for gpu info*/
2327 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2328 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002329
2330 if (gpu_count > 0) {
2331 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2332 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2333 assert(!err);
2334 /* For cube demo we just grab the first physical device */
2335 demo->gpu = physical_devices[0];
2336 free(physical_devices);
2337 } else {
2338 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2339 "Do you have a compatible Vulkan installable client driver (ICD) "
2340 "installed?\nPlease look at the Getting Started guide for "
2341 "additional information.\n",
2342 "vkEnumeratePhysicalDevices Failure");
2343 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002344
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002345 /* Look for validation layers */
2346 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002347 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002348 uint32_t device_layer_count = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002349 err =
2350 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002351 assert(!err);
2352
Dustin Graves7c287352016-01-27 13:48:06 -07002353 if (device_layer_count > 0) {
2354 VkLayerProperties *device_layers =
2355 malloc(sizeof(VkLayerProperties) * device_layer_count);
2356 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2357 device_layers);
2358 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002359
Dustin Graves7c287352016-01-27 13:48:06 -07002360 if (demo->validate) {
2361 validation_found = demo_check_layers(device_validation_layer_count,
2362 demo->device_validation_layers,
2363 device_layer_count,
2364 device_layers);
2365 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002366 }
Dustin Graves7c287352016-01-27 13:48:06 -07002367
2368 free(device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002369 }
2370
Dustin Graves7c287352016-01-27 13:48:06 -07002371 if (demo->validate && !validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06002372 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find "
Dustin Graves7c287352016-01-27 13:48:06 -07002373 "a required validation layer.\n\n"
2374 "Please look at the Getting Started guide for additional "
2375 "information.\n",
2376 "vkCreateDevice Failure");
2377 }
2378
2379 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002380 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002381 VkBool32 swapchainExtFound = 0;
2382 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002383 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002384
Karl Schultze4dc75c2016-02-02 15:37:51 -07002385 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2386 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002387 assert(!err);
2388
Dustin Graves7c287352016-01-27 13:48:06 -07002389 if (device_extension_count > 0) {
2390 VkExtensionProperties *device_extensions =
2391 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2392 err = vkEnumerateDeviceExtensionProperties(
2393 demo->gpu, NULL, &device_extension_count, device_extensions);
2394 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002395
Dustin Graves7c287352016-01-27 13:48:06 -07002396 for (uint32_t i = 0; i < device_extension_count; i++) {
2397 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2398 device_extensions[i].extensionName)) {
2399 swapchainExtFound = 1;
2400 demo->extension_names[demo->enabled_extension_count++] =
2401 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2402 }
2403 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002404 }
Dustin Graves7c287352016-01-27 13:48:06 -07002405
2406 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002407 }
Dustin Graves7c287352016-01-27 13:48:06 -07002408
Tony Barbourcc69f452015-10-08 13:59:42 -06002409 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002410 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2411 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2412 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002413 "Vulkan installable client driver (ICD) installed?\nPlease "
2414 "look at the Getting Started guide for additional "
2415 "information.\n",
2416 "vkCreateInstance Failure");
2417 }
2418
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002419 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002420 demo->CreateDebugReportCallback =
2421 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2422 demo->inst, "vkCreateDebugReportCallbackEXT");
2423 demo->DestroyDebugReportCallback =
2424 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2425 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002426 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002427 ERR_EXIT(
2428 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2429 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002430 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002431 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002432 ERR_EXIT(
2433 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2434 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002435 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002436 demo->DebugReportMessage =
2437 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2438 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002439 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002440 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002441 "vkGetProcAddr Failure");
2442 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002443
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002444 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06002445 PFN_vkDebugReportCallbackEXT callback;
2446 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002447 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002448 dbgCreateInfo.pNext = NULL;
2449 dbgCreateInfo.pfnCallback = callback;
2450 dbgCreateInfo.pUserData = NULL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002451 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002452 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002453 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2454 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002455 switch (err) {
2456 case VK_SUCCESS:
2457 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002458 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002459 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2460 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002461 break;
2462 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002463 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2464 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002465 break;
2466 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002467 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002468 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002469
2470 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002471 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2472 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002473 assert(demo->queue_count >= 1);
2474
Karl Schultze4dc75c2016-02-02 15:37:51 -07002475 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2476 demo->queue_count * sizeof(VkQueueFamilyProperties));
2477 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2478 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002479 // Find a queue that supports gfx
2480 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002481 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2482 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002483 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2484 break;
2485 }
2486 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002487 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002488 // If app has specific feature requirements it should check supported
2489 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002490 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002491 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002492
Tony Barbourda2bad52016-01-22 14:36:40 -07002493 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2494 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2495 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2496 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2497 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2498}
2499
Karl Schultze4dc75c2016-02-02 15:37:51 -07002500static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002501 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002502 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002503 const VkDeviceQueueCreateInfo queue = {
2504 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2505 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002506 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002507 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002508 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002509
2510 VkDeviceCreateInfo device = {
2511 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2512 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002513 .queueCreateInfoCount = 1,
2514 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002515 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002516 .ppEnabledLayerNames =
2517 (const char *const *)((demo->validate)
2518 ? demo->device_validation_layers
2519 : NULL),
Tony Barbourda2bad52016-01-22 14:36:40 -07002520 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002521 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2522 .pEnabledFeatures =
2523 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002524 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002525
Chia-I Wu63636062015-10-26 21:10:41 +08002526 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002527 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002528}
2529
Karl Schultze4dc75c2016-02-02 15:37:51 -07002530static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002531 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002532 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002533
Karl Schultze4dc75c2016-02-02 15:37:51 -07002534// Create a WSI surface for the window:
Ian Elliottaaae5352015-07-06 14:27:58 -06002535#ifdef _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002536 VkWin32SurfaceCreateInfoKHR createInfo;
2537 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2538 createInfo.pNext = NULL;
2539 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002540 createInfo.hinstance = demo->connection;
2541 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002542
Karl Schultze4dc75c2016-02-02 15:37:51 -07002543 err =
2544 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002545
Ian Elliottaaae5352015-07-06 14:27:58 -06002546#else // _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002547 VkXcbSurfaceCreateInfoKHR createInfo;
2548 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2549 createInfo.pNext = NULL;
2550 createInfo.flags = 0;
2551 createInfo.connection = demo->connection;
2552 createInfo.window = demo->window;
2553
Karl Schultze4dc75c2016-02-02 15:37:51 -07002554 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottaaae5352015-07-06 14:27:58 -06002555#endif // _WIN32
2556
Tony Barbourcc69f452015-10-08 13:59:42 -06002557 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002558 VkBool32 *supportsPresent =
2559 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002560 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002561 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002562 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002563 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002564
2565 // Search for a graphics and a present queue in the array of queue
2566 // families, try to find one that supports both
2567 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002568 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002569 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002570 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2571 if (graphicsQueueNodeIndex == UINT32_MAX) {
2572 graphicsQueueNodeIndex = i;
2573 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002574
Ian Elliottaaae5352015-07-06 14:27:58 -06002575 if (supportsPresent[i] == VK_TRUE) {
2576 graphicsQueueNodeIndex = i;
2577 presentQueueNodeIndex = i;
2578 break;
2579 }
2580 }
2581 }
2582 if (presentQueueNodeIndex == UINT32_MAX) {
2583 // If didn't find a queue that supports both graphics and present, then
2584 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002585 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002586 if (supportsPresent[i] == VK_TRUE) {
2587 presentQueueNodeIndex = i;
2588 break;
2589 }
2590 }
2591 }
2592 free(supportsPresent);
2593
2594 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002595 if (graphicsQueueNodeIndex == UINT32_MAX ||
2596 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002597 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002598 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002599 }
2600
2601 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002602 // synchronization, and appropriate tracking for QueueSubmit.
2603 // NOTE: While it is possible for an application to use a separate graphics
2604 // and a present queues, this demo program assumes it is only using
2605 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002606 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2607 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002608 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002609 }
2610
2611 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002612
Tony Barbourda2bad52016-01-22 14:36:40 -07002613 demo_create_device(demo);
2614
2615 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2616 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2617 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2618 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2619 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2620
Karl Schultze4dc75c2016-02-02 15:37:51 -07002621 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2622 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002623
Ian Elliottaaae5352015-07-06 14:27:58 -06002624 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002625 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002626 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002627 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002628 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002629 VkSurfaceFormatKHR *surfFormats =
2630 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002631 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002632 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002633 assert(!err);
2634 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2635 // the surface has no preferred format. Otherwise, at least one
2636 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002637 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002638 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002639 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002640 assert(formatCount >= 1);
2641 demo->format = surfFormats[0].format;
2642 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002643 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002644
David Pinedo2bb7c932015-06-18 17:03:14 -06002645 demo->quit = false;
2646 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002647
2648 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002649 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002650}
2651
Karl Schultze4dc75c2016-02-02 15:37:51 -07002652static void demo_init_connection(struct demo *demo) {
Ian Elliott639ca472015-04-16 15:23:05 -06002653#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002654 const xcb_setup_t *setup;
2655 xcb_screen_iterator_t iter;
2656 int scr;
2657
2658 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002659 if (demo->connection == NULL) {
2660 printf("Cannot find a compatible Vulkan installable client driver "
2661 "(ICD).\nExiting ...\n");
2662 fflush(stdout);
2663 exit(1);
2664 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002665
2666 setup = xcb_get_setup(demo->connection);
2667 iter = xcb_setup_roots_iterator(setup);
2668 while (scr-- > 0)
2669 xcb_screen_next(&iter);
2670
2671 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002672#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002673}
2674
Karl Schultze4dc75c2016-02-02 15:37:51 -07002675static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002676 vec3 eye = {0.0f, 3.0f, 5.0f};
2677 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002678 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002679
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002680 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002681 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002682
Piers Daniell735ee532015-02-23 16:23:13 -07002683 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002684 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002685 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002686 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002687 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002688 if (strcmp(argv[i], "--break") == 0) {
2689 demo->use_break = true;
2690 continue;
2691 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002692 if (strcmp(argv[i], "--validate") == 0) {
2693 demo->validate = true;
2694 continue;
2695 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002696 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2697 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2698 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002699 i++;
2700 continue;
2701 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002702
Karl Schultze4dc75c2016-02-02 15:37:51 -07002703 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
2704 "[--c <framecount>]\n",
2705 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002706 fflush(stderr);
2707 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002708 }
2709
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002710 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002711 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002712
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002713 demo->width = 500;
2714 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002715
2716 demo->spin_angle = 0.01f;
2717 demo->spin_increment = 0.01f;
2718 demo->pause = false;
2719
Karl Schultze4dc75c2016-02-02 15:37:51 -07002720 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
2721 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002722 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2723 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002724}
2725
Ian Elliott639ca472015-04-16 15:23:05 -06002726#ifdef _WIN32
Mark Young418b9d12016-01-06 14:26:04 -07002727// Include header required for parsing the command line options.
2728#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002729
Karl Schultze4dc75c2016-02-02 15:37:51 -07002730int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
2731 int nCmdShow) {
2732 MSG msg; // message
2733 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002734 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002735 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06002736
Karl Schultze4dc75c2016-02-02 15:37:51 -07002737 // Use the CommandLine functions to get the command line arguments.
2738 // Unfortunately, Microsoft outputs
2739 // this information as wide characters for Unicode, and we simply want the
2740 // Ascii version to be compatible
2741 // with the non-Windows side. So, we have to convert the information to
2742 // Ascii character strings.
2743 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
2744 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07002745 argc = 0;
2746 }
2747
Karl Schultze4dc75c2016-02-02 15:37:51 -07002748 if (argc > 0) {
2749 argv = (char **)malloc(sizeof(char *) * argc);
2750 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002751 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002752 } else {
2753 for (int iii = 0; iii < argc; iii++) {
2754 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07002755 size_t numConverted = 0;
2756
Karl Schultze4dc75c2016-02-02 15:37:51 -07002757 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
2758 if (argv[iii] != NULL) {
2759 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
2760 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07002761 }
2762 }
2763 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002764 } else {
Mark Young418b9d12016-01-06 14:26:04 -07002765 argv = NULL;
2766 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002767
2768 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07002769
2770 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002771 if (argc > 0 && argv != NULL) {
2772 for (int iii = 0; iii < argc; iii++) {
2773 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002774 free(argv[iii]);
2775 }
2776 }
2777 free(argv);
2778 }
2779
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002780 demo.connection = hInstance;
2781 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002782 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002783 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002784
2785 demo_prepare(&demo);
2786
Karl Schultze4dc75c2016-02-02 15:37:51 -07002787 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07002788
2789 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07002790 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002791 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002792 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06002793 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002794 done = true; // if found, quit app
2795 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06002796 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07002797 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06002798 DispatchMessage(&msg);
2799 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002800 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002801 }
2802
2803 demo_cleanup(&demo);
2804
Karl Schultze4dc75c2016-02-02 15:37:51 -07002805 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002806}
2807#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002808int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002809 struct demo demo;
2810
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002811 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002812 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002813 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002814
2815 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002816 demo_run(&demo);
2817
2818 demo_cleanup(&demo);
2819
Karl Schultz0218c002016-03-22 17:06:13 -06002820 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002821}
Ian Elliott639ca472015-04-16 15:23:05 -06002822#endif // _WIN32