blob: 1b7b32279ffaf72dbe4c200a9dcac7053dc777ce [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
Tony-LunarG495604b2019-06-13 15:32:05 -06002 * Copyright (c) 2015-2019 The Khronos Group Inc.
3 * Copyright (c) 2015-2019 Valve Corporation
4 * Copyright (c) 2015-2019 LunarG, Inc.
Ian Elliottd940ca22017-03-22 08:31:27 -06005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Chia-I Wu <olv@lunarg.com>
19 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20 * Author: Ian Elliott <ian@LunarG.com>
21 * Author: Ian Elliott <ianelliott@google.com>
22 * Author: Jon Ashburn <jon@lunarg.com>
23 * Author: Gwan-gyeong Mun <elongbug@gmail.com>
24 * Author: Tony Barbour <tony@LunarG.com>
25 * Author: Bill Hollings <bill.hollings@brenwill.com>
26 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070027
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060029#include <stdio.h>
Ian Elliottd940ca22017-03-22 08:31:27 -060030#include <stdarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060031#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070035#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090036#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060037#include <X11/Xutil.h>
Joey Bzdek15eb0702017-06-07 09:40:36 -060038#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
39#include <linux/input.h>
Tony Barbour2593b182016-04-19 10:57:58 -060040#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060041
Ian Elliott639ca472015-04-16 15:23:05 -060042#ifdef _WIN32
43#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060044#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070045#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060046
Cody Northrop8707a6e2016-04-26 19:59:19 -060047#ifdef ANDROID
48#include "vulkan_wrapper.h"
49#else
David Pinedoc5193c12015-11-06 12:54:48 -070050#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060051#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070052
David Pinedo541526f2015-11-24 09:00:24 -070053#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060054#include "linmath.h"
Mark Lobodzinski0edf1382018-04-10 15:40:04 -060055#include "object_type_string_helper.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060056
Ian Elliottd940ca22017-03-22 08:31:27 -060057#include "gettime.h"
58#include "inttypes.h"
59#define MILLION 1000000L
60#define BILLION 1000000000L
61
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060062#define DEMO_TEXTURE_COUNT 1
Lenny Komowffe446c2018-11-19 17:08:04 -070063#define APP_SHORT_NAME "vkcube"
64#define APP_LONG_NAME "Vulkan Cube"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060065
Tony Barbour66a56c32016-09-21 13:10:56 -060066// Allow a maximum of two outstanding presentation operations.
67#define FRAME_LAG 2
68
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060069#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
70
Tony Barbourfdc2d352015-04-22 09:02:32 -060071#if defined(NDEBUG) && defined(__GNUC__)
72#define U_ASSERT_ONLY __attribute__((unused))
73#else
74#define U_ASSERT_ONLY
75#endif
76
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090077#if defined(__GNUC__)
78#define UNUSED __attribute__((unused))
79#else
80#define UNUSED
81#endif
82
Ian Elliott07264132015-04-28 11:35:02 -060083#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060084bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070085#define ERR_EXIT(err_msg, err_class) \
86 do { \
87 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
88 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070089 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -060090void DbgMsg(char *fmt, ...) {
91 va_list va;
92 va_start(va, fmt);
Mike Weiblene9847ff2019-06-27 15:18:32 -060093 vprintf(fmt, va);
Ian Elliottd940ca22017-03-22 08:31:27 -060094 va_end(va);
Mike Weiblene9847ff2019-06-27 15:18:32 -060095 fflush(stdout);
Ian Elliottd940ca22017-03-22 08:31:27 -060096}
Ian Elliott07264132015-04-28 11:35:02 -060097
Michael Lentinec6cde4b2016-04-18 13:20:45 -050098#elif defined __ANDROID__
99#include <android/log.h>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700100#define ERR_EXIT(err_msg, err_class) \
101 do { \
Lenny Komowffe446c2018-11-19 17:08:04 -0700102 ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", err_msg)); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700103 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500104 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600105#ifdef VARARGS_WORKS_ON_ANDROID
106void DbgMsg(const char *fmt, ...) {
107 va_list va;
108 va_start(va, fmt);
Lenny Komowffe446c2018-11-19 17:08:04 -0700109 __android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, va);
Ian Elliottd940ca22017-03-22 08:31:27 -0600110 va_end(va);
111}
112#else // VARARGS_WORKS_ON_ANDROID
113#define DbgMsg(fmt, ...) \
114 do { \
Lenny Komowffe446c2018-11-19 17:08:04 -0700115 ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, ##__VA_ARGS__)); \
Ian Elliottd940ca22017-03-22 08:31:27 -0600116 } while (0)
117#endif // VARARGS_WORKS_ON_ANDROID
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500118#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700119#define ERR_EXIT(err_msg, err_class) \
120 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -0800121 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700122 fflush(stdout); \
123 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700124 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600125void DbgMsg(char *fmt, ...) {
126 va_list va;
127 va_start(va, fmt);
Mike Weiblene9847ff2019-06-27 15:18:32 -0600128 vprintf(fmt, va);
Ian Elliottd940ca22017-03-22 08:31:27 -0600129 va_end(va);
Mike Weiblene9847ff2019-06-27 15:18:32 -0600130 fflush(stdout);
Ian Elliottd940ca22017-03-22 08:31:27 -0600131}
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500132#endif
Ian Elliott07264132015-04-28 11:35:02 -0600133
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700134#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
135 { \
136 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
137 if (demo->fp##entrypoint == NULL) { \
138 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
139 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700140 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600141
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600142static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
143
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700144#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
145 { \
146 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
147 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
148 if (demo->fp##entrypoint == NULL) { \
149 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
150 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700151 }
Ian Elliott673898b2015-06-22 15:07:49 -0600152
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600153/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700154 * structure to track all objects related to a texture.
155 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600156struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600157 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700158
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600159 VkImage image;
Tony-LunarGbc9fc052018-09-21 13:47:06 -0600160 VkBuffer buffer;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600161 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600162
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800163 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500164 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600165 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700166 int32_t tex_width, tex_height;
167};
168
Karl Schultze4dc75c2016-02-02 15:37:51 -0700169static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170
Karl Schultz0218c002016-03-22 17:06:13 -0600171static int validation_error = 0;
172
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600173struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600174 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700175 float mvp[4][4];
176 float position[12 * 3][4];
177 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600178};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600179
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600180//--------------------------------------------------------------------------------------
181// Mesh and VertexFormat Data
182//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700183// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600184static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800185 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600186 -1.0f,-1.0f, 1.0f,
187 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 -1.0f, 1.0f,-1.0f,
190 -1.0f,-1.0f,-1.0f,
191
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800192 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600193 1.0f, 1.0f,-1.0f,
194 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600197 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600198
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800199 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600200 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600201 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800202 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600203 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600204 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600205
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600207 -1.0f, 1.0f, 1.0f,
208 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800209 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600211 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800213 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214 1.0f, 1.0f, 1.0f,
215 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 1.0f,-1.0f,-1.0f,
218 1.0f, 1.0f,-1.0f,
219
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600222 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f,-1.0f, 1.0f,
225 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600226};
227
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600228static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700229 0.0f, 1.0f, // -X side
230 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800231 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800232 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233 0.0f, 0.0f,
234 0.0f, 1.0f,
235
Rene Lindsay76867e82016-07-29 10:49:11 -0700236 1.0f, 1.0f, // -Z side
237 0.0f, 0.0f,
238 0.0f, 1.0f,
239 1.0f, 1.0f,
240 1.0f, 0.0f,
241 0.0f, 0.0f,
242
243 1.0f, 0.0f, // -Y side
244 1.0f, 1.0f,
245 0.0f, 1.0f,
246 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600247 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800248 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600249
Rene Lindsay76867e82016-07-29 10:49:11 -0700250 1.0f, 0.0f, // +Y side
251 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800252 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600253 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700254 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600255 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600256
Rene Lindsay76867e82016-07-29 10:49:11 -0700257 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800258 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700259 0.0f, 1.0f,
260 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600261 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800262 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700263
264 0.0f, 0.0f, // +Z side
265 0.0f, 1.0f,
266 1.0f, 0.0f,
267 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600268 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700269 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600270};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700271// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600272
Karl Schultze4dc75c2016-02-02 15:37:51 -0700273void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600274 int i;
275
276 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700277 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600278 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
279 }
280 printf("\n");
281 fflush(stdout);
282}
283
Karl Schultze4dc75c2016-02-02 15:37:51 -0700284void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600285 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700286 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600287 printf("\n");
288 fflush(stdout);
289}
290
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600291typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600292 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800293 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600294 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600295 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700296 VkBuffer uniform_buffer;
297 VkDeviceMemory uniform_memory;
298 VkFramebuffer framebuffer;
299 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700300} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600301
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600302struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500303#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600304#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700305 HINSTANCE connection; // hInstance - Windows Instance
306 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
307 HWND window; // hWnd - window handle
308 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700309#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700310 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600311 Window xlib_window;
312 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700313#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700314 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600315 xcb_connection_t *connection;
316 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600317 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800318 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900319#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
320 struct wl_display *display;
321 struct wl_registry *registry;
322 struct wl_compositor *compositor;
323 struct wl_surface *window;
324 struct wl_shell *shell;
325 struct wl_shell_surface *shell_surface;
Joey Bzdek15eb0702017-06-07 09:40:36 -0600326 struct wl_seat *seat;
327 struct wl_pointer *pointer;
328 struct wl_keyboard *keyboard;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500329#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mike Schuchardt837d5162018-03-08 12:57:02 -0700330 struct ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500331#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
332 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500333#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700334 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600335 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700336 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600337 bool separate_present_queue;
Jeremy Kniager602e4182018-01-19 10:52:34 -0700338 bool is_minimized;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600339
Ian Elliott53622a72017-03-28 11:06:33 -0600340 bool VK_KHR_incremental_present_enabled;
341
Ian Elliottd940ca22017-03-22 08:31:27 -0600342 bool VK_GOOGLE_display_timing_enabled;
343 bool syncd_with_actual_presents;
344 uint64_t refresh_duration;
345 uint64_t refresh_duration_multiplier;
346 uint64_t target_IPD; // image present duration (inverse of frame rate)
347 uint64_t prev_desired_present_time;
348 uint32_t next_present_id;
349 uint32_t last_early_id; // 0 if no early images
350 uint32_t last_late_id; // 0 if no late images
351
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600352 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600353 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600354 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600355 VkQueue graphics_queue;
356 VkQueue present_queue;
357 uint32_t graphics_queue_family_index;
358 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600359 VkSemaphore image_acquired_semaphores[FRAME_LAG];
360 VkSemaphore draw_complete_semaphores[FRAME_LAG];
361 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600362 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600363 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600364 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600365
Tony Barbourda2bad52016-01-22 14:36:40 -0700366 uint32_t enabled_extension_count;
367 uint32_t enabled_layer_count;
368 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600369 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700370
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600372 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600373 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700375 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
376 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
377 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
378 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600379 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
380 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
381 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
382 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
383 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600384 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
385 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600386 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600387 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700388 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600389 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600390 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600391 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600392
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800393 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600394 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395
396 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600397 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600398
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600399 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800400 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500401 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600402 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403 } depth;
404
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600405 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600406 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600407
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700408 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500409 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600410 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600411 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800412 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600413 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600414
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600415 mat4x4 projection_matrix;
416 mat4x4 view_matrix;
417 mat4x4 model_matrix;
418
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600419 float spin_angle;
420 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600421 bool pause;
422
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600423 VkShaderModule vert_shader_module;
424 VkShaderModule frag_shader_module;
425
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600426 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800427
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600428 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600429 int32_t curFrame;
430 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600431 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600432 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600433 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600434 bool suppress_popups;
Mark Young66510e52017-11-10 10:05:14 -0700435
436 PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
437 PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
438 PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
439 PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
440 PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
441 PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
442 PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
443 VkDebugUtilsMessengerEXT dbg_messenger;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600444
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600445 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600446 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600447};
448
Mark Young66510e52017-11-10 10:05:14 -0700449VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
450 VkDebugUtilsMessageTypeFlagsEXT messageType,
451 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
452 void *pUserData) {
453 char prefix[64] = "";
454 char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000);
lenny-lunargfbe22392016-06-06 11:07:53 -0600455 assert(message);
Mark Young66510e52017-11-10 10:05:14 -0700456 struct demo *demo = (struct demo *)pUserData;
lenny-lunargfbe22392016-06-06 11:07:53 -0600457
Mark Young66510e52017-11-10 10:05:14 -0700458 if (demo->use_break) {
459#ifndef WIN32
460 raise(SIGTRAP);
461#else
462 DebugBreak();
463#endif
464 }
465
466 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
467 strcat(prefix, "VERBOSE : ");
468 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
469 strcat(prefix, "INFO : ");
470 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
471 strcat(prefix, "WARNING : ");
472 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
473 strcat(prefix, "ERROR : ");
474 }
475
476 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
477 strcat(prefix, "GENERAL");
lenny-lunargfbe22392016-06-06 11:07:53 -0600478 } else {
Mark Young66510e52017-11-10 10:05:14 -0700479 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
480 strcat(prefix, "VALIDATION");
481 validation_error = 1;
482 }
483 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
484 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
485 strcat(prefix, "|");
486 }
487 strcat(prefix, "PERFORMANCE");
488 }
489 }
490
491 sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber,
492 pCallbackData->pMessageIdName, pCallbackData->pMessage);
493 if (pCallbackData->objectCount > 0) {
494 char tmp_message[500];
495 sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount);
496 strcat(message, tmp_message);
497 for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
498 if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
499 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object,
Mark Young44212682018-02-21 15:29:41 -0700500 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700501 (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName);
502 } else {
503 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object,
Mark Young44212682018-02-21 15:29:41 -0700504 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700505 (void *)(pCallbackData->pObjects[object].objectHandle));
506 }
507 strcat(message, tmp_message);
508 }
509 }
510 if (pCallbackData->cmdBufLabelCount > 0) {
511 char tmp_message[500];
512 sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount);
513 strcat(message, tmp_message);
514 for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
515 sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label,
516 pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0],
517 pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2],
518 pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]);
519 strcat(message, tmp_message);
520 }
lenny-lunargfbe22392016-06-06 11:07:53 -0600521 }
522
523#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600524
Tony Barbour602ca4f2016-09-12 14:02:43 -0600525 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600526 if (!demo->suppress_popups)
527 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600528 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600529
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600530#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600531
Mike Schuchardt837d5162018-03-08 12:57:02 -0700532 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600533 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700534 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600535 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700536 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600537 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700538 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
539 __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600540 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600541 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600542 }
Cody Northropaf28a532016-09-27 10:50:57 -0600543
lenny-lunargfbe22392016-06-06 11:07:53 -0600544#else
Cody Northropaf28a532016-09-27 10:50:57 -0600545
lenny-lunargfbe22392016-06-06 11:07:53 -0600546 printf("%s\n", message);
547 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600548
lenny-lunargfbe22392016-06-06 11:07:53 -0600549#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600550
lenny-lunargfbe22392016-06-06 11:07:53 -0600551 free(message);
552
Mark Young66510e52017-11-10 10:05:14 -0700553 // Don't bail out, but keep going.
lenny-lunargfbe22392016-06-06 11:07:53 -0600554 return false;
555}
556
Ian Elliottd940ca22017-03-22 08:31:27 -0600557bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
558 // The desired time was the earliest time that the present should have
559 // occured. In almost every case, the actual time should be later than the
560 // desired time. We should only consider the actual time "late" if it is
561 // after "desired + rdur".
562 if (actual <= desired) {
563 // The actual time was before or equal to the desired time. This will
564 // probably never happen, but in case it does, return false since the
565 // present was obviously NOT late.
566 return false;
567 }
Liam Middlebrook881fe392018-05-03 15:52:32 -0700568 uint64_t deadline = desired + rdur;
Ian Elliottd940ca22017-03-22 08:31:27 -0600569 if (actual > deadline) {
570 return true;
571 } else {
572 return false;
573 }
574}
Dave Houlton5fa47912018-02-16 11:02:26 -0700575bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600576 if (earliest < actual) {
577 // Consider whether this present could have occured earlier. Make sure
578 // that earliest time was at least 2msec earlier than actual time, and
579 // that the margin was at least 2msec:
580 uint64_t diff = actual - earliest;
581 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
582 // This present could have occured earlier because both: 1) the
583 // earliest time was at least 2 msec before actual time, and 2) the
584 // margin was at least 2msec.
585 return true;
586 }
587 }
588 return false;
589}
590
Tony-LunarG6cebf142019-09-11 14:32:23 -0600591// Forward declarations:
Ian Elliottcf074d32015-10-16 18:02:43 -0600592static void demo_resize(struct demo *demo);
Tony-LunarG6cebf142019-09-11 14:32:23 -0600593static void demo_create_surface(struct demo *demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600594
Dave Houlton5fa47912018-02-16 11:02:26 -0700595static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700596 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600597 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700598 if ((typeBits & 1) == 1) {
599 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700600 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700601 *typeIndex = i;
602 return true;
603 }
604 }
605 typeBits >>= 1;
606 }
607 // No memory types matched, return failure
608 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600609}
610
Karl Schultze4dc75c2016-02-02 15:37:51 -0700611static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600612 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600613
Tony Barbource7524e2016-07-28 12:01:45 -0600614 // This function could get called twice if the texture uses a staging buffer
615 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700616 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600617
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600618 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600619 assert(!err);
620
Tony Barbource7524e2016-07-28 12:01:45 -0600621 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700622 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700623 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
624 assert(!err);
625
Karl Schultze4dc75c2016-02-02 15:37:51 -0700626 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700627 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
628 .pNext = NULL,
629 .waitSemaphoreCount = 0,
630 .pWaitSemaphores = NULL,
631 .pWaitDstStageMask = NULL,
632 .commandBufferCount = 1,
633 .pCommandBuffers = cmd_bufs,
634 .signalSemaphoreCount = 0,
635 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600636
Tony Barbource7524e2016-07-28 12:01:45 -0600637 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600638 assert(!err);
639
Tony Barbource7524e2016-07-28 12:01:45 -0600640 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600641 assert(!err);
642
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600643 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600644 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600645 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600646}
647
Dave Houlton5fa47912018-02-16 11:02:26 -0700648static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
649 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600650 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600651 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600652
Dave Houlton5fa47912018-02-16 11:02:26 -0700653 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
654 .pNext = NULL,
655 .srcAccessMask = srcAccessMask,
656 .dstAccessMask = 0,
Tony-LunarGa141b962018-05-30 11:33:19 -0600657 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
658 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Dave Houlton5fa47912018-02-16 11:02:26 -0700659 .oldLayout = old_image_layout,
660 .newLayout = new_image_layout,
661 .image = image,
662 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600663
Tony Barboureb5077b2016-10-19 15:23:36 -0600664 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700665 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
666 /* Make sure anything that was copying from this image has completed */
667 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
668 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600669
Dave Houlton5fa47912018-02-16 11:02:26 -0700670 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
671 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
672 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700673
Dave Houlton5fa47912018-02-16 11:02:26 -0700674 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
675 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
676 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700677
Dave Houlton5fa47912018-02-16 11:02:26 -0700678 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
679 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
680 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600681
Dave Houlton5fa47912018-02-16 11:02:26 -0700682 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
683 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
684 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600685
Dave Houlton5fa47912018-02-16 11:02:26 -0700686 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
687 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
688 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600689
Dave Houlton5fa47912018-02-16 11:02:26 -0700690 default:
691 image_memory_barrier.dstAccessMask = 0;
692 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600693 }
694
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600695 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600696
Dave Houlton5fa47912018-02-16 11:02:26 -0700697 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600698}
699
Karl Schultze4dc75c2016-02-02 15:37:51 -0700700static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Lenny Komowe830b592018-02-23 16:18:36 -0700701 VkDebugUtilsLabelEXT label;
702 memset(&label, 0, sizeof(label));
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700703 const VkCommandBufferBeginInfo cmd_buf_info = {
704 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
705 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600706 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700707 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700708 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800709 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700710 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
711 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800712 };
713 const VkRenderPassBeginInfo rp_begin = {
714 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
715 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800716 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700717 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800718 .renderArea.offset.x = 0,
719 .renderArea.offset.y = 0,
720 .renderArea.extent.width = demo->width,
721 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600722 .clearValueCount = 2,
723 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700724 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800725 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600726
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600727 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Mark Young66510e52017-11-10 10:05:14 -0700728
729 if (demo->validate) {
730 // Set a name for the command buffer
731 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
732 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
733 .pNext = NULL,
734 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
735 .objectHandle = (uint64_t)cmd_buf,
736 .pObjectName = "CubeDrawCommandBuf",
737 };
738 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
739
740 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
741 label.pNext = NULL;
742 label.pLabelName = "DrawBegin";
743 label.color[0] = 0.4f;
744 label.color[1] = 0.3f;
745 label.color[2] = 0.2f;
746 label.color[3] = 0.1f;
747 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
748 }
749
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600750 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800751 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Mark Young66510e52017-11-10 10:05:14 -0700752
753 if (demo->validate) {
754 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
755 label.pNext = NULL;
756 label.pLabelName = "InsideRenderPass";
757 label.color[0] = 8.4f;
758 label.color[1] = 7.3f;
759 label.color[2] = 6.2f;
760 label.color[3] = 7.1f;
761 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
762 }
763
Karl Schultze4dc75c2016-02-02 15:37:51 -0700764 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700765 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
766 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600767 VkViewport viewport;
768 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700769 viewport.height = (float)demo->height;
770 viewport.width = (float)demo->width;
771 viewport.minDepth = (float)0.0f;
772 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700773 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600774
775 VkRect2D scissor;
776 memset(&scissor, 0, sizeof(scissor));
777 scissor.extent.width = demo->width;
778 scissor.extent.height = demo->height;
779 scissor.offset.x = 0;
780 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700781 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Mark Young66510e52017-11-10 10:05:14 -0700782
783 if (demo->validate) {
784 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
785 label.pNext = NULL;
786 label.pLabelName = "ActualDraw";
Mike Schuchardt4cf3aa42018-02-23 12:44:05 -0700787 label.color[0] = -0.4f;
788 label.color[1] = -0.3f;
789 label.color[2] = -0.2f;
790 label.color[3] = -0.1f;
Mark Young66510e52017-11-10 10:05:14 -0700791 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
792 }
793
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600794 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Mark Young66510e52017-11-10 10:05:14 -0700795 if (demo->validate) {
796 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
797 }
798
Tony Barbour98390392016-07-28 10:50:13 -0600799 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600800 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800801 vkCmdEndRenderPass(cmd_buf);
Mark Young66510e52017-11-10 10:05:14 -0700802 if (demo->validate) {
803 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
804 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600805
Tony Barbour22e06272016-09-07 16:07:56 -0600806 if (demo->separate_present_queue) {
807 // We have to transfer ownership from the graphics queue family to the
808 // present queue family to be able to present. Note that we don't have
809 // to transfer from present queue family back to graphics queue family at
810 // the start of the next frame because we don't care about the image's
811 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700812 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
813 .pNext = NULL,
814 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600815 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700816 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
817 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
818 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
819 .dstQueueFamilyIndex = demo->present_queue_family_index,
820 .image = demo->swapchain_image_resources[demo->current_buffer].image,
821 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600822
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600823 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
824 NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600825 }
Mark Young66510e52017-11-10 10:05:14 -0700826 if (demo->validate) {
827 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
828 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600829 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830 assert(!err);
831}
832
Tony Barbour22e06272016-09-07 16:07:56 -0600833void demo_build_image_ownership_cmd(struct demo *demo, int i) {
834 VkResult U_ASSERT_ONLY err;
835
836 const VkCommandBufferBeginInfo cmd_buf_info = {
837 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
838 .pNext = NULL,
839 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
840 .pInheritanceInfo = NULL,
841 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700842 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600843 assert(!err);
844
Dave Houlton5fa47912018-02-16 11:02:26 -0700845 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
846 .pNext = NULL,
847 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600848 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700849 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
850 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
851 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
852 .dstQueueFamilyIndex = demo->present_queue_family_index,
853 .image = demo->swapchain_image_resources[i].image,
854 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600855
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600856 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
857 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700858 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600859 assert(!err);
860}
861
Karl Schultze4dc75c2016-02-02 15:37:51 -0700862void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600863 mat4x4 MVP, Model, VP;
864 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600865 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600866 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600867
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600868 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600869
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700870 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600871 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700872 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600873 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600874
Dave Houlton5fa47912018-02-16 11:02:26 -0700875 err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0,
876 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600877 assert(!err);
878
Karl Schultze4dc75c2016-02-02 15:37:51 -0700879 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600880
Tony Barboura72d9492017-01-11 13:35:09 -0700881 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600882}
883
Ian Elliottd940ca22017-03-22 08:31:27 -0600884void DemoUpdateTargetIPD(struct demo *demo) {
885 // Look at what happened to previous presents, and make appropriate
886 // adjustments in timing:
887 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700888 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600889 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600890
Dave Houlton5fa47912018-02-16 11:02:26 -0700891 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600892 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600893 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700894 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600895 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700896 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600897 assert(!err);
898
899 bool early = false;
900 bool late = false;
901 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700902 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600903 if (!demo->syncd_with_actual_presents) {
904 // This is the first time that we've received an
905 // actualPresentTime for this swapchain. In order to not
906 // perceive these early frames as "late", we need to sync-up
907 // our future desiredPresentTime's with the
908 // actualPresentTime(s) that we're receiving now.
909 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600910
Ian Elliottd940ca22017-03-22 08:31:27 -0600911 // So that we don't suspect any pending presents as late,
912 // record them all as suspected-late presents:
913 demo->last_late_id = demo->next_present_id - 1;
914 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600915 demo->syncd_with_actual_presents = true;
916 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700917 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600918 demo->refresh_duration)) {
919 // This image could have been presented earlier. We don't want
920 // to decrease the target_IPD until we've seen early presents
921 // for at least two seconds.
922 if (demo->last_early_id == past[i].presentID) {
923 // We've now seen two seconds worth of early presents.
924 // Flag it as such, and reset the counter:
925 early = true;
926 demo->last_early_id = 0;
927 } else if (demo->last_early_id == 0) {
928 // This is the first early present we've seen.
929 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700930 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
931 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600932 demo->last_early_id = past[i].presentID + howManyPresents;
933 } else {
934 // We are in the midst of a set of early images,
935 // and so we won't do anything.
936 }
937 late = false;
938 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700939 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600940 // This image was presented after its desired time. Since
941 // there's a delay between calling vkQueuePresentKHR and when
942 // we get the timing data, several presents may have been late.
943 // Thus, we need to threat all of the outstanding presents as
944 // being likely late, so that we only increase the target_IPD
945 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700946 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600947 late = true;
948 // Record the last suspected-late present:
949 demo->last_late_id = demo->next_present_id - 1;
950 } else {
951 // We are in the midst of a set of likely-late images,
952 // and so we won't do anything.
953 }
954 early = false;
955 demo->last_early_id = 0;
956 } else {
957 // Since this image was not presented early or late, reset
958 // any sets of early or late presentIDs:
959 early = false;
960 late = false;
961 calibrate_next = true;
962 demo->last_early_id = 0;
963 demo->last_late_id = 0;
964 }
965 }
966
967 if (early) {
968 // Since we've seen at least two-seconds worth of presnts that
969 // could have occured earlier than desired, let's decrease the
970 // target_IPD (i.e. increase the frame rate):
971 //
972 // TODO(ianelliott): Try to calculate a better target_IPD based
973 // on the most recently-seen present (this is overly-simplistic).
974 demo->refresh_duration_multiplier--;
975 if (demo->refresh_duration_multiplier == 0) {
976 // This should never happen, but in case it does, don't
977 // try to go faster.
978 demo->refresh_duration_multiplier = 1;
979 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700980 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600981 }
982 if (late) {
983 // Since we found a new instance of a late present, we want to
984 // increase the target_IPD (i.e. decrease the frame rate):
985 //
986 // TODO(ianelliott): Try to calculate a better target_IPD based
987 // on the most recently-seen present (this is overly-simplistic).
988 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -0700989 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600990 }
991
992 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700993 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
994 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600995 }
Karl Schultza33771f2018-05-28 16:16:47 -0600996 free(past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600997 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600998}
999
Karl Schultze4dc75c2016-02-02 15:37:51 -07001000static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001001 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001002
Damien Leonec336d822017-04-14 16:10:14 -07001003 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +03001004 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1005 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001006
Tony Barbour8b7c9112017-06-29 13:34:41 -06001007 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -06001008 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -07001009 err =
1010 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1011 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001012
Tony Barbour6914e6d2017-06-02 12:11:29 -06001013 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1014 // demo->swapchain is out of date (e.g. the window was resized) and
1015 // must be recreated:
1016 demo_resize(demo);
1017 } else if (err == VK_SUBOPTIMAL_KHR) {
1018 // demo->swapchain is not as optimal as it could be, but the platform's
1019 // presentation engine will still present the image correctly.
1020 break;
Tony-LunarG6cebf142019-09-11 14:32:23 -06001021 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1022 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1023 demo_create_surface(demo);
1024 demo_resize(demo);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001025 } else {
1026 assert(!err);
1027 }
Tony Barbour8b7c9112017-06-29 13:34:41 -06001028 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001029
Tony Barbour3eafe1f2017-06-14 11:59:55 -06001030 demo_update_data_buffer(demo);
1031
Ian Elliottd940ca22017-03-22 08:31:27 -06001032 if (demo->VK_GOOGLE_display_timing_enabled) {
1033 // Look at what happened to previous presents, and make appropriate
1034 // adjustments in timing:
1035 DemoUpdateTargetIPD(demo);
1036
1037 // Note: a real application would position its geometry to that it's in
1038 // the correct locatoin for when the next image is presented. It might
1039 // also wait, so that there's less latency between any input and when
1040 // the next image is rendered/presented. This demo program is so
1041 // simple that it doesn't do either of those.
1042 }
1043
Tony Barbource7524e2016-07-28 12:01:45 -06001044 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001045 // that the image won't be rendered to until the presentation
1046 // engine has fully released ownership to the application, and it is
1047 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001048 VkPipelineStageFlags pipe_stage_flags;
1049 VkSubmitInfo submit_info;
1050 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1051 submit_info.pNext = NULL;
1052 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1053 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1054 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001055 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001056 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001057 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001058 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001059 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -07001060 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001061 assert(!err);
1062
Tony Barbour22e06272016-09-07 16:07:56 -06001063 if (demo->separate_present_queue) {
1064 // If we are using separate queues, change image ownership to the
1065 // present queue before presenting, waiting for the draw complete
1066 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001067 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001068 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1069 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001070 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001071 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001072 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001073 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001074 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001075 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1076 assert(!err);
1077 }
1078
1079 // If we are using separate queues we have to wait for image ownership,
1080 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001081 VkPresentInfoKHR present = {
1082 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001083 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001084 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -07001085 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1086 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001087 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001088 .pSwapchains = &demo->swapchain,
1089 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001090 };
1091
Dave Airlie48a68f92019-04-12 17:04:48 +10001092 VkRectLayerKHR rect;
1093 VkPresentRegionKHR region;
1094 VkPresentRegionsKHR regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001095 if (demo->VK_KHR_incremental_present_enabled) {
1096 // If using VK_KHR_incremental_present, we provide a hint of the region
1097 // that contains changed content relative to the previously-presented
1098 // image. The implementation can use this hint in order to save
1099 // work/power (by only copying the region in the hint). The
1100 // implementation is free to ignore the hint though, and so we must
1101 // ensure that the entire image has the correctly-drawn content.
1102 uint32_t eighthOfWidth = demo->width / 8;
1103 uint32_t eighthOfHeight = demo->height / 8;
Dave Airlie48a68f92019-04-12 17:04:48 +10001104
1105 rect.offset.x = eighthOfWidth;
1106 rect.offset.y = eighthOfHeight;
1107 rect.extent.width = eighthOfWidth * 6;
1108 rect.extent.height = eighthOfHeight * 6;
1109 rect.layer = 0;
1110
1111 region.rectangleCount = 1;
1112 region.pRectangles = &rect;
1113
1114 regions.sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR;
1115 regions.pNext = present.pNext;
1116 regions.swapchainCount = present.swapchainCount;
1117 regions.pRegions = &region;
Ian Elliott53622a72017-03-28 11:06:33 -06001118 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001119 }
1120
Ian Elliottd940ca22017-03-22 08:31:27 -06001121 if (demo->VK_GOOGLE_display_timing_enabled) {
1122 VkPresentTimeGOOGLE ptime;
1123 if (demo->prev_desired_present_time == 0) {
1124 // This must be the first present for this swapchain.
1125 //
1126 // We don't know where we are relative to the presentation engine's
1127 // display's refresh cycle. We also don't know how long rendering
1128 // takes. Let's make a grossly-simplified assumption that the
1129 // desiredPresentTime should be half way between now and
1130 // now+target_IPD. We will adjust over time.
1131 uint64_t curtime = getTimeInNanoseconds();
1132 if (curtime == 0) {
1133 // Since we didn't find out the current time, don't give a
1134 // desiredPresentTime:
1135 ptime.desiredPresentTime = 0;
1136 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001137 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1138 }
1139 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001140 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001141 }
1142 ptime.presentID = demo->next_present_id++;
1143 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001144
1145 VkPresentTimesInfoGOOGLE present_time = {
1146 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1147 .pNext = present.pNext,
1148 .swapchainCount = present.swapchainCount,
1149 .pTimes = &ptime,
1150 };
1151 if (demo->VK_GOOGLE_display_timing_enabled) {
1152 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001153 }
1154 }
1155
Tony Barboura2a67812016-07-18 13:21:06 -06001156 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001157 demo->frame_index += 1;
1158 demo->frame_index %= FRAME_LAG;
1159
Ian Elliottcf074d32015-10-16 18:02:43 -06001160 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1161 // demo->swapchain is out of date (e.g. the window was resized) and
1162 // must be recreated:
1163 demo_resize(demo);
1164 } else if (err == VK_SUBOPTIMAL_KHR) {
1165 // demo->swapchain is not as optimal as it could be, but the platform's
1166 // presentation engine will still present the image correctly.
Tony-LunarG6cebf142019-09-11 14:32:23 -06001167 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1168 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1169 demo_create_surface(demo);
1170 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06001171 } else {
1172 assert(!err);
1173 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001174}
1175
Karl Schultze4dc75c2016-02-02 15:37:51 -07001176static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001177 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001178 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001179
Ian Elliottb08e0d92015-11-20 11:55:46 -07001180 // Check the surface capabilities and formats
1181 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001182 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001183 assert(!err);
1184
Ian Elliott8528eff2015-08-07 15:56:59 -06001185 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001186 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001187 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001188 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001189 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001190 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001191 assert(!err);
1192
Ian Elliotta0781972015-08-21 15:09:33 -06001193 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001194 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1195 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1196 // If the surface size is undefined, the size is set to the size
1197 // of the images requested, which must fit within the minimum and
1198 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001199 swapchainExtent.width = demo->width;
1200 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001201
1202 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1203 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1204 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1205 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1206 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001207
Ian Elliottcf7f7482016-08-19 03:46:58 -06001208 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1209 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1210 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1211 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1212 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001213 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001214 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001215 swapchainExtent = surfCapabilities.currentExtent;
1216 demo->width = surfCapabilities.currentExtent.width;
1217 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001218 }
1219
Jeremy Kniager602e4182018-01-19 10:52:34 -07001220 if (demo->width == 0 || demo->height == 0) {
1221 demo->is_minimized = true;
1222 return;
1223 } else {
1224 demo->is_minimized = false;
1225 }
1226
Tony Barbour66a56c32016-09-21 13:10:56 -06001227 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001228 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001229 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001230
Ian Elliottb18fdde2016-10-03 12:11:12 -06001231 // There are times when you may wish to use another present mode. The
1232 // following code shows how to select them, and the comments provide some
1233 // reasons you may wish to use them.
1234 //
1235 // It should be noted that Vulkan 1.0 doesn't provide a method for
1236 // synchronizing rendering with the presentation engine's display. There
1237 // is a method provided for throttling rendering with the display, but
1238 // there are some presentation engines for which this method will not work.
1239 // If an application doesn't throttle its rendering, and if it renders much
1240 // faster than the refresh rate of the display, this can waste power on
1241 // mobile devices. That is because power is being spent rendering images
1242 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001243
Ian Elliottb18fdde2016-10-03 12:11:12 -06001244 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1245 // tearing, or have some way of synchronizing their rendering with the
1246 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001247 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1248 // generally render a new presentable image every refresh cycle, but are
1249 // occasionally early. In this case, the application wants the new image
1250 // to be displayed instead of the previously-queued-for-presentation image
1251 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001252 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1253 // render a new presentable image every refresh cycle, but are occasionally
1254 // late. In this case (perhaps because of stuttering/latency concerns),
1255 // the application wants the late image to be immediately displayed, even
1256 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001257
Dave Houlton5fa47912018-02-16 11:02:26 -07001258 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001259 for (size_t i = 0; i < presentModeCount; ++i) {
1260 if (presentModes[i] == demo->presentMode) {
1261 swapchainPresentMode = demo->presentMode;
1262 break;
1263 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001264 }
1265 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001266 if (swapchainPresentMode != demo->presentMode) {
1267 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1268 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001269
Tony Barbour84376fe2017-01-06 15:54:22 -07001270 // Determine the number of VkImages to use in the swap chain.
1271 // Application desires to acquire 3 images at a time for triple
1272 // buffering
1273 uint32_t desiredNumOfSwapchainImages = 3;
1274 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1275 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1276 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001277 // If maxImageCount is 0, we can ask for as many images as we want;
1278 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001279 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001280 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001281 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001282 }
1283
Tony Barbour9fd6d352015-09-16 15:01:32 -06001284 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001285 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001286 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001287 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001288 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001289 }
1290
Tony Barbour820b55b2017-03-14 08:55:46 -06001291 // Find a supported composite alpha mode - one of these is guaranteed to be set
1292 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1293 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1294 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1295 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1296 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1297 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1298 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001299 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001300 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1301 compositeAlpha = compositeAlphaFlags[i];
1302 break;
1303 }
1304 }
1305
Tony Barboura2a67812016-07-18 13:21:06 -06001306 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001307 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001308 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001309 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001310 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001311 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001312 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001313 .imageExtent =
1314 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001315 .width = swapchainExtent.width,
1316 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001317 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001318 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001319 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001320 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001321 .imageArrayLayers = 1,
1322 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1323 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001324 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001325 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001326 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001327 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001328 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001329 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001330 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001331 assert(!err);
1332
Ian Elliottcf074d32015-10-16 18:02:43 -06001333 // If we just re-created an existing swapchain, we should destroy the old
1334 // swapchain at this point.
1335 // Note: destroying the swapchain also cleans up all its associated
1336 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001337 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001338 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001339 }
1340
Dave Houlton5fa47912018-02-16 11:02:26 -07001341 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001342 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001343
Dave Houlton5fa47912018-02-16 11:02:26 -07001344 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001345 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001346 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001347 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001348
Dave Houlton5fa47912018-02-16 11:02:26 -07001349 demo->swapchain_image_resources =
1350 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001351 assert(demo->swapchain_image_resources);
1352
Ian Elliotta0781972015-08-21 15:09:33 -06001353 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001354 VkImageViewCreateInfo color_image_view = {
1355 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001356 .pNext = NULL,
1357 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001358 .components =
1359 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001360 .r = VK_COMPONENT_SWIZZLE_R,
1361 .g = VK_COMPONENT_SWIZZLE_G,
1362 .b = VK_COMPONENT_SWIZZLE_B,
1363 .a = VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001364 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001365 .subresourceRange =
1366 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001367 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1368 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001369 };
1370
Tony Barboura72d9492017-01-11 13:35:09 -07001371 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001372
Tony Barboura72d9492017-01-11 13:35:09 -07001373 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001374
Dave Houlton5fa47912018-02-16 11:02:26 -07001375 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001376 assert(!err);
1377 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001378
Ian Elliottd940ca22017-03-22 08:31:27 -06001379 if (demo->VK_GOOGLE_display_timing_enabled) {
1380 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001381 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001382 assert(!err);
1383 demo->refresh_duration = rc_dur.refreshDuration;
1384
1385 demo->syncd_with_actual_presents = false;
1386 // Initially target 1X the refresh duration:
1387 demo->target_IPD = demo->refresh_duration;
1388 demo->refresh_duration_multiplier = 1;
1389 demo->prev_desired_present_time = 0;
1390 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001391 }
1392
Jason Chen5d4fee92019-04-04 12:45:29 +08001393 if (NULL != swapchainImages) {
1394 free(swapchainImages);
1395 }
1396
Mark Young04fd3d82016-01-25 14:43:50 -07001397 if (NULL != presentModes) {
1398 free(presentModes);
1399 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400}
1401
Karl Schultze4dc75c2016-02-02 15:37:51 -07001402static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001403 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001404 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001405 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001407 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001409 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001410 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001411 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001412 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001413 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001414 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415 .flags = 0,
1416 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001417
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001418 VkImageViewCreateInfo view = {
1419 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001420 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001421 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001422 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001423 .subresourceRange =
1424 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001426 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001428
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001429 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001430 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001431 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432
1433 demo->depth.format = depth_format;
1434
1435 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001436 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001437 assert(!err);
1438
Karl Schultze4dc75c2016-02-02 15:37:51 -07001439 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001440 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001441
Chia-I Wuf48700b2015-11-10 16:21:09 +08001442 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001443 demo->depth.mem_alloc.pNext = NULL;
1444 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1445 demo->depth.mem_alloc.memoryTypeIndex = 0;
1446
Dave Houlton5fa47912018-02-16 11:02:26 -07001447 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001448 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001449 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001450
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001451 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001452 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001453 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001454
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001455 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001456 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001457 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001458
1459 /* create image view */
1460 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001461 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001462 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001463}
1464
Karl Schultzb7940402018-05-29 13:09:22 -06001465/* Convert ppm image data from header file into RGBA texture image */
1466#include "lunarg.ppm.h"
Dave Houlton5fa47912018-02-16 11:02:26 -07001467bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Karl Schultzb7940402018-05-29 13:09:22 -06001468 (void)filename;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001469 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001470 cPtr = (char *)lunarg_ppm;
1471 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001472 return false;
1473 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001474 while (strncmp(cPtr++, "\n", 1))
1475 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001476 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001477 if (rgba_data == NULL) {
1478 return true;
1479 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001480 while (strncmp(cPtr++, "\n", 1))
1481 ;
1482 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001483 return false;
1484 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001485 while (strncmp(cPtr++, "\n", 1))
1486 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001487 for (int y = 0; y < *height; y++) {
1488 uint8_t *rowPtr = rgba_data;
1489 for (int x = 0; x < *width; x++) {
1490 memcpy(rowPtr, cPtr, 3);
1491 rowPtr[3] = 255; /* Alpha of 1 */
1492 rowPtr += 4;
1493 cPtr += 3;
1494 }
1495 rgba_data += layout->rowPitch;
1496 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001497 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001498}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001499
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001500static void demo_prepare_texture_buffer(struct demo *demo, const char *filename, struct texture_object *tex_obj) {
1501 int32_t tex_width;
1502 int32_t tex_height;
1503 VkResult U_ASSERT_ONLY err;
1504 bool U_ASSERT_ONLY pass;
1505
1506 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
1507 ERR_EXIT("Failed to load textures", "Load Texture Failure");
1508 }
1509
1510 tex_obj->tex_width = tex_width;
1511 tex_obj->tex_height = tex_height;
1512
1513 const VkBufferCreateInfo buffer_create_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
1514 .pNext = NULL,
1515 .flags = 0,
1516 .size = tex_width * tex_height * 4,
1517 .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1518 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1519 .queueFamilyIndexCount = 0,
1520 .pQueueFamilyIndices = NULL};
1521
1522 err = vkCreateBuffer(demo->device, &buffer_create_info, NULL, &tex_obj->buffer);
1523 assert(!err);
1524
1525 VkMemoryRequirements mem_reqs;
1526 vkGetBufferMemoryRequirements(demo->device, tex_obj->buffer, &mem_reqs);
1527
1528 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1529 tex_obj->mem_alloc.pNext = NULL;
1530 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1531 tex_obj->mem_alloc.memoryTypeIndex = 0;
1532
1533 VkFlags requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1534 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, requirements, &tex_obj->mem_alloc.memoryTypeIndex);
1535 assert(pass);
1536
1537 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
1538 assert(!err);
1539
1540 /* bind memory */
1541 err = vkBindBufferMemory(demo->device, tex_obj->buffer, tex_obj->mem, 0);
1542 assert(!err);
1543
1544 VkSubresourceLayout layout;
1545 memset(&layout, 0, sizeof(layout));
1546 layout.rowPitch = tex_width * 4;
1547
1548 void *data;
1549 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
1550 assert(!err);
1551
1552 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1553 fprintf(stderr, "Error loading texture: %s\n", filename);
1554 }
1555
1556 vkUnmapMemory(demo->device, tex_obj->mem);
1557}
1558
Dave Houlton5fa47912018-02-16 11:02:26 -07001559static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1560 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001561 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001562 int32_t tex_width;
1563 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001564 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001565 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001566
Karl Schultze4dc75c2016-02-02 15:37:51 -07001567 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001568 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001569 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001570
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001571 tex_obj->tex_width = tex_width;
1572 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001573
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001574 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001575 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001576 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001577 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001578 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001579 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001580 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001581 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001582 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001583 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001584 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001585 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001586 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001587 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001588
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001589 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001590
Dave Houlton5fa47912018-02-16 11:02:26 -07001591 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001592 assert(!err);
1593
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001594 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001595
Chia-I Wuf48700b2015-11-10 16:21:09 +08001596 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001597 tex_obj->mem_alloc.pNext = NULL;
1598 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1599 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001600
Dave Houlton5fa47912018-02-16 11:02:26 -07001601 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001602 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001603
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001604 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001605 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001606 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001607
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001608 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001609 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001610 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001611
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001612 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001613 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001614 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001615 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001616 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001617 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001618 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001619 void *data;
1620
Dave Houlton5fa47912018-02-16 11:02:26 -07001621 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001622
Dave Houlton5fa47912018-02-16 11:02:26 -07001623 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001624 assert(!err);
1625
1626 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1627 fprintf(stderr, "Error loading texture: %s\n", filename);
1628 }
1629
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001630 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001631 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001632
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001633 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001634}
1635
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001636static void demo_destroy_texture(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001637 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001638 vkFreeMemory(demo->device, tex_objs->mem, NULL);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001639 if (tex_objs->image) vkDestroyImage(demo->device, tex_objs->image, NULL);
1640 if (tex_objs->buffer) vkDestroyBuffer(demo->device, tex_objs->buffer, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001641}
1642
Karl Schultze4dc75c2016-02-02 15:37:51 -07001643static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001644 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001645 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001646 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001647
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001648 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001649
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001650 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001651 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001652
Dave Houlton5fa47912018-02-16 11:02:26 -07001653 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001654 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001655 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1656 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001657 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1658 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001659 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1660 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001661 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001662 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001663 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001664 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001665
Tony Barbour16156cb2016-10-19 14:15:49 -06001666 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001667 demo_prepare_texture_buffer(demo, tex_files[i], &demo->staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001668
Dave Houlton5fa47912018-02-16 11:02:26 -07001669 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1670 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1671 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001672
Dave Houlton5fa47912018-02-16 11:02:26 -07001673 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1674 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001675 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001676
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001677 VkBufferImageCopy copy_region = {
1678 .bufferOffset = 0,
1679 .bufferRowLength = demo->staging_texture.tex_width,
1680 .bufferImageHeight = demo->staging_texture.tex_height,
1681 .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1682 .imageOffset = {0, 0, 0},
1683 .imageExtent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001684 };
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001685
1686 vkCmdCopyBufferToImage(demo->cmd, demo->staging_texture.buffer, demo->textures[i].image,
1687 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001688
Dave Houlton5fa47912018-02-16 11:02:26 -07001689 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1690 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001691 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001692
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001693 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001694 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1695 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001696 }
1697
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001698 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001699 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001700 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001701 .magFilter = VK_FILTER_NEAREST,
1702 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001703 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001704 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1705 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1706 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001708 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001709 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001710 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001711 .minLod = 0.0f,
1712 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001713 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001714 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001715 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001716
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001717 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001718 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001720 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001721 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001722 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001723 .components =
1724 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001725 VK_COMPONENT_SWIZZLE_R,
1726 VK_COMPONENT_SWIZZLE_G,
1727 VK_COMPONENT_SWIZZLE_B,
1728 VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001729 },
1730 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001731 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001732 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001733
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001735 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001736 assert(!err);
1737
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001738 /* create image view */
1739 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001740 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001741 assert(!err);
1742 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001743}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744
Tony Barboura72d9492017-01-11 13:35:09 -07001745void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001746 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001747 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001748 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001749 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001750 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001751 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001752 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001753 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001754
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001755 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001756 mat4x4_mul(MVP, VP, demo->model_matrix);
1757 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001758 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001759
Karl Schultza2615462017-01-20 13:19:20 -07001760 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001761 data.position[i][0] = g_vertex_buffer_data[i * 3];
1762 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1763 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001764 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001765 data.attr[i][0] = g_uv_buffer_data[2 * i];
1766 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001767 data.attr[i][2] = 0;
1768 data.attr[i][3] = 0;
1769 }
1770
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001771 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001772 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001773 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001774 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001775
Tony Barboura72d9492017-01-11 13:35:09 -07001776 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001777 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001778 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001779
Dave Houlton5fa47912018-02-16 11:02:26 -07001780 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001781
Tony Barboura72d9492017-01-11 13:35:09 -07001782 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1783 mem_alloc.pNext = NULL;
1784 mem_alloc.allocationSize = mem_reqs.size;
1785 mem_alloc.memoryTypeIndex = 0;
1786
Dave Houlton5fa47912018-02-16 11:02:26 -07001787 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1788 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1789 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001790 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001791
Dave Houlton5fa47912018-02-16 11:02:26 -07001792 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001793 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001794
Dave Houlton5fa47912018-02-16 11:02:26 -07001795 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, 0, (void **)&pData);
Tony Barboura72d9492017-01-11 13:35:09 -07001796 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001797
Tony Barboura72d9492017-01-11 13:35:09 -07001798 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001799
Tony Barboura72d9492017-01-11 13:35:09 -07001800 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001801
Tony Barboura72d9492017-01-11 13:35:09 -07001802 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001803 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001804 assert(!err);
1805 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001806}
1807
Karl Schultze4dc75c2016-02-02 15:37:51 -07001808static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001809 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001810 [0] =
1811 {
1812 .binding = 0,
1813 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1814 .descriptorCount = 1,
1815 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1816 .pImmutableSamplers = NULL,
1817 },
1818 [1] =
1819 {
1820 .binding = 1,
1821 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1822 .descriptorCount = DEMO_TEXTURE_COUNT,
1823 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1824 .pImmutableSamplers = NULL,
1825 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001826 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001827 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001828 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001829 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001830 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001831 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001832 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001833 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001834
Dave Houlton5fa47912018-02-16 11:02:26 -07001835 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001836 assert(!err);
1837
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001838 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001839 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1840 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001841 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001842 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001843 };
1844
Dave Houlton5fa47912018-02-16 11:02:26 -07001845 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001846 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001847}
1848
Karl Schultze4dc75c2016-02-02 15:37:51 -07001849static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001850 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1851 // because at the start of the renderpass, we don't care about their contents.
1852 // At the start of the subpass, the color attachment's layout will be transitioned
1853 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1854 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1855 // the renderpass, the color attachment's layout will be transitioned to
1856 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1857 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001858 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001859 [0] =
1860 {
1861 .format = demo->format,
1862 .flags = 0,
1863 .samples = VK_SAMPLE_COUNT_1_BIT,
1864 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1865 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1866 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1867 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1868 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1869 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1870 },
1871 [1] =
1872 {
1873 .format = demo->depth.format,
1874 .flags = 0,
1875 .samples = VK_SAMPLE_COUNT_1_BIT,
1876 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1877 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1878 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1879 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1880 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1881 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1882 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001883 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001884 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001885 .attachment = 0,
1886 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001887 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001888 const VkAttachmentReference depth_reference = {
1889 .attachment = 1,
1890 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1891 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001892 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001893 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1894 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001895 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001896 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001897 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001898 .pColorAttachments = &color_reference,
1899 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001900 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001901 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001902 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001903 };
Tony-LunarG495604b2019-06-13 15:32:05 -06001904
1905 VkSubpassDependency attachmentDependencies[2] = {
1906 [0] =
1907 {
1908 // Depth buffer is shared between swapchain images
1909 .srcSubpass = VK_SUBPASS_EXTERNAL,
1910 .dstSubpass = 0,
1911 .srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1912 .dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1913 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1914 .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1915 .dependencyFlags = 0,
1916 },
1917 [1] =
1918 {
1919 // Image Layout Transition
1920 .srcSubpass = VK_SUBPASS_EXTERNAL,
1921 .dstSubpass = 0,
1922 .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1923 .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1924 .srcAccessMask = 0,
1925 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
1926 .dependencyFlags = 0,
1927 },
1928 };
1929
Chia-I Wuf973e322015-07-08 13:34:24 +08001930 const VkRenderPassCreateInfo rp_info = {
1931 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1932 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001933 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001934 .attachmentCount = 2,
1935 .pAttachments = attachments,
1936 .subpassCount = 1,
1937 .pSubpasses = &subpass,
Tony-LunarG495604b2019-06-13 15:32:05 -06001938 .dependencyCount = 2,
1939 .pDependencies = attachmentDependencies,
Chia-I Wuf973e322015-07-08 13:34:24 +08001940 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001941 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001942
Chia-I Wu63636062015-10-26 21:10:41 +08001943 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001944 assert(!err);
1945}
1946
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001947static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001948 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001949 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001950 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001951
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001952 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1953 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001954 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001955 moduleCreateInfo.codeSize = size;
1956 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001957
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001958 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1959 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001960
1961 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001962}
1963
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001964static void demo_prepare_vs(struct demo *demo) {
1965 const uint32_t vs_code[] = {
1966#include "cube.vert.inc"
1967 };
1968 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001969}
1970
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001971static void demo_prepare_fs(struct demo *demo) {
1972 const uint32_t fs_code[] = {
1973#include "cube.frag.inc"
1974 };
1975 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001976}
1977
Karl Schultze4dc75c2016-02-02 15:37:51 -07001978static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001979 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001980 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001981 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001982 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001983 VkPipelineRasterizationStateCreateInfo rs;
1984 VkPipelineColorBlendStateCreateInfo cb;
1985 VkPipelineDepthStencilStateCreateInfo ds;
1986 VkPipelineViewportStateCreateInfo vp;
1987 VkPipelineMultisampleStateCreateInfo ms;
1988 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1989 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001990 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001991
Piers Daniellba839d12015-09-29 13:01:09 -06001992 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1993 memset(&dynamicState, 0, sizeof dynamicState);
1994 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1995 dynamicState.pDynamicStates = dynamicStateEnables;
1996
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001997 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001998 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001999 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002000
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002001 memset(&vi, 0, sizeof(vi));
2002 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2003
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002004 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002005 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002006 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002007
2008 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002009 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2010 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002011 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002012 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002013 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002014 rs.rasterizerDiscardEnable = VK_FALSE;
2015 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002016 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002017
2018 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002019 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2020 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002021 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002022 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002023 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002024 cb.attachmentCount = 1;
2025 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002026
Tony Barbour29645d02015-01-16 14:27:35 -07002027 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002028 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002029 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002030 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002031 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002032 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002033
2034 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002035 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002036 ds.depthTestEnable = VK_TRUE;
2037 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002038 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002039 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002040 ds.back.failOp = VK_STENCIL_OP_KEEP;
2041 ds.back.passOp = VK_STENCIL_OP_KEEP;
2042 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002043 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002044 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002045
Tony Barbour29645d02015-01-16 14:27:35 -07002046 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002047 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002048 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002049 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002051 demo_prepare_vs(demo);
2052 demo_prepare_fs(demo);
2053
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002054 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002055 VkPipelineShaderStageCreateInfo shaderStages[2];
2056 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2057
Karl Schultze4dc75c2016-02-02 15:37:51 -07002058 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2059 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002060 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002061 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002062
Karl Schultze4dc75c2016-02-02 15:37:51 -07002063 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2064 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002065 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002066 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002067
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002068 memset(&pipelineCache, 0, sizeof(pipelineCache));
2069 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002070
Dave Houlton5fa47912018-02-16 11:02:26 -07002071 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002072 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002073
Karl Schultze4dc75c2016-02-02 15:37:51 -07002074 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002075 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002076 pipeline.pRasterizationState = &rs;
2077 pipeline.pColorBlendState = &cb;
2078 pipeline.pMultisampleState = &ms;
2079 pipeline.pViewportState = &vp;
2080 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002081 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002082 pipeline.pStages = shaderStages;
2083 pipeline.renderPass = demo->render_pass;
2084 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002085
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002086 pipeline.renderPass = demo->render_pass;
2087
Dave Houlton5fa47912018-02-16 11:02:26 -07002088 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002089 assert(!err);
2090
Chia-I Wu63636062015-10-26 21:10:41 +08002091 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2092 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002093}
2094
Karl Schultze4dc75c2016-02-02 15:37:51 -07002095static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002096 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07002097 [0] =
2098 {
2099 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2100 .descriptorCount = demo->swapchainImageCount,
2101 },
2102 [1] =
2103 {
2104 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2105 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2106 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002107 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002108 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002109 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002110 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002111 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002112 .poolSizeCount = 2,
2113 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002114 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002115 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002116
Dave Houlton5fa47912018-02-16 11:02:26 -07002117 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002118 assert(!err);
2119}
2120
Karl Schultze4dc75c2016-02-02 15:37:51 -07002121static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002122 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002123 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002124 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002125
Dave Houlton5fa47912018-02-16 11:02:26 -07002126 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2127 .pNext = NULL,
2128 .descriptorPool = demo->desc_pool,
2129 .descriptorSetCount = 1,
2130 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002131
2132 VkDescriptorBufferInfo buffer_info;
2133 buffer_info.offset = 0;
2134 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002135
Chia-I Wuae721ba2015-05-25 16:27:55 +08002136 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002137 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002138 tex_descs[i].sampler = demo->textures[i].sampler;
2139 tex_descs[i].imageView = demo->textures[i].view;
Karl Schultze88bc842018-09-11 16:23:14 -06002140 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002141 }
2142
2143 memset(&writes, 0, sizeof(writes));
2144
2145 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002146 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002147 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002148 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002149
2150 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002151 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002152 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002153 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002154 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002155
Tony Barboura72d9492017-01-11 13:35:09 -07002156 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2157 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2158 assert(!err);
2159 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2160 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2161 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2162 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2163 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002164}
2165
Karl Schultze4dc75c2016-02-02 15:37:51 -07002166static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002167 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002168 attachments[1] = demo->depth.view;
2169
Chia-I Wuf973e322015-07-08 13:34:24 +08002170 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002171 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2172 .pNext = NULL,
2173 .renderPass = demo->render_pass,
2174 .attachmentCount = 2,
2175 .pAttachments = attachments,
2176 .width = demo->width,
2177 .height = demo->height,
2178 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002179 };
2180 VkResult U_ASSERT_ONLY err;
2181 uint32_t i;
2182
Tony Barbourd8e504f2015-10-08 14:26:24 -06002183 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002184 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002185 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002186 assert(!err);
2187 }
2188}
2189
Karl Schultze4dc75c2016-02-02 15:37:51 -07002190static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002191 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002192 if (demo->cmd_pool == VK_NULL_HANDLE) {
2193 const VkCommandPoolCreateInfo cmd_pool_info = {
2194 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2195 .pNext = NULL,
2196 .queueFamilyIndex = demo->graphics_queue_family_index,
2197 .flags = 0,
2198 };
2199 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2200 assert(!err);
2201 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002202
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002203 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002204 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002205 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002206 .commandPool = demo->cmd_pool,
2207 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002208 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002209 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002210 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2211 assert(!err);
2212 VkCommandBufferBeginInfo cmd_buf_info = {
2213 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2214 .pNext = NULL,
2215 .flags = 0,
2216 .pInheritanceInfo = NULL,
2217 };
2218 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2219 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002220
2221 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002222
2223 if (demo->is_minimized) {
2224 demo->prepared = false;
2225 return;
2226 }
2227
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002228 demo_prepare_depth(demo);
2229 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002230 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002231
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002232 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002233 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002234 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002235
Ian Elliotta0781972015-08-21 15:09:33 -06002236 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002237 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002238 assert(!err);
2239 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002240
Tony Barbour22e06272016-09-07 16:07:56 -06002241 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002242 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002243 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2244 .pNext = NULL,
2245 .queueFamilyIndex = demo->present_queue_family_index,
2246 .flags = 0,
2247 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002248 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002249 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002250 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002251 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2252 .pNext = NULL,
2253 .commandPool = demo->present_cmd_pool,
2254 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2255 .commandBufferCount = 1,
2256 };
2257 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002258 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2259 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002260 assert(!err);
2261 demo_build_image_ownership_cmd(demo, i);
2262 }
2263 }
2264
Chia-I Wu63ea9262015-03-26 13:14:16 +08002265 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002266 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002267
Chia-I Wuf973e322015-07-08 13:34:24 +08002268 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002269
Ian Elliotta0781972015-08-21 15:09:33 -06002270 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002271 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002272 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002273 }
2274
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002275 /*
2276 * Prepare functions above may generate pipeline commands
2277 * that need to be flushed before beginning the render loop.
2278 */
2279 demo_flush_init_cmd(demo);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06002280 if (demo->staging_texture.buffer) {
2281 demo_destroy_texture(demo, &demo->staging_texture);
Tony Barbour16156cb2016-10-19 14:15:49 -06002282 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002283
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002284 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002285 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002286}
2287
Karl Schultze4dc75c2016-02-02 15:37:51 -07002288static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002289 uint32_t i;
2290
2291 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002292 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002293
Tony Barbour66a56c32016-09-21 13:10:56 -06002294 // Wait for fences from present operations
2295 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002296 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002297 vkDestroyFence(demo->device, demo->fences[i], NULL);
2298 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2299 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2300 if (demo->separate_present_queue) {
2301 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2302 }
2303 }
2304
Jeremy Kniager602e4182018-01-19 10:52:34 -07002305 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2306 if (!demo->is_minimized) {
2307 for (i = 0; i < demo->swapchainImageCount; i++) {
2308 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2309 }
2310 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002311
Jeremy Kniager602e4182018-01-19 10:52:34 -07002312 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2313 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2314 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2315 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2316 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002317
Jeremy Kniager602e4182018-01-19 10:52:34 -07002318 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2319 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2320 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2321 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2322 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2323 }
2324 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002325
Jeremy Kniager602e4182018-01-19 10:52:34 -07002326 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2327 vkDestroyImage(demo->device, demo->depth.image, NULL);
2328 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002329
Jeremy Kniager602e4182018-01-19 10:52:34 -07002330 for (i = 0; i < demo->swapchainImageCount; i++) {
2331 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2332 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2333 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2334 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2335 }
2336 free(demo->swapchain_image_resources);
2337 free(demo->queue_props);
2338 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002339
Jeremy Kniager602e4182018-01-19 10:52:34 -07002340 if (demo->separate_present_queue) {
2341 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2342 }
Tony Barbour22e06272016-09-07 16:07:56 -06002343 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002344 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002345 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002346 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07002347 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002348 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002349 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002350
Tony Barbour153cb062016-12-07 13:43:36 -07002351#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002352 XDestroyWindow(demo->display, demo->xlib_window);
2353 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002354#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002355 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002356 xcb_disconnect(demo->connection);
2357 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002358#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002359 wl_keyboard_destroy(demo->keyboard);
2360 wl_pointer_destroy(demo->pointer);
2361 wl_seat_destroy(demo->seat);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002362 wl_shell_surface_destroy(demo->shell_surface);
2363 wl_surface_destroy(demo->window);
2364 wl_shell_destroy(demo->shell);
2365 wl_compositor_destroy(demo->compositor);
2366 wl_registry_destroy(demo->registry);
2367 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002368#endif
Karl Schultz86429112017-06-20 11:27:59 -06002369
2370 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002371}
2372
Karl Schultze4dc75c2016-02-02 15:37:51 -07002373static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002374 uint32_t i;
2375
Mike Stroyanbb60b382015-10-28 09:46:57 -06002376 // Don't react to resize until after first initialization.
2377 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002378 if (demo->is_minimized) {
2379 demo_prepare(demo);
2380 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002381 return;
2382 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002383 // In order to properly resize the window, we must re-create the swapchain
2384 // AND redo the command buffers, etc.
2385 //
2386 // First, perform part of the demo_cleanup() function:
2387 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002388 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002389
2390 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002391 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002392 }
Chia-I Wu63636062015-10-26 21:10:41 +08002393 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002394
Chia-I Wu63636062015-10-26 21:10:41 +08002395 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2396 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2397 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2398 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2399 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002400
2401 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002402 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2403 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2404 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2405 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002406 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002407
Chia-I Wu63636062015-10-26 21:10:41 +08002408 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2409 vkDestroyImage(demo->device, demo->depth.image, NULL);
2410 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002411
Ian Elliottcf074d32015-10-16 18:02:43 -06002412 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002413 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002414 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002415 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2416 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002417 }
Chia-I Wu63636062015-10-26 21:10:41 +08002418 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002419 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002420 if (demo->separate_present_queue) {
2421 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2422 }
Tony Barboura72d9492017-01-11 13:35:09 -07002423 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002424
Ian Elliottcf074d32015-10-16 18:02:43 -06002425 // Second, re-perform the demo_prepare() function, which will re-create the
2426 // swapchain:
2427 demo_prepare(demo);
2428}
2429
David Pinedo2bb7c932015-06-18 17:03:14 -06002430// On MS-Windows, make this a global, so it's available to WndProc()
2431struct demo demo;
2432
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002433#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002434static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002435 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002436
Ian Elliott639ca472015-04-16 15:23:05 -06002437 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002438 demo->curFrame++;
Petr Krausd88e4e52019-01-23 18:45:04 +01002439 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002440 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002441 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002442}
Ian Elliott639ca472015-04-16 15:23:05 -06002443
2444// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002445LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2446 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002447 case WM_CLOSE:
2448 PostQuitMessage(validation_error);
2449 break;
2450 case WM_PAINT:
2451 // The validation callback calls MessageBox which can generate paint
2452 // events - don't make more Vulkan calls if we got here from the
2453 // callback
2454 if (!in_callback) {
2455 demo_run(&demo);
2456 }
2457 break;
2458 case WM_GETMINMAXINFO: // set window's minimum size
2459 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2460 return 0;
Aaron Hagan500b9c32018-10-03 21:56:29 -04002461 case WM_ERASEBKGND:
2462 return 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002463 case WM_SIZE:
2464 // Resize the application to the new window size, except when
2465 // it was minimized. Vulkan doesn't support images or swapchains
2466 // with width=0 and height=0.
2467 if (wParam != SIZE_MINIMIZED) {
2468 demo.width = lParam & 0xffff;
2469 demo.height = (lParam & 0xffff0000) >> 16;
2470 demo_resize(&demo);
2471 }
2472 break;
Petr Krausd88e4e52019-01-23 18:45:04 +01002473 case WM_KEYDOWN:
2474 switch (wParam) {
2475 case VK_ESCAPE:
2476 PostQuitMessage(validation_error);
2477 break;
2478 case VK_LEFT:
2479 demo.spin_angle -= demo.spin_increment;
2480 break;
2481 case VK_RIGHT:
2482 demo.spin_angle += demo.spin_increment;
2483 break;
2484 case VK_SPACE:
2485 demo.pause = !demo.pause;
2486 break;
2487 }
2488 return 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002489 default:
2490 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002491 }
2492 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2493}
2494
Karl Schultze4dc75c2016-02-02 15:37:51 -07002495static void demo_create_window(struct demo *demo) {
2496 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002497
2498 // Initialize the window class structure:
2499 win_class.cbSize = sizeof(WNDCLASSEX);
2500 win_class.style = CS_HREDRAW | CS_VREDRAW;
2501 win_class.lpfnWndProc = WndProc;
2502 win_class.cbClsExtra = 0;
2503 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002504 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002505 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2506 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2507 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2508 win_class.lpszMenuName = NULL;
2509 win_class.lpszClassName = demo->name;
2510 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2511 // Register window class:
2512 if (!RegisterClassEx(&win_class)) {
2513 // It didn't work, so try to give a useful error:
2514 printf("Unexpected error trying to start the application!\n");
2515 fflush(stdout);
2516 exit(1);
2517 }
2518 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002519 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002520 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002521 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002522 demo->name, // class name
2523 demo->name, // app name
2524 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002525 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002526 100, 100, // x/y coords
2527 wr.right - wr.left, // width
2528 wr.bottom - wr.top, // height
2529 NULL, // handle to parent
2530 NULL, // handle to menu
2531 demo->connection, // hInstance
2532 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002533 if (!demo->window) {
2534 // It didn't work, so try to give a useful error:
2535 printf("Cannot create a window in which to draw!\n");
2536 fflush(stdout);
2537 exit(1);
2538 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002539 // Window client area size must be at least 1 pixel high, to prevent crash.
2540 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002541 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002542}
Tony Barbour8295ac42016-08-08 11:04:17 -06002543#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002544static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002545 const char *display_envar = getenv("DISPLAY");
2546 if (display_envar == NULL || display_envar[0] == '\0') {
2547 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2548 fflush(stdout);
2549 exit(1);
2550 }
Tony Barbour2593b182016-04-19 10:57:58 -06002551
Tony Barbouree9cd372017-01-31 16:09:58 -07002552 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002553 demo->display = XOpenDisplay(NULL);
2554 long visualMask = VisualScreenMask;
2555 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002556 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002557 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002558 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002559
Dave Houlton5fa47912018-02-16 11:02:26 -07002560 Colormap colormap =
2561 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002562
Dave Houlton5fa47912018-02-16 11:02:26 -07002563 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002564 windowAttributes.colormap = colormap;
2565 windowAttributes.background_pixel = 0xFFFFFFFF;
2566 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002567 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002568
Dave Houlton5fa47912018-02-16 11:02:26 -07002569 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2570 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2571 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002572
2573 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2574 XMapWindow(demo->display, demo->xlib_window);
2575 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002576 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002577}
2578static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002579 switch (event->type) {
2580 case ClientMessage:
2581 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002582 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002583 case KeyPress:
2584 switch (event->xkey.keycode) {
2585 case 0x9: // Escape
2586 demo->quit = true;
2587 break;
2588 case 0x71: // left arrow key
2589 demo->spin_angle -= demo->spin_increment;
2590 break;
2591 case 0x72: // right arrow key
2592 demo->spin_angle += demo->spin_increment;
2593 break;
2594 case 0x41: // space bar
2595 demo->pause = !demo->pause;
2596 break;
2597 }
Tony Barbour2593b182016-04-19 10:57:58 -06002598 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002599 case ConfigureNotify:
2600 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2601 demo->width = event->xconfigure.width;
2602 demo->height = event->xconfigure.height;
2603 demo_resize(demo);
2604 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002605 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002606 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002607 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002608 }
Tony Barbour2593b182016-04-19 10:57:58 -06002609}
2610
2611static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002612 while (!demo->quit) {
2613 XEvent event;
2614
2615 if (demo->pause) {
2616 XNextEvent(demo->display, &event);
2617 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002618 }
2619 while (XPending(demo->display) > 0) {
2620 XNextEvent(demo->display, &event);
2621 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002622 }
2623
Tony Barbour2593b182016-04-19 10:57:58 -06002624 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002625 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002626 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002627 }
2628}
Tony Barbour153cb062016-12-07 13:43:36 -07002629#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002630static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002631 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002632 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002633 case XCB_EXPOSE:
2634 // TODO: Resize window
2635 break;
2636 case XCB_CLIENT_MESSAGE:
2637 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2638 demo->quit = true;
2639 }
2640 break;
2641 case XCB_KEY_RELEASE: {
2642 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002643
Dave Houlton5fa47912018-02-16 11:02:26 -07002644 switch (key->detail) {
2645 case 0x9: // Escape
2646 demo->quit = true;
2647 break;
2648 case 0x71: // left arrow key
2649 demo->spin_angle -= demo->spin_increment;
2650 break;
2651 case 0x72: // right arrow key
2652 demo->spin_angle += demo->spin_increment;
2653 break;
2654 case 0x41: // space bar
2655 demo->pause = !demo->pause;
2656 break;
2657 }
2658 } break;
2659 case XCB_CONFIGURE_NOTIFY: {
2660 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2661 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2662 demo->width = cfg->width;
2663 demo->height = cfg->height;
2664 demo_resize(demo);
2665 }
2666 } break;
2667 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002668 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002669 }
2670}
2671
Tony Barbour2593b182016-04-19 10:57:58 -06002672static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002673 xcb_flush(demo->connection);
2674
2675 while (!demo->quit) {
2676 xcb_generic_event_t *event;
2677
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002678 if (demo->pause) {
2679 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002680 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002681 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002682 }
2683 while (event) {
2684 demo_handle_xcb_event(demo, event);
2685 free(event);
2686 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002687 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002688
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002689 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002690 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002691 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002692 }
2693}
2694
Tony Barbour2593b182016-04-19 10:57:58 -06002695static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002696 uint32_t value_mask, value_list[32];
2697
Tony Barbour2593b182016-04-19 10:57:58 -06002698 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002699
2700 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2701 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002702 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002703
Dave Houlton5fa47912018-02-16 11:02:26 -07002704 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2705 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002706
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002707 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002708 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2709 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002710
Dave Houlton5fa47912018-02-16 11:02:26 -07002711 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2712 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002713
Dave Houlton5fa47912018-02-16 11:02:26 -07002714 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002715 &(*demo->atom_wm_delete_window).atom);
2716 free(reply);
2717
Tony Barbour2593b182016-04-19 10:57:58 -06002718 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002719
Karl Schultze4dc75c2016-02-02 15:37:51 -07002720 // Force the x/y coordinates to 100,100 results are identical in consecutive
2721 // runs
2722 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002723 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002724}
Tony Barbour6b131662016-08-25 13:14:45 -06002725// VK_USE_PLATFORM_XCB_KHR
2726#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002727static void demo_run(struct demo *demo) {
2728 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002729 if (demo->pause) {
2730 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002731 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002732 wl_display_dispatch_pending(demo->display); // don't block
2733 demo_draw(demo);
2734 demo->curFrame++;
2735 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2736 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002737 }
2738}
2739
Dave Houlton5fa47912018-02-16 11:02:26 -07002740static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002741 wl_shell_surface_pong(shell_surface, serial);
2742}
2743
Dave Houlton5fa47912018-02-16 11:02:26 -07002744static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED,
2745 int32_t width UNUSED, int32_t height UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002746
Dave Houlton5fa47912018-02-16 11:02:26 -07002747static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002748
Dave Houlton5fa47912018-02-16 11:02:26 -07002749static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002750
2751static void demo_create_window(struct demo *demo) {
2752 demo->window = wl_compositor_create_surface(demo->compositor);
2753 if (!demo->window) {
2754 printf("Can not create wayland_surface from compositor!\n");
2755 fflush(stdout);
2756 exit(1);
2757 }
2758
2759 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2760 if (!demo->shell_surface) {
2761 printf("Can not get shell_surface from wayland_surface!\n");
2762 fflush(stdout);
2763 exit(1);
2764 }
Dave Houlton5fa47912018-02-16 11:02:26 -07002765 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002766 wl_shell_surface_set_toplevel(demo->shell_surface);
2767 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2768}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002769#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2770static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002771 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002772
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002773 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002774 demo->curFrame++;
2775}
Karl Schultz206b1c52018-04-13 18:02:07 -06002776#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2777static void demo_run(struct demo *demo) {
2778 demo_draw(demo);
2779 demo->curFrame++;
2780 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2781 demo->quit = TRUE;
2782 }
2783}
Damien Leone600c3052017-01-31 10:26:07 -07002784#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2785static VkResult demo_create_display_surface(struct demo *demo) {
2786 VkResult U_ASSERT_ONLY err;
2787 uint32_t display_count;
2788 uint32_t mode_count;
2789 uint32_t plane_count;
2790 VkDisplayPropertiesKHR display_props;
2791 VkDisplayKHR display;
2792 VkDisplayModePropertiesKHR mode_props;
2793 VkDisplayPlanePropertiesKHR *plane_props;
2794 VkBool32 found_plane = VK_FALSE;
2795 uint32_t plane_index;
2796 VkExtent2D image_extent;
2797 VkDisplaySurfaceCreateInfoKHR create_info;
2798
2799 // Get the first display
2800 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2801 assert(!err);
2802
2803 if (display_count == 0) {
2804 printf("Cannot find any display!\n");
2805 fflush(stdout);
2806 exit(1);
2807 }
2808
2809 display_count = 1;
2810 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2811 assert(!err || (err == VK_INCOMPLETE));
2812
2813 display = display_props.display;
2814
2815 // Get the first mode of the display
2816 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2817 assert(!err);
2818
2819 if (mode_count == 0) {
2820 printf("Cannot find any mode for the display!\n");
2821 fflush(stdout);
2822 exit(1);
2823 }
2824
2825 mode_count = 1;
2826 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2827 assert(!err || (err == VK_INCOMPLETE));
2828
2829 // Get the list of planes
2830 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2831 assert(!err);
2832
2833 if (plane_count == 0) {
2834 printf("Cannot find any plane!\n");
2835 fflush(stdout);
2836 exit(1);
2837 }
2838
2839 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2840 assert(plane_props);
2841
2842 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2843 assert(!err);
2844
2845 // Find a plane compatible with the display
2846 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2847 uint32_t supported_count;
2848 VkDisplayKHR *supported_displays;
2849
2850 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002851 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002852 continue;
2853 }
2854
2855 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2856 assert(!err);
2857
2858 if (supported_count == 0) {
2859 continue;
2860 }
2861
2862 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2863 assert(supported_displays);
2864
2865 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2866 assert(!err);
2867
2868 for (uint32_t i = 0; i < supported_count; i++) {
2869 if (supported_displays[i] == display) {
2870 found_plane = VK_TRUE;
2871 break;
2872 }
2873 }
2874
2875 free(supported_displays);
2876
2877 if (found_plane) {
2878 break;
2879 }
2880 }
2881
2882 if (!found_plane) {
2883 printf("Cannot find a plane compatible with the display!\n");
2884 fflush(stdout);
2885 exit(1);
2886 }
2887
2888 free(plane_props);
2889
Tony Barbour820b55b2017-03-14 08:55:46 -06002890 VkDisplayPlaneCapabilitiesKHR planeCaps;
2891 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2892 // Find a supported alpha mode
Mark Young66510e52017-11-10 10:05:14 -07002893 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2894 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002895 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2896 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2897 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2898 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2899 };
2900 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2901 if (planeCaps.supportedAlpha & alphaModes[i]) {
2902 alphaMode = alphaModes[i];
2903 break;
2904 }
2905 }
Damien Leone600c3052017-01-31 10:26:07 -07002906 image_extent.width = mode_props.parameters.visibleRegion.width;
2907 image_extent.height = mode_props.parameters.visibleRegion.height;
2908
2909 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2910 create_info.pNext = NULL;
2911 create_info.flags = 0;
2912 create_info.displayMode = mode_props.displayMode;
2913 create_info.planeIndex = plane_index;
2914 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2915 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002916 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002917 create_info.globalAlpha = 1.0f;
2918 create_info.imageExtent = image_extent;
2919
2920 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2921}
2922
Dave Houlton5fa47912018-02-16 11:02:26 -07002923static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07002924 while (!demo->quit) {
2925 demo_draw(demo);
2926 demo->curFrame++;
2927
2928 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2929 demo->quit = true;
2930 }
2931 }
2932}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002933#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002934
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002935/*
2936 * Return 1 (true) if all layer names specified in check_names
2937 * can be found in given layer properties.
2938 */
Dave Houlton5fa47912018-02-16 11:02:26 -07002939static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002940 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002941 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002942 for (uint32_t j = 0; j < layer_count; j++) {
2943 if (!strcmp(check_names[i], layers[j].layerName)) {
2944 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002945 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002946 }
2947 }
2948 if (!found) {
2949 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2950 return 0;
2951 }
2952 }
2953 return 1;
2954}
2955
Karl Schultze4dc75c2016-02-02 15:37:51 -07002956static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002957 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002958 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002959 uint32_t instance_layer_count = 0;
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06002960 char *instance_validation_layers[] = {"VK_LAYER_KHRONOS_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06002961 demo->enabled_extension_count = 0;
2962 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002963 demo->is_minimized = false;
2964 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002965
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06002966 // Look for validation layers
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002967 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002968 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002969 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002970 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002971
Karl Schultz7c50ad62016-04-01 12:07:03 -06002972 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002973 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2974 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002975 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002976
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06002977 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
Dave Houlton5fa47912018-02-16 11:02:26 -07002978 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002979 if (validation_found) {
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06002980 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
2981 demo->enabled_layers[0] = "VK_LAYER_KHRONOS_validation";
Karl Schultz7c50ad62016-04-01 12:07:03 -06002982 }
2983 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002984 }
Dustin Graves7c287352016-01-27 13:48:06 -07002985
Karl Schultz7c50ad62016-04-01 12:07:03 -06002986 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002987 ERR_EXIT(
2988 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
2989 "Please look at the Getting Started guide for additional information.\n",
2990 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06002991 }
Dustin Graves7c287352016-01-27 13:48:06 -07002992 }
2993
2994 /* Look for instance extensions */
2995 VkBool32 surfaceExtFound = 0;
2996 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002997 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002998
Dave Houlton5fa47912018-02-16 11:02:26 -07002999 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003000 assert(!err);
3001
Dustin Graves7c287352016-01-27 13:48:06 -07003002 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003003 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3004 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003005 assert(!err);
3006 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003007 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003008 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003009 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003010 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003011#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003012 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003013 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003014 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003015 }
Tony Barbour153cb062016-12-07 13:43:36 -07003016#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003017 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06003018 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003019 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06003020 }
Tony Barbour153cb062016-12-07 13:43:36 -07003021#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003022 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003023 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003024 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003025 }
Tony Barbour153cb062016-12-07 13:43:36 -07003026#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003027 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003028 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003029 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003030 }
Damien Leone600c3052017-01-31 10:26:07 -07003031#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003032 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07003033 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003034 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07003035 }
Tony Barbour153cb062016-12-07 13:43:36 -07003036#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003037 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003038 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003039 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003040 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003041#elif defined(VK_USE_PLATFORM_IOS_MVK)
3042 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3043 platformSurfaceExtFound = 1;
3044 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3045 }
3046#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3047 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3048 platformSurfaceExtFound = 1;
3049 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3050 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003051#endif
Mark Young66510e52017-11-10 10:05:14 -07003052 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3053 if (demo->validate) {
3054 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3055 }
3056 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003057 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003058 }
Dustin Graves7c287352016-01-27 13:48:06 -07003059
3060 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003061 }
Dustin Graves7c287352016-01-27 13:48:06 -07003062
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003063 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003064 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3065 " extension.\n\n"
3066 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3067 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06003068 "vkCreateInstance Failure");
3069 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003070 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003071#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003072 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3073 " extension.\n\n"
3074 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3075 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003076 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003077#elif defined(VK_USE_PLATFORM_IOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003078 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_IOS_SURFACE_EXTENSION_NAME
3079 " extension.\n\n"
3080 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3081 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003082 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003083#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003084 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_MACOS_SURFACE_EXTENSION_NAME
3085 " extension.\n\n"
3086 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3087 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003088 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003089#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003090 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3091 " extension.\n\n"
3092 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3093 "Please look at the Getting Started guide for additional information.\n",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003094 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003095#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003096 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3097 " extension.\n\n"
3098 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3099 "Please look at the Getting Started guide for additional information.\n",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003100 "vkCreateInstance Failure");
Damien Leone600c3052017-01-31 10:26:07 -07003101#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003102 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3103 " extension.\n\n"
3104 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3105 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07003106 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003107#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003108 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3109 " extension.\n\n"
3110 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3111 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003112 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003113#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003114 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3115 " extension.\n\n"
3116 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3117 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06003118 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003119#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003120 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003121 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003122 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003123 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003124 .pApplicationName = APP_SHORT_NAME,
3125 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003126 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003127 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003128 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003129 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003130 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003131 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003132 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003133 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003134 .enabledLayerCount = demo->enabled_layer_count,
3135 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3136 .enabledExtensionCount = demo->enabled_extension_count,
3137 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003138 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003139
3140 /*
3141 * This is info for a temp callback to use during CreateInstance.
3142 * After the instance is created, we use the instance-based
3143 * function to register the final callback.
3144 */
Mark Young66510e52017-11-10 10:05:14 -07003145 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003146 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003147 // VK_EXT_debug_utils style
3148 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3149 dbg_messenger_create_info.pNext = NULL;
3150 dbg_messenger_create_info.flags = 0;
3151 dbg_messenger_create_info.messageSeverity =
3152 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3153 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3154 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3155 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3156 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3157 dbg_messenger_create_info.pUserData = demo;
3158 inst_info.pNext = &dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003159 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003160
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003161 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003162
Chia-I Wu63636062015-10-26 21:10:41 +08003163 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003164 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003165 ERR_EXIT(
3166 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3167 "Please look at the Getting Started guide for additional information.\n",
3168 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003169 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003170 ERR_EXIT(
3171 "Cannot find a specified extension library.\n"
3172 "Make sure your layers path is set appropriately.\n",
3173 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003174 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003175 ERR_EXIT(
3176 "vkCreateInstance failed.\n\n"
3177 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3178 "Please look at the Getting Started guide for additional information.\n",
3179 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003180 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003181
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003182 /* Make initial call to query gpu_count, then second call for gpu info*/
3183 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
Petr Kraus2f1dbdb2018-04-14 14:45:17 +02003184 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003185
3186 if (gpu_count > 0) {
3187 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3188 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3189 assert(!err);
3190 /* For cube demo we just grab the first physical device */
3191 demo->gpu = physical_devices[0];
3192 free(physical_devices);
3193 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003194 ERR_EXIT(
3195 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3196 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3197 "Please look at the Getting Started guide for additional information.\n",
3198 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003199 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003200
Dustin Graves7c287352016-01-27 13:48:06 -07003201 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003202 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003203 VkBool32 swapchainExtFound = 0;
3204 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003205 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003206
Dave Houlton5fa47912018-02-16 11:02:26 -07003207 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003208 assert(!err);
3209
Dustin Graves7c287352016-01-27 13:48:06 -07003210 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003211 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3212 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003213 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003214
Dustin Graves7c287352016-01-27 13:48:06 -07003215 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003216 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003217 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003218 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003219 }
3220 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003221 }
Dustin Graves7c287352016-01-27 13:48:06 -07003222
Ian Elliott53622a72017-03-28 11:06:33 -06003223 if (demo->VK_KHR_incremental_present_enabled) {
3224 // Even though the user "enabled" the extension via the command
3225 // line, we must make sure that it's enumerated for use with the
3226 // device. Therefore, disable it here, and re-enable it again if
3227 // enumerated.
3228 demo->VK_KHR_incremental_present_enabled = false;
3229 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003230 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3231 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003232 demo->VK_KHR_incremental_present_enabled = true;
3233 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3234 }
3235 assert(demo->enabled_extension_count < 64);
3236 }
3237 if (!demo->VK_KHR_incremental_present_enabled) {
3238 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3239 }
3240 }
3241
Ian Elliottd940ca22017-03-22 08:31:27 -06003242 if (demo->VK_GOOGLE_display_timing_enabled) {
3243 // Even though the user "enabled" the extension via the command
3244 // line, we must make sure that it's enumerated for use with the
3245 // device. Therefore, disable it here, and re-enable it again if
3246 // enumerated.
3247 demo->VK_GOOGLE_display_timing_enabled = false;
3248 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003249 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3250 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003251 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003252 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003253 }
3254 assert(demo->enabled_extension_count < 64);
3255 }
3256 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003257 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003258 }
3259 }
3260
Dustin Graves7c287352016-01-27 13:48:06 -07003261 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003262 }
Dustin Graves7c287352016-01-27 13:48:06 -07003263
Tony Barbourcc69f452015-10-08 13:59:42 -06003264 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003265 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3266 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3267 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003268 "vkCreateInstance Failure");
3269 }
3270
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003271 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003272 // Setup VK_EXT_debug_utils function pointers always (we use them for
3273 // debug labels and names).
3274 demo->CreateDebugUtilsMessengerEXT =
3275 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3276 demo->DestroyDebugUtilsMessengerEXT =
3277 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3278 demo->SubmitDebugUtilsMessageEXT =
3279 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3280 demo->CmdBeginDebugUtilsLabelEXT =
3281 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3282 demo->CmdEndDebugUtilsLabelEXT =
3283 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3284 demo->CmdInsertDebugUtilsLabelEXT =
3285 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3286 demo->SetDebugUtilsObjectNameEXT =
3287 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3288 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3289 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3290 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3291 NULL == demo->SetDebugUtilsObjectNameEXT) {
3292 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003293 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003294
Mark Young66510e52017-11-10 10:05:14 -07003295 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003296 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003297 case VK_SUCCESS:
3298 break;
3299 case VK_ERROR_OUT_OF_HOST_MEMORY:
Mark Young66510e52017-11-10 10:05:14 -07003300 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003301 break;
3302 default:
Mark Young66510e52017-11-10 10:05:14 -07003303 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003304 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003305 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003306 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003307 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003308
3309 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003310 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003311 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003312
Dave Houlton5fa47912018-02-16 11:02:26 -07003313 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3314 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003315
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003316 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003317 // If app has specific feature requirements it should check supported
3318 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003319 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003320 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003321
Tony Barbourda2bad52016-01-22 14:36:40 -07003322 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3323 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3324 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3325 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3326 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3327}
3328
Karl Schultze4dc75c2016-02-02 15:37:51 -07003329static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003330 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003331 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003332 VkDeviceQueueCreateInfo queues[2];
3333 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3334 queues[0].pNext = NULL;
3335 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3336 queues[0].queueCount = 1;
3337 queues[0].pQueuePriorities = queue_priorities;
3338 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003339
3340 VkDeviceCreateInfo device = {
3341 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3342 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003343 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003344 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003345 .enabledLayerCount = 0,
3346 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003347 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003348 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003349 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003350 };
Tony Barbour22e06272016-09-07 16:07:56 -06003351 if (demo->separate_present_queue) {
3352 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3353 queues[1].pNext = NULL;
3354 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3355 queues[1].queueCount = 1;
3356 queues[1].pQueuePriorities = queue_priorities;
3357 queues[1].flags = 0;
3358 device.queueCreateInfoCount = 2;
3359 }
Chia-I Wu63636062015-10-26 21:10:41 +08003360 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003361 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003362}
3363
Tony-LunarG6cebf142019-09-11 14:32:23 -06003364static void demo_create_surface(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003365 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003366
Karl Schultze4dc75c2016-02-02 15:37:51 -07003367// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003368#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003369 VkWin32SurfaceCreateInfoKHR createInfo;
3370 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3371 createInfo.pNext = NULL;
3372 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003373 createInfo.hinstance = demo->connection;
3374 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003375
Dave Houlton5fa47912018-02-16 11:02:26 -07003376 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003377#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003378 VkWaylandSurfaceCreateInfoKHR createInfo;
3379 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3380 createInfo.pNext = NULL;
3381 createInfo.flags = 0;
3382 createInfo.display = demo->display;
3383 createInfo.surface = demo->window;
3384
Dave Houlton5fa47912018-02-16 11:02:26 -07003385 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003386#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3387 VkAndroidSurfaceCreateInfoKHR createInfo;
3388 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3389 createInfo.pNext = NULL;
3390 createInfo.flags = 0;
Mike Schuchardt837d5162018-03-08 12:57:02 -07003391 createInfo.window = (struct ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003392
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003393 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003394#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3395 VkXlibSurfaceCreateInfoKHR createInfo;
3396 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3397 createInfo.pNext = NULL;
3398 createInfo.flags = 0;
3399 createInfo.dpy = demo->display;
3400 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003401
Dave Houlton5fa47912018-02-16 11:02:26 -07003402 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003403#elif defined(VK_USE_PLATFORM_XCB_KHR)
3404 VkXcbSurfaceCreateInfoKHR createInfo;
3405 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3406 createInfo.pNext = NULL;
3407 createInfo.flags = 0;
3408 createInfo.connection = demo->connection;
3409 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003410
Tony Barbour153cb062016-12-07 13:43:36 -07003411 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003412#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3413 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003414#elif defined(VK_USE_PLATFORM_IOS_MVK)
3415 VkIOSSurfaceCreateInfoMVK surface;
3416 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3417 surface.pNext = NULL;
3418 surface.flags = 0;
3419 surface.pView = demo->window;
3420
3421 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3422#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3423 VkMacOSSurfaceCreateInfoMVK surface;
3424 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3425 surface.pNext = NULL;
3426 surface.flags = 0;
3427 surface.pView = demo->window;
3428
3429 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003430#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003431 assert(!err);
Tony-LunarG6cebf142019-09-11 14:32:23 -06003432}
3433
3434static void demo_init_vk_swapchain(struct demo *demo) {
3435 VkResult U_ASSERT_ONLY err;
3436
3437 demo_create_surface(demo);
Ian Elliottaaae5352015-07-06 14:27:58 -06003438
Tony Barbourcc69f452015-10-08 13:59:42 -06003439 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003440 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003441 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003442 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003443 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003444
3445 // Search for a graphics and a present queue in the array of queue
3446 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003447 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3448 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003449 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003450 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3451 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3452 graphicsQueueFamilyIndex = i;
3453 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003454
Tony Barbourab2a0362016-09-06 11:40:12 -06003455 if (supportsPresent[i] == VK_TRUE) {
3456 graphicsQueueFamilyIndex = i;
3457 presentQueueFamilyIndex = i;
3458 break;
3459 }
3460 }
3461 }
3462
3463 if (presentQueueFamilyIndex == UINT32_MAX) {
3464 // If didn't find a queue that supports both graphics and present, then
3465 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003466 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003467 if (supportsPresent[i] == VK_TRUE) {
3468 presentQueueFamilyIndex = i;
3469 break;
3470 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003471 }
3472 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003473
3474 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003475 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3476 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003477 }
3478
Tony Barbourab2a0362016-09-06 11:40:12 -06003479 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3480 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003481 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003482 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003483
Tony Barbourda2bad52016-01-22 14:36:40 -07003484 demo_create_device(demo);
3485
3486 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3487 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3488 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3489 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3490 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003491 if (demo->VK_GOOGLE_display_timing_enabled) {
3492 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3493 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3494 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003495
Dave Houlton5fa47912018-02-16 11:02:26 -07003496 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003497
Tony Barbour22e06272016-09-07 16:07:56 -06003498 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003499 demo->present_queue = demo->graphics_queue;
3500 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003501 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003502 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003503
Ian Elliottaaae5352015-07-06 14:27:58 -06003504 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003505 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003506 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003507 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003508 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3509 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003510 assert(!err);
3511 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3512 // the surface has no preferred format. Otherwise, at least one
3513 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003514 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003515 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003516 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003517 assert(formatCount >= 1);
3518 demo->format = surfFormats[0].format;
3519 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003520 demo->color_space = surfFormats[0].colorSpace;
Jason Chen5d4fee92019-04-04 12:45:29 +08003521 free(surfFormats);
Ian Elliotte4602cd2015-04-21 16:41:02 -06003522
David Pinedo2bb7c932015-06-18 17:03:14 -06003523 demo->quit = false;
3524 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003525
Tony Barbource7524e2016-07-28 12:01:45 -06003526 // Create semaphores to synchronize acquiring presentable buffers before
3527 // rendering and waiting for drawing to be complete before presenting
3528 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3529 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3530 .pNext = NULL,
3531 .flags = 0,
3532 };
Tony Barbource7524e2016-07-28 12:01:45 -06003533
Tony Barbour66a56c32016-09-21 13:10:56 -06003534 // Create fences that we can use to throttle if we get too far
3535 // ahead of the image presents
3536 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003537 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003538 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003539 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3540 assert(!err);
3541
Dave Houlton5fa47912018-02-16 11:02:26 -07003542 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003543 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003544
Dave Houlton5fa47912018-02-16 11:02:26 -07003545 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003546 assert(!err);
3547
3548 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003549 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003550 assert(!err);
3551 }
Tony Barbour22e06272016-09-07 16:07:56 -06003552 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003553 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003554
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003555 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003556 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003557}
3558
Tony Barbour153cb062016-12-07 13:43:36 -07003559#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003560static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3561 wl_fixed_t sy) {}
3562
3563static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3564
3565static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3566
3567static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3568 uint32_t state) {
3569 struct demo *demo = data;
3570 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3571 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
3572 }
3573}
3574
3575static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3576
3577static const struct wl_pointer_listener pointer_listener = {
3578 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3579};
3580
3581static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3582
3583static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3584 struct wl_array *keys) {}
3585
3586static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3587
3588static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3589 uint32_t state) {
3590 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3591 struct demo *demo = data;
3592 switch (key) {
3593 case KEY_ESC: // Escape
3594 demo->quit = true;
3595 break;
3596 case KEY_LEFT: // left arrow key
3597 demo->spin_angle -= demo->spin_increment;
3598 break;
3599 case KEY_RIGHT: // right arrow key
3600 demo->spin_angle += demo->spin_increment;
3601 break;
3602 case KEY_SPACE: // space bar
3603 demo->pause = !demo->pause;
3604 break;
3605 }
3606}
3607
3608static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3609 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3610
3611static const struct wl_keyboard_listener keyboard_listener = {
3612 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3613};
3614
3615static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3616 // Subscribe to pointer events
3617 struct demo *demo = data;
3618 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3619 demo->pointer = wl_seat_get_pointer(seat);
3620 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3621 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3622 wl_pointer_destroy(demo->pointer);
3623 demo->pointer = NULL;
3624 }
3625 // Subscribe to keyboard events
3626 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3627 demo->keyboard = wl_seat_get_keyboard(seat);
3628 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3629 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3630 wl_keyboard_destroy(demo->keyboard);
3631 demo->keyboard = NULL;
3632 }
3633}
3634
3635static const struct wl_seat_listener seat_listener = {
3636 seat_handle_capabilities,
3637};
3638
3639static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003640 uint32_t version UNUSED) {
3641 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003642 // pickup wayland objects when they appear
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003643 if (strcmp(interface, "wl_compositor") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003644 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003645 } else if (strcmp(interface, "wl_shell") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003646 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3647 } else if (strcmp(interface, "wl_seat") == 0) {
3648 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3649 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003650 }
3651}
3652
Dave Houlton5fa47912018-02-16 11:02:26 -07003653static void registry_handle_global_remove(void *data UNUSED, struct wl_registry *registry UNUSED, uint32_t name UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003654
Dave Houlton5fa47912018-02-16 11:02:26 -07003655static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003656#endif
3657
Karl Schultze4dc75c2016-02-02 15:37:51 -07003658static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003659#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003660 const xcb_setup_t *setup;
3661 xcb_screen_iterator_t iter;
3662 int scr;
3663
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003664 const char *display_envar = getenv("DISPLAY");
3665 if (display_envar == NULL || display_envar[0] == '\0') {
3666 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3667 fflush(stdout);
3668 exit(1);
3669 }
3670
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003671 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003672 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003673 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003674 fflush(stdout);
3675 exit(1);
3676 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003677
3678 setup = xcb_get_setup(demo->connection);
3679 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003680 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003681
3682 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003683#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3684 demo->display = wl_display_connect(NULL);
3685
3686 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003687 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003688 fflush(stdout);
3689 exit(1);
3690 }
3691
3692 demo->registry = wl_display_get_registry(demo->display);
3693 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3694 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003695#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003696}
3697
Karl Schultze4dc75c2016-02-02 15:37:51 -07003698static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003699 vec3 eye = {0.0f, 3.0f, 5.0f};
3700 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003701 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003702
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003703 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003704 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003705 demo->frameCount = INT32_MAX;
Mike Schuchardtaed62462019-07-26 07:56:30 -07003706
3707#if defined(VK_USE_PLATFORM_MACOS_MVK)
3708 // MoltenVK may not allow host coherent mapping to linear tiled images
3709 // Force the use of a staging buffer to be safe
3710 demo->use_staging_buffer = true;
3711#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003712
Piers Daniell735ee532015-02-23 16:23:13 -07003713 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003714 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003715 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003716 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003717 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003718 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3719 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003720 i++;
3721 continue;
3722 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003723 if (strcmp(argv[i], "--break") == 0) {
3724 demo->use_break = true;
3725 continue;
3726 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003727 if (strcmp(argv[i], "--validate") == 0) {
3728 demo->validate = true;
3729 continue;
3730 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003731 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3732 demo->validate = true;
3733 demo->validate_checks_disabled = true;
3734 continue;
3735 }
Tony Barbour2593b182016-04-19 10:57:58 -06003736 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003737 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003738 continue;
3739 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003740 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3741 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003742 i++;
3743 continue;
3744 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003745 if (strcmp(argv[i], "--suppress_popups") == 0) {
3746 demo->suppress_popups = true;
3747 continue;
3748 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003749 if (strcmp(argv[i], "--display_timing") == 0) {
3750 demo->VK_GOOGLE_display_timing_enabled = true;
3751 continue;
3752 }
Ian Elliott53622a72017-03-28 11:06:33 -06003753 if (strcmp(argv[i], "--incremental_present") == 0) {
3754 demo->VK_KHR_incremental_present_enabled = true;
3755 continue;
3756 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003757
Cody Northropaf28a532016-09-27 10:50:57 -06003758#if defined(ANDROID)
Lenny Komowffe446c2018-11-19 17:08:04 -07003759 ERR_EXIT("Usage: vkcube [--validate]\n", "Usage");
Cody Northropaf28a532016-09-27 10:50:57 -06003760#else
Tony-LunarGd74734f2019-06-04 14:11:43 -06003761 char *message =
3762 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled]\n"
3763 "\t[--break] [--c <framecount>] [--suppress_popups]\n"
3764 "\t[--incremental_present] [--display_timing]\n"
3765 "\t[--present_mode <present mode enum>]\n"
3766 "\t<present_mode_enum>\n"
3767 "\t\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3768 "\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3769 "\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
3770 "\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n";
3771 int length = snprintf(NULL, 0, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3772 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3773 char *usage = (char *)malloc(length + 1);
3774 if (!usage) {
3775 exit(1);
3776 }
3777 snprintf(usage, length + 1, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3778 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3779#if defined(_WIN32)
3780 if (!demo->suppress_popups) MessageBox(NULL, usage, "Usage Error", MB_OK);
3781#else
Tony-LunarG122ac062019-06-13 16:21:57 -06003782 fprintf(stderr, "%s", usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003783 fflush(stderr);
Tony-LunarGd74734f2019-06-04 14:11:43 -06003784#endif
3785 free(usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003786 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003787#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003788 }
3789
Tony Barbour153cb062016-12-07 13:43:36 -07003790 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003791
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003792 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003793
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003794 demo->width = 500;
3795 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003796
Tony Barbour66a56c32016-09-21 13:10:56 -06003797 demo->spin_angle = 4.0f;
3798 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003799 demo->pause = false;
3800
Dave Houlton5fa47912018-02-16 11:02:26 -07003801 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003802 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3803 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003804
Dave Houlton5fa47912018-02-16 11:02:26 -07003805 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003806}
3807
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003808#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003809// Include header required for parsing the command line options.
3810#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003811
Dave Houlton5fa47912018-02-16 11:02:26 -07003812int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3813 MSG msg; // message
3814 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003815 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003816 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003817
Jamie Madillb2ff6502017-03-15 16:17:46 -04003818 // Ensure wParam is initialized.
3819 msg.wParam = 0;
3820
Karl Schultze4dc75c2016-02-02 15:37:51 -07003821 // Use the CommandLine functions to get the command line arguments.
3822 // Unfortunately, Microsoft outputs
3823 // this information as wide characters for Unicode, and we simply want the
3824 // Ascii version to be compatible
3825 // with the non-Windows side. So, we have to convert the information to
3826 // Ascii character strings.
3827 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3828 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003829 argc = 0;
3830 }
3831
Karl Schultze4dc75c2016-02-02 15:37:51 -07003832 if (argc > 0) {
3833 argv = (char **)malloc(sizeof(char *) * argc);
3834 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003835 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003836 } else {
3837 for (int iii = 0; iii < argc; iii++) {
3838 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003839 size_t numConverted = 0;
3840
Karl Schultze4dc75c2016-02-02 15:37:51 -07003841 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3842 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003843 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003844 }
3845 }
3846 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003847 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003848 argv = NULL;
3849 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003850
3851 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003852
3853 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003854 if (argc > 0 && argv != NULL) {
3855 for (int iii = 0; iii < argc; iii++) {
3856 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003857 free(argv[iii]);
3858 }
3859 }
3860 free(argv);
3861 }
3862
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003863 demo.connection = hInstance;
Lenny Komowffe446c2018-11-19 17:08:04 -07003864 strncpy(demo.name, "Vulkan Cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003865 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003866 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003867
3868 demo_prepare(&demo);
3869
Dave Houlton5fa47912018-02-16 11:02:26 -07003870 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003871
3872 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003873 while (!done) {
Petr Krausd88e4e52019-01-23 18:45:04 +01003874 if (demo.pause) {
3875 const BOOL succ = WaitMessage();
3876
3877 if (!succ) {
3878 struct demo *tmp = &demo;
3879 struct demo *demo = tmp;
3880 ERR_EXIT("WaitMessage() failed on paused demo", "event loop error");
3881 }
3882 }
Ian Elliotta748eaf2015-04-28 15:50:36 -06003883 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003884 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003885 {
Dave Houlton5fa47912018-02-16 11:02:26 -07003886 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07003887 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003888 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003889 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003890 DispatchMessage(&msg);
3891 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003892 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003893 }
3894
3895 demo_cleanup(&demo);
3896
Karl Schultze4dc75c2016-02-02 15:37:51 -07003897 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003898}
Bill Hollingsf4352142017-02-14 22:58:56 -05003899
3900#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
Karl Schultz206b1c52018-04-13 18:02:07 -06003901static void demo_main(struct demo *demo, void *view, int argc, const char *argv[]) {
Bill Hollingsf4352142017-02-14 22:58:56 -05003902
Dave Houlton5fa47912018-02-16 11:02:26 -07003903 demo_init(demo, argc, (char **)argv);
Tony Barbourc65cd752017-03-13 15:29:35 -06003904 demo->window = view;
3905 demo_init_vk_swapchain(demo);
3906 demo_prepare(demo);
3907 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003908}
3909
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003910#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3911#include <android/log.h>
3912#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003913#include "android_util.h"
3914
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003915static bool initialized = false;
3916static bool active = false;
3917struct demo demo;
3918
Dave Houlton5fa47912018-02-16 11:02:26 -07003919static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003920
Dave Houlton5fa47912018-02-16 11:02:26 -07003921static void processCommand(struct android_app *app, int32_t cmd) {
3922 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003923 case APP_CMD_INIT_WINDOW: {
3924 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003925 // We're getting a new window. If the app is starting up, we
3926 // need to initialize. If the app has already been
3927 // initialized, that means that we lost our previous window,
3928 // which means that we have a lot of work to do. At a minimum,
3929 // we need to destroy the swapchain and surface associated with
3930 // the old window, and create a new surface and swapchain.
3931 // However, since there are a lot of other objects/state that
3932 // is tied to the swapchain, it's easiest to simply cleanup and
3933 // start over (i.e. use a brute-force approach of re-starting
3934 // the app)
3935 if (demo.prepared) {
3936 demo_cleanup(&demo);
3937 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003938
3939 // Parse Intents into argc, argv
3940 // Use the following key to send arguments, i.e.
3941 // --es args "--validate"
3942 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07003943 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003944 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003945 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003946
3947 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07003948 for (int i = 0; i < argc; i++) __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003949
3950 demo_init(&demo, argc, argv);
3951
3952 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07003953 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003954
Dave Houlton5fa47912018-02-16 11:02:26 -07003955 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003956 demo_init_vk_swapchain(&demo);
3957 demo_prepare(&demo);
3958 initialized = true;
3959 }
3960 break;
3961 }
3962 case APP_CMD_GAINED_FOCUS: {
3963 active = true;
3964 break;
3965 }
3966 case APP_CMD_LOST_FOCUS: {
3967 active = false;
3968 break;
3969 }
3970 }
3971}
3972
Dave Houlton5fa47912018-02-16 11:02:26 -07003973void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06003974#ifdef ANDROID
3975 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07003976 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06003977#endif
3978
Ian Elliottf05d5d12016-05-17 12:03:35 -06003979 demo.prepared = false;
3980
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003981 app->onAppCmd = processCommand;
3982 app->onInputEvent = processInput;
3983
Dave Houlton5fa47912018-02-16 11:02:26 -07003984 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003985 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07003986 struct android_poll_source *source;
3987 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003988 if (source) {
3989 source->process(app, source);
3990 }
3991
3992 if (app->destroyRequested != 0) {
3993 demo_cleanup(&demo);
3994 return;
3995 }
3996 }
3997 if (initialized && active) {
3998 demo_run(&demo);
3999 }
4000 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004001}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004002#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004003int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004004 struct demo demo;
4005
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004006 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004007#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004008 demo_create_xcb_window(&demo);
4009#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4010 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004011#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4012 demo_create_window(&demo);
4013#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004014
Tony Barbourcc69f452015-10-08 13:59:42 -06004015 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004016
4017 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004018
Tony Barbour153cb062016-12-07 13:43:36 -07004019#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004020 demo_run_xcb(&demo);
4021#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4022 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004023#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4024 demo_run(&demo);
Damien Leone600c3052017-01-31 10:26:07 -07004025#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4026 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004027#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004028
4029 demo_cleanup(&demo);
4030
Karl Schultz0218c002016-03-22 17:06:13 -06004031 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004032}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004033#endif