blob: e85747c8ff6493cb7ab7423419537d504bdd6f70 [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
Karl Schultze4dc75c2016-02-02 15:37:51 -07002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
Ian Elliotta748eaf2015-04-28 15:50:36 -06005 *
Karl Schultze4dc75c2016-02-02 15:37:51 -07006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
Ian Elliotta748eaf2015-04-28 15:50:36 -060012 *
Karl Schultze4dc75c2016-02-02 15:37:51 -070013 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
Ian Elliotta748eaf2015-04-28 15:50:36 -060015 *
Karl Schultze4dc75c2016-02-02 15:37:51 -070016 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Ian Elliotta748eaf2015-04-28 15:50:36 -060017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultze4dc75c2016-02-02 15:37:51 -070018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
Courtney Goeltzenleuchter37328982015-10-30 11:14:30 -060024 *
25 * Author: Chia-I Wu <olv@lunarg.com>
26 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
27 * Author: Ian Elliott <ian@LunarG.com>
28 * Author: Jon Ashburn <jon@lunarg.com>
Ian Elliotta748eaf2015-04-28 15:50:36 -060029 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070030
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060031#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060032#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <stdbool.h>
36#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070037#include <signal.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060038
Ian Elliott639ca472015-04-16 15:23:05 -060039#ifdef _WIN32
40#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060041#define APP_NAME_STR_LEN 80
Ian Elliott639ca472015-04-16 15:23:05 -060042#endif // _WIN32
43
David Pinedoc5193c12015-11-06 12:54:48 -070044#include <vulkan/vulkan.h>
Ian Elliottb08e0d92015-11-20 11:55:46 -070045
David Pinedo541526f2015-11-24 09:00:24 -070046#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047#include "linmath.h"
48
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060049#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbourfdc2d352015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliott07264132015-04-28 11:35:02 -060061#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -070062#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060067
Karl Schultze4dc75c2016-02-02 15:37:51 -070068#else // _WIN32
Ian Elliott07264132015-04-28 11:35:02 -060069
Karl Schultze4dc75c2016-02-02 15:37:51 -070070#define ERR_EXIT(err_msg, err_class) \
71 do { \
72 printf(err_msg); \
73 fflush(stdout); \
74 exit(1); \
75 } while (0)
Ian Elliott07264132015-04-28 11:35:02 -060076#endif // _WIN32
77
Karl Schultze4dc75c2016-02-02 15:37:51 -070078#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
79 { \
80 demo->fp##entrypoint = \
81 (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
82 if (demo->fp##entrypoint == NULL) { \
83 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \
84 "vkGetInstanceProcAddr Failure"); \
85 } \
86 }
Ian Elliottaaae5352015-07-06 14:27:58 -060087
Jon Ashburn7d8342c2015-10-08 15:58:23 -060088static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
89
Karl Schultze4dc75c2016-02-02 15:37:51 -070090#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
91 { \
92 if (!g_gdpa) \
93 g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \
94 demo->inst, "vkGetDeviceProcAddr"); \
95 demo->fp##entrypoint = \
96 (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
97 if (demo->fp##entrypoint == NULL) { \
98 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \
99 "vkGetDeviceProcAddr Failure"); \
100 } \
101 }
Ian Elliott673898b2015-06-22 15:07:49 -0600102
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600103/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700104 * structure to track all objects related to a texture.
105 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600106struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600107 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700108
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600109 VkImage image;
110 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600111
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800112 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500113 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600114 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700115 int32_t tex_width, tex_height;
116};
117
Karl Schultze4dc75c2016-02-02 15:37:51 -0700118static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600119
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600120struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600121 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700122 float mvp[4][4];
123 float position[12 * 3][4];
124 float color[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600125};
126
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600127struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600128 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700129 float mvp[4][4];
130 float position[12 * 3][4];
131 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600132};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600133
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600134//--------------------------------------------------------------------------------------
135// Mesh and VertexFormat Data
136//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700137// clang-format off
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600138struct Vertex
139{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600140 float posX, posY, posZ, posW; // Position data
141 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600142};
143
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600144struct VertexPosTex
145{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600146 float posX, posY, posZ, posW; // Position data
147 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600148};
149
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600150#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600151#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600152
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600153static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800154 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600155 -1.0f,-1.0f, 1.0f,
156 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800157 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600158 -1.0f, 1.0f,-1.0f,
159 -1.0f,-1.0f,-1.0f,
160
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800161 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 1.0f, 1.0f,-1.0f,
163 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800164 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600166 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800168 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600170 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800171 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600173 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800175 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176 -1.0f, 1.0f, 1.0f,
177 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800178 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600180 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800182 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183 1.0f, 1.0f, 1.0f,
184 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800185 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600186 1.0f,-1.0f,-1.0f,
187 1.0f, 1.0f,-1.0f,
188
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800189 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600191 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800192 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600193 1.0f,-1.0f, 1.0f,
194 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600195};
196
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600197static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 0.0f, 0.0f, // -X side
199 1.0f, 0.0f,
200 1.0f, 1.0f,
201 1.0f, 1.0f,
202 0.0f, 1.0f,
203 0.0f, 0.0f,
204
205 1.0f, 0.0f, // -Z side
206 0.0f, 1.0f,
207 0.0f, 0.0f,
208 1.0f, 0.0f,
209 1.0f, 1.0f,
210 0.0f, 1.0f,
211
212 1.0f, 1.0f, // -Y side
213 1.0f, 0.0f,
214 0.0f, 0.0f,
215 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600216 0.0f, 0.0f,
217 0.0f, 1.0f,
218
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800219 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600220 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800221 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600222 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600224 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800226 1.0f, 1.0f, // +X side
227 0.0f, 1.0f,
228 0.0f, 0.0f,
229 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600230 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600231 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 0.0f, 1.0f, // +Z side
234 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600235 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600236 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800237 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600238 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600239};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700240// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600241
Karl Schultze4dc75c2016-02-02 15:37:51 -0700242void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600243 int i;
244
245 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700246 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600247 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
248 }
249 printf("\n");
250 fflush(stdout);
251}
252
Karl Schultze4dc75c2016-02-02 15:37:51 -0700253void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600254 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700255 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600256 printf("\n");
257 fflush(stdout);
258}
259
Karl Schultze4dc75c2016-02-02 15:37:51 -0700260VKAPI_ATTR VkBool32 VKAPI_CALL
261dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
262 uint64_t srcObject, size_t location, int32_t msgCode,
263 const char *pLayerPrefix, const char *pMsg, void *pUserData) {
264 char *message = (char *)malloc(strlen(pMsg) + 100);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600265
Karl Schultze4dc75c2016-02-02 15:37:51 -0700266 assert(message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600267
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700268 if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700269 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode,
270 pMsg);
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -0700271 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700272 // We know that we're submitting queues without fences, ignore this
273 // warning
274 if (strstr(pMsg,
275 "vkQueueSubmit parameter, VkFence fence, is null pointer")) {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600276 return false;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600277 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700278 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode,
279 pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600280 } else {
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600281 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600282 }
283
284#ifdef _WIN32
285 MessageBox(NULL, message, "Alert", MB_OK);
286#else
Karl Schultze4dc75c2016-02-02 15:37:51 -0700287 printf("%s\n", message);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600288 fflush(stdout);
289#endif
290 free(message);
Courtney Goeltzenleuchterab328192015-09-04 13:52:24 -0600291
Courtney Goeltzenleuchter8cda44e2015-09-08 16:57:22 -0600292 /*
293 * false indicates that layer should not bail-out of an
294 * API call that had validation failures. This may mean that the
295 * app dies inside the driver due to invalid parameter(s).
296 * That's what would happen without validation layers, so we'll
297 * keep that behavior here.
298 */
299 return false;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600300}
301
Karl Schultze4dc75c2016-02-02 15:37:51 -0700302VkBool32 BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType,
303 uint64_t srcObject, size_t location, int32_t msgCode,
304 const char *pLayerPrefix, const char *pMsg,
305 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700306#ifndef WIN32
307 raise(SIGTRAP);
308#else
309 DebugBreak();
310#endif
311
312 return false;
313}
314
Ian Elliotta0781972015-08-21 15:09:33 -0600315typedef struct _SwapchainBuffers {
Ian Elliottaaae5352015-07-06 14:27:58 -0600316 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800317 VkCommandBuffer cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600318 VkImageView view;
Ian Elliotta0781972015-08-21 15:09:33 -0600319} SwapchainBuffers;
Ian Elliottaaae5352015-07-06 14:27:58 -0600320
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600322#ifdef _WIN32
323#define APP_NAME_STR_LEN 80
324 HINSTANCE connection; // hInstance - Windows Instance
325 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
Karl Schultze4dc75c2016-02-02 15:37:51 -0700326 HWND window; // hWnd - window handle
327#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600328 xcb_connection_t *connection;
329 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800330 xcb_window_t window;
331 xcb_intern_atom_reply_t *atom_wm_delete_window;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700332#endif // _WIN32
Ian Elliottb08e0d92015-11-20 11:55:46 -0700333 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600334 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700335 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600337 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600338 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600339 VkDevice device;
340 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700341 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600342 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600343 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600344 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600345
Tony Barbourda2bad52016-01-22 14:36:40 -0700346 uint32_t enabled_extension_count;
347 uint32_t enabled_layer_count;
348 char *extension_names[64];
349 char *device_validation_layers[64];
350
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600352 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600353 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
Karl Schultze4dc75c2016-02-02 15:37:51 -0700355 PFN_vkGetPhysicalDeviceSurfaceSupportKHR
356 fpGetPhysicalDeviceSurfaceSupportKHR;
357 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR
358 fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
359 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR
360 fpGetPhysicalDeviceSurfaceFormatsKHR;
361 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR
362 fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600363 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
364 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
365 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
366 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
367 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600368 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600369 VkSwapchainKHR swapchain;
Ian Elliotta0781972015-08-21 15:09:33 -0600370 SwapchainBuffers *buffers;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800372 VkCommandPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
374 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600375 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600377 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800378 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500379 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600380 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600381 } depth;
382
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600383 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600384
385 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600386 VkBuffer buf;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800387 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500388 VkDeviceMemory mem;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -0600389 VkDescriptorBufferInfo buffer_info;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600390 } uniform_data;
391
Karl Schultze4dc75c2016-02-02 15:37:51 -0700392 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500393 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600394 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600395 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800396 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600397 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600398
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600399 mat4x4 projection_matrix;
400 mat4x4 view_matrix;
401 mat4x4 model_matrix;
402
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600403 float spin_angle;
404 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600405 bool pause;
406
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600407 VkShaderModule vert_shader_module;
408 VkShaderModule frag_shader_module;
409
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600410 VkDescriptorPool desc_pool;
411 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800412
Tony Barbourd8e504f2015-10-08 14:26:24 -0600413 VkFramebuffer *framebuffers;
Chia-I Wuf973e322015-07-08 13:34:24 +0800414
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600415 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600416 int32_t curFrame;
417 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600418 bool validate;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600419 bool use_break;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700420 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
421 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
422 VkDebugReportCallbackEXT msg_callback;
423 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600424
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600425 uint32_t current_buffer;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -0600426 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600427};
428
Ian Elliottcf074d32015-10-16 18:02:43 -0600429// Forward declaration:
430static void demo_resize(struct demo *demo);
431
Karl Schultze4dc75c2016-02-02 15:37:51 -0700432static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
433 VkFlags requirements_mask,
434 uint32_t *typeIndex) {
435 // Search memtypes to find first index with those properties
436 for (uint32_t i = 0; i < 32; i++) {
437 if ((typeBits & 1) == 1) {
438 // Type is available, does it match user properties?
439 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
440 requirements_mask) == requirements_mask) {
441 *typeIndex = i;
442 return true;
443 }
444 }
445 typeBits >>= 1;
446 }
447 // No memory types matched, return failure
448 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600449}
450
Karl Schultze4dc75c2016-02-02 15:37:51 -0700451static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600452 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600453
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600454 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600455 return;
456
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600457 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458 assert(!err);
459
Karl Schultze4dc75c2016-02-02 15:37:51 -0700460 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800461 VkFence nullFence = VK_NULL_HANDLE;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700462 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
463 .pNext = NULL,
464 .waitSemaphoreCount = 0,
465 .pWaitSemaphores = NULL,
466 .pWaitDstStageMask = NULL,
467 .commandBufferCount = 1,
468 .pCommandBuffers = cmd_bufs,
469 .signalSemaphoreCount = 0,
470 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600472 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473 assert(!err);
474
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600475 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476 assert(!err);
477
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600478 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600479 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600480}
481
Karl Schultze4dc75c2016-02-02 15:37:51 -0700482static void demo_set_image_layout(struct demo *demo, VkImage image,
483 VkImageAspectFlags aspectMask,
484 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700485 VkImageLayout new_image_layout,
486 VkAccessFlagBits srcAccessMask) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600487 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600488
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 if (demo->cmd == VK_NULL_HANDLE) {
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800490 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +0800491 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600492 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800493 .commandPool = demo->cmd_pool,
494 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700495 .commandBufferCount = 1,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600496 };
497
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800498 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600499 assert(!err);
500
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700501 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
502 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600503 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800504 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600505 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800506 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800507 .occlusionQueryEnable = VK_FALSE,
508 .queryFlags = 0,
509 .pipelineStatistics = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600510 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700511 VkCommandBufferBeginInfo cmd_buf_info = {
512 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
513 .pNext = NULL,
514 .flags = 0,
515 .pInheritanceInfo = &cmd_buf_hinfo,
516 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600517 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600518 assert(!err);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600519 }
520
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600521 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600522 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600523 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700524 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800525 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600526 .oldLayout = old_image_layout,
527 .newLayout = new_image_layout,
528 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700529 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600530
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800531 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600532 /* Make sure anything that was copying from this image has completed */
Chia-I Wu19574622015-10-27 19:54:37 +0800533 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600534 }
535
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700536 if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700537 image_memory_barrier.dstAccessMask =
538 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700539 }
540
541 if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700542 image_memory_barrier.dstAccessMask =
543 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700544 }
545
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600546 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600547 /* Make sure any Copy or CPU writes to image are flushed */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700548 image_memory_barrier.dstAccessMask =
549 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600550 }
551
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600552 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600553
Tony Barbour50bbca42015-06-29 16:20:35 -0600554 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
555 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600556
Karl Schultze4dc75c2016-02-02 15:37:51 -0700557 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
558 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600559}
560
Karl Schultze4dc75c2016-02-02 15:37:51 -0700561static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700562 VkCommandBufferInheritanceInfo cmd_buf_hinfo = {
563 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600564 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800565 .renderPass = VK_NULL_HANDLE,
Cody Northropa0a178c2015-08-11 11:35:58 -0600566 .subpass = 0,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800567 .framebuffer = VK_NULL_HANDLE,
Chia-I Wu4ac4a8b2015-11-11 10:18:12 +0800568 .occlusionQueryEnable = VK_FALSE,
569 .queryFlags = 0,
570 .pipelineStatistics = 0,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700571 };
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700572 const VkCommandBufferBeginInfo cmd_buf_info = {
573 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
574 .pNext = NULL,
575 .flags = 0,
576 .pInheritanceInfo = &cmd_buf_hinfo,
577 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800578 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700579 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
580 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800581 };
582 const VkRenderPassBeginInfo rp_begin = {
583 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
584 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800585 .renderPass = demo->render_pass,
586 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800587 .renderArea.offset.x = 0,
588 .renderArea.offset.y = 0,
589 .renderArea.extent.width = demo->width,
590 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600591 .clearValueCount = 2,
592 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700593 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800594 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600595
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600596 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600597 assert(!err);
598
Chia-I Wuf051d262015-10-27 19:25:11 +0800599 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Chia-I Wua74c5b22015-07-07 11:50:03 +0800600
Karl Schultze4dc75c2016-02-02 15:37:51 -0700601 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
602 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
603 demo->pipeline_layout, 0, 1, &demo->desc_set, 0,
604 NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600605
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600606 VkViewport viewport;
607 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700608 viewport.height = (float)demo->height;
609 viewport.width = (float)demo->width;
610 viewport.minDepth = (float)0.0f;
611 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700612 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600613
614 VkRect2D scissor;
615 memset(&scissor, 0, sizeof(scissor));
616 scissor.extent.width = demo->width;
617 scissor.extent.height = demo->height;
618 scissor.offset.x = 0;
619 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700620 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600621
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600622 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800623 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624
Tony Barbour15a4ef62015-10-21 10:14:48 -0600625 VkImageMemoryBarrier prePresentBarrier = {
626 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
627 .pNext = NULL,
Chia-I Wu19574622015-10-27 19:54:37 +0800628 .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Tony Barboure5af5392016-01-05 09:59:05 -0700629 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600630 .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Ian Elliottf4a4e442015-11-17 17:29:40 -0700631 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tony Barbour15a4ef62015-10-21 10:14:48 -0600632 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800633 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700634 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour15a4ef62015-10-21 10:14:48 -0600635
636 prePresentBarrier.image = demo->buffers[demo->current_buffer].image;
637 VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700638 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
639 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
640 NULL, 1, pmemory_barrier);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600641
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600642 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643 assert(!err);
644}
645
Karl Schultze4dc75c2016-02-02 15:37:51 -0700646void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600647 mat4x4 MVP, Model, VP;
648 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600649 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600650 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600651
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600652 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600654 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600655 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700656 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
657 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600658 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600659
Karl Schultze4dc75c2016-02-02 15:37:51 -0700660 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
661 demo->uniform_data.mem_alloc.allocationSize, 0,
662 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600663 assert(!err);
664
Karl Schultze4dc75c2016-02-02 15:37:51 -0700665 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600666
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -0600667 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600668}
669
Karl Schultze4dc75c2016-02-02 15:37:51 -0700670static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600671 VkResult U_ASSERT_ONLY err;
Ian Elliottaaae5352015-07-06 14:27:58 -0600672 VkSemaphore presentCompleteSemaphore;
673 VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {
674 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
675 .pNext = NULL,
Mark Lobodzinskie7ba7f12015-10-08 10:44:07 -0600676 .flags = 0,
Ian Elliottaaae5352015-07-06 14:27:58 -0600677 };
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800678 VkFence nullFence = VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600679
Karl Schultze4dc75c2016-02-02 15:37:51 -0700680 err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo,
681 NULL, &presentCompleteSemaphore);
Ian Elliottaaae5352015-07-06 14:27:58 -0600682 assert(!err);
683
684 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700685 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Ian Elliottaaae5352015-07-06 14:27:58 -0600686 presentCompleteSemaphore,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700687 (VkFence)0, // TODO: Show use of fence
Ian Elliottaaae5352015-07-06 14:27:58 -0600688 &demo->current_buffer);
Ian Elliottcf074d32015-10-16 18:02:43 -0600689 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
690 // demo->swapchain is out of date (e.g. the window was resized) and
691 // must be recreated:
692 demo_resize(demo);
693 demo_draw(demo);
Chia-I Wu63636062015-10-26 21:10:41 +0800694 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600695 return;
696 } else if (err == VK_SUBOPTIMAL_KHR) {
697 // demo->swapchain is not as optimal as it could be, but the platform's
698 // presentation engine will still present the image correctly.
699 } else {
700 assert(!err);
701 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600702
Tony Barbour15a4ef62015-10-21 10:14:48 -0600703 // Assume the command buffer has been run on current_buffer before so
704 // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL
705 demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700706 VK_IMAGE_ASPECT_COLOR_BIT,
707 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700708 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
709 0);
Tony Barbour15a4ef62015-10-21 10:14:48 -0600710 demo_flush_init_cmd(demo);
711
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600712 // Wait for the present complete semaphore to be signaled to ensure
713 // that the image won't be rendered to until the presentation
714 // engine has fully released ownership to the application, and it is
715 // okay to render to the image.
716
Karl Schultze4dc75c2016-02-02 15:37:51 -0700717 // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
718 VkPipelineStageFlags pipe_stage_flags =
719 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
720 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
721 .pNext = NULL,
722 .waitSemaphoreCount = 1,
723 .pWaitSemaphores = &presentCompleteSemaphore,
724 .pWaitDstStageMask = &pipe_stage_flags,
725 .commandBufferCount = 1,
726 .pCommandBuffers =
727 &demo->buffers[demo->current_buffer].cmd,
728 .signalSemaphoreCount = 0,
729 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600730
731 err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600732 assert(!err);
733
Ian Elliotta0781972015-08-21 15:09:33 -0600734 VkPresentInfoKHR present = {
735 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600736 .pNext = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600737 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700738 .pSwapchains = &demo->swapchain,
739 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600740 };
741
Karl Schultze4dc75c2016-02-02 15:37:51 -0700742 // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER?
Ian Elliotta0781972015-08-21 15:09:33 -0600743 err = demo->fpQueuePresentKHR(demo->queue, &present);
Ian Elliottcf074d32015-10-16 18:02:43 -0600744 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
745 // demo->swapchain is out of date (e.g. the window was resized) and
746 // must be recreated:
747 demo_resize(demo);
748 } else if (err == VK_SUBOPTIMAL_KHR) {
749 // demo->swapchain is not as optimal as it could be, but the platform's
750 // presentation engine will still present the image correctly.
751 } else {
752 assert(!err);
753 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600754
Chia-I Wucbb564e2015-04-16 22:02:10 +0800755 err = vkQueueWaitIdle(demo->queue);
756 assert(err == VK_SUCCESS);
Mike Stroyan15fed6b2015-08-28 10:58:06 -0600757
Chia-I Wu63636062015-10-26 21:10:41 +0800758 vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600759}
760
Karl Schultze4dc75c2016-02-02 15:37:51 -0700761static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600762 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -0600763 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -0600764
Ian Elliottb08e0d92015-11-20 11:55:46 -0700765 // Check the surface capabilities and formats
766 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700767 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
768 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -0600769 assert(!err);
770
Ian Elliott8528eff2015-08-07 15:56:59 -0600771 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700772 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
773 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600774 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -0600775 VkPresentModeKHR *presentModes =
776 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -0600777 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700778 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
779 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -0600780 assert(!err);
781
Ian Elliotta0781972015-08-21 15:09:33 -0600782 VkExtent2D swapchainExtent;
Ian Elliottaaae5352015-07-06 14:27:58 -0600783 // width and height are either both -1, or both not -1.
Karl Schultze4dc75c2016-02-02 15:37:51 -0700784 if (surfCapabilities.currentExtent.width == (uint32_t)-1) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600785 // If the surface size is undefined, the size is set to
786 // the size of the images requested.
Ian Elliotta0781972015-08-21 15:09:33 -0600787 swapchainExtent.width = demo->width;
788 swapchainExtent.height = demo->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700789 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -0600790 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -0700791 swapchainExtent = surfCapabilities.currentExtent;
792 demo->width = surfCapabilities.currentExtent.width;
793 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -0600794 }
795
796 // If mailbox mode is available, use it, as is the lowest-latency non-
Ian Elliott3ebce342015-07-27 13:53:11 -0600797 // tearing mode. If not, try IMMEDIATE which will usually be available,
798 // and is fastest (though it tears). If not, fall back to FIFO which is
799 // always available.
Ian Elliotta0781972015-08-21 15:09:33 -0600800 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600801 for (size_t i = 0; i < presentModeCount; i++) {
Ian Elliotta0781972015-08-21 15:09:33 -0600802 if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
803 swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600804 break;
805 }
Ian Elliotta0781972015-08-21 15:09:33 -0600806 if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) &&
807 (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) {
808 swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Ian Elliott3ebce342015-07-27 13:53:11 -0600809 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600810 }
811
812 // Determine the number of VkImage's to use in the swap chain (we desire to
813 // own only 1 image at a time, besides the images being displayed and
814 // queued for display):
Karl Schultze4dc75c2016-02-02 15:37:51 -0700815 uint32_t desiredNumberOfSwapchainImages =
816 surfCapabilities.minImageCount + 1;
Ian Elliottb08e0d92015-11-20 11:55:46 -0700817 if ((surfCapabilities.maxImageCount > 0) &&
Karl Schultze4dc75c2016-02-02 15:37:51 -0700818 (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -0600819 // Application must settle for fewer images than desired:
Ian Elliottb08e0d92015-11-20 11:55:46 -0700820 desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -0600821 }
822
Tony Barbour9fd6d352015-09-16 15:01:32 -0600823 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700824 if (surfCapabilities.supportedTransforms &
825 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -0700826 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -0600827 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700828 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -0600829 }
830
Ian Elliottcf074d32015-10-16 18:02:43 -0600831 const VkSwapchainCreateInfoKHR swapchain = {
Ian Elliott5b97eae2015-09-22 10:20:23 -0600832 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800833 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700834 .surface = demo->surface,
Ian Elliotta0781972015-08-21 15:09:33 -0600835 .minImageCount = desiredNumberOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800836 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -0600837 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700838 .imageExtent =
839 {
840 .width = swapchainExtent.width, .height = swapchainExtent.height,
841 },
Ian Elliottb08e0d92015-11-20 11:55:46 -0700842 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -0600843 .preTransform = preTransform,
Ian Elliott86f29a82015-12-28 15:25:33 -0700844 .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700845 .imageArrayLayers = 1,
846 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
847 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -0600848 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -0600849 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -0600850 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -0600851 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600852 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600853 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600854
Karl Schultze4dc75c2016-02-02 15:37:51 -0700855 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL,
856 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800857 assert(!err);
858
Ian Elliottcf074d32015-10-16 18:02:43 -0600859 // If we just re-created an existing swapchain, we should destroy the old
860 // swapchain at this point.
861 // Note: destroying the swapchain also cleans up all its associated
862 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800863 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -0700864 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -0600865 }
866
867 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600868 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -0600869 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800870
Karl Schultze4dc75c2016-02-02 15:37:51 -0700871 VkImage *swapchainImages =
872 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -0600873 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -0600874 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -0600875 &demo->swapchainImageCount,
876 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -0600877 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -0600878
Karl Schultze4dc75c2016-02-02 15:37:51 -0700879 demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) *
880 demo->swapchainImageCount);
Ian Elliottaaae5352015-07-06 14:27:58 -0600881 assert(demo->buffers);
882
Ian Elliotta0781972015-08-21 15:09:33 -0600883 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600884 VkImageViewCreateInfo color_image_view = {
885 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600886 .pNext = NULL,
887 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700888 .components =
889 {
890 .r = VK_COMPONENT_SWIZZLE_R,
891 .g = VK_COMPONENT_SWIZZLE_G,
892 .b = VK_COMPONENT_SWIZZLE_B,
893 .a = VK_COMPONENT_SWIZZLE_A,
894 },
895 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
896 .baseMipLevel = 0,
897 .levelCount = 1,
898 .baseArrayLayer = 0,
899 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600900 .viewType = VK_IMAGE_VIEW_TYPE_2D,
901 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600902 };
903
Ian Elliotta0781972015-08-21 15:09:33 -0600904 demo->buffers[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600905
Karl Schultze4dc75c2016-02-02 15:37:51 -0700906 // Render loop will expect image to have been used before and in
907 // VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
908 // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image
909 // to that state
910 demo_set_image_layout(
911 demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700912 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
913 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600914
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600915 color_image_view.image = demo->buffers[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600916
Karl Schultze4dc75c2016-02-02 15:37:51 -0700917 err = vkCreateImageView(demo->device, &color_image_view, NULL,
918 &demo->buffers[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600919 assert(!err);
920 }
Karl Schultze4dc75c2016-02-02 15:37:51 -0700921
Mark Young04fd3d82016-01-25 14:43:50 -0700922 if (NULL != presentModes) {
923 free(presentModes);
924 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600925}
926
Karl Schultze4dc75c2016-02-02 15:37:51 -0700927static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -0600928 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600929 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600930 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600931 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600932 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600933 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700934 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600935 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -0600936 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +0800937 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -0600938 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -0600939 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600940 .flags = 0,
941 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200942
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600943 VkImageViewCreateInfo view = {
944 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600945 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +0800946 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -0600947 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700948 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
949 .baseMipLevel = 0,
950 .levelCount = 1,
951 .baseArrayLayer = 0,
952 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600953 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600954 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600955 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600956
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500957 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600958 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600959 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600960
961 demo->depth.format = depth_format;
962
963 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700964 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600965 assert(!err);
966
Karl Schultze4dc75c2016-02-02 15:37:51 -0700967 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -0600968 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600969
Chia-I Wuf48700b2015-11-10 16:21:09 +0800970 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +0200971 demo->depth.mem_alloc.pNext = NULL;
972 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
973 demo->depth.mem_alloc.memoryTypeIndex = 0;
974
Karl Schultze4dc75c2016-02-02 15:37:51 -0700975 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
976 0, /* No requirements */
977 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -0600978 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600979
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500980 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700981 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
982 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500983 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600984
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500985 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -0700986 err =
987 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500988 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600989
Karl Schultze4dc75c2016-02-02 15:37:51 -0700990 demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT,
991 VK_IMAGE_LAYOUT_UNDEFINED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700992 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
993 0);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600994
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600995 /* create image view */
996 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +0800997 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600998 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600999}
1000
Tony Barbour1a86d032015-09-21 15:17:33 -06001001/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001002bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001003 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
1004 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001005 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001006
Tony Barbour1a86d032015-09-21 15:17:33 -06001007 if (!fPtr)
1008 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001009
Tony Barbour1a86d032015-09-21 15:17:33 -06001010 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001011 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1012 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001013 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001014 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001015
Tony Barbour1a86d032015-09-21 15:17:33 -06001016 do {
1017 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001018 if (cPtr == NULL) {
1019 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001020 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001021 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001022 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001023
Tony Barbour1a86d032015-09-21 15:17:33 -06001024 sscanf(header, "%u %u", height, width);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001025 if (rgba_data == NULL) {
1026 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001027 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001028 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001029 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001030 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001031 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1032 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001033 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001034 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001035
Karl Schultze4dc75c2016-02-02 15:37:51 -07001036 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001037 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001038 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001039 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001040 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001041 rowPtr[3] = 255; /* Alpha of 1 */
1042 rowPtr += 4;
1043 }
1044 rgba_data += layout->rowPitch;
1045 }
1046 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001047 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001048}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001049
Karl Schultze4dc75c2016-02-02 15:37:51 -07001050static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001051 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001052 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001053 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001054 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001055 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001056 int32_t tex_width;
1057 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001058 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001059 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001060
Karl Schultze4dc75c2016-02-02 15:37:51 -07001061 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
David Pinedo30bd71d2015-04-23 08:16:57 -06001062 printf("Failed to load textures\n");
1063 fflush(stdout);
1064 exit(1);
1065 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001067 tex_obj->tex_width = tex_width;
1068 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001069
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001070 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001071 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001072 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001073 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001074 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001075 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001076 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001077 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001078 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001079 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001080 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001081 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001082 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001083 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001084
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001085 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086
Karl Schultze4dc75c2016-02-02 15:37:51 -07001087 err =
1088 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001089 assert(!err);
1090
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001091 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001092
Chia-I Wuf48700b2015-11-10 16:21:09 +08001093 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001094 tex_obj->mem_alloc.pNext = NULL;
1095 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1096 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001097
Karl Schultze4dc75c2016-02-02 15:37:51 -07001098 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1099 required_props,
1100 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001101 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001102
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001103 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001104 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001105 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001106 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001107
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001108 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001109 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001110 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001112 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001113 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001114 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001115 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001116 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001117 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001118 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119 void *data;
1120
Karl Schultze4dc75c2016-02-02 15:37:51 -07001121 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1122 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001123
Karl Schultze4dc75c2016-02-02 15:37:51 -07001124 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1125 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001126 assert(!err);
1127
1128 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1129 fprintf(stderr, "Error loading texture: %s\n", filename);
1130 }
1131
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001132 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001133 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001134
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001135 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001136 demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001137 VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout,
1138 VK_ACCESS_HOST_WRITE_BIT);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001139 /* setting the image layout does not reference the actual memory so no need
1140 * to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001141}
1142
Karl Schultze4dc75c2016-02-02 15:37:51 -07001143static void demo_destroy_texture_image(struct demo *demo,
1144 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001145 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001146 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1147 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001148}
1149
Karl Schultze4dc75c2016-02-02 15:37:51 -07001150static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001151 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001152 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001153 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001154
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001155 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001156
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001157 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001158 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001159
Karl Schultze4dc75c2016-02-02 15:37:51 -07001160 if ((props.linearTilingFeatures &
1161 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1162 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001163 /* Device can texture using linear textures */
1164 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Karl Schultze4dc75c2016-02-02 15:37:51 -07001165 VK_IMAGE_TILING_LINEAR,
1166 VK_IMAGE_USAGE_SAMPLED_BIT,
1167 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
1168 } else if (props.optimalTilingFeatures &
1169 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001170 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001171 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001172
1173 memset(&staging_texture, 0, sizeof(staging_texture));
1174 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001175 VK_IMAGE_TILING_LINEAR,
1176 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1177 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001178
Karl Schultze4dc75c2016-02-02 15:37:51 -07001179 demo_prepare_texture_image(
1180 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1181 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1182 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001183
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001184 demo_set_image_layout(demo, staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001185 VK_IMAGE_ASPECT_COLOR_BIT,
1186 staging_texture.imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001187 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1188 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001189
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001190 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001191 VK_IMAGE_ASPECT_COLOR_BIT,
1192 demo->textures[i].imageLayout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001193 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1194 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001195
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001196 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001197 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1198 .srcOffset = {0, 0, 0},
1199 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1200 .dstOffset = {0, 0, 0},
1201 .extent = {staging_texture.tex_width,
1202 staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001203 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001204 vkCmdCopyImage(
1205 demo->cmd, staging_texture.image,
1206 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1207 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001208
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001209 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001210 VK_IMAGE_ASPECT_COLOR_BIT,
1211 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001212 demo->textures[i].imageLayout,
1213 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001214
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001215 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001216
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001217 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001218 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001219 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1220 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001221 }
1222
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001223 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001224 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001225 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001226 .magFilter = VK_FILTER_NEAREST,
1227 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001228 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001229 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1230 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1231 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001233 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001234 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001235 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001236 .minLod = 0.0f,
1237 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001238 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001239 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001240 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001241
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001242 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001243 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001244 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001245 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001246 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001247 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001248 .components =
1249 {
1250 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1251 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1252 },
1253 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001254 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001255 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001256
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001257 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001258 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001259 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001260 assert(!err);
1261
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001262 /* create image view */
1263 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001264 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001265 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001266 assert(!err);
1267 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001268}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001269
Karl Schultze4dc75c2016-02-02 15:37:51 -07001270void demo_prepare_cube_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001271 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001272 VkMemoryRequirements mem_reqs;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001273 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001274 int i;
1275 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001276 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001277 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001278 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001279
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001280 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001281 mat4x4_mul(MVP, VP, demo->model_matrix);
1282 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001283 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001284
Karl Schultze4dc75c2016-02-02 15:37:51 -07001285 for (i = 0; i < 12 * 3; i++) {
1286 data.position[i][0] = g_vertex_buffer_data[i * 3];
1287 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1288 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001289 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001290 data.attr[i][0] = g_uv_buffer_data[2 * i];
1291 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001292 data.attr[i][2] = 0;
1293 data.attr[i][3] = 0;
1294 }
1295
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001296 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001297 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001298 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001299 buf_info.size = sizeof(data);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001300 err =
1301 vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001302 assert(!err);
1303
Karl Schultze4dc75c2016-02-02 15:37:51 -07001304 vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf,
1305 &mem_reqs);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001306
Chia-I Wuf48700b2015-11-10 16:21:09 +08001307 demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001308 demo->uniform_data.mem_alloc.pNext = NULL;
1309 demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size;
1310 demo->uniform_data.mem_alloc.memoryTypeIndex = 0;
1311
Karl Schultze4dc75c2016-02-02 15:37:51 -07001312 pass = memory_type_from_properties(
1313 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1314 &demo->uniform_data.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001315 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001316
Karl Schultze4dc75c2016-02-02 15:37:51 -07001317 err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL,
1318 &(demo->uniform_data.mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001319 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001320
Karl Schultze4dc75c2016-02-02 15:37:51 -07001321 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0,
1322 demo->uniform_data.mem_alloc.allocationSize, 0,
1323 (void **)&pData);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001324 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001325
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001326 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001327
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001328 vkUnmapMemory(demo->device, demo->uniform_data.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001329
Karl Schultze4dc75c2016-02-02 15:37:51 -07001330 err = vkBindBufferMemory(demo->device, demo->uniform_data.buf,
1331 demo->uniform_data.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001332 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001333
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001334 demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf;
1335 demo->uniform_data.buffer_info.offset = 0;
1336 demo->uniform_data.buffer_info.range = sizeof(data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001337}
1338
Karl Schultze4dc75c2016-02-02 15:37:51 -07001339static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001340 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001341 [0] =
1342 {
1343 .binding = 0,
1344 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1345 .descriptorCount = 1,
1346 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1347 .pImmutableSamplers = NULL,
1348 },
1349 [1] =
1350 {
1351 .binding = 1,
1352 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1353 .descriptorCount = DEMO_TEXTURE_COUNT,
1354 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1355 .pImmutableSamplers = NULL,
1356 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001357 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001358 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001359 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001360 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001361 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001362 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001363 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001364 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1367 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001368 assert(!err);
1369
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001370 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001371 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1372 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001373 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001374 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001375 };
1376
Karl Schultze4dc75c2016-02-02 15:37:51 -07001377 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001378 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001379 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380}
1381
Karl Schultze4dc75c2016-02-02 15:37:51 -07001382static void demo_prepare_render_pass(struct demo *demo) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001383 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001384 [0] =
1385 {
1386 .format = demo->format,
1387 .samples = VK_SAMPLE_COUNT_1_BIT,
1388 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1389 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1390 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1391 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1392 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1393 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1394 },
1395 [1] =
1396 {
1397 .format = demo->depth.format,
1398 .samples = VK_SAMPLE_COUNT_1_BIT,
1399 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1400 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1401 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1402 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1403 .initialLayout =
1404 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1405 .finalLayout =
1406 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1407 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001408 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001409 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001410 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001411 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001412 const VkAttachmentReference depth_reference = {
1413 .attachment = 1,
1414 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1415 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001416 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001417 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1418 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001419 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001420 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001421 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001422 .pColorAttachments = &color_reference,
1423 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001424 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001425 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001426 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001427 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001428 const VkRenderPassCreateInfo rp_info = {
1429 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1430 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001431 .attachmentCount = 2,
1432 .pAttachments = attachments,
1433 .subpassCount = 1,
1434 .pSubpasses = &subpass,
1435 .dependencyCount = 0,
1436 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001437 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001438 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001439
Chia-I Wu63636062015-10-26 21:10:41 +08001440 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001441 assert(!err);
1442}
1443
Karl Schultze4dc75c2016-02-02 15:37:51 -07001444static VkShaderModule
1445demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001446 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001447 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001448 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001449
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001450 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1451 moduleCreateInfo.pNext = NULL;
1452
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001453 moduleCreateInfo.codeSize = size;
1454 moduleCreateInfo.pCode = code;
1455 moduleCreateInfo.flags = 0;
1456 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1457 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001458
1459 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001460}
1461
Karl Schultze4dc75c2016-02-02 15:37:51 -07001462char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001463 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001464 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001465 void *shader_code;
1466
1467 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001468 if (!fp)
1469 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001470
1471 fseek(fp, 0L, SEEK_END);
1472 size = ftell(fp);
1473
1474 fseek(fp, 0L, SEEK_SET);
1475
1476 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001477 retval = fread(shader_code, size, 1, fp);
1478 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001479
1480 *psize = size;
1481
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001482 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001483 return shader_code;
1484}
1485
Karl Schultze4dc75c2016-02-02 15:37:51 -07001486static VkShaderModule demo_prepare_vs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001487 void *vertShaderCode;
1488 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001489
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001490 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
1491
Karl Schultze4dc75c2016-02-02 15:37:51 -07001492 demo->vert_shader_module =
1493 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001494
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001495 free(vertShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001496
1497 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001498}
1499
Karl Schultze4dc75c2016-02-02 15:37:51 -07001500static VkShaderModule demo_prepare_fs(struct demo *demo) {
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001501 void *fragShaderCode;
1502 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001503
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001504 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001505
Karl Schultze4dc75c2016-02-02 15:37:51 -07001506 demo->frag_shader_module =
1507 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001508
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001509 free(fragShaderCode);
Chia-I Wu46176f12015-10-31 00:31:16 +08001510
1511 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001512}
1513
Karl Schultze4dc75c2016-02-02 15:37:51 -07001514static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001515 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001516 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001517 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001518 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001519 VkPipelineRasterizationStateCreateInfo rs;
1520 VkPipelineColorBlendStateCreateInfo cb;
1521 VkPipelineDepthStencilStateCreateInfo ds;
1522 VkPipelineViewportStateCreateInfo vp;
1523 VkPipelineMultisampleStateCreateInfo ms;
1524 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1525 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001526 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001527
Piers Daniellba839d12015-09-29 13:01:09 -06001528 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1529 memset(&dynamicState, 0, sizeof dynamicState);
1530 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1531 dynamicState.pDynamicStates = dynamicStateEnables;
1532
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001533 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001534 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001535 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001536
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001537 memset(&vi, 0, sizeof(vi));
1538 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1539
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001540 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001541 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001542 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001543
1544 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001545 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1546 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001547 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001548 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001549 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001550 rs.rasterizerDiscardEnable = VK_FALSE;
1551 rs.depthBiasEnable = VK_FALSE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001552
1553 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001554 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1555 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001556 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001557 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001558 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001559 cb.attachmentCount = 1;
1560 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561
Tony Barbour29645d02015-01-16 14:27:35 -07001562 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001563 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001564 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001565 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1566 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001567 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001568 dynamicStateEnables[dynamicState.dynamicStateCount++] =
1569 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001570
1571 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001572 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001573 ds.depthTestEnable = VK_TRUE;
1574 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001575 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001576 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001577 ds.back.failOp = VK_STENCIL_OP_KEEP;
1578 ds.back.passOp = VK_STENCIL_OP_KEEP;
1579 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001580 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001581 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582
Tony Barbour29645d02015-01-16 14:27:35 -07001583 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001584 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001585 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001586 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001587
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001588 // Two stages: vs and fs
1589 pipeline.stageCount = 2;
1590 VkPipelineShaderStageCreateInfo shaderStages[2];
1591 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1592
Karl Schultze4dc75c2016-02-02 15:37:51 -07001593 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1594 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001595 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001596 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001597
Karl Schultze4dc75c2016-02-02 15:37:51 -07001598 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1599 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08001600 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001601 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001602
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001603 memset(&pipelineCache, 0, sizeof(pipelineCache));
1604 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001605
Karl Schultze4dc75c2016-02-02 15:37:51 -07001606 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
1607 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001608 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001609
Karl Schultze4dc75c2016-02-02 15:37:51 -07001610 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001611 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001612 pipeline.pRasterizationState = &rs;
1613 pipeline.pColorBlendState = &cb;
1614 pipeline.pMultisampleState = &ms;
1615 pipeline.pViewportState = &vp;
1616 pipeline.pDepthStencilState = &ds;
1617 pipeline.pStages = shaderStages;
1618 pipeline.renderPass = demo->render_pass;
1619 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001620
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001621 pipeline.renderPass = demo->render_pass;
1622
Karl Schultze4dc75c2016-02-02 15:37:51 -07001623 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
1624 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001625 assert(!err);
1626
Chia-I Wu63636062015-10-26 21:10:41 +08001627 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1628 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001629}
1630
Karl Schultze4dc75c2016-02-02 15:37:51 -07001631static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001632 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001633 [0] =
1634 {
1635 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1636 .descriptorCount = 1,
1637 },
1638 [1] =
1639 {
1640 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1641 .descriptorCount = DEMO_TEXTURE_COUNT,
1642 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001643 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001644 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001645 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001646 .pNext = NULL,
Courtney Goeltzenleuchtercf92aa42015-09-16 16:12:45 -06001647 .maxSets = 1,
Chia-I Wuf051d262015-10-27 19:25:11 +08001648 .poolSizeCount = 2,
1649 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001650 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001651 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001652
Karl Schultze4dc75c2016-02-02 15:37:51 -07001653 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
1654 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001655 assert(!err);
1656}
1657
Karl Schultze4dc75c2016-02-02 15:37:51 -07001658static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001659 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001660 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001661 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001662 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001663
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001664 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001665 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06001666 .pNext = NULL,
1667 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001668 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001669 .pSetLayouts = &demo->desc_layout};
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001670 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set);
Cody Northrop15954692015-08-03 12:47:29 -06001671 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001672
Chia-I Wuae721ba2015-05-25 16:27:55 +08001673 memset(&tex_descs, 0, sizeof(tex_descs));
1674 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001675 tex_descs[i].sampler = demo->textures[i].sampler;
1676 tex_descs[i].imageView = demo->textures[i].view;
1677 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001678 }
1679
1680 memset(&writes, 0, sizeof(writes));
1681
1682 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001683 writes[0].dstSet = demo->desc_set;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001684 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001685 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001686 writes[0].pBufferInfo = &demo->uniform_data.buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001687
1688 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001689 writes[1].dstSet = demo->desc_set;
1690 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001691 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001692 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001693 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08001694
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001695 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001696}
1697
Karl Schultze4dc75c2016-02-02 15:37:51 -07001698static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001699 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06001700 attachments[1] = demo->depth.view;
1701
Chia-I Wuf973e322015-07-08 13:34:24 +08001702 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001703 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1704 .pNext = NULL,
1705 .renderPass = demo->render_pass,
1706 .attachmentCount = 2,
1707 .pAttachments = attachments,
1708 .width = demo->width,
1709 .height = demo->height,
1710 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08001711 };
1712 VkResult U_ASSERT_ONLY err;
1713 uint32_t i;
1714
Karl Schultze4dc75c2016-02-02 15:37:51 -07001715 demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount *
1716 sizeof(VkFramebuffer));
Tony Barbourd8e504f2015-10-08 14:26:24 -06001717 assert(demo->framebuffers);
1718
1719 for (i = 0; i < demo->swapchainImageCount; i++) {
Cody Northrop2e11cad2015-08-04 10:47:08 -06001720 attachments[0] = demo->buffers[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001721 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
1722 &demo->framebuffers[i]);
Chia-I Wuf973e322015-07-08 13:34:24 +08001723 assert(!err);
1724 }
1725}
1726
Karl Schultze4dc75c2016-02-02 15:37:51 -07001727static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06001728 VkResult U_ASSERT_ONLY err;
1729
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001730 const VkCommandPoolCreateInfo cmd_pool_info = {
1731 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06001732 .pNext = NULL,
1733 .queueFamilyIndex = demo->graphics_queue_node_index,
1734 .flags = 0,
1735 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001736 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
1737 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06001738 assert(!err);
1739
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001740 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08001741 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001743 .commandPool = demo->cmd_pool,
1744 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001745 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001746 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747
1748 demo_prepare_buffers(demo);
1749 demo_prepare_depth(demo);
1750 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001751 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001753 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001754 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001755 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001756
Ian Elliotta0781972015-08-21 15:09:33 -06001757 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001758 err =
1759 vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001760 assert(!err);
1761 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001762
Chia-I Wu63ea9262015-03-26 13:14:16 +08001763 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001764 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001765
Chia-I Wuf973e322015-07-08 13:34:24 +08001766 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001767
Ian Elliotta0781972015-08-21 15:09:33 -06001768 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001769 demo->current_buffer = i;
1770 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1771 }
1772
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001773 /*
1774 * Prepare functions above may generate pipeline commands
1775 * that need to be flushed before beginning the render loop.
1776 */
1777 demo_flush_init_cmd(demo);
1778
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001779 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06001780 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001781}
1782
Karl Schultze4dc75c2016-02-02 15:37:51 -07001783static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06001784 uint32_t i;
1785
1786 demo->prepared = false;
1787
Tony Barbourd8e504f2015-10-08 14:26:24 -06001788 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001789 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06001790 }
Tony Barbourd8e504f2015-10-08 14:26:24 -06001791 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001792 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08001793
Chia-I Wu63636062015-10-26 21:10:41 +08001794 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1795 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1796 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1797 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1798 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001799
1800 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001801 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1802 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1803 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1804 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001805 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001806 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001807
Chia-I Wu63636062015-10-26 21:10:41 +08001808 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1809 vkDestroyImage(demo->device, demo->depth.image, NULL);
1810 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001811
Chia-I Wu63636062015-10-26 21:10:41 +08001812 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1813 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001814
Ian Elliotta0781972015-08-21 15:09:33 -06001815 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001816 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001817 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1818 &demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001819 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001820 free(demo->buffers);
David Pinedo2bb7c932015-06-18 17:03:14 -06001821
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001822 free(demo->queue_props);
1823
Chia-I Wu63636062015-10-26 21:10:41 +08001824 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
1825 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001826 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07001827 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001828 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07001829 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08001830 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06001831
1832#ifndef _WIN32
1833 xcb_destroy_window(demo->connection, demo->window);
1834 xcb_disconnect(demo->connection);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06001835 free(demo->atom_wm_delete_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001836#endif // _WIN32
1837}
1838
Karl Schultze4dc75c2016-02-02 15:37:51 -07001839static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06001840 uint32_t i;
1841
Mike Stroyanbb60b382015-10-28 09:46:57 -06001842 // Don't react to resize until after first initialization.
1843 if (!demo->prepared) {
1844 return;
1845 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001846 // In order to properly resize the window, we must re-create the swapchain
1847 // AND redo the command buffers, etc.
1848 //
1849 // First, perform part of the demo_cleanup() function:
1850 demo->prepared = false;
1851
1852 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001853 vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001854 }
1855 free(demo->framebuffers);
Chia-I Wu63636062015-10-26 21:10:41 +08001856 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001857
Chia-I Wu63636062015-10-26 21:10:41 +08001858 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
1859 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
1860 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
1861 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
1862 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001863
1864 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001865 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
1866 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
1867 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
1868 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001869 }
Ian Elliottcf074d32015-10-16 18:02:43 -06001870
Chia-I Wu63636062015-10-26 21:10:41 +08001871 vkDestroyImageView(demo->device, demo->depth.view, NULL);
1872 vkDestroyImage(demo->device, demo->depth.image, NULL);
1873 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001874
Chia-I Wu63636062015-10-26 21:10:41 +08001875 vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL);
1876 vkFreeMemory(demo->device, demo->uniform_data.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001877
1878 for (i = 0; i < demo->swapchainImageCount; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08001879 vkDestroyImageView(demo->device, demo->buffers[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001880 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
1881 &demo->buffers[i].cmd);
Ian Elliottcf074d32015-10-16 18:02:43 -06001882 }
Chia-I Wu63636062015-10-26 21:10:41 +08001883 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001884 free(demo->buffers);
1885
Ian Elliottcf074d32015-10-16 18:02:43 -06001886 // Second, re-perform the demo_prepare() function, which will re-create the
1887 // swapchain:
1888 demo_prepare(demo);
1889}
1890
David Pinedo2bb7c932015-06-18 17:03:14 -06001891// On MS-Windows, make this a global, so it's available to WndProc()
1892struct demo demo;
1893
Ian Elliott639ca472015-04-16 15:23:05 -06001894#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07001895static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001896 if (!demo->prepared)
1897 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001898 // Wait for work to finish before updating MVP.
1899 vkDeviceWaitIdle(demo->device);
1900 demo_update_data_buffer(demo);
1901
1902 demo_draw(demo);
1903
1904 // Wait for work to finish before updating MVP.
1905 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001906
David Pinedo2bb7c932015-06-18 17:03:14 -06001907 demo->curFrame++;
1908
Karl Schultze4dc75c2016-02-02 15:37:51 -07001909 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
1910 demo->quit = true;
David Pinedo2bb7c932015-06-18 17:03:14 -06001911 demo_cleanup(demo);
1912 ExitProcess(0);
1913 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001914}
Ian Elliott639ca472015-04-16 15:23:05 -06001915
1916// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001917LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
1918 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001919 case WM_CLOSE:
Ian Elliott639ca472015-04-16 15:23:05 -06001920 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001921 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06001922 case WM_PAINT:
Ian Elliott639ca472015-04-16 15:23:05 -06001923 demo_run(&demo);
Tony Barbourc8a59892015-10-20 12:49:46 -06001924 break;
Ian Elliott5ef45e92015-10-21 15:14:07 -06001925 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07001926 // Resize the application to the new window size, except when
1927 // it was minimized. Vulkan doesn't support images or swapchains
1928 // with width=0 and height=0.
1929 if (wParam != SIZE_MINIMIZED) {
1930 demo.width = lParam & 0xffff;
1931 demo.height = lParam & 0xffff0000 >> 16;
1932 demo_resize(&demo);
1933 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06001934 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001935 default:
1936 break;
1937 }
1938 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1939}
1940
Karl Schultze4dc75c2016-02-02 15:37:51 -07001941static void demo_create_window(struct demo *demo) {
1942 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06001943
1944 // Initialize the window class structure:
1945 win_class.cbSize = sizeof(WNDCLASSEX);
1946 win_class.style = CS_HREDRAW | CS_VREDRAW;
1947 win_class.lpfnWndProc = WndProc;
1948 win_class.cbClsExtra = 0;
1949 win_class.cbWndExtra = 0;
1950 win_class.hInstance = demo->connection; // hInstance
1951 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1952 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1953 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1954 win_class.lpszMenuName = NULL;
1955 win_class.lpszClassName = demo->name;
1956 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1957 // Register window class:
1958 if (!RegisterClassEx(&win_class)) {
1959 // It didn't work, so try to give a useful error:
1960 printf("Unexpected error trying to start the application!\n");
1961 fflush(stdout);
1962 exit(1);
1963 }
1964 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001965 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06001966 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001967 demo->window = CreateWindowEx(0,
1968 demo->name, // class name
1969 demo->name, // app name
1970 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07001971 WS_VISIBLE | WS_SYSMENU,
1972 100, 100, // x/y coords
1973 wr.right - wr.left, // width
1974 wr.bottom - wr.top, // height
1975 NULL, // handle to parent
1976 NULL, // handle to menu
1977 demo->connection, // hInstance
1978 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06001979 if (!demo->window) {
1980 // It didn't work, so try to give a useful error:
1981 printf("Cannot create a window in which to draw!\n");
1982 fflush(stdout);
1983 exit(1);
1984 }
1985}
1986#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987static void demo_handle_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001988 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07001989 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001990 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001991 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001992 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001993 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001994 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07001995 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
1996 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001997 demo->quit = true;
1998 }
1999 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002000 case XCB_KEY_RELEASE: {
2001 const xcb_key_release_event_t *key =
2002 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002003
Karl Schultze4dc75c2016-02-02 15:37:51 -07002004 switch (key->detail) {
2005 case 0x9: // Escape
2006 demo->quit = true;
2007 break;
2008 case 0x71: // left arrow key
2009 demo->spin_angle += demo->spin_increment;
2010 break;
2011 case 0x72: // right arrow key
2012 demo->spin_angle -= demo->spin_increment;
2013 break;
2014 case 0x41:
2015 demo->pause = !demo->pause;
2016 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002017 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002018 } break;
2019 case XCB_CONFIGURE_NOTIFY: {
2020 const xcb_configure_notify_event_t *cfg =
2021 (const xcb_configure_notify_event_t *)event;
2022 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002023 demo->width = cfg->width;
2024 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002025 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002026 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002027 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002028 default:
2029 break;
2030 }
2031}
2032
Karl Schultze4dc75c2016-02-02 15:37:51 -07002033static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002034 xcb_flush(demo->connection);
2035
2036 while (!demo->quit) {
2037 xcb_generic_event_t *event;
2038
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002039 if (demo->pause) {
2040 event = xcb_wait_for_event(demo->connection);
2041 } else {
2042 event = xcb_poll_for_event(demo->connection);
2043 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002044 if (event) {
2045 demo_handle_event(demo, event);
2046 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002047 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002048
2049 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002050 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002051 demo_update_data_buffer(demo);
2052
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002053 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07002054
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002055 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002056 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002057 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002058 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002059 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002060 }
2061}
2062
Karl Schultze4dc75c2016-02-02 15:37:51 -07002063static void demo_create_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002064 uint32_t value_mask, value_list[32];
2065
2066 demo->window = xcb_generate_id(demo->connection);
2067
2068 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2069 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002070 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002071 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002072
Karl Schultze4dc75c2016-02-02 15:37:51 -07002073 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->window,
2074 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2075 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2076 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002077
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002078 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002079 xcb_intern_atom_cookie_t cookie =
2080 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2081 xcb_intern_atom_reply_t *reply =
2082 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002083
Karl Schultze4dc75c2016-02-02 15:37:51 -07002084 xcb_intern_atom_cookie_t cookie2 =
2085 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2086 demo->atom_wm_delete_window =
2087 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002088
Karl Schultze4dc75c2016-02-02 15:37:51 -07002089 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->window,
2090 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002091 &(*demo->atom_wm_delete_window).atom);
2092 free(reply);
2093
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002094 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002095
Karl Schultze4dc75c2016-02-02 15:37:51 -07002096 // Force the x/y coordinates to 100,100 results are identical in consecutive
2097 // runs
2098 const uint32_t coords[] = {100, 100};
David Pinedo2bb7c932015-06-18 17:03:14 -06002099 xcb_configure_window(demo->connection, demo->window,
2100 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002101}
Ian Elliott639ca472015-04-16 15:23:05 -06002102#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002103
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002104/*
2105 * Return 1 (true) if all layer names specified in check_names
2106 * can be found in given layer properties.
2107 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002108static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002109 uint32_t layer_count,
2110 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002111 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002112 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002113 for (uint32_t j = 0; j < layer_count; j++) {
2114 if (!strcmp(check_names[i], layers[j].layerName)) {
2115 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002116 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002117 }
2118 }
2119 if (!found) {
2120 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2121 return 0;
2122 }
2123 }
2124 return 1;
2125}
2126
Karl Schultze4dc75c2016-02-02 15:37:51 -07002127static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002128 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002129 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002130 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002131 uint32_t instance_layer_count = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002132 uint32_t device_validation_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002133 uint32_t enabled_extension_count = 0;
2134 uint32_t enabled_layer_count = 0;
2135
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002136 char *instance_validation_layers[] = {
Courtney Goeltzenleuchter5170a4c2016-02-06 17:11:22 -07002137 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_mem_tracker",
Karl Schultze4dc75c2016-02-02 15:37:51 -07002138 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_draw_state",
2139 "VK_LAYER_LUNARG_param_checker", "VK_LAYER_LUNARG_swapchain",
2140 "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_image",
Dustin Gravescfe5ade2016-01-26 16:30:22 -07002141 "VK_LAYER_GOOGLE_unique_objects",
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002142 };
2143
Courtney Goeltzenleuchter5170a4c2016-02-06 17:11:22 -07002144 demo->device_validation_layers[0] = "VK_LAYER_GOOGLE_threading";
Tony Barbourda2bad52016-01-22 14:36:40 -07002145 demo->device_validation_layers[1] = "VK_LAYER_LUNARG_mem_tracker";
Dustin Gravescfe5ade2016-01-26 16:30:22 -07002146 demo->device_validation_layers[2] = "VK_LAYER_LUNARG_object_tracker";
2147 demo->device_validation_layers[3] = "VK_LAYER_LUNARG_draw_state";
2148 demo->device_validation_layers[4] = "VK_LAYER_LUNARG_param_checker";
2149 demo->device_validation_layers[5] = "VK_LAYER_LUNARG_swapchain";
2150 demo->device_validation_layers[6] = "VK_LAYER_LUNARG_device_limits";
2151 demo->device_validation_layers[7] = "VK_LAYER_LUNARG_image";
2152 demo->device_validation_layers[8] = "VK_LAYER_GOOGLE_unique_objects";
2153 device_validation_layer_count = 9;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002154
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002155 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002156 VkBool32 validation_found = 0;
Courtney Goeltzenleuchtere06f1192015-09-14 17:22:16 -06002157 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002158 assert(!err);
2159
Dustin Graves7c287352016-01-27 13:48:06 -07002160 if (instance_layer_count > 0) {
2161 VkLayerProperties *instance_layers =
2162 malloc(sizeof(VkLayerProperties) * instance_layer_count);
2163 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2164 instance_layers);
2165 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002166
Dustin Graves7c287352016-01-27 13:48:06 -07002167 if (demo->validate) {
2168 validation_found = demo_check_layers(
2169 ARRAY_SIZE(instance_validation_layers),
2170 instance_validation_layers, instance_layer_count,
2171 instance_layers);
2172
2173 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002174 }
Dustin Graves7c287352016-01-27 13:48:06 -07002175
2176 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002177 }
2178
Dustin Graves7c287352016-01-27 13:48:06 -07002179 if (demo->validate && !validation_found) {
2180 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find"
2181 "required validation layer.\n\n"
2182 "Please look at the Getting Started guide for additional "
2183 "information.\n",
2184 "vkCreateInstance Failure");
2185 }
2186
2187 /* Look for instance extensions */
2188 VkBool32 surfaceExtFound = 0;
2189 VkBool32 platformSurfaceExtFound = 0;
2190 memset(extension_names, 0, sizeof(extension_names));
2191
Karl Schultze4dc75c2016-02-02 15:37:51 -07002192 err = vkEnumerateInstanceExtensionProperties(
2193 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002194 assert(!err);
2195
Dustin Graves7c287352016-01-27 13:48:06 -07002196 if (instance_extension_count > 0) {
2197 VkExtensionProperties *instance_extensions =
2198 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2199 err = vkEnumerateInstanceExtensionProperties(
2200 NULL, &instance_extension_count, instance_extensions);
2201 assert(!err);
2202 for (uint32_t i = 0; i < instance_extension_count; i++) {
2203 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
2204 instance_extensions[i].extensionName)) {
2205 surfaceExtFound = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002206 extension_names[enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07002207 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002208 }
Dustin Graves7c287352016-01-27 13:48:06 -07002209#ifdef _WIN32
2210 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
2211 instance_extensions[i].extensionName)) {
2212 platformSurfaceExtFound = 1;
2213 extension_names[enabled_extension_count++] =
2214 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
2215 }
2216#else // _WIN32
2217 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
2218 instance_extensions[i].extensionName)) {
2219 platformSurfaceExtFound = 1;
2220 extension_names[enabled_extension_count++] =
2221 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
2222 }
2223#endif // _WIN32
2224 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
2225 instance_extensions[i].extensionName)) {
2226 if (demo->validate) {
2227 extension_names[enabled_extension_count++] =
2228 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
2229 }
2230 }
2231 assert(enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002232 }
Dustin Graves7c287352016-01-27 13:48:06 -07002233
2234 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002235 }
Dustin Graves7c287352016-01-27 13:48:06 -07002236
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002237 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002238 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2239 "the " VK_KHR_SURFACE_EXTENSION_NAME
2240 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06002241 "Vulkan installable client driver (ICD) installed?\nPlease "
2242 "look at the Getting Started guide for additional "
2243 "information.\n",
2244 "vkCreateInstance Failure");
2245 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002246 if (!platformSurfaceExtFound) {
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002247#ifdef _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002248 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2249 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2250 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002251 "Vulkan installable client driver (ICD) installed?\nPlease "
2252 "look at the Getting Started guide for additional "
2253 "information.\n",
2254 "vkCreateInstance Failure");
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002255#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002256 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
2257 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2258 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002259 "Vulkan installable client driver (ICD) installed?\nPlease "
2260 "look at the Getting Started guide for additional "
2261 "information.\n",
2262 "vkCreateInstance Failure");
2263#endif // _WIN32
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002264 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002265 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002266 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002267 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002268 .pApplicationName = APP_SHORT_NAME,
2269 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002270 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002271 .engineVersion = 0,
Tony Barbour4308a1a2016-02-25 13:58:50 -07002272 .apiVersion = VK_MAKE_VERSION(1, 0, 0),
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002273 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002274 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002275 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002276 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002277 .pApplicationInfo = &app,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002278 .enabledLayerCount = enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002279 .ppEnabledLayerNames =
2280 (const char *const *)((demo->validate) ? instance_validation_layers
2281 : NULL),
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002282 .enabledExtensionCount = enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002283 .ppEnabledExtensionNames = (const char *const *)extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002284 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002285
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002286 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002287
Chia-I Wu63636062015-10-26 21:10:41 +08002288 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002289 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2290 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002291 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002292 "additional information.\n",
2293 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06002294 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002295 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07002296 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002297 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002298 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002299 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2300 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002301 "the Getting Started guide for additional information.\n",
2302 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002303 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002304
Tobin Ehlis8a724d82015-09-07 15:16:39 -06002305 /* Make initial call to query gpu_count, then second call for gpu info*/
2306 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
2307 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07002308
2309 if (gpu_count > 0) {
2310 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
2311 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
2312 assert(!err);
2313 /* For cube demo we just grab the first physical device */
2314 demo->gpu = physical_devices[0];
2315 free(physical_devices);
2316 } else {
2317 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
2318 "Do you have a compatible Vulkan installable client driver (ICD) "
2319 "installed?\nPlease look at the Getting Started guide for "
2320 "additional information.\n",
2321 "vkEnumeratePhysicalDevices Failure");
2322 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002323
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002324 /* Look for validation layers */
2325 validation_found = 0;
Tony Barbourda2bad52016-01-22 14:36:40 -07002326 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002327 uint32_t device_layer_count = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002328 err =
2329 vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002330 assert(!err);
2331
Dustin Graves7c287352016-01-27 13:48:06 -07002332 if (device_layer_count > 0) {
2333 VkLayerProperties *device_layers =
2334 malloc(sizeof(VkLayerProperties) * device_layer_count);
2335 err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count,
2336 device_layers);
2337 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002338
Dustin Graves7c287352016-01-27 13:48:06 -07002339 if (demo->validate) {
2340 validation_found = demo_check_layers(device_validation_layer_count,
2341 demo->device_validation_layers,
2342 device_layer_count,
2343 device_layers);
2344 demo->enabled_layer_count = device_validation_layer_count;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002345 }
Dustin Graves7c287352016-01-27 13:48:06 -07002346
2347 free(device_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002348 }
2349
Dustin Graves7c287352016-01-27 13:48:06 -07002350 if (demo->validate && !validation_found) {
2351 ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find"
2352 "a required validation layer.\n\n"
2353 "Please look at the Getting Started guide for additional "
2354 "information.\n",
2355 "vkCreateDevice Failure");
2356 }
2357
2358 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002359 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07002360 VkBool32 swapchainExtFound = 0;
2361 demo->enabled_extension_count = 0;
2362 memset(extension_names, 0, sizeof(extension_names));
2363
Karl Schultze4dc75c2016-02-02 15:37:51 -07002364 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
2365 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002366 assert(!err);
2367
Dustin Graves7c287352016-01-27 13:48:06 -07002368 if (device_extension_count > 0) {
2369 VkExtensionProperties *device_extensions =
2370 malloc(sizeof(VkExtensionProperties) * device_extension_count);
2371 err = vkEnumerateDeviceExtensionProperties(
2372 demo->gpu, NULL, &device_extension_count, device_extensions);
2373 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06002374
Dustin Graves7c287352016-01-27 13:48:06 -07002375 for (uint32_t i = 0; i < device_extension_count; i++) {
2376 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
2377 device_extensions[i].extensionName)) {
2378 swapchainExtFound = 1;
2379 demo->extension_names[demo->enabled_extension_count++] =
2380 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
2381 }
2382 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002383 }
Dustin Graves7c287352016-01-27 13:48:06 -07002384
2385 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002386 }
Dustin Graves7c287352016-01-27 13:48:06 -07002387
Tony Barbourcc69f452015-10-08 13:59:42 -06002388 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002389 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
2390 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
2391 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002392 "Vulkan installable client driver (ICD) installed?\nPlease "
2393 "look at the Getting Started guide for additional "
2394 "information.\n",
2395 "vkCreateInstance Failure");
2396 }
2397
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002398 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002399 demo->CreateDebugReportCallback =
2400 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
2401 demo->inst, "vkCreateDebugReportCallbackEXT");
2402 demo->DestroyDebugReportCallback =
2403 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
2404 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002405 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002406 ERR_EXIT(
2407 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
2408 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002409 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002410 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002411 ERR_EXIT(
2412 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
2413 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06002414 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002415 demo->DebugReportMessage =
2416 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
2417 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002418 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002419 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07002420 "vkGetProcAddr Failure");
2421 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002422
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002423 PFN_vkDebugReportCallbackEXT callback;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002424
2425 if (!demo->use_break) {
2426 callback = dbgFunc;
2427 } else {
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002428 callback = dbgFunc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002429 // TODO add a break callback defined locally since there is no
2430 // longer
Jon Ashburn9549c8e2015-12-15 08:47:46 -07002431 // one included in the loader
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002432 }
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07002433 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
2434 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002435 dbgCreateInfo.pNext = NULL;
2436 dbgCreateInfo.pfnCallback = callback;
2437 dbgCreateInfo.pUserData = NULL;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002438 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07002439 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002440 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
2441 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002442 switch (err) {
2443 case VK_SUCCESS:
2444 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002445 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002446 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
2447 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002448 break;
2449 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002450 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
2451 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002452 break;
2453 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002454 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002455 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002456
2457 /* Call with NULL data to get count */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002458 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2459 NULL);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002460 assert(demo->queue_count >= 1);
2461
Karl Schultze4dc75c2016-02-02 15:37:51 -07002462 demo->queue_props = (VkQueueFamilyProperties *)malloc(
2463 demo->queue_count * sizeof(VkQueueFamilyProperties));
2464 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count,
2465 demo->queue_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002466 // Find a queue that supports gfx
2467 uint32_t gfx_queue_idx = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002468 for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count;
2469 gfx_queue_idx++) {
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002470 if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT)
2471 break;
2472 }
2473 assert(gfx_queue_idx < demo->queue_count);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002474 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002475 // If app has specific feature requirements it should check supported
2476 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06002477 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002478 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06002479
Tony Barbourda2bad52016-01-22 14:36:40 -07002480 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
2481 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
2482 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
2483 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
2484 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
2485}
2486
Karl Schultze4dc75c2016-02-02 15:37:51 -07002487static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07002488 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002489 float queue_priorities[1] = {0.0};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002490 const VkDeviceQueueCreateInfo queue = {
2491 .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
2492 .pNext = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07002493 .queueFamilyIndex = demo->graphics_queue_node_index,
Chia-I Wu8b497442015-11-06 06:42:02 +08002494 .queueCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002495 .pQueuePriorities = queue_priorities};
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002496
2497 VkDeviceCreateInfo device = {
2498 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2499 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08002500 .queueCreateInfoCount = 1,
2501 .pQueueCreateInfos = &queue,
Tony Barbourda2bad52016-01-22 14:36:40 -07002502 .enabledLayerCount = demo->enabled_layer_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002503 .ppEnabledLayerNames =
2504 (const char *const *)((demo->validate)
2505 ? demo->device_validation_layers
2506 : NULL),
Tony Barbourda2bad52016-01-22 14:36:40 -07002507 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002508 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
2509 .pEnabledFeatures =
2510 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06002511 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002512
Chia-I Wu63636062015-10-26 21:10:41 +08002513 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002514 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002515}
2516
Karl Schultze4dc75c2016-02-02 15:37:51 -07002517static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07002518 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06002519 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002520
Karl Schultze4dc75c2016-02-02 15:37:51 -07002521// Create a WSI surface for the window:
Ian Elliottaaae5352015-07-06 14:27:58 -06002522#ifdef _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002523 VkWin32SurfaceCreateInfoKHR createInfo;
2524 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
2525 createInfo.pNext = NULL;
2526 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07002527 createInfo.hinstance = demo->connection;
2528 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07002529
Karl Schultze4dc75c2016-02-02 15:37:51 -07002530 err =
2531 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottb08e0d92015-11-20 11:55:46 -07002532
Ian Elliottaaae5352015-07-06 14:27:58 -06002533#else // _WIN32
Ian Elliotta7a731c2015-12-11 15:52:12 -07002534 VkXcbSurfaceCreateInfoKHR createInfo;
2535 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
2536 createInfo.pNext = NULL;
2537 createInfo.flags = 0;
2538 createInfo.connection = demo->connection;
2539 createInfo.window = demo->window;
2540
Karl Schultze4dc75c2016-02-02 15:37:51 -07002541 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Ian Elliottaaae5352015-07-06 14:27:58 -06002542#endif // _WIN32
2543
Tony Barbourcc69f452015-10-08 13:59:42 -06002544 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002545 VkBool32 *supportsPresent =
2546 (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32));
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002547 for (i = 0; i < demo->queue_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002548 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06002549 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002550 }
Ian Elliottaaae5352015-07-06 14:27:58 -06002551
2552 // Search for a graphics and a present queue in the array of queue
2553 // families, try to find one that supports both
2554 uint32_t graphicsQueueNodeIndex = UINT32_MAX;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002555 uint32_t presentQueueNodeIndex = UINT32_MAX;
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002556 for (i = 0; i < demo->queue_count; i++) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002557 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
2558 if (graphicsQueueNodeIndex == UINT32_MAX) {
2559 graphicsQueueNodeIndex = i;
2560 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002561
Ian Elliottaaae5352015-07-06 14:27:58 -06002562 if (supportsPresent[i] == VK_TRUE) {
2563 graphicsQueueNodeIndex = i;
2564 presentQueueNodeIndex = i;
2565 break;
2566 }
2567 }
2568 }
2569 if (presentQueueNodeIndex == UINT32_MAX) {
2570 // If didn't find a queue that supports both graphics and present, then
2571 // find a separate present queue.
Courtney Goeltzenleuchterfe95fca2015-07-15 17:40:20 -06002572 for (uint32_t i = 0; i < demo->queue_count; ++i) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002573 if (supportsPresent[i] == VK_TRUE) {
2574 presentQueueNodeIndex = i;
2575 break;
2576 }
2577 }
2578 }
2579 free(supportsPresent);
2580
2581 // Generate error if could not find both a graphics and a present queue
Karl Schultze4dc75c2016-02-02 15:37:51 -07002582 if (graphicsQueueNodeIndex == UINT32_MAX ||
2583 presentQueueNodeIndex == UINT32_MAX) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002584 ERR_EXIT("Could not find a graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002585 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002586 }
2587
2588 // TODO: Add support for separate queues, including presentation,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002589 // synchronization, and appropriate tracking for QueueSubmit.
2590 // NOTE: While it is possible for an application to use a separate graphics
2591 // and a present queues, this demo program assumes it is only using
2592 // one:
Ian Elliottaaae5352015-07-06 14:27:58 -06002593 if (graphicsQueueNodeIndex != presentQueueNodeIndex) {
2594 ERR_EXIT("Could not find a common graphics and a present queue\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06002595 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06002596 }
2597
2598 demo->graphics_queue_node_index = graphicsQueueNodeIndex;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002599
Tony Barbourda2bad52016-01-22 14:36:40 -07002600 demo_create_device(demo);
2601
2602 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
2603 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
2604 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
2605 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
2606 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
2607
Karl Schultze4dc75c2016-02-02 15:37:51 -07002608 vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0,
2609 &demo->queue);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002610
Ian Elliottaaae5352015-07-06 14:27:58 -06002611 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06002612 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002613 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002614 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06002615 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06002616 VkSurfaceFormatKHR *surfFormats =
2617 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07002618 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07002619 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06002620 assert(!err);
2621 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
2622 // the surface has no preferred format. Otherwise, at least one
2623 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002624 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002625 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002626 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06002627 assert(formatCount >= 1);
2628 demo->format = surfFormats[0].format;
2629 }
Ian Elliott8528eff2015-08-07 15:56:59 -06002630 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002631
David Pinedo2bb7c932015-06-18 17:03:14 -06002632 demo->quit = false;
2633 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002634
2635 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06002636 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002637}
2638
Karl Schultze4dc75c2016-02-02 15:37:51 -07002639static void demo_init_connection(struct demo *demo) {
Ian Elliott639ca472015-04-16 15:23:05 -06002640#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002641 const xcb_setup_t *setup;
2642 xcb_screen_iterator_t iter;
2643 int scr;
2644
2645 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002646 if (demo->connection == NULL) {
2647 printf("Cannot find a compatible Vulkan installable client driver "
2648 "(ICD).\nExiting ...\n");
2649 fflush(stdout);
2650 exit(1);
2651 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002652
2653 setup = xcb_get_setup(demo->connection);
2654 iter = xcb_setup_roots_iterator(setup);
2655 while (scr-- > 0)
2656 xcb_screen_next(&iter);
2657
2658 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002659#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002660}
2661
Karl Schultze4dc75c2016-02-02 15:37:51 -07002662static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002663 vec3 eye = {0.0f, 3.0f, 5.0f};
2664 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002665 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002666
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002667 memset(demo, 0, sizeof(*demo));
Tony Barbour1a86d032015-09-21 15:17:33 -06002668 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002669
Piers Daniell735ee532015-02-23 16:23:13 -07002670 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002671 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002672 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002673 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002674 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06002675 if (strcmp(argv[i], "--break") == 0) {
2676 demo->use_break = true;
2677 continue;
2678 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002679 if (strcmp(argv[i], "--validate") == 0) {
2680 demo->validate = true;
2681 continue;
2682 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002683 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
2684 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
2685 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002686 i++;
2687 continue;
2688 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002689
Karl Schultze4dc75c2016-02-02 15:37:51 -07002690 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] "
2691 "[--c <framecount>]\n",
2692 APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002693 fflush(stderr);
2694 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002695 }
2696
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002697 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002698 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002699
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002700 demo->width = 500;
2701 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002702
2703 demo->spin_angle = 0.01f;
2704 demo->spin_increment = 0.01f;
2705 demo->pause = false;
2706
Karl Schultze4dc75c2016-02-02 15:37:51 -07002707 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
2708 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002709 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2710 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002711}
2712
Ian Elliott639ca472015-04-16 15:23:05 -06002713#ifdef _WIN32
Mark Young418b9d12016-01-06 14:26:04 -07002714// Include header required for parsing the command line options.
2715#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002716
Karl Schultze4dc75c2016-02-02 15:37:51 -07002717int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
2718 int nCmdShow) {
2719 MSG msg; // message
2720 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002721 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002722 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06002723
Karl Schultze4dc75c2016-02-02 15:37:51 -07002724 // Use the CommandLine functions to get the command line arguments.
2725 // Unfortunately, Microsoft outputs
2726 // this information as wide characters for Unicode, and we simply want the
2727 // Ascii version to be compatible
2728 // with the non-Windows side. So, we have to convert the information to
2729 // Ascii character strings.
2730 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
2731 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07002732 argc = 0;
2733 }
2734
Karl Schultze4dc75c2016-02-02 15:37:51 -07002735 if (argc > 0) {
2736 argv = (char **)malloc(sizeof(char *) * argc);
2737 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002738 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002739 } else {
2740 for (int iii = 0; iii < argc; iii++) {
2741 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07002742 size_t numConverted = 0;
2743
Karl Schultze4dc75c2016-02-02 15:37:51 -07002744 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
2745 if (argv[iii] != NULL) {
2746 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
2747 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07002748 }
2749 }
2750 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002751 } else {
Mark Young418b9d12016-01-06 14:26:04 -07002752 argv = NULL;
2753 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002754
2755 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07002756
2757 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07002758 if (argc > 0 && argv != NULL) {
2759 for (int iii = 0; iii < argc; iii++) {
2760 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07002761 free(argv[iii]);
2762 }
2763 }
2764 free(argv);
2765 }
2766
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002767 demo.connection = hInstance;
2768 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002769 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002770 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06002771
2772 demo_prepare(&demo);
2773
Karl Schultze4dc75c2016-02-02 15:37:51 -07002774 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07002775
2776 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07002777 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002778 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002779 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06002780 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002781 done = true; // if found, quit app
2782 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06002783 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07002784 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06002785 DispatchMessage(&msg);
2786 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002787 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06002788 }
2789
2790 demo_cleanup(&demo);
2791
Karl Schultze4dc75c2016-02-02 15:37:51 -07002792 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002793}
2794#else // _WIN32
Karl Schultze4dc75c2016-02-02 15:37:51 -07002795int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002796 struct demo demo;
2797
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002798 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002799 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06002800 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002801
2802 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002803 demo_run(&demo);
2804
2805 demo_cleanup(&demo);
2806
2807 return 0;
2808}
Ian Elliott639ca472015-04-16 15:23:05 -06002809#endif // _WIN32