blob: ed503392781c2ecc4d45774e65e7e73af3bbcf9c [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
Richard S. Wright Jr3c1ea4c2021-01-06 15:12:19 -050028#define VK_ENABLE_BETA_EXTENSIONS
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060029#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060030#include <stdio.h>
Ian Elliottd940ca22017-03-22 08:31:27 -060031#include <stdarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060032#include <stdlib.h>
33#include <string.h>
34#include <stdbool.h>
35#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070036#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090037#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060038#include <X11/Xutil.h>
Joey Bzdek15eb0702017-06-07 09:40:36 -060039#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
40#include <linux/input.h>
Tony Barbour2593b182016-04-19 10:57:58 -060041#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060042
Ian Elliott639ca472015-04-16 15:23:05 -060043#ifdef _WIN32
Biswapriyo Nathffe404f2020-08-25 01:47:41 +053044#ifdef _MSC_VER
Ian Elliott639ca472015-04-16 15:23:05 -060045#pragma comment(linker, "/subsystem:windows")
Biswapriyo Nathc472fa22020-08-25 23:49:34 +053046#endif // MSVC
Ian Elliott639ca472015-04-16 15:23:05 -060047#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070048#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060049
Cody Northrop8707a6e2016-04-26 19:59:19 -060050#ifdef ANDROID
51#include "vulkan_wrapper.h"
52#else
David Pinedoc5193c12015-11-06 12:54:48 -070053#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060054#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070055
David Pinedo541526f2015-11-24 09:00:24 -070056#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060057#include "linmath.h"
Mark Lobodzinski0edf1382018-04-10 15:40:04 -060058#include "object_type_string_helper.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060059
Ian Elliottd940ca22017-03-22 08:31:27 -060060#include "gettime.h"
61#include "inttypes.h"
62#define MILLION 1000000L
63#define BILLION 1000000000L
64
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060065#define DEMO_TEXTURE_COUNT 1
Lenny Komowffe446c2018-11-19 17:08:04 -070066#define APP_SHORT_NAME "vkcube"
67#define APP_LONG_NAME "Vulkan Cube"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060068
Tony Barbour66a56c32016-09-21 13:10:56 -060069// Allow a maximum of two outstanding presentation operations.
70#define FRAME_LAG 2
71
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060072#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
73
Tony Barbourfdc2d352015-04-22 09:02:32 -060074#if defined(NDEBUG) && defined(__GNUC__)
75#define U_ASSERT_ONLY __attribute__((unused))
76#else
77#define U_ASSERT_ONLY
78#endif
79
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090080#if defined(__GNUC__)
81#define UNUSED __attribute__((unused))
82#else
83#define UNUSED
84#endif
85
Ian Elliott07264132015-04-28 11:35:02 -060086#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060087bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070088#define ERR_EXIT(err_msg, err_class) \
89 do { \
90 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
91 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070092 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -060093void DbgMsg(char *fmt, ...) {
94 va_list va;
95 va_start(va, fmt);
Mike Weiblene9847ff2019-06-27 15:18:32 -060096 vprintf(fmt, va);
Ian Elliottd940ca22017-03-22 08:31:27 -060097 va_end(va);
Mike Weiblene9847ff2019-06-27 15:18:32 -060098 fflush(stdout);
Ian Elliottd940ca22017-03-22 08:31:27 -060099}
Ian Elliott07264132015-04-28 11:35:02 -0600100
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500101#elif defined __ANDROID__
102#include <android/log.h>
Jeremy Kniager979b5312019-11-22 14:55:57 -0700103#define ERR_EXIT(err_msg, err_class) \
104 do { \
Lenny Komowffe446c2018-11-19 17:08:04 -0700105 ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", err_msg)); \
Jeremy Kniager979b5312019-11-22 14:55:57 -0700106 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500107 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600108#ifdef VARARGS_WORKS_ON_ANDROID
109void DbgMsg(const char *fmt, ...) {
110 va_list va;
111 va_start(va, fmt);
Lenny Komowffe446c2018-11-19 17:08:04 -0700112 __android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, va);
Ian Elliottd940ca22017-03-22 08:31:27 -0600113 va_end(va);
114}
115#else // VARARGS_WORKS_ON_ANDROID
Jeremy Kniager979b5312019-11-22 14:55:57 -0700116#define DbgMsg(fmt, ...) \
117 do { \
Lenny Komowffe446c2018-11-19 17:08:04 -0700118 ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, ##__VA_ARGS__)); \
Ian Elliottd940ca22017-03-22 08:31:27 -0600119 } while (0)
120#endif // VARARGS_WORKS_ON_ANDROID
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500121#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700122#define ERR_EXIT(err_msg, err_class) \
123 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -0800124 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700125 fflush(stdout); \
126 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700127 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600128void DbgMsg(char *fmt, ...) {
129 va_list va;
130 va_start(va, fmt);
Mike Weiblene9847ff2019-06-27 15:18:32 -0600131 vprintf(fmt, va);
Ian Elliottd940ca22017-03-22 08:31:27 -0600132 va_end(va);
Mike Weiblene9847ff2019-06-27 15:18:32 -0600133 fflush(stdout);
Ian Elliottd940ca22017-03-22 08:31:27 -0600134}
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500135#endif
Ian Elliott07264132015-04-28 11:35:02 -0600136
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700137#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
138 { \
139 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
140 if (demo->fp##entrypoint == NULL) { \
141 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
142 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700143 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600144
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600145static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
146
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700147#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
148 { \
149 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
150 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
151 if (demo->fp##entrypoint == NULL) { \
152 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
153 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700154 }
Ian Elliott673898b2015-06-22 15:07:49 -0600155
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600156/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700157 * structure to track all objects related to a texture.
158 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600159struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600160 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700161
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600162 VkImage image;
Tony-LunarGbc9fc052018-09-21 13:47:06 -0600163 VkBuffer buffer;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600164 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600165
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800166 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500167 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600168 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700169 int32_t tex_width, tex_height;
170};
171
Karl Schultze4dc75c2016-02-02 15:37:51 -0700172static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173
Karl Schultz0218c002016-03-22 17:06:13 -0600174static int validation_error = 0;
175
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600176struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600177 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700178 float mvp[4][4];
179 float position[12 * 3][4];
180 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600181};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600182
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600183//--------------------------------------------------------------------------------------
184// Mesh and VertexFormat Data
185//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700186// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600187static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 -1.0f,-1.0f, 1.0f,
190 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 -1.0f, 1.0f,-1.0f,
193 -1.0f,-1.0f,-1.0f,
194
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 1.0f, 1.0f,-1.0f,
197 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600200 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600201
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800202 -1.0f,-1.0f,-1.0f, // -Y side
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,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800205 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600206 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600207 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800209 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 -1.0f, 1.0f, 1.0f,
211 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800212 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600213 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600214 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600215
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 1.0f, 1.0f, 1.0f,
218 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800219 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220 1.0f,-1.0f,-1.0f,
221 1.0f, 1.0f,-1.0f,
222
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800226 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227 1.0f,-1.0f, 1.0f,
228 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600229};
230
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600231static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700232 0.0f, 1.0f, // -X side
233 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 0.0f, 0.0f,
237 0.0f, 1.0f,
238
Rene Lindsay76867e82016-07-29 10:49:11 -0700239 1.0f, 1.0f, // -Z side
240 0.0f, 0.0f,
241 0.0f, 1.0f,
242 1.0f, 1.0f,
243 1.0f, 0.0f,
244 0.0f, 0.0f,
245
246 1.0f, 0.0f, // -Y side
247 1.0f, 1.0f,
248 0.0f, 1.0f,
249 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600250 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800251 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600252
Rene Lindsay76867e82016-07-29 10:49:11 -0700253 1.0f, 0.0f, // +Y side
254 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800255 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600256 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700257 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600258 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600259
Rene Lindsay76867e82016-07-29 10:49:11 -0700260 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800261 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700262 0.0f, 1.0f,
263 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600264 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800265 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700266
267 0.0f, 0.0f, // +Z side
268 0.0f, 1.0f,
269 1.0f, 0.0f,
270 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600271 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700272 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600273};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700274// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600275
Karl Schultze4dc75c2016-02-02 15:37:51 -0700276void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600277 int i;
278
279 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700280 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600281 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
282 }
283 printf("\n");
284 fflush(stdout);
285}
286
Karl Schultze4dc75c2016-02-02 15:37:51 -0700287void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600288 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700289 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600290 printf("\n");
291 fflush(stdout);
292}
293
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600294typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600295 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800296 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600297 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600298 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700299 VkBuffer uniform_buffer;
300 VkDeviceMemory uniform_memory;
Tony-LunarG37af49f2019-12-19 12:04:19 -0700301 void *uniform_memory_ptr;
Tony Barboura72d9492017-01-11 13:35:09 -0700302 VkFramebuffer framebuffer;
303 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700304} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600305
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600306struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500307#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600308#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700309 HINSTANCE connection; // hInstance - Windows Instance
310 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
311 HWND window; // hWnd - window handle
312 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700313#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700314 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600315 Window xlib_window;
316 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700317#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700318 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319 xcb_connection_t *connection;
320 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600321 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800322 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900323#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
324 struct wl_display *display;
325 struct wl_registry *registry;
326 struct wl_compositor *compositor;
327 struct wl_surface *window;
Tony-LunarG5ceb7be2019-12-05 16:05:40 -0700328 struct wl_shell *shell;
329 struct wl_shell_surface *shell_surface;
Joey Bzdek15eb0702017-06-07 09:40:36 -0600330 struct wl_seat *seat;
331 struct wl_pointer *pointer;
332 struct wl_keyboard *keyboard;
Nicolas Caramellic8be8c82020-07-13 17:22:09 +0200333#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
334 IDirectFB *dfb;
335 IDirectFBSurface *window;
336 IDirectFBEventBuffer *event_buffer;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500337#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mike Schuchardt837d5162018-03-08 12:57:02 -0700338 struct ANativeWindow *window;
Bill Hollings0a0625a2019-07-15 17:39:18 -0400339#elif defined(VK_USE_PLATFORM_METAL_EXT)
340 void *caMetalLayer;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500341#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700342 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600343 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700344 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600345 bool separate_present_queue;
Jeremy Kniager602e4182018-01-19 10:52:34 -0700346 bool is_minimized;
Tony-LunarG50e737c2020-07-16 14:26:03 -0600347 uint32_t gpu_number;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600348
Ian Elliott53622a72017-03-28 11:06:33 -0600349 bool VK_KHR_incremental_present_enabled;
350
Ian Elliottd940ca22017-03-22 08:31:27 -0600351 bool VK_GOOGLE_display_timing_enabled;
352 bool syncd_with_actual_presents;
353 uint64_t refresh_duration;
354 uint64_t refresh_duration_multiplier;
355 uint64_t target_IPD; // image present duration (inverse of frame rate)
356 uint64_t prev_desired_present_time;
357 uint32_t next_present_id;
358 uint32_t last_early_id; // 0 if no early images
359 uint32_t last_late_id; // 0 if no late images
360
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600361 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600362 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600363 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600364 VkQueue graphics_queue;
365 VkQueue present_queue;
366 uint32_t graphics_queue_family_index;
367 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600368 VkSemaphore image_acquired_semaphores[FRAME_LAG];
369 VkSemaphore draw_complete_semaphores[FRAME_LAG];
370 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600371 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600372 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600373 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
Tony Barbourda2bad52016-01-22 14:36:40 -0700375 uint32_t enabled_extension_count;
376 uint32_t enabled_layer_count;
377 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600378 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700379
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600381 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600382 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700384 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
385 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
386 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
387 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600388 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
389 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
390 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
391 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
392 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600393 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
394 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600395 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600396 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700397 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600398 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600399 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600400 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600401
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800402 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600403 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404
405 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600406 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600407
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600408 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800409 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500410 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600411 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600412 } depth;
413
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600414 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600415 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600416
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700417 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500418 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600419 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600420 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800421 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600422 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600423
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600424 mat4x4 projection_matrix;
425 mat4x4 view_matrix;
426 mat4x4 model_matrix;
427
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600428 float spin_angle;
429 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600430 bool pause;
431
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600432 VkShaderModule vert_shader_module;
433 VkShaderModule frag_shader_module;
434
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600435 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800436
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600437 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600438 int32_t curFrame;
439 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600440 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600441 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600442 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600443 bool suppress_popups;
Mark Young66510e52017-11-10 10:05:14 -0700444
445 PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
446 PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
447 PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
448 PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
449 PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
450 PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
451 PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
452 VkDebugUtilsMessengerEXT dbg_messenger;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600453
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600454 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600455 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600456};
457
Mark Young66510e52017-11-10 10:05:14 -0700458VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
459 VkDebugUtilsMessageTypeFlagsEXT messageType,
460 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
461 void *pUserData) {
462 char prefix[64] = "";
463 char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000);
lenny-lunargfbe22392016-06-06 11:07:53 -0600464 assert(message);
Mark Young66510e52017-11-10 10:05:14 -0700465 struct demo *demo = (struct demo *)pUserData;
lenny-lunargfbe22392016-06-06 11:07:53 -0600466
Mark Young66510e52017-11-10 10:05:14 -0700467 if (demo->use_break) {
468#ifndef WIN32
469 raise(SIGTRAP);
470#else
471 DebugBreak();
472#endif
473 }
474
475 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
476 strcat(prefix, "VERBOSE : ");
477 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
478 strcat(prefix, "INFO : ");
479 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
480 strcat(prefix, "WARNING : ");
481 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
482 strcat(prefix, "ERROR : ");
483 }
484
485 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
486 strcat(prefix, "GENERAL");
lenny-lunargfbe22392016-06-06 11:07:53 -0600487 } else {
Mark Young66510e52017-11-10 10:05:14 -0700488 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
489 strcat(prefix, "VALIDATION");
490 validation_error = 1;
491 }
492 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
493 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
494 strcat(prefix, "|");
495 }
496 strcat(prefix, "PERFORMANCE");
497 }
498 }
499
500 sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber,
501 pCallbackData->pMessageIdName, pCallbackData->pMessage);
502 if (pCallbackData->objectCount > 0) {
503 char tmp_message[500];
504 sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount);
505 strcat(message, tmp_message);
506 for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
507 if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
508 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object,
Mark Young44212682018-02-21 15:29:41 -0700509 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700510 (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName);
511 } else {
512 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object,
Mark Young44212682018-02-21 15:29:41 -0700513 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700514 (void *)(pCallbackData->pObjects[object].objectHandle));
515 }
516 strcat(message, tmp_message);
517 }
518 }
519 if (pCallbackData->cmdBufLabelCount > 0) {
520 char tmp_message[500];
521 sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount);
522 strcat(message, tmp_message);
523 for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
524 sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label,
525 pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0],
526 pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2],
527 pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]);
528 strcat(message, tmp_message);
529 }
lenny-lunargfbe22392016-06-06 11:07:53 -0600530 }
531
532#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600533
Tony Barbour602ca4f2016-09-12 14:02:43 -0600534 in_callback = true;
Jeremy Kniager979b5312019-11-22 14:55:57 -0700535 if (!demo->suppress_popups) MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600536 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600537
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600538#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600539
Mike Schuchardt837d5162018-03-08 12:57:02 -0700540 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
Jeremy Kniager979b5312019-11-22 14:55:57 -0700541 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700542 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
Jeremy Kniager979b5312019-11-22 14:55:57 -0700543 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700544 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600545 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700546 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
547 __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600548 } else {
Jeremy Kniager979b5312019-11-22 14:55:57 -0700549 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600550 }
Cody Northropaf28a532016-09-27 10:50:57 -0600551
lenny-lunargfbe22392016-06-06 11:07:53 -0600552#else
Cody Northropaf28a532016-09-27 10:50:57 -0600553
lenny-lunargfbe22392016-06-06 11:07:53 -0600554 printf("%s\n", message);
555 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600556
lenny-lunargfbe22392016-06-06 11:07:53 -0600557#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600558
lenny-lunargfbe22392016-06-06 11:07:53 -0600559 free(message);
560
Mark Young66510e52017-11-10 10:05:14 -0700561 // Don't bail out, but keep going.
lenny-lunargfbe22392016-06-06 11:07:53 -0600562 return false;
563}
564
Ian Elliottd940ca22017-03-22 08:31:27 -0600565bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
566 // The desired time was the earliest time that the present should have
567 // occured. In almost every case, the actual time should be later than the
568 // desired time. We should only consider the actual time "late" if it is
569 // after "desired + rdur".
570 if (actual <= desired) {
571 // The actual time was before or equal to the desired time. This will
572 // probably never happen, but in case it does, return false since the
573 // present was obviously NOT late.
574 return false;
575 }
Liam Middlebrook881fe392018-05-03 15:52:32 -0700576 uint64_t deadline = desired + rdur;
Ian Elliottd940ca22017-03-22 08:31:27 -0600577 if (actual > deadline) {
578 return true;
579 } else {
580 return false;
581 }
582}
Dave Houlton5fa47912018-02-16 11:02:26 -0700583bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600584 if (earliest < actual) {
585 // Consider whether this present could have occured earlier. Make sure
586 // that earliest time was at least 2msec earlier than actual time, and
587 // that the margin was at least 2msec:
588 uint64_t diff = actual - earliest;
589 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
590 // This present could have occured earlier because both: 1) the
591 // earliest time was at least 2 msec before actual time, and 2) the
592 // margin was at least 2msec.
593 return true;
594 }
595 }
596 return false;
597}
598
Tony-LunarG6cebf142019-09-11 14:32:23 -0600599// Forward declarations:
Ian Elliottcf074d32015-10-16 18:02:43 -0600600static void demo_resize(struct demo *demo);
Tony-LunarG6cebf142019-09-11 14:32:23 -0600601static void demo_create_surface(struct demo *demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600602
Dave Houlton5fa47912018-02-16 11:02:26 -0700603static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700604 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600605 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700606 if ((typeBits & 1) == 1) {
607 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700608 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700609 *typeIndex = i;
610 return true;
611 }
612 }
613 typeBits >>= 1;
614 }
615 // No memory types matched, return failure
616 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600617}
618
Karl Schultze4dc75c2016-02-02 15:37:51 -0700619static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600620 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600621
Tony Barbource7524e2016-07-28 12:01:45 -0600622 // This function could get called twice if the texture uses a staging buffer
623 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700624 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600625
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600626 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600627 assert(!err);
628
Tony Barbource7524e2016-07-28 12:01:45 -0600629 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700630 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700631 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
632 assert(!err);
633
Karl Schultze4dc75c2016-02-02 15:37:51 -0700634 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700635 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
636 .pNext = NULL,
637 .waitSemaphoreCount = 0,
638 .pWaitSemaphores = NULL,
639 .pWaitDstStageMask = NULL,
640 .commandBufferCount = 1,
641 .pCommandBuffers = cmd_bufs,
642 .signalSemaphoreCount = 0,
643 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600644
Tony Barbource7524e2016-07-28 12:01:45 -0600645 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600646 assert(!err);
647
Tony Barbource7524e2016-07-28 12:01:45 -0600648 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600649 assert(!err);
650
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600651 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600652 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600653 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600654}
655
Dave Houlton5fa47912018-02-16 11:02:26 -0700656static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
657 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600658 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600659 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600660
Dave Houlton5fa47912018-02-16 11:02:26 -0700661 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
662 .pNext = NULL,
663 .srcAccessMask = srcAccessMask,
664 .dstAccessMask = 0,
Tony-LunarGa141b962018-05-30 11:33:19 -0600665 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
666 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Dave Houlton5fa47912018-02-16 11:02:26 -0700667 .oldLayout = old_image_layout,
668 .newLayout = new_image_layout,
669 .image = image,
670 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600671
Tony Barboureb5077b2016-10-19 15:23:36 -0600672 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700673 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
674 /* Make sure anything that was copying from this image has completed */
675 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
676 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600677
Dave Houlton5fa47912018-02-16 11:02:26 -0700678 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
679 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
680 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700681
Dave Houlton5fa47912018-02-16 11:02:26 -0700682 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
683 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
684 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700685
Dave Houlton5fa47912018-02-16 11:02:26 -0700686 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
687 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
688 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600689
Dave Houlton5fa47912018-02-16 11:02:26 -0700690 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
691 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
692 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600693
Dave Houlton5fa47912018-02-16 11:02:26 -0700694 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
695 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
696 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600697
Dave Houlton5fa47912018-02-16 11:02:26 -0700698 default:
699 image_memory_barrier.dstAccessMask = 0;
700 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600701 }
702
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600703 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600704
Dave Houlton5fa47912018-02-16 11:02:26 -0700705 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600706}
707
Karl Schultze4dc75c2016-02-02 15:37:51 -0700708static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Lenny Komowe830b592018-02-23 16:18:36 -0700709 VkDebugUtilsLabelEXT label;
710 memset(&label, 0, sizeof(label));
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700711 const VkCommandBufferBeginInfo cmd_buf_info = {
712 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
713 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600714 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700715 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700716 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800717 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700718 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
719 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800720 };
721 const VkRenderPassBeginInfo rp_begin = {
722 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
723 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800724 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700725 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800726 .renderArea.offset.x = 0,
727 .renderArea.offset.y = 0,
728 .renderArea.extent.width = demo->width,
729 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600730 .clearValueCount = 2,
731 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700732 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800733 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600734
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600735 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Mark Young66510e52017-11-10 10:05:14 -0700736
737 if (demo->validate) {
738 // Set a name for the command buffer
739 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
740 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
741 .pNext = NULL,
742 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
743 .objectHandle = (uint64_t)cmd_buf,
744 .pObjectName = "CubeDrawCommandBuf",
745 };
746 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
747
748 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
749 label.pNext = NULL;
750 label.pLabelName = "DrawBegin";
751 label.color[0] = 0.4f;
752 label.color[1] = 0.3f;
753 label.color[2] = 0.2f;
754 label.color[3] = 0.1f;
755 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
756 }
757
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600758 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800759 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Mark Young66510e52017-11-10 10:05:14 -0700760
761 if (demo->validate) {
762 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
763 label.pNext = NULL;
764 label.pLabelName = "InsideRenderPass";
765 label.color[0] = 8.4f;
766 label.color[1] = 7.3f;
767 label.color[2] = 6.2f;
768 label.color[3] = 7.1f;
769 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
770 }
771
Karl Schultze4dc75c2016-02-02 15:37:51 -0700772 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700773 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
774 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600775 VkViewport viewport;
776 memset(&viewport, 0, sizeof(viewport));
Jeremy Kniager979b5312019-11-22 14:55:57 -0700777 float viewport_dimension;
778 if (demo->width < demo->height) {
779 viewport_dimension = (float)demo->width;
780 viewport.y = (demo->height - demo->width) / 2.0f;
781 } else {
782 viewport_dimension = (float)demo->height;
783 viewport.x = (demo->width - demo->height) / 2.0f;
784 }
785 viewport.height = viewport_dimension;
786 viewport.width = viewport_dimension;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700787 viewport.minDepth = (float)0.0f;
788 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700789 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600790
791 VkRect2D scissor;
792 memset(&scissor, 0, sizeof(scissor));
793 scissor.extent.width = demo->width;
794 scissor.extent.height = demo->height;
795 scissor.offset.x = 0;
796 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700797 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Mark Young66510e52017-11-10 10:05:14 -0700798
799 if (demo->validate) {
800 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
801 label.pNext = NULL;
802 label.pLabelName = "ActualDraw";
Mike Schuchardt4cf3aa42018-02-23 12:44:05 -0700803 label.color[0] = -0.4f;
804 label.color[1] = -0.3f;
805 label.color[2] = -0.2f;
806 label.color[3] = -0.1f;
Mark Young66510e52017-11-10 10:05:14 -0700807 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
808 }
809
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600810 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Mark Young66510e52017-11-10 10:05:14 -0700811 if (demo->validate) {
812 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
813 }
814
Tony Barbour98390392016-07-28 10:50:13 -0600815 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600816 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800817 vkCmdEndRenderPass(cmd_buf);
Mark Young66510e52017-11-10 10:05:14 -0700818 if (demo->validate) {
819 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
820 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600821
Tony Barbour22e06272016-09-07 16:07:56 -0600822 if (demo->separate_present_queue) {
823 // We have to transfer ownership from the graphics queue family to the
824 // present queue family to be able to present. Note that we don't have
825 // to transfer from present queue family back to graphics queue family at
826 // the start of the next frame because we don't care about the image's
827 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700828 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
829 .pNext = NULL,
830 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600831 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700832 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
833 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
834 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
835 .dstQueueFamilyIndex = demo->present_queue_family_index,
836 .image = demo->swapchain_image_resources[demo->current_buffer].image,
837 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600838
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600839 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
840 NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600841 }
Mark Young66510e52017-11-10 10:05:14 -0700842 if (demo->validate) {
843 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
844 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600845 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600846 assert(!err);
847}
848
Tony Barbour22e06272016-09-07 16:07:56 -0600849void demo_build_image_ownership_cmd(struct demo *demo, int i) {
850 VkResult U_ASSERT_ONLY err;
851
852 const VkCommandBufferBeginInfo cmd_buf_info = {
853 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
854 .pNext = NULL,
855 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
856 .pInheritanceInfo = NULL,
857 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700858 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600859 assert(!err);
860
Dave Houlton5fa47912018-02-16 11:02:26 -0700861 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
862 .pNext = NULL,
863 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600864 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700865 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
866 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
867 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
868 .dstQueueFamilyIndex = demo->present_queue_family_index,
869 .image = demo->swapchain_image_resources[i].image,
870 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600871
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600872 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
873 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700874 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600875 assert(!err);
876}
877
Karl Schultze4dc75c2016-02-02 15:37:51 -0700878void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600879 mat4x4 MVP, Model, VP;
880 int matrixSize = sizeof(MVP);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600881
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600882 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600883
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700884 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600885 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700886 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600887 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600888
Tony-LunarG37af49f2019-12-19 12:04:19 -0700889 memcpy(demo->swapchain_image_resources[demo->current_buffer].uniform_memory_ptr, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600890}
891
Ian Elliottd940ca22017-03-22 08:31:27 -0600892void DemoUpdateTargetIPD(struct demo *demo) {
893 // Look at what happened to previous presents, and make appropriate
894 // adjustments in timing:
895 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700896 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600897 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600898
Dave Houlton5fa47912018-02-16 11:02:26 -0700899 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600900 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600901 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700902 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600903 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700904 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600905 assert(!err);
906
907 bool early = false;
908 bool late = false;
909 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700910 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600911 if (!demo->syncd_with_actual_presents) {
912 // This is the first time that we've received an
913 // actualPresentTime for this swapchain. In order to not
914 // perceive these early frames as "late", we need to sync-up
915 // our future desiredPresentTime's with the
916 // actualPresentTime(s) that we're receiving now.
917 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600918
Ian Elliottd940ca22017-03-22 08:31:27 -0600919 // So that we don't suspect any pending presents as late,
920 // record them all as suspected-late presents:
921 demo->last_late_id = demo->next_present_id - 1;
922 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600923 demo->syncd_with_actual_presents = true;
924 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700925 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600926 demo->refresh_duration)) {
927 // This image could have been presented earlier. We don't want
928 // to decrease the target_IPD until we've seen early presents
929 // for at least two seconds.
930 if (demo->last_early_id == past[i].presentID) {
931 // We've now seen two seconds worth of early presents.
932 // Flag it as such, and reset the counter:
933 early = true;
934 demo->last_early_id = 0;
935 } else if (demo->last_early_id == 0) {
936 // This is the first early present we've seen.
937 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700938 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
939 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600940 demo->last_early_id = past[i].presentID + howManyPresents;
941 } else {
942 // We are in the midst of a set of early images,
943 // and so we won't do anything.
944 }
945 late = false;
946 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700947 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600948 // This image was presented after its desired time. Since
949 // there's a delay between calling vkQueuePresentKHR and when
950 // we get the timing data, several presents may have been late.
951 // Thus, we need to threat all of the outstanding presents as
952 // being likely late, so that we only increase the target_IPD
953 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700954 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600955 late = true;
956 // Record the last suspected-late present:
957 demo->last_late_id = demo->next_present_id - 1;
958 } else {
959 // We are in the midst of a set of likely-late images,
960 // and so we won't do anything.
961 }
962 early = false;
963 demo->last_early_id = 0;
964 } else {
965 // Since this image was not presented early or late, reset
966 // any sets of early or late presentIDs:
967 early = false;
968 late = false;
969 calibrate_next = true;
970 demo->last_early_id = 0;
971 demo->last_late_id = 0;
972 }
973 }
974
975 if (early) {
976 // Since we've seen at least two-seconds worth of presnts that
977 // could have occured earlier than desired, let's decrease the
978 // target_IPD (i.e. increase the frame rate):
979 //
980 // TODO(ianelliott): Try to calculate a better target_IPD based
981 // on the most recently-seen present (this is overly-simplistic).
982 demo->refresh_duration_multiplier--;
983 if (demo->refresh_duration_multiplier == 0) {
984 // This should never happen, but in case it does, don't
985 // try to go faster.
986 demo->refresh_duration_multiplier = 1;
987 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700988 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600989 }
990 if (late) {
991 // Since we found a new instance of a late present, we want to
992 // increase the target_IPD (i.e. decrease the frame rate):
993 //
994 // TODO(ianelliott): Try to calculate a better target_IPD based
995 // on the most recently-seen present (this is overly-simplistic).
996 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -0700997 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600998 }
999
1000 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001001 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
1002 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -06001003 }
Karl Schultza33771f2018-05-28 16:16:47 -06001004 free(past);
Ian Elliottd940ca22017-03-22 08:31:27 -06001005 }
Ian Elliottd940ca22017-03-22 08:31:27 -06001006}
1007
Karl Schultze4dc75c2016-02-02 15:37:51 -07001008static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001009 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001010
Damien Leonec336d822017-04-14 16:10:14 -07001011 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +03001012 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1013 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001014
Tony Barbour8b7c9112017-06-29 13:34:41 -06001015 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -06001016 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -07001017 err =
1018 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1019 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001020
Tony Barbour6914e6d2017-06-02 12:11:29 -06001021 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1022 // demo->swapchain is out of date (e.g. the window was resized) and
1023 // must be recreated:
1024 demo_resize(demo);
1025 } else if (err == VK_SUBOPTIMAL_KHR) {
1026 // demo->swapchain is not as optimal as it could be, but the platform's
1027 // presentation engine will still present the image correctly.
1028 break;
Tony-LunarG6cebf142019-09-11 14:32:23 -06001029 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1030 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1031 demo_create_surface(demo);
1032 demo_resize(demo);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001033 } else {
1034 assert(!err);
1035 }
Tony Barbour8b7c9112017-06-29 13:34:41 -06001036 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001037
Tony Barbour3eafe1f2017-06-14 11:59:55 -06001038 demo_update_data_buffer(demo);
1039
Ian Elliottd940ca22017-03-22 08:31:27 -06001040 if (demo->VK_GOOGLE_display_timing_enabled) {
1041 // Look at what happened to previous presents, and make appropriate
1042 // adjustments in timing:
1043 DemoUpdateTargetIPD(demo);
1044
1045 // Note: a real application would position its geometry to that it's in
1046 // the correct locatoin for when the next image is presented. It might
1047 // also wait, so that there's less latency between any input and when
1048 // the next image is rendered/presented. This demo program is so
1049 // simple that it doesn't do either of those.
1050 }
1051
Tony Barbource7524e2016-07-28 12:01:45 -06001052 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001053 // that the image won't be rendered to until the presentation
1054 // engine has fully released ownership to the application, and it is
1055 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001056 VkPipelineStageFlags pipe_stage_flags;
1057 VkSubmitInfo submit_info;
1058 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1059 submit_info.pNext = NULL;
1060 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1061 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1062 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001063 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001064 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001065 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001066 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001067 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -07001068 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001069 assert(!err);
1070
Tony Barbour22e06272016-09-07 16:07:56 -06001071 if (demo->separate_present_queue) {
1072 // If we are using separate queues, change image ownership to the
1073 // present queue before presenting, waiting for the draw complete
1074 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001075 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001076 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1077 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001078 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001079 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001080 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001081 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001082 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001083 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1084 assert(!err);
1085 }
1086
1087 // If we are using separate queues we have to wait for image ownership,
1088 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001089 VkPresentInfoKHR present = {
1090 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001091 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001092 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -07001093 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1094 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001095 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001096 .pSwapchains = &demo->swapchain,
1097 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001098 };
1099
Dave Airlie48a68f92019-04-12 17:04:48 +10001100 VkRectLayerKHR rect;
1101 VkPresentRegionKHR region;
1102 VkPresentRegionsKHR regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001103 if (demo->VK_KHR_incremental_present_enabled) {
1104 // If using VK_KHR_incremental_present, we provide a hint of the region
1105 // that contains changed content relative to the previously-presented
1106 // image. The implementation can use this hint in order to save
1107 // work/power (by only copying the region in the hint). The
1108 // implementation is free to ignore the hint though, and so we must
1109 // ensure that the entire image has the correctly-drawn content.
1110 uint32_t eighthOfWidth = demo->width / 8;
1111 uint32_t eighthOfHeight = demo->height / 8;
Dave Airlie48a68f92019-04-12 17:04:48 +10001112
1113 rect.offset.x = eighthOfWidth;
1114 rect.offset.y = eighthOfHeight;
1115 rect.extent.width = eighthOfWidth * 6;
1116 rect.extent.height = eighthOfHeight * 6;
1117 rect.layer = 0;
1118
1119 region.rectangleCount = 1;
1120 region.pRectangles = &rect;
1121
1122 regions.sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR;
1123 regions.pNext = present.pNext;
1124 regions.swapchainCount = present.swapchainCount;
1125 regions.pRegions = &region;
Ian Elliott53622a72017-03-28 11:06:33 -06001126 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001127 }
1128
Ian Elliottd940ca22017-03-22 08:31:27 -06001129 if (demo->VK_GOOGLE_display_timing_enabled) {
1130 VkPresentTimeGOOGLE ptime;
1131 if (demo->prev_desired_present_time == 0) {
1132 // This must be the first present for this swapchain.
1133 //
1134 // We don't know where we are relative to the presentation engine's
1135 // display's refresh cycle. We also don't know how long rendering
1136 // takes. Let's make a grossly-simplified assumption that the
1137 // desiredPresentTime should be half way between now and
1138 // now+target_IPD. We will adjust over time.
1139 uint64_t curtime = getTimeInNanoseconds();
1140 if (curtime == 0) {
1141 // Since we didn't find out the current time, don't give a
1142 // desiredPresentTime:
1143 ptime.desiredPresentTime = 0;
1144 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001145 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1146 }
1147 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001148 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001149 }
1150 ptime.presentID = demo->next_present_id++;
1151 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001152
1153 VkPresentTimesInfoGOOGLE present_time = {
1154 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1155 .pNext = present.pNext,
1156 .swapchainCount = present.swapchainCount,
1157 .pTimes = &ptime,
1158 };
1159 if (demo->VK_GOOGLE_display_timing_enabled) {
1160 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001161 }
1162 }
1163
Tony Barboura2a67812016-07-18 13:21:06 -06001164 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001165 demo->frame_index += 1;
1166 demo->frame_index %= FRAME_LAG;
1167
Ian Elliottcf074d32015-10-16 18:02:43 -06001168 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1169 // demo->swapchain is out of date (e.g. the window was resized) and
1170 // must be recreated:
1171 demo_resize(demo);
1172 } else if (err == VK_SUBOPTIMAL_KHR) {
1173 // demo->swapchain is not as optimal as it could be, but the platform's
1174 // presentation engine will still present the image correctly.
Tony-LunarG6cebf142019-09-11 14:32:23 -06001175 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1176 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1177 demo_create_surface(demo);
1178 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06001179 } else {
1180 assert(!err);
1181 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001182}
1183
Karl Schultze4dc75c2016-02-02 15:37:51 -07001184static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001185 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001186 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001187
Ian Elliottb08e0d92015-11-20 11:55:46 -07001188 // Check the surface capabilities and formats
1189 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001190 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001191 assert(!err);
1192
Ian Elliott8528eff2015-08-07 15:56:59 -06001193 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001194 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001195 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001196 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001197 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001198 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001199 assert(!err);
1200
Ian Elliotta0781972015-08-21 15:09:33 -06001201 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001202 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1203 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1204 // If the surface size is undefined, the size is set to the size
1205 // of the images requested, which must fit within the minimum and
1206 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001207 swapchainExtent.width = demo->width;
1208 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001209
1210 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1211 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1212 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1213 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1214 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001215
Ian Elliottcf7f7482016-08-19 03:46:58 -06001216 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1217 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1218 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1219 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1220 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001221 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001222 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001223 swapchainExtent = surfCapabilities.currentExtent;
1224 demo->width = surfCapabilities.currentExtent.width;
1225 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001226 }
1227
Jeremy Kniager602e4182018-01-19 10:52:34 -07001228 if (demo->width == 0 || demo->height == 0) {
1229 demo->is_minimized = true;
1230 return;
1231 } else {
1232 demo->is_minimized = false;
1233 }
1234
Tony Barbour66a56c32016-09-21 13:10:56 -06001235 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001236 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001237 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001238
Ian Elliottb18fdde2016-10-03 12:11:12 -06001239 // There are times when you may wish to use another present mode. The
1240 // following code shows how to select them, and the comments provide some
1241 // reasons you may wish to use them.
1242 //
1243 // It should be noted that Vulkan 1.0 doesn't provide a method for
1244 // synchronizing rendering with the presentation engine's display. There
1245 // is a method provided for throttling rendering with the display, but
1246 // there are some presentation engines for which this method will not work.
1247 // If an application doesn't throttle its rendering, and if it renders much
1248 // faster than the refresh rate of the display, this can waste power on
1249 // mobile devices. That is because power is being spent rendering images
1250 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001251
Ian Elliottb18fdde2016-10-03 12:11:12 -06001252 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1253 // tearing, or have some way of synchronizing their rendering with the
1254 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001255 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1256 // generally render a new presentable image every refresh cycle, but are
1257 // occasionally early. In this case, the application wants the new image
1258 // to be displayed instead of the previously-queued-for-presentation image
1259 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001260 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1261 // render a new presentable image every refresh cycle, but are occasionally
1262 // late. In this case (perhaps because of stuttering/latency concerns),
1263 // the application wants the late image to be immediately displayed, even
1264 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001265
Dave Houlton5fa47912018-02-16 11:02:26 -07001266 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001267 for (size_t i = 0; i < presentModeCount; ++i) {
1268 if (presentModes[i] == demo->presentMode) {
1269 swapchainPresentMode = demo->presentMode;
1270 break;
1271 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001272 }
1273 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001274 if (swapchainPresentMode != demo->presentMode) {
1275 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1276 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001277
Tony Barbour84376fe2017-01-06 15:54:22 -07001278 // Determine the number of VkImages to use in the swap chain.
1279 // Application desires to acquire 3 images at a time for triple
1280 // buffering
1281 uint32_t desiredNumOfSwapchainImages = 3;
1282 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1283 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1284 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001285 // If maxImageCount is 0, we can ask for as many images as we want;
1286 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001287 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001288 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001289 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001290 }
1291
Tony Barbour9fd6d352015-09-16 15:01:32 -06001292 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001293 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001294 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001295 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001296 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001297 }
1298
Tony Barbour820b55b2017-03-14 08:55:46 -06001299 // Find a supported composite alpha mode - one of these is guaranteed to be set
1300 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1301 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1302 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1303 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1304 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1305 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1306 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001307 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001308 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1309 compositeAlpha = compositeAlphaFlags[i];
1310 break;
1311 }
1312 }
1313
Tony Barboura2a67812016-07-18 13:21:06 -06001314 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001315 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001316 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001317 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001318 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001319 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001320 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001321 .imageExtent =
1322 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001323 .width = swapchainExtent.width,
1324 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001325 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001326 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001327 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001328 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001329 .imageArrayLayers = 1,
1330 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1331 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001332 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001333 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001334 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001335 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001336 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001337 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001338 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001339 assert(!err);
1340
Ian Elliottcf074d32015-10-16 18:02:43 -06001341 // If we just re-created an existing swapchain, we should destroy the old
1342 // swapchain at this point.
1343 // Note: destroying the swapchain also cleans up all its associated
1344 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001345 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001346 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001347 }
1348
Dave Houlton5fa47912018-02-16 11:02:26 -07001349 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001350 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001351
Dave Houlton5fa47912018-02-16 11:02:26 -07001352 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001353 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001354 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001355 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001356
Dave Houlton5fa47912018-02-16 11:02:26 -07001357 demo->swapchain_image_resources =
1358 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001359 assert(demo->swapchain_image_resources);
1360
Ian Elliotta0781972015-08-21 15:09:33 -06001361 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001362 VkImageViewCreateInfo color_image_view = {
1363 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001364 .pNext = NULL,
1365 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 .components =
1367 {
Richard S. Wright Jrca51bc72021-01-06 13:03:18 -05001368 .r = VK_COMPONENT_SWIZZLE_IDENTITY,
1369 .g = VK_COMPONENT_SWIZZLE_IDENTITY,
1370 .b = VK_COMPONENT_SWIZZLE_IDENTITY,
1371 .a = VK_COMPONENT_SWIZZLE_IDENTITY,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001372 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001373 .subresourceRange =
1374 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001375 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1376 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001377 };
1378
Tony Barboura72d9492017-01-11 13:35:09 -07001379 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380
Tony Barboura72d9492017-01-11 13:35:09 -07001381 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001382
Dave Houlton5fa47912018-02-16 11:02:26 -07001383 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001384 assert(!err);
1385 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001386
Ian Elliottd940ca22017-03-22 08:31:27 -06001387 if (demo->VK_GOOGLE_display_timing_enabled) {
1388 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001389 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001390 assert(!err);
1391 demo->refresh_duration = rc_dur.refreshDuration;
1392
1393 demo->syncd_with_actual_presents = false;
1394 // Initially target 1X the refresh duration:
1395 demo->target_IPD = demo->refresh_duration;
1396 demo->refresh_duration_multiplier = 1;
1397 demo->prev_desired_present_time = 0;
1398 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001399 }
1400
Jason Chen5d4fee92019-04-04 12:45:29 +08001401 if (NULL != swapchainImages) {
1402 free(swapchainImages);
1403 }
1404
Mark Young04fd3d82016-01-25 14:43:50 -07001405 if (NULL != presentModes) {
1406 free(presentModes);
1407 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408}
1409
Karl Schultze4dc75c2016-02-02 15:37:51 -07001410static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001411 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001412 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001413 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001415 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001417 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001419 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001420 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001421 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001422 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423 .flags = 0,
1424 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001425
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001426 VkImageViewCreateInfo view = {
1427 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001429 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001430 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001431 .subresourceRange =
1432 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001433 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001434 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001436
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001437 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001438 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001439 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001440
1441 demo->depth.format = depth_format;
1442
1443 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001444 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001445 assert(!err);
1446
Karl Schultze4dc75c2016-02-02 15:37:51 -07001447 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001448 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001449
Chia-I Wuf48700b2015-11-10 16:21:09 +08001450 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001451 demo->depth.mem_alloc.pNext = NULL;
1452 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1453 demo->depth.mem_alloc.memoryTypeIndex = 0;
1454
Dave Houlton5fa47912018-02-16 11:02:26 -07001455 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001456 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001457 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001458
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001459 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001460 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001461 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001462
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001463 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001464 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001465 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001466
1467 /* create image view */
1468 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001469 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001470 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001471}
1472
Karl Schultzb7940402018-05-29 13:09:22 -06001473/* Convert ppm image data from header file into RGBA texture image */
1474#include "lunarg.ppm.h"
Dave Houlton5fa47912018-02-16 11:02:26 -07001475bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Karl Schultzb7940402018-05-29 13:09:22 -06001476 (void)filename;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001477 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001478 cPtr = (char *)lunarg_ppm;
1479 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001480 return false;
1481 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001482 while (strncmp(cPtr++, "\n", 1))
1483 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001484 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001485 if (rgba_data == NULL) {
1486 return true;
1487 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001488 while (strncmp(cPtr++, "\n", 1))
1489 ;
1490 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001491 return false;
1492 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001493 while (strncmp(cPtr++, "\n", 1))
1494 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001495 for (int y = 0; y < *height; y++) {
1496 uint8_t *rowPtr = rgba_data;
1497 for (int x = 0; x < *width; x++) {
1498 memcpy(rowPtr, cPtr, 3);
1499 rowPtr[3] = 255; /* Alpha of 1 */
1500 rowPtr += 4;
1501 cPtr += 3;
1502 }
1503 rgba_data += layout->rowPitch;
1504 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001505 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001506}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001507
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001508static void demo_prepare_texture_buffer(struct demo *demo, const char *filename, struct texture_object *tex_obj) {
1509 int32_t tex_width;
1510 int32_t tex_height;
1511 VkResult U_ASSERT_ONLY err;
1512 bool U_ASSERT_ONLY pass;
1513
1514 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
1515 ERR_EXIT("Failed to load textures", "Load Texture Failure");
1516 }
1517
1518 tex_obj->tex_width = tex_width;
1519 tex_obj->tex_height = tex_height;
1520
1521 const VkBufferCreateInfo buffer_create_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
1522 .pNext = NULL,
1523 .flags = 0,
1524 .size = tex_width * tex_height * 4,
1525 .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1526 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1527 .queueFamilyIndexCount = 0,
1528 .pQueueFamilyIndices = NULL};
1529
1530 err = vkCreateBuffer(demo->device, &buffer_create_info, NULL, &tex_obj->buffer);
1531 assert(!err);
1532
1533 VkMemoryRequirements mem_reqs;
1534 vkGetBufferMemoryRequirements(demo->device, tex_obj->buffer, &mem_reqs);
1535
1536 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1537 tex_obj->mem_alloc.pNext = NULL;
1538 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1539 tex_obj->mem_alloc.memoryTypeIndex = 0;
1540
1541 VkFlags requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1542 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, requirements, &tex_obj->mem_alloc.memoryTypeIndex);
1543 assert(pass);
1544
1545 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
1546 assert(!err);
1547
1548 /* bind memory */
1549 err = vkBindBufferMemory(demo->device, tex_obj->buffer, tex_obj->mem, 0);
1550 assert(!err);
1551
1552 VkSubresourceLayout layout;
1553 memset(&layout, 0, sizeof(layout));
1554 layout.rowPitch = tex_width * 4;
1555
1556 void *data;
1557 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
1558 assert(!err);
1559
1560 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1561 fprintf(stderr, "Error loading texture: %s\n", filename);
1562 }
1563
1564 vkUnmapMemory(demo->device, tex_obj->mem);
1565}
1566
Dave Houlton5fa47912018-02-16 11:02:26 -07001567static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1568 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001569 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001570 int32_t tex_width;
1571 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001572 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001573 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001574
Karl Schultze4dc75c2016-02-02 15:37:51 -07001575 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001576 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001577 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001578
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001579 tex_obj->tex_width = tex_width;
1580 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001581
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001582 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001583 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001584 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001585 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001586 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001587 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001588 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001589 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001590 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001591 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001592 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001593 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001594 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001595 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001596
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001597 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001598
Dave Houlton5fa47912018-02-16 11:02:26 -07001599 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001600 assert(!err);
1601
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001602 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001603
Chia-I Wuf48700b2015-11-10 16:21:09 +08001604 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001605 tex_obj->mem_alloc.pNext = NULL;
1606 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1607 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001608
Dave Houlton5fa47912018-02-16 11:02:26 -07001609 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001610 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001611
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001612 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001613 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001614 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001615
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001616 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001617 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001618 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001619
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001620 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001621 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001622 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001623 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001624 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001625 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001626 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001627 void *data;
1628
Dave Houlton5fa47912018-02-16 11:02:26 -07001629 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001630
Dave Houlton5fa47912018-02-16 11:02:26 -07001631 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001632 assert(!err);
1633
1634 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1635 fprintf(stderr, "Error loading texture: %s\n", filename);
1636 }
1637
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001638 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001639 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001640
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001641 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001642}
1643
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001644static void demo_destroy_texture(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001645 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001646 vkFreeMemory(demo->device, tex_objs->mem, NULL);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001647 if (tex_objs->image) vkDestroyImage(demo->device, tex_objs->image, NULL);
1648 if (tex_objs->buffer) vkDestroyBuffer(demo->device, tex_objs->buffer, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001649}
1650
Karl Schultze4dc75c2016-02-02 15:37:51 -07001651static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001652 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001653 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001654 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001655
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001656 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001657
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001658 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001659 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001660
Dave Houlton5fa47912018-02-16 11:02:26 -07001661 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001662 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001663 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1664 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001665 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1666 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001667 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1668 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001669 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001670 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001671 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001672 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001673
Tony Barbour16156cb2016-10-19 14:15:49 -06001674 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001675 demo_prepare_texture_buffer(demo, tex_files[i], &demo->staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001676
Dave Houlton5fa47912018-02-16 11:02:26 -07001677 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1678 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1679 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001680
Dave Houlton5fa47912018-02-16 11:02:26 -07001681 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1682 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001683 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001684
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001685 VkBufferImageCopy copy_region = {
1686 .bufferOffset = 0,
1687 .bufferRowLength = demo->staging_texture.tex_width,
1688 .bufferImageHeight = demo->staging_texture.tex_height,
1689 .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1690 .imageOffset = {0, 0, 0},
1691 .imageExtent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001692 };
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001693
1694 vkCmdCopyBufferToImage(demo->cmd, demo->staging_texture.buffer, demo->textures[i].image,
1695 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001696
Dave Houlton5fa47912018-02-16 11:02:26 -07001697 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1698 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001699 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001700
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001701 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001702 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1703 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001704 }
1705
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001706 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001707 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001708 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001709 .magFilter = VK_FILTER_NEAREST,
1710 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001711 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001712 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1713 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1714 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001715 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001716 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001717 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001718 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719 .minLod = 0.0f,
1720 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001721 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001722 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001723 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001724
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001725 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001726 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001728 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001729 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001730 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001731 .components =
1732 {
Richard S. Wright Jrca51bc72021-01-06 13:03:18 -05001733 VK_COMPONENT_SWIZZLE_IDENTITY,
1734 VK_COMPONENT_SWIZZLE_IDENTITY,
1735 VK_COMPONENT_SWIZZLE_IDENTITY,
1736 VK_COMPONENT_SWIZZLE_IDENTITY,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001737 },
1738 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001739 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001740 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001741
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001743 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744 assert(!err);
1745
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001746 /* create image view */
1747 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001748 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001749 assert(!err);
1750 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752
Tony Barboura72d9492017-01-11 13:35:09 -07001753void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001754 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001755 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001756 VkMemoryAllocateInfo mem_alloc;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001757 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001758 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001759 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001760 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001761
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001762 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001763 mat4x4_mul(MVP, VP, demo->model_matrix);
1764 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001765 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001766
Karl Schultza2615462017-01-20 13:19:20 -07001767 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001768 data.position[i][0] = g_vertex_buffer_data[i * 3];
1769 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1770 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001771 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001772 data.attr[i][0] = g_uv_buffer_data[2 * i];
1773 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001774 data.attr[i][2] = 0;
1775 data.attr[i][3] = 0;
1776 }
1777
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001778 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001779 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001780 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001781 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001782
Tony Barboura72d9492017-01-11 13:35:09 -07001783 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001784 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001785 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001786
Dave Houlton5fa47912018-02-16 11:02:26 -07001787 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001788
Tony Barboura72d9492017-01-11 13:35:09 -07001789 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1790 mem_alloc.pNext = NULL;
1791 mem_alloc.allocationSize = mem_reqs.size;
1792 mem_alloc.memoryTypeIndex = 0;
1793
Dave Houlton5fa47912018-02-16 11:02:26 -07001794 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1795 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1796 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001797 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001798
Dave Houlton5fa47912018-02-16 11:02:26 -07001799 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001800 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001801
Tony-LunarG37af49f2019-12-19 12:04:19 -07001802 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, 0,
1803 &demo->swapchain_image_resources[i].uniform_memory_ptr);
Tony Barboura72d9492017-01-11 13:35:09 -07001804 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001805
Tony-LunarG37af49f2019-12-19 12:04:19 -07001806 memcpy(demo->swapchain_image_resources[i].uniform_memory_ptr, &data, sizeof data);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001807
Tony Barboura72d9492017-01-11 13:35:09 -07001808 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001809 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001810 assert(!err);
1811 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001812}
1813
Karl Schultze4dc75c2016-02-02 15:37:51 -07001814static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001815 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001816 [0] =
1817 {
1818 .binding = 0,
1819 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1820 .descriptorCount = 1,
1821 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1822 .pImmutableSamplers = NULL,
1823 },
1824 [1] =
1825 {
1826 .binding = 1,
1827 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1828 .descriptorCount = DEMO_TEXTURE_COUNT,
1829 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1830 .pImmutableSamplers = NULL,
1831 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001832 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001833 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001834 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001835 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001836 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001837 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001838 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001839 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001840
Dave Houlton5fa47912018-02-16 11:02:26 -07001841 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001842 assert(!err);
1843
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001844 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001845 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1846 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001847 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001848 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001849 };
1850
Dave Houlton5fa47912018-02-16 11:02:26 -07001851 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001853}
1854
Karl Schultze4dc75c2016-02-02 15:37:51 -07001855static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001856 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1857 // because at the start of the renderpass, we don't care about their contents.
1858 // At the start of the subpass, the color attachment's layout will be transitioned
1859 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1860 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1861 // the renderpass, the color attachment's layout will be transitioned to
1862 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1863 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001864 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001865 [0] =
1866 {
1867 .format = demo->format,
1868 .flags = 0,
1869 .samples = VK_SAMPLE_COUNT_1_BIT,
1870 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1871 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1872 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1873 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1874 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1875 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1876 },
1877 [1] =
1878 {
1879 .format = demo->depth.format,
1880 .flags = 0,
1881 .samples = VK_SAMPLE_COUNT_1_BIT,
1882 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1883 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1884 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1885 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1886 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1887 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1888 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001889 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001890 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001891 .attachment = 0,
1892 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001893 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001894 const VkAttachmentReference depth_reference = {
1895 .attachment = 1,
1896 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1897 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001898 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001899 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1900 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001901 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001902 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001903 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001904 .pColorAttachments = &color_reference,
1905 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001906 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001907 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001908 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001909 };
Tony-LunarG495604b2019-06-13 15:32:05 -06001910
1911 VkSubpassDependency attachmentDependencies[2] = {
1912 [0] =
1913 {
1914 // Depth buffer is shared between swapchain images
1915 .srcSubpass = VK_SUBPASS_EXTERNAL,
1916 .dstSubpass = 0,
1917 .srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1918 .dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1919 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1920 .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1921 .dependencyFlags = 0,
1922 },
1923 [1] =
1924 {
1925 // Image Layout Transition
1926 .srcSubpass = VK_SUBPASS_EXTERNAL,
1927 .dstSubpass = 0,
1928 .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1929 .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1930 .srcAccessMask = 0,
1931 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
1932 .dependencyFlags = 0,
1933 },
1934 };
1935
Chia-I Wuf973e322015-07-08 13:34:24 +08001936 const VkRenderPassCreateInfo rp_info = {
1937 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1938 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001939 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001940 .attachmentCount = 2,
1941 .pAttachments = attachments,
1942 .subpassCount = 1,
1943 .pSubpasses = &subpass,
Tony-LunarG495604b2019-06-13 15:32:05 -06001944 .dependencyCount = 2,
1945 .pDependencies = attachmentDependencies,
Chia-I Wuf973e322015-07-08 13:34:24 +08001946 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001947 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001948
Chia-I Wu63636062015-10-26 21:10:41 +08001949 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001950 assert(!err);
1951}
1952
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001953static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001954 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001955 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001956 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001957
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001958 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1959 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001960 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001961 moduleCreateInfo.codeSize = size;
1962 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001963
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001964 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1965 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001966
1967 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001968}
1969
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001970static void demo_prepare_vs(struct demo *demo) {
1971 const uint32_t vs_code[] = {
1972#include "cube.vert.inc"
1973 };
1974 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001975}
1976
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001977static void demo_prepare_fs(struct demo *demo) {
1978 const uint32_t fs_code[] = {
1979#include "cube.frag.inc"
1980 };
1981 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982}
1983
Karl Schultze4dc75c2016-02-02 15:37:51 -07001984static void demo_prepare_pipeline(struct demo *demo) {
Tony-LunarG8ee48dc2020-05-07 10:02:58 -06001985#define NUM_DYNAMIC_STATES 2 /*Viewport + Scissor*/
1986
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001987 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001988 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001989 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001990 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001991 VkPipelineRasterizationStateCreateInfo rs;
1992 VkPipelineColorBlendStateCreateInfo cb;
1993 VkPipelineDepthStencilStateCreateInfo ds;
1994 VkPipelineViewportStateCreateInfo vp;
1995 VkPipelineMultisampleStateCreateInfo ms;
Tony-LunarG8ee48dc2020-05-07 10:02:58 -06001996 VkDynamicState dynamicStateEnables[NUM_DYNAMIC_STATES];
Karl Schultze4dc75c2016-02-02 15:37:51 -07001997 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001998 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001999
Piers Daniellba839d12015-09-29 13:01:09 -06002000 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2001 memset(&dynamicState, 0, sizeof dynamicState);
2002 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2003 dynamicState.pDynamicStates = dynamicStateEnables;
2004
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002005 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002006 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002007 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002008
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002009 memset(&vi, 0, sizeof(vi));
2010 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2011
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002012 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002013 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002014 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002015
2016 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002017 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2018 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002019 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002020 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002021 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002022 rs.rasterizerDiscardEnable = VK_FALSE;
2023 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002024 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002025
2026 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002027 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2028 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002029 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002030 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002031 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002032 cb.attachmentCount = 1;
2033 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002034
Tony Barbour29645d02015-01-16 14:27:35 -07002035 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002036 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002037 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002038 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002039 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002040 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002041
2042 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002043 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002044 ds.depthTestEnable = VK_TRUE;
2045 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002046 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002047 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002048 ds.back.failOp = VK_STENCIL_OP_KEEP;
2049 ds.back.passOp = VK_STENCIL_OP_KEEP;
2050 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002051 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002052 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002053
Tony Barbour29645d02015-01-16 14:27:35 -07002054 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002055 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002056 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002057 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002058
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002059 demo_prepare_vs(demo);
2060 demo_prepare_fs(demo);
2061
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002062 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002063 VkPipelineShaderStageCreateInfo shaderStages[2];
2064 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2065
Karl Schultze4dc75c2016-02-02 15:37:51 -07002066 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2067 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002068 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002069 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002070
Karl Schultze4dc75c2016-02-02 15:37:51 -07002071 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2072 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002073 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002074 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002075
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002076 memset(&pipelineCache, 0, sizeof(pipelineCache));
2077 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002078
Dave Houlton5fa47912018-02-16 11:02:26 -07002079 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002080 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002081
Karl Schultze4dc75c2016-02-02 15:37:51 -07002082 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002083 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002084 pipeline.pRasterizationState = &rs;
2085 pipeline.pColorBlendState = &cb;
2086 pipeline.pMultisampleState = &ms;
2087 pipeline.pViewportState = &vp;
2088 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002089 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002090 pipeline.pStages = shaderStages;
2091 pipeline.renderPass = demo->render_pass;
2092 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002093
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002094 pipeline.renderPass = demo->render_pass;
2095
Dave Houlton5fa47912018-02-16 11:02:26 -07002096 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002097 assert(!err);
2098
Chia-I Wu63636062015-10-26 21:10:41 +08002099 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2100 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002101}
2102
Karl Schultze4dc75c2016-02-02 15:37:51 -07002103static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002104 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07002105 [0] =
2106 {
2107 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2108 .descriptorCount = demo->swapchainImageCount,
2109 },
2110 [1] =
2111 {
2112 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2113 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2114 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002115 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002116 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002117 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002118 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002119 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002120 .poolSizeCount = 2,
2121 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002122 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002123 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002124
Dave Houlton5fa47912018-02-16 11:02:26 -07002125 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002126 assert(!err);
2127}
2128
Karl Schultze4dc75c2016-02-02 15:37:51 -07002129static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002130 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002131 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002132 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002133
Dave Houlton5fa47912018-02-16 11:02:26 -07002134 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2135 .pNext = NULL,
2136 .descriptorPool = demo->desc_pool,
2137 .descriptorSetCount = 1,
2138 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002139
2140 VkDescriptorBufferInfo buffer_info;
2141 buffer_info.offset = 0;
2142 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002143
Chia-I Wuae721ba2015-05-25 16:27:55 +08002144 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002145 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002146 tex_descs[i].sampler = demo->textures[i].sampler;
2147 tex_descs[i].imageView = demo->textures[i].view;
Karl Schultze88bc842018-09-11 16:23:14 -06002148 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002149 }
2150
2151 memset(&writes, 0, sizeof(writes));
2152
2153 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002154 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002155 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002156 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002157
2158 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002159 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002160 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002161 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002162 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002163
Tony Barboura72d9492017-01-11 13:35:09 -07002164 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2165 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2166 assert(!err);
2167 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2168 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2169 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2170 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2171 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002172}
2173
Karl Schultze4dc75c2016-02-02 15:37:51 -07002174static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002175 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002176 attachments[1] = demo->depth.view;
2177
Chia-I Wuf973e322015-07-08 13:34:24 +08002178 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002179 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2180 .pNext = NULL,
2181 .renderPass = demo->render_pass,
2182 .attachmentCount = 2,
2183 .pAttachments = attachments,
2184 .width = demo->width,
2185 .height = demo->height,
2186 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002187 };
2188 VkResult U_ASSERT_ONLY err;
2189 uint32_t i;
2190
Tony Barbourd8e504f2015-10-08 14:26:24 -06002191 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002192 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002193 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002194 assert(!err);
2195 }
2196}
2197
Karl Schultze4dc75c2016-02-02 15:37:51 -07002198static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002199 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002200 if (demo->cmd_pool == VK_NULL_HANDLE) {
2201 const VkCommandPoolCreateInfo cmd_pool_info = {
2202 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2203 .pNext = NULL,
2204 .queueFamilyIndex = demo->graphics_queue_family_index,
2205 .flags = 0,
2206 };
2207 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2208 assert(!err);
2209 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002210
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002211 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002212 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002213 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002214 .commandPool = demo->cmd_pool,
2215 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002216 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002217 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002218 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2219 assert(!err);
2220 VkCommandBufferBeginInfo cmd_buf_info = {
2221 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2222 .pNext = NULL,
2223 .flags = 0,
2224 .pInheritanceInfo = NULL,
2225 };
2226 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2227 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002228
2229 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002230
2231 if (demo->is_minimized) {
2232 demo->prepared = false;
2233 return;
2234 }
2235
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002236 demo_prepare_depth(demo);
2237 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002238 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002239
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002240 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002241 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002242 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002243
Ian Elliotta0781972015-08-21 15:09:33 -06002244 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002245 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002246 assert(!err);
2247 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002248
Tony Barbour22e06272016-09-07 16:07:56 -06002249 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002250 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002251 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2252 .pNext = NULL,
2253 .queueFamilyIndex = demo->present_queue_family_index,
2254 .flags = 0,
2255 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002256 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002257 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002258 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002259 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2260 .pNext = NULL,
2261 .commandPool = demo->present_cmd_pool,
2262 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2263 .commandBufferCount = 1,
2264 };
2265 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002266 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2267 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002268 assert(!err);
2269 demo_build_image_ownership_cmd(demo, i);
2270 }
2271 }
2272
Chia-I Wu63ea9262015-03-26 13:14:16 +08002273 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002274 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002275
Chia-I Wuf973e322015-07-08 13:34:24 +08002276 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002277
Ian Elliotta0781972015-08-21 15:09:33 -06002278 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002279 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002280 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002281 }
2282
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002283 /*
2284 * Prepare functions above may generate pipeline commands
2285 * that need to be flushed before beginning the render loop.
2286 */
2287 demo_flush_init_cmd(demo);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06002288 if (demo->staging_texture.buffer) {
2289 demo_destroy_texture(demo, &demo->staging_texture);
Tony Barbour16156cb2016-10-19 14:15:49 -06002290 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002291
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002292 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002293 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002294}
2295
Karl Schultze4dc75c2016-02-02 15:37:51 -07002296static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002297 uint32_t i;
2298
2299 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002300 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002301
Tony Barbour66a56c32016-09-21 13:10:56 -06002302 // Wait for fences from present operations
2303 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002304 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002305 vkDestroyFence(demo->device, demo->fences[i], NULL);
2306 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2307 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2308 if (demo->separate_present_queue) {
2309 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2310 }
2311 }
2312
Jeremy Kniager602e4182018-01-19 10:52:34 -07002313 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2314 if (!demo->is_minimized) {
2315 for (i = 0; i < demo->swapchainImageCount; i++) {
2316 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2317 }
2318 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002319
Jeremy Kniager602e4182018-01-19 10:52:34 -07002320 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2321 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2322 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2323 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2324 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002325
Jeremy Kniager602e4182018-01-19 10:52:34 -07002326 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2327 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2328 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2329 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2330 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2331 }
2332 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002333
Jeremy Kniager602e4182018-01-19 10:52:34 -07002334 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2335 vkDestroyImage(demo->device, demo->depth.image, NULL);
2336 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002337
Jeremy Kniager602e4182018-01-19 10:52:34 -07002338 for (i = 0; i < demo->swapchainImageCount; i++) {
2339 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2340 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2341 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
Tony-LunarG37af49f2019-12-19 12:04:19 -07002342 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002343 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2344 }
2345 free(demo->swapchain_image_resources);
2346 free(demo->queue_props);
2347 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002348
Jeremy Kniager602e4182018-01-19 10:52:34 -07002349 if (demo->separate_present_queue) {
2350 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2351 }
Tony Barbour22e06272016-09-07 16:07:56 -06002352 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002353 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002354 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002355 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07002356 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002357 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002358 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002359
Tony Barbour153cb062016-12-07 13:43:36 -07002360#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002361 XDestroyWindow(demo->display, demo->xlib_window);
2362 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002363#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002364 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002365 xcb_disconnect(demo->connection);
2366 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002367#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002368 wl_keyboard_destroy(demo->keyboard);
2369 wl_pointer_destroy(demo->pointer);
2370 wl_seat_destroy(demo->seat);
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002371 wl_shell_surface_destroy(demo->shell_surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002372 wl_surface_destroy(demo->window);
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002373 wl_shell_destroy(demo->shell);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002374 wl_compositor_destroy(demo->compositor);
2375 wl_registry_destroy(demo->registry);
2376 wl_display_disconnect(demo->display);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02002377#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
2378 demo->event_buffer->Release(demo->event_buffer);
2379 demo->window->Release(demo->window);
2380 demo->dfb->Release(demo->dfb);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002381#endif
Karl Schultz86429112017-06-20 11:27:59 -06002382
2383 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002384}
2385
Karl Schultze4dc75c2016-02-02 15:37:51 -07002386static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002387 uint32_t i;
2388
Mike Stroyanbb60b382015-10-28 09:46:57 -06002389 // Don't react to resize until after first initialization.
2390 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002391 if (demo->is_minimized) {
2392 demo_prepare(demo);
2393 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002394 return;
2395 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002396 // In order to properly resize the window, we must re-create the swapchain
2397 // AND redo the command buffers, etc.
2398 //
2399 // First, perform part of the demo_cleanup() function:
2400 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002401 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002402
2403 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002404 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002405 }
Chia-I Wu63636062015-10-26 21:10:41 +08002406 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002407
Chia-I Wu63636062015-10-26 21:10:41 +08002408 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2409 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2410 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2411 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2412 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002413
2414 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002415 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2416 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2417 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2418 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002419 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002420
Chia-I Wu63636062015-10-26 21:10:41 +08002421 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2422 vkDestroyImage(demo->device, demo->depth.image, NULL);
2423 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002424
Ian Elliottcf074d32015-10-16 18:02:43 -06002425 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002426 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002427 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002428 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
Tony-LunarG37af49f2019-12-19 12:04:19 -07002429 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07002430 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002431 }
Chia-I Wu63636062015-10-26 21:10:41 +08002432 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002433 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002434 if (demo->separate_present_queue) {
2435 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2436 }
Tony Barboura72d9492017-01-11 13:35:09 -07002437 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002438
Ian Elliottcf074d32015-10-16 18:02:43 -06002439 // Second, re-perform the demo_prepare() function, which will re-create the
2440 // swapchain:
2441 demo_prepare(demo);
2442}
2443
David Pinedo2bb7c932015-06-18 17:03:14 -06002444// On MS-Windows, make this a global, so it's available to WndProc()
2445struct demo demo;
2446
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002447#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002448static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002449 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002450
Ian Elliott639ca472015-04-16 15:23:05 -06002451 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002452 demo->curFrame++;
Petr Krausd88e4e52019-01-23 18:45:04 +01002453 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002454 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002455 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002456}
Ian Elliott639ca472015-04-16 15:23:05 -06002457
2458// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002459LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2460 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002461 case WM_CLOSE:
2462 PostQuitMessage(validation_error);
2463 break;
2464 case WM_PAINT:
2465 // The validation callback calls MessageBox which can generate paint
2466 // events - don't make more Vulkan calls if we got here from the
2467 // callback
2468 if (!in_callback) {
2469 demo_run(&demo);
2470 }
2471 break;
2472 case WM_GETMINMAXINFO: // set window's minimum size
2473 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2474 return 0;
Aaron Hagan500b9c32018-10-03 21:56:29 -04002475 case WM_ERASEBKGND:
2476 return 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002477 case WM_SIZE:
2478 // Resize the application to the new window size, except when
2479 // it was minimized. Vulkan doesn't support images or swapchains
2480 // with width=0 and height=0.
2481 if (wParam != SIZE_MINIMIZED) {
2482 demo.width = lParam & 0xffff;
2483 demo.height = (lParam & 0xffff0000) >> 16;
2484 demo_resize(&demo);
2485 }
2486 break;
Petr Krausd88e4e52019-01-23 18:45:04 +01002487 case WM_KEYDOWN:
2488 switch (wParam) {
2489 case VK_ESCAPE:
2490 PostQuitMessage(validation_error);
2491 break;
2492 case VK_LEFT:
2493 demo.spin_angle -= demo.spin_increment;
2494 break;
2495 case VK_RIGHT:
2496 demo.spin_angle += demo.spin_increment;
2497 break;
2498 case VK_SPACE:
2499 demo.pause = !demo.pause;
2500 break;
2501 }
2502 return 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002503 default:
2504 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002505 }
2506 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2507}
2508
Karl Schultze4dc75c2016-02-02 15:37:51 -07002509static void demo_create_window(struct demo *demo) {
2510 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002511
2512 // Initialize the window class structure:
2513 win_class.cbSize = sizeof(WNDCLASSEX);
2514 win_class.style = CS_HREDRAW | CS_VREDRAW;
2515 win_class.lpfnWndProc = WndProc;
2516 win_class.cbClsExtra = 0;
2517 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002518 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002519 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2520 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2521 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2522 win_class.lpszMenuName = NULL;
2523 win_class.lpszClassName = demo->name;
2524 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2525 // Register window class:
2526 if (!RegisterClassEx(&win_class)) {
2527 // It didn't work, so try to give a useful error:
2528 printf("Unexpected error trying to start the application!\n");
2529 fflush(stdout);
2530 exit(1);
2531 }
2532 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002533 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002534 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002535 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002536 demo->name, // class name
2537 demo->name, // app name
2538 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002539 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002540 100, 100, // x/y coords
2541 wr.right - wr.left, // width
2542 wr.bottom - wr.top, // height
2543 NULL, // handle to parent
2544 NULL, // handle to menu
2545 demo->connection, // hInstance
2546 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002547 if (!demo->window) {
2548 // It didn't work, so try to give a useful error:
2549 printf("Cannot create a window in which to draw!\n");
2550 fflush(stdout);
2551 exit(1);
2552 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002553 // Window client area size must be at least 1 pixel high, to prevent crash.
2554 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002555 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002556}
Tony Barbour8295ac42016-08-08 11:04:17 -06002557#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002558static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002559 const char *display_envar = getenv("DISPLAY");
2560 if (display_envar == NULL || display_envar[0] == '\0') {
2561 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2562 fflush(stdout);
2563 exit(1);
2564 }
Tony Barbour2593b182016-04-19 10:57:58 -06002565
Tony Barbouree9cd372017-01-31 16:09:58 -07002566 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002567 demo->display = XOpenDisplay(NULL);
2568 long visualMask = VisualScreenMask;
2569 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002570 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002571 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002572 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002573
Dave Houlton5fa47912018-02-16 11:02:26 -07002574 Colormap colormap =
2575 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002576
Dave Houlton5fa47912018-02-16 11:02:26 -07002577 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002578 windowAttributes.colormap = colormap;
2579 windowAttributes.background_pixel = 0xFFFFFFFF;
2580 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002581 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002582
Dave Houlton5fa47912018-02-16 11:02:26 -07002583 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2584 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2585 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002586
2587 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2588 XMapWindow(demo->display, demo->xlib_window);
2589 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002590 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002591}
2592static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002593 switch (event->type) {
2594 case ClientMessage:
2595 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002596 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002597 case KeyPress:
2598 switch (event->xkey.keycode) {
2599 case 0x9: // Escape
2600 demo->quit = true;
2601 break;
2602 case 0x71: // left arrow key
2603 demo->spin_angle -= demo->spin_increment;
2604 break;
2605 case 0x72: // right arrow key
2606 demo->spin_angle += demo->spin_increment;
2607 break;
2608 case 0x41: // space bar
2609 demo->pause = !demo->pause;
2610 break;
2611 }
Tony Barbour2593b182016-04-19 10:57:58 -06002612 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002613 case ConfigureNotify:
2614 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2615 demo->width = event->xconfigure.width;
2616 demo->height = event->xconfigure.height;
2617 demo_resize(demo);
2618 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002619 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002620 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002621 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002622 }
Tony Barbour2593b182016-04-19 10:57:58 -06002623}
2624
2625static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002626 while (!demo->quit) {
2627 XEvent event;
2628
2629 if (demo->pause) {
2630 XNextEvent(demo->display, &event);
2631 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002632 }
2633 while (XPending(demo->display) > 0) {
2634 XNextEvent(demo->display, &event);
2635 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002636 }
2637
Tony Barbour2593b182016-04-19 10:57:58 -06002638 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002639 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002640 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002641 }
2642}
Tony Barbour153cb062016-12-07 13:43:36 -07002643#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002644static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002645 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002646 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002647 case XCB_EXPOSE:
2648 // TODO: Resize window
2649 break;
2650 case XCB_CLIENT_MESSAGE:
2651 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2652 demo->quit = true;
2653 }
2654 break;
2655 case XCB_KEY_RELEASE: {
2656 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002657
Dave Houlton5fa47912018-02-16 11:02:26 -07002658 switch (key->detail) {
2659 case 0x9: // Escape
2660 demo->quit = true;
2661 break;
2662 case 0x71: // left arrow key
2663 demo->spin_angle -= demo->spin_increment;
2664 break;
2665 case 0x72: // right arrow key
2666 demo->spin_angle += demo->spin_increment;
2667 break;
2668 case 0x41: // space bar
2669 demo->pause = !demo->pause;
2670 break;
2671 }
2672 } break;
2673 case XCB_CONFIGURE_NOTIFY: {
2674 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2675 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2676 demo->width = cfg->width;
2677 demo->height = cfg->height;
2678 demo_resize(demo);
2679 }
2680 } break;
2681 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002682 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002683 }
2684}
2685
Tony Barbour2593b182016-04-19 10:57:58 -06002686static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002687 xcb_flush(demo->connection);
2688
2689 while (!demo->quit) {
2690 xcb_generic_event_t *event;
2691
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002692 if (demo->pause) {
2693 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002694 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002695 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002696 }
2697 while (event) {
2698 demo_handle_xcb_event(demo, event);
2699 free(event);
2700 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002701 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002702
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002703 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002704 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002705 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002706 }
2707}
2708
Tony Barbour2593b182016-04-19 10:57:58 -06002709static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002710 uint32_t value_mask, value_list[32];
2711
Tony Barbour2593b182016-04-19 10:57:58 -06002712 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002713
2714 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2715 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002716 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002717
Dave Houlton5fa47912018-02-16 11:02:26 -07002718 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2719 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002720
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002721 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002722 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2723 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002724
Dave Houlton5fa47912018-02-16 11:02:26 -07002725 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2726 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002727
Dave Houlton5fa47912018-02-16 11:02:26 -07002728 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002729 &(*demo->atom_wm_delete_window).atom);
2730 free(reply);
2731
Tony Barbour2593b182016-04-19 10:57:58 -06002732 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002733
Karl Schultze4dc75c2016-02-02 15:37:51 -07002734 // Force the x/y coordinates to 100,100 results are identical in consecutive
2735 // runs
2736 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002737 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002738}
Tony Barbour6b131662016-08-25 13:14:45 -06002739// VK_USE_PLATFORM_XCB_KHR
2740#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002741static void demo_run(struct demo *demo) {
2742 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002743 if (demo->pause) {
2744 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002745 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002746 wl_display_dispatch_pending(demo->display); // don't block
2747 demo_draw(demo);
2748 demo->curFrame++;
2749 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2750 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002751 }
2752}
2753
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002754static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) {
2755 wl_shell_surface_pong(shell_surface, serial);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002756}
2757
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002758static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED,
2759 int32_t width UNUSED, int32_t height UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002760
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002761static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002762
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002763static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002764
2765static void demo_create_window(struct demo *demo) {
2766 demo->window = wl_compositor_create_surface(demo->compositor);
2767 if (!demo->window) {
2768 printf("Can not create wayland_surface from compositor!\n");
2769 fflush(stdout);
2770 exit(1);
2771 }
2772
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002773 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2774 if (!demo->shell_surface) {
2775 printf("Can not get shell_surface from wayland_surface!\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002776 fflush(stdout);
2777 exit(1);
2778 }
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002779 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo);
2780 wl_shell_surface_set_toplevel(demo->shell_surface);
2781 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002782}
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02002783#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
2784static void demo_create_directfb_window(struct demo *demo) {
2785 DFBResult ret;
2786
2787 ret = DirectFBInit(NULL, NULL);
2788 if (ret) {
2789 printf("DirectFBInit failed to initialize DirectFB!\n");
2790 fflush(stdout);
2791 exit(1);
2792 }
2793
2794 ret = DirectFBCreate(&demo->dfb);
2795 if (ret) {
2796 printf("DirectFBCreate failed to create main interface of DirectFB!\n");
2797 fflush(stdout);
2798 exit(1);
2799 }
2800
2801 DFBSurfaceDescription desc;
2802 desc.flags = DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT;
2803 desc.caps = DSCAPS_PRIMARY;
2804 desc.width = demo->width;
2805 desc.height = demo->height;
2806 ret = demo->dfb->CreateSurface(demo->dfb, &desc, &demo->window);
2807 if (ret) {
2808 printf("CreateSurface failed to create DirectFB surface interface!\n");
2809 fflush(stdout);
2810 exit(1);
2811 }
2812
2813 ret = demo->dfb->CreateInputEventBuffer(demo->dfb, DICAPS_KEYS, DFB_FALSE, &demo->event_buffer);
2814 if (ret) {
2815 printf("CreateInputEventBuffer failed to create DirectFB event buffer interface!\n");
2816 fflush(stdout);
2817 exit(1);
2818 }
2819}
2820
2821static void demo_handle_directfb_event(struct demo *demo, const DFBInputEvent *event) {
2822 if (event->type != DIET_KEYPRESS) return;
2823 switch (event->key_symbol) {
2824 case DIKS_ESCAPE: // Escape
2825 demo->quit = true;
2826 break;
2827 case DIKS_CURSOR_LEFT: // left arrow key
2828 demo->spin_angle -= demo->spin_increment;
2829 break;
2830 case DIKS_CURSOR_RIGHT: // right arrow key
2831 demo->spin_angle += demo->spin_increment;
2832 break;
2833 case DIKS_SPACE: // space bar
2834 demo->pause = !demo->pause;
2835 break;
2836 default:
2837 break;
2838 }
2839}
2840
2841static void demo_run_directfb(struct demo *demo) {
2842 while (!demo->quit) {
2843 DFBInputEvent event;
2844
2845 if (demo->pause) {
2846 demo->event_buffer->WaitForEvent(demo->event_buffer);
Nicolas Caramelli6d4f1c62020-07-13 18:10:07 +02002847 if (!demo->event_buffer->GetEvent(demo->event_buffer, DFB_EVENT(&event))) demo_handle_directfb_event(demo, &event);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02002848 } else {
Nicolas Caramelli6d4f1c62020-07-13 18:10:07 +02002849 if (!demo->event_buffer->GetEvent(demo->event_buffer, DFB_EVENT(&event))) demo_handle_directfb_event(demo, &event);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02002850
2851 demo_draw(demo);
2852 demo->curFrame++;
2853 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2854 }
2855 }
2856}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002857#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2858static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002859 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002860
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002861 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002862 demo->curFrame++;
2863}
Bill Hollings0a0625a2019-07-15 17:39:18 -04002864#elif defined(VK_USE_PLATFORM_METAL_EXT)
Karl Schultz206b1c52018-04-13 18:02:07 -06002865static void demo_run(struct demo *demo) {
2866 demo_draw(demo);
2867 demo->curFrame++;
2868 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2869 demo->quit = TRUE;
2870 }
2871}
Damien Leone600c3052017-01-31 10:26:07 -07002872#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2873static VkResult demo_create_display_surface(struct demo *demo) {
2874 VkResult U_ASSERT_ONLY err;
2875 uint32_t display_count;
2876 uint32_t mode_count;
2877 uint32_t plane_count;
2878 VkDisplayPropertiesKHR display_props;
2879 VkDisplayKHR display;
2880 VkDisplayModePropertiesKHR mode_props;
2881 VkDisplayPlanePropertiesKHR *plane_props;
2882 VkBool32 found_plane = VK_FALSE;
2883 uint32_t plane_index;
2884 VkExtent2D image_extent;
2885 VkDisplaySurfaceCreateInfoKHR create_info;
2886
2887 // Get the first display
2888 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2889 assert(!err);
2890
2891 if (display_count == 0) {
2892 printf("Cannot find any display!\n");
2893 fflush(stdout);
2894 exit(1);
2895 }
2896
2897 display_count = 1;
2898 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2899 assert(!err || (err == VK_INCOMPLETE));
2900
2901 display = display_props.display;
2902
2903 // Get the first mode of the display
2904 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2905 assert(!err);
2906
2907 if (mode_count == 0) {
2908 printf("Cannot find any mode for the display!\n");
2909 fflush(stdout);
2910 exit(1);
2911 }
2912
2913 mode_count = 1;
2914 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2915 assert(!err || (err == VK_INCOMPLETE));
2916
2917 // Get the list of planes
2918 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2919 assert(!err);
2920
2921 if (plane_count == 0) {
2922 printf("Cannot find any plane!\n");
2923 fflush(stdout);
2924 exit(1);
2925 }
2926
2927 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2928 assert(plane_props);
2929
2930 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2931 assert(!err);
2932
2933 // Find a plane compatible with the display
2934 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2935 uint32_t supported_count;
2936 VkDisplayKHR *supported_displays;
2937
2938 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002939 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002940 continue;
2941 }
2942
2943 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2944 assert(!err);
2945
2946 if (supported_count == 0) {
2947 continue;
2948 }
2949
2950 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2951 assert(supported_displays);
2952
2953 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2954 assert(!err);
2955
2956 for (uint32_t i = 0; i < supported_count; i++) {
2957 if (supported_displays[i] == display) {
2958 found_plane = VK_TRUE;
2959 break;
2960 }
2961 }
2962
2963 free(supported_displays);
2964
2965 if (found_plane) {
2966 break;
2967 }
2968 }
2969
2970 if (!found_plane) {
2971 printf("Cannot find a plane compatible with the display!\n");
2972 fflush(stdout);
2973 exit(1);
2974 }
2975
2976 free(plane_props);
2977
Tony Barbour820b55b2017-03-14 08:55:46 -06002978 VkDisplayPlaneCapabilitiesKHR planeCaps;
2979 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2980 // Find a supported alpha mode
Mark Young66510e52017-11-10 10:05:14 -07002981 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2982 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002983 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2984 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2985 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2986 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2987 };
2988 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2989 if (planeCaps.supportedAlpha & alphaModes[i]) {
2990 alphaMode = alphaModes[i];
2991 break;
2992 }
2993 }
Damien Leone600c3052017-01-31 10:26:07 -07002994 image_extent.width = mode_props.parameters.visibleRegion.width;
2995 image_extent.height = mode_props.parameters.visibleRegion.height;
2996
2997 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2998 create_info.pNext = NULL;
2999 create_info.flags = 0;
3000 create_info.displayMode = mode_props.displayMode;
3001 create_info.planeIndex = plane_index;
3002 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
3003 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06003004 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07003005 create_info.globalAlpha = 1.0f;
3006 create_info.imageExtent = image_extent;
3007
3008 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
3009}
3010
Dave Houlton5fa47912018-02-16 11:02:26 -07003011static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07003012 while (!demo->quit) {
3013 demo_draw(demo);
3014 demo->curFrame++;
3015
3016 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
3017 demo->quit = true;
3018 }
3019 }
3020}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003021#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003022
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003023/*
3024 * Return 1 (true) if all layer names specified in check_names
3025 * can be found in given layer properties.
3026 */
Dave Houlton5fa47912018-02-16 11:02:26 -07003027static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003028 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003029 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003030 for (uint32_t j = 0; j < layer_count; j++) {
3031 if (!strcmp(check_names[i], layers[j].layerName)) {
3032 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07003033 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003034 }
3035 }
3036 if (!found) {
3037 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
3038 return 0;
3039 }
3040 }
3041 return 1;
3042}
3043
Karl Schultze4dc75c2016-02-02 15:37:51 -07003044static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003045 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003046 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003047 uint32_t instance_layer_count = 0;
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003048 char *instance_validation_layers[] = {"VK_LAYER_KHRONOS_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06003049 demo->enabled_extension_count = 0;
3050 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07003051 demo->is_minimized = false;
3052 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003053
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003054 // Look for validation layers
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003055 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003056 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003057 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07003058 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003059
Karl Schultz7c50ad62016-04-01 12:07:03 -06003060 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003061 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
3062 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06003063 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003064
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003065 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
Dave Houlton5fa47912018-02-16 11:02:26 -07003066 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06003067 if (validation_found) {
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003068 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
3069 demo->enabled_layers[0] = "VK_LAYER_KHRONOS_validation";
Karl Schultz7c50ad62016-04-01 12:07:03 -06003070 }
3071 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003072 }
Dustin Graves7c287352016-01-27 13:48:06 -07003073
Karl Schultz7c50ad62016-04-01 12:07:03 -06003074 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003075 ERR_EXIT(
3076 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
3077 "Please look at the Getting Started guide for additional information.\n",
3078 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06003079 }
Dustin Graves7c287352016-01-27 13:48:06 -07003080 }
3081
3082 /* Look for instance extensions */
3083 VkBool32 surfaceExtFound = 0;
3084 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003085 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003086
Dave Houlton5fa47912018-02-16 11:02:26 -07003087 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003088 assert(!err);
3089
Dustin Graves7c287352016-01-27 13:48:06 -07003090 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003091 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3092 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003093 assert(!err);
3094 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003095 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003096 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003097 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003098 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003099#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003100 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003101 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003102 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003103 }
Tony Barbour153cb062016-12-07 13:43:36 -07003104#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003105 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06003106 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003107 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06003108 }
Tony Barbour153cb062016-12-07 13:43:36 -07003109#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003110 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003111 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003112 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003113 }
Tony Barbour153cb062016-12-07 13:43:36 -07003114#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003115 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003116 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003117 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003118 }
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02003119#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
3120 if (!strcmp(VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3121 platformSurfaceExtFound = 1;
3122 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME;
3123 }
Damien Leone600c3052017-01-31 10:26:07 -07003124#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003125 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07003126 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003127 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07003128 }
Tony Barbour153cb062016-12-07 13:43:36 -07003129#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003130 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003131 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003132 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003133 }
Bill Hollings0a0625a2019-07-15 17:39:18 -04003134#elif defined(VK_USE_PLATFORM_METAL_EXT)
3135 if (!strcmp(VK_EXT_METAL_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Bill Hollingsf4352142017-02-14 22:58:56 -05003136 platformSurfaceExtFound = 1;
Bill Hollings0a0625a2019-07-15 17:39:18 -04003137 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_METAL_SURFACE_EXTENSION_NAME;
Bill Hollingsf4352142017-02-14 22:58:56 -05003138 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003139#endif
Richard S. Wright Jrca51bc72021-01-06 13:03:18 -05003140 if (!strcmp(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3141 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME;
3142 }
Mark Young66510e52017-11-10 10:05:14 -07003143 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3144 if (demo->validate) {
3145 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3146 }
3147 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003148 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003149 }
Dustin Graves7c287352016-01-27 13:48:06 -07003150
3151 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003152 }
Dustin Graves7c287352016-01-27 13:48:06 -07003153
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003154 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003155 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3156 " extension.\n\n"
3157 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3158 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06003159 "vkCreateInstance Failure");
3160 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003161 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003162#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003163 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3164 " extension.\n\n"
3165 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3166 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003167 "vkCreateInstance Failure");
Bill Hollings0a0625a2019-07-15 17:39:18 -04003168#elif defined(VK_USE_PLATFORM_METAL_EXT)
3169 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_METAL_SURFACE_EXTENSION_NAME
Dave Houlton5fa47912018-02-16 11:02:26 -07003170 " extension.\n\n"
3171 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3172 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003173 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003174#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003175 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3176 " extension.\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",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003179 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003180#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003181 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3182 " extension.\n\n"
3183 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3184 "Please look at the Getting Started guide for additional information.\n",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003185 "vkCreateInstance Failure");
Damien Leone600c3052017-01-31 10:26:07 -07003186#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003187 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3188 " extension.\n\n"
3189 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3190 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07003191 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003192#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003193 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3194 " extension.\n\n"
3195 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3196 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003197 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003198#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003199 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3200 " extension.\n\n"
3201 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3202 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06003203 "vkCreateInstance Failure");
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02003204#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
3205 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME
3206 " extension.\n\n"
3207 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3208 "Please look at the Getting Started guide for additional information.\n",
3209 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003210#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003211 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003212 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003213 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003214 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003215 .pApplicationName = APP_SHORT_NAME,
3216 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003217 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003218 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003219 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003220 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003221 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003222 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003223 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003224 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003225 .enabledLayerCount = demo->enabled_layer_count,
3226 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3227 .enabledExtensionCount = demo->enabled_extension_count,
3228 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003229 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003230
3231 /*
3232 * This is info for a temp callback to use during CreateInstance.
3233 * After the instance is created, we use the instance-based
3234 * function to register the final callback.
3235 */
Mark Young66510e52017-11-10 10:05:14 -07003236 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003237 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003238 // VK_EXT_debug_utils style
3239 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3240 dbg_messenger_create_info.pNext = NULL;
3241 dbg_messenger_create_info.flags = 0;
3242 dbg_messenger_create_info.messageSeverity =
3243 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3244 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3245 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3246 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3247 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3248 dbg_messenger_create_info.pUserData = demo;
3249 inst_info.pNext = &dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003250 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003251
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003252 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003253
Chia-I Wu63636062015-10-26 21:10:41 +08003254 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003255 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003256 ERR_EXIT(
3257 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3258 "Please look at the Getting Started guide for additional information.\n",
3259 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003260 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003261 ERR_EXIT(
3262 "Cannot find a specified extension library.\n"
3263 "Make sure your layers path is set appropriately.\n",
3264 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003265 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003266 ERR_EXIT(
3267 "vkCreateInstance failed.\n\n"
3268 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3269 "Please look at the Getting Started guide for additional information.\n",
3270 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003271 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003272
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003273 /* Make initial call to query gpu_count, then second call for gpu info*/
3274 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
Petr Kraus2f1dbdb2018-04-14 14:45:17 +02003275 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003276
3277 if (gpu_count > 0) {
3278 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3279 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3280 assert(!err);
Tony-LunarG50e737c2020-07-16 14:26:03 -06003281 if (demo->gpu_number > gpu_count - 1) {
3282 fprintf(stderr, "Gpu %u specified is not present, gpu count = %u\n", demo->gpu_number, gpu_count);
3283 fprintf(stderr, "Continuing with gpu 0\n");
3284 demo->gpu_number = 0;
3285 }
3286 demo->gpu = physical_devices[demo->gpu_number];
Dustin Graves7c287352016-01-27 13:48:06 -07003287 free(physical_devices);
3288 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003289 ERR_EXIT(
3290 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3291 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3292 "Please look at the Getting Started guide for additional information.\n",
3293 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003294 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003295
Dustin Graves7c287352016-01-27 13:48:06 -07003296 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003297 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003298 VkBool32 swapchainExtFound = 0;
3299 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003300 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003301
Dave Houlton5fa47912018-02-16 11:02:26 -07003302 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003303 assert(!err);
3304
Dustin Graves7c287352016-01-27 13:48:06 -07003305 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003306 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3307 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003308 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003309
Dustin Graves7c287352016-01-27 13:48:06 -07003310 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003311 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003312 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003313 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003314 }
Richard S. Wright Jrca51bc72021-01-06 13:03:18 -05003315 if (!strcmp(VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME, device_extensions[i].extensionName)) {
3316 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME;
3317 }
Dustin Graves7c287352016-01-27 13:48:06 -07003318 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003319 }
Dustin Graves7c287352016-01-27 13:48:06 -07003320
Ian Elliott53622a72017-03-28 11:06:33 -06003321 if (demo->VK_KHR_incremental_present_enabled) {
3322 // Even though the user "enabled" the extension via the command
3323 // line, we must make sure that it's enumerated for use with the
3324 // device. Therefore, disable it here, and re-enable it again if
3325 // enumerated.
3326 demo->VK_KHR_incremental_present_enabled = false;
3327 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003328 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3329 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003330 demo->VK_KHR_incremental_present_enabled = true;
3331 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3332 }
3333 assert(demo->enabled_extension_count < 64);
3334 }
3335 if (!demo->VK_KHR_incremental_present_enabled) {
3336 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3337 }
3338 }
3339
Ian Elliottd940ca22017-03-22 08:31:27 -06003340 if (demo->VK_GOOGLE_display_timing_enabled) {
3341 // Even though the user "enabled" the extension via the command
3342 // line, we must make sure that it's enumerated for use with the
3343 // device. Therefore, disable it here, and re-enable it again if
3344 // enumerated.
3345 demo->VK_GOOGLE_display_timing_enabled = false;
3346 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003347 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3348 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003349 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003350 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003351 }
3352 assert(demo->enabled_extension_count < 64);
3353 }
3354 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003355 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003356 }
3357 }
3358
Dustin Graves7c287352016-01-27 13:48:06 -07003359 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003360 }
Dustin Graves7c287352016-01-27 13:48:06 -07003361
Tony Barbourcc69f452015-10-08 13:59:42 -06003362 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003363 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3364 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3365 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003366 "vkCreateInstance Failure");
3367 }
3368
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003369 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003370 // Setup VK_EXT_debug_utils function pointers always (we use them for
3371 // debug labels and names).
3372 demo->CreateDebugUtilsMessengerEXT =
3373 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3374 demo->DestroyDebugUtilsMessengerEXT =
3375 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3376 demo->SubmitDebugUtilsMessageEXT =
3377 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3378 demo->CmdBeginDebugUtilsLabelEXT =
3379 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3380 demo->CmdEndDebugUtilsLabelEXT =
3381 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3382 demo->CmdInsertDebugUtilsLabelEXT =
3383 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3384 demo->SetDebugUtilsObjectNameEXT =
3385 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3386 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3387 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3388 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3389 NULL == demo->SetDebugUtilsObjectNameEXT) {
3390 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003391 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003392
Mark Young66510e52017-11-10 10:05:14 -07003393 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003394 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003395 case VK_SUCCESS:
3396 break;
3397 case VK_ERROR_OUT_OF_HOST_MEMORY:
Mark Young66510e52017-11-10 10:05:14 -07003398 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003399 break;
3400 default:
Mark Young66510e52017-11-10 10:05:14 -07003401 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003402 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003403 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003404 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003405 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003406
3407 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003408 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003409 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003410
Dave Houlton5fa47912018-02-16 11:02:26 -07003411 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3412 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003413
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003414 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003415 // If app has specific feature requirements it should check supported
3416 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003417 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003418 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003419
Tony Barbourda2bad52016-01-22 14:36:40 -07003420 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3421 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3422 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3423 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3424 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3425}
3426
Karl Schultze4dc75c2016-02-02 15:37:51 -07003427static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003428 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003429 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003430 VkDeviceQueueCreateInfo queues[2];
3431 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3432 queues[0].pNext = NULL;
3433 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3434 queues[0].queueCount = 1;
3435 queues[0].pQueuePriorities = queue_priorities;
3436 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003437
3438 VkDeviceCreateInfo device = {
3439 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3440 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003441 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003442 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003443 .enabledLayerCount = 0,
3444 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003445 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003446 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003447 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003448 };
Tony Barbour22e06272016-09-07 16:07:56 -06003449 if (demo->separate_present_queue) {
3450 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3451 queues[1].pNext = NULL;
3452 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3453 queues[1].queueCount = 1;
3454 queues[1].pQueuePriorities = queue_priorities;
3455 queues[1].flags = 0;
3456 device.queueCreateInfoCount = 2;
3457 }
Chia-I Wu63636062015-10-26 21:10:41 +08003458 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003459 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003460}
3461
Tony-LunarG6cebf142019-09-11 14:32:23 -06003462static void demo_create_surface(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003463 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003464
Karl Schultze4dc75c2016-02-02 15:37:51 -07003465// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003466#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003467 VkWin32SurfaceCreateInfoKHR createInfo;
3468 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3469 createInfo.pNext = NULL;
3470 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003471 createInfo.hinstance = demo->connection;
3472 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003473
Dave Houlton5fa47912018-02-16 11:02:26 -07003474 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003475#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003476 VkWaylandSurfaceCreateInfoKHR createInfo;
3477 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3478 createInfo.pNext = NULL;
3479 createInfo.flags = 0;
3480 createInfo.display = demo->display;
3481 createInfo.surface = demo->window;
3482
Dave Houlton5fa47912018-02-16 11:02:26 -07003483 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003484#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3485 VkAndroidSurfaceCreateInfoKHR createInfo;
3486 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3487 createInfo.pNext = NULL;
3488 createInfo.flags = 0;
Mike Schuchardt837d5162018-03-08 12:57:02 -07003489 createInfo.window = (struct ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003490
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003491 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003492#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3493 VkXlibSurfaceCreateInfoKHR createInfo;
3494 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3495 createInfo.pNext = NULL;
3496 createInfo.flags = 0;
3497 createInfo.dpy = demo->display;
3498 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003499
Dave Houlton5fa47912018-02-16 11:02:26 -07003500 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003501#elif defined(VK_USE_PLATFORM_XCB_KHR)
3502 VkXcbSurfaceCreateInfoKHR createInfo;
3503 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3504 createInfo.pNext = NULL;
3505 createInfo.flags = 0;
3506 createInfo.connection = demo->connection;
3507 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003508
Tony Barbour153cb062016-12-07 13:43:36 -07003509 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02003510#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
3511 VkDirectFBSurfaceCreateInfoEXT createInfo;
3512 createInfo.sType = VK_STRUCTURE_TYPE_DIRECTFB_SURFACE_CREATE_INFO_EXT;
3513 createInfo.pNext = NULL;
3514 createInfo.flags = 0;
3515 createInfo.dfb = demo->dfb;
3516 createInfo.surface = demo->window;
3517
3518 err = vkCreateDirectFBSurfaceEXT(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003519#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3520 err = demo_create_display_surface(demo);
Bill Hollings0a0625a2019-07-15 17:39:18 -04003521#elif defined(VK_USE_PLATFORM_METAL_EXT)
3522 VkMetalSurfaceCreateInfoEXT surface;
3523 surface.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
Bill Hollingsf4352142017-02-14 22:58:56 -05003524 surface.pNext = NULL;
3525 surface.flags = 0;
Bill Hollings0a0625a2019-07-15 17:39:18 -04003526 surface.pLayer = demo->caMetalLayer;
Bill Hollingsf4352142017-02-14 22:58:56 -05003527
Bill Hollings0a0625a2019-07-15 17:39:18 -04003528 err = vkCreateMetalSurfaceEXT(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003529#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003530 assert(!err);
Tony-LunarG6cebf142019-09-11 14:32:23 -06003531}
3532
Joshua Ashton1fbefe02020-04-02 06:45:48 +01003533static VkSurfaceFormatKHR pick_surface_format(const VkSurfaceFormatKHR *surfaceFormats, uint32_t count) {
3534 // Prefer non-SRGB formats...
3535 for (uint32_t i = 0; i < count; i++) {
3536 const VkFormat format = surfaceFormats[i].format;
3537
3538 if (format == VK_FORMAT_R8G8B8A8_UNORM || format == VK_FORMAT_B8G8R8A8_UNORM ||
3539 format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 || format == VK_FORMAT_A2R10G10B10_UNORM_PACK32 ||
3540 format == VK_FORMAT_R16G16B16A16_SFLOAT) {
3541 return surfaceFormats[i];
3542 }
3543 }
3544
3545 printf("Can't find our preferred formats... Falling back to first exposed format. Rendering may be incorrect.\n");
3546
3547 assert(count >= 1);
3548 return surfaceFormats[0];
3549}
3550
Tony-LunarG6cebf142019-09-11 14:32:23 -06003551static void demo_init_vk_swapchain(struct demo *demo) {
3552 VkResult U_ASSERT_ONLY err;
3553
3554 demo_create_surface(demo);
Ian Elliottaaae5352015-07-06 14:27:58 -06003555
Tony Barbourcc69f452015-10-08 13:59:42 -06003556 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003557 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003558 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003559 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003560 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003561
3562 // Search for a graphics and a present queue in the array of queue
3563 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003564 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3565 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003566 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003567 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3568 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3569 graphicsQueueFamilyIndex = i;
3570 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003571
Tony Barbourab2a0362016-09-06 11:40:12 -06003572 if (supportsPresent[i] == VK_TRUE) {
3573 graphicsQueueFamilyIndex = i;
3574 presentQueueFamilyIndex = i;
3575 break;
3576 }
3577 }
3578 }
3579
3580 if (presentQueueFamilyIndex == UINT32_MAX) {
3581 // If didn't find a queue that supports both graphics and present, then
3582 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003583 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003584 if (supportsPresent[i] == VK_TRUE) {
3585 presentQueueFamilyIndex = i;
3586 break;
3587 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003588 }
3589 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003590
3591 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003592 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3593 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003594 }
3595
Tony Barbourab2a0362016-09-06 11:40:12 -06003596 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3597 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003598 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003599 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003600
Tony Barbourda2bad52016-01-22 14:36:40 -07003601 demo_create_device(demo);
3602
3603 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3604 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3605 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3606 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3607 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003608 if (demo->VK_GOOGLE_display_timing_enabled) {
3609 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3610 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3611 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003612
Dave Houlton5fa47912018-02-16 11:02:26 -07003613 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003614
Tony Barbour22e06272016-09-07 16:07:56 -06003615 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003616 demo->present_queue = demo->graphics_queue;
3617 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003618 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003619 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003620
Ian Elliottaaae5352015-07-06 14:27:58 -06003621 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003622 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003623 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003624 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003625 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3626 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003627 assert(!err);
Joshua Ashton1fbefe02020-04-02 06:45:48 +01003628 VkSurfaceFormatKHR surfaceFormat = pick_surface_format(surfFormats, formatCount);
3629 demo->format = surfaceFormat.format;
3630 demo->color_space = surfaceFormat.colorSpace;
Jason Chen5d4fee92019-04-04 12:45:29 +08003631 free(surfFormats);
Ian Elliotte4602cd2015-04-21 16:41:02 -06003632
David Pinedo2bb7c932015-06-18 17:03:14 -06003633 demo->quit = false;
3634 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003635
Tony Barbource7524e2016-07-28 12:01:45 -06003636 // Create semaphores to synchronize acquiring presentable buffers before
3637 // rendering and waiting for drawing to be complete before presenting
3638 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3639 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3640 .pNext = NULL,
3641 .flags = 0,
3642 };
Tony Barbource7524e2016-07-28 12:01:45 -06003643
Tony Barbour66a56c32016-09-21 13:10:56 -06003644 // Create fences that we can use to throttle if we get too far
3645 // ahead of the image presents
3646 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003647 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003648 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003649 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3650 assert(!err);
3651
Dave Houlton5fa47912018-02-16 11:02:26 -07003652 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003653 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003654
Dave Houlton5fa47912018-02-16 11:02:26 -07003655 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003656 assert(!err);
3657
3658 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003659 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003660 assert(!err);
3661 }
Tony Barbour22e06272016-09-07 16:07:56 -06003662 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003663 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003664
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003665 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003666 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003667}
3668
Tony Barbour153cb062016-12-07 13:43:36 -07003669#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003670static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3671 wl_fixed_t sy) {}
3672
3673static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3674
3675static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3676
3677static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3678 uint32_t state) {
3679 struct demo *demo = data;
3680 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07003681 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
Joey Bzdek15eb0702017-06-07 09:40:36 -06003682 }
3683}
3684
3685static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3686
3687static const struct wl_pointer_listener pointer_listener = {
3688 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3689};
3690
3691static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3692
3693static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3694 struct wl_array *keys) {}
3695
3696static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3697
3698static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3699 uint32_t state) {
3700 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3701 struct demo *demo = data;
3702 switch (key) {
3703 case KEY_ESC: // Escape
3704 demo->quit = true;
3705 break;
3706 case KEY_LEFT: // left arrow key
3707 demo->spin_angle -= demo->spin_increment;
3708 break;
3709 case KEY_RIGHT: // right arrow key
3710 demo->spin_angle += demo->spin_increment;
3711 break;
3712 case KEY_SPACE: // space bar
3713 demo->pause = !demo->pause;
3714 break;
3715 }
3716}
3717
3718static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3719 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3720
3721static const struct wl_keyboard_listener keyboard_listener = {
3722 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3723};
3724
3725static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3726 // Subscribe to pointer events
3727 struct demo *demo = data;
3728 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3729 demo->pointer = wl_seat_get_pointer(seat);
3730 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3731 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3732 wl_pointer_destroy(demo->pointer);
3733 demo->pointer = NULL;
3734 }
3735 // Subscribe to keyboard events
3736 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3737 demo->keyboard = wl_seat_get_keyboard(seat);
3738 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3739 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3740 wl_keyboard_destroy(demo->keyboard);
3741 demo->keyboard = NULL;
3742 }
3743}
3744
3745static const struct wl_seat_listener seat_listener = {
3746 seat_handle_capabilities,
3747};
3748
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07003749static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003750 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003751 // pickup wayland objects when they appear
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07003752 if (strcmp(interface, "wl_compositor") == 0) {
Georges Basile Stavracas Neto7da3ea72019-10-05 11:53:39 -03003753 uint32_t minVersion = version < 4 ? version : 4;
3754 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, minVersion);
3755 if (demo->VK_KHR_incremental_present_enabled && minVersion < 4) {
3756 fprintf(stderr, "Wayland compositor doesn't support VK_KHR_incremental_present, disabling.\n");
3757 demo->VK_KHR_incremental_present_enabled = false;
3758 }
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07003759 } else if (strcmp(interface, "wl_shell") == 0) {
3760 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3761 } else if (strcmp(interface, "wl_seat") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003762 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3763 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003764 }
3765}
3766
Dave Houlton5fa47912018-02-16 11:02:26 -07003767static 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 +09003768
Dave Houlton5fa47912018-02-16 11:02:26 -07003769static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003770#endif
3771
Karl Schultze4dc75c2016-02-02 15:37:51 -07003772static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003773#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003774 const xcb_setup_t *setup;
3775 xcb_screen_iterator_t iter;
3776 int scr;
3777
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003778 const char *display_envar = getenv("DISPLAY");
3779 if (display_envar == NULL || display_envar[0] == '\0') {
3780 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3781 fflush(stdout);
3782 exit(1);
3783 }
3784
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003785 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003786 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003787 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003788 fflush(stdout);
3789 exit(1);
3790 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003791
3792 setup = xcb_get_setup(demo->connection);
3793 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003794 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003795
3796 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003797#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3798 demo->display = wl_display_connect(NULL);
3799
3800 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003801 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003802 fflush(stdout);
3803 exit(1);
3804 }
3805
3806 demo->registry = wl_display_get_registry(demo->display);
3807 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3808 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003809#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003810}
3811
Karl Schultze4dc75c2016-02-02 15:37:51 -07003812static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003813 vec3 eye = {0.0f, 3.0f, 5.0f};
3814 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003815 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003816
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003817 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003818 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003819 demo->frameCount = INT32_MAX;
Tony-LunarG50e737c2020-07-16 14:26:03 -06003820 /* For cube demo we just grab the first physical device by default */
3821 demo->gpu_number = 0;
Jeremy Kniager979b5312019-11-22 14:55:57 -07003822
Piers Daniell735ee532015-02-23 16:23:13 -07003823 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003824 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003825 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003826 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003827 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003828 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3829 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003830 i++;
3831 continue;
3832 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003833 if (strcmp(argv[i], "--break") == 0) {
3834 demo->use_break = true;
3835 continue;
3836 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003837 if (strcmp(argv[i], "--validate") == 0) {
3838 demo->validate = true;
3839 continue;
3840 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003841 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3842 demo->validate = true;
3843 demo->validate_checks_disabled = true;
3844 continue;
3845 }
Tony Barbour2593b182016-04-19 10:57:58 -06003846 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003847 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003848 continue;
3849 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003850 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3851 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003852 i++;
3853 continue;
3854 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003855 if (strcmp(argv[i], "--suppress_popups") == 0) {
3856 demo->suppress_popups = true;
3857 continue;
3858 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003859 if (strcmp(argv[i], "--display_timing") == 0) {
3860 demo->VK_GOOGLE_display_timing_enabled = true;
3861 continue;
3862 }
Ian Elliott53622a72017-03-28 11:06:33 -06003863 if (strcmp(argv[i], "--incremental_present") == 0) {
3864 demo->VK_KHR_incremental_present_enabled = true;
3865 continue;
3866 }
Tony-LunarG50e737c2020-07-16 14:26:03 -06003867 if ((strcmp(argv[i], "--gpu_number") == 0) && (i < argc - 1)) {
3868 demo->gpu_number = atoi(argv[i + 1]);
3869 i++;
3870 continue;
3871 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003872
Cody Northropaf28a532016-09-27 10:50:57 -06003873#if defined(ANDROID)
Lenny Komowffe446c2018-11-19 17:08:04 -07003874 ERR_EXIT("Usage: vkcube [--validate]\n", "Usage");
Cody Northropaf28a532016-09-27 10:50:57 -06003875#else
Tony-LunarGd74734f2019-06-04 14:11:43 -06003876 char *message =
3877 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled]\n"
3878 "\t[--break] [--c <framecount>] [--suppress_popups]\n"
3879 "\t[--incremental_present] [--display_timing]\n"
Tony-LunarG50e737c2020-07-16 14:26:03 -06003880 "\t[--gpu_number <index of physical device>]\n"
Tony-LunarGd74734f2019-06-04 14:11:43 -06003881 "\t[--present_mode <present mode enum>]\n"
3882 "\t<present_mode_enum>\n"
3883 "\t\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3884 "\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3885 "\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
3886 "\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n";
3887 int length = snprintf(NULL, 0, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3888 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3889 char *usage = (char *)malloc(length + 1);
3890 if (!usage) {
3891 exit(1);
3892 }
3893 snprintf(usage, length + 1, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3894 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3895#if defined(_WIN32)
3896 if (!demo->suppress_popups) MessageBox(NULL, usage, "Usage Error", MB_OK);
3897#else
Tony-LunarG122ac062019-06-13 16:21:57 -06003898 fprintf(stderr, "%s", usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003899 fflush(stderr);
Tony-LunarGd74734f2019-06-04 14:11:43 -06003900#endif
3901 free(usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003902 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003903#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003904 }
3905
Tony Barbour153cb062016-12-07 13:43:36 -07003906 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003907
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003908 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003909
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003910 demo->width = 500;
3911 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003912
Tony Barbour66a56c32016-09-21 13:10:56 -06003913 demo->spin_angle = 4.0f;
3914 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003915 demo->pause = false;
3916
Dave Houlton5fa47912018-02-16 11:02:26 -07003917 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003918 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3919 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003920
Dave Houlton5fa47912018-02-16 11:02:26 -07003921 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003922}
3923
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003924#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003925// Include header required for parsing the command line options.
3926#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003927
Dave Houlton5fa47912018-02-16 11:02:26 -07003928int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3929 MSG msg; // message
3930 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003931 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003932 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003933
Jamie Madillb2ff6502017-03-15 16:17:46 -04003934 // Ensure wParam is initialized.
3935 msg.wParam = 0;
3936
Karl Schultze4dc75c2016-02-02 15:37:51 -07003937 // Use the CommandLine functions to get the command line arguments.
3938 // Unfortunately, Microsoft outputs
3939 // this information as wide characters for Unicode, and we simply want the
3940 // Ascii version to be compatible
3941 // with the non-Windows side. So, we have to convert the information to
3942 // Ascii character strings.
3943 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3944 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003945 argc = 0;
3946 }
3947
Karl Schultze4dc75c2016-02-02 15:37:51 -07003948 if (argc > 0) {
3949 argv = (char **)malloc(sizeof(char *) * argc);
3950 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003951 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003952 } else {
3953 for (int iii = 0; iii < argc; iii++) {
3954 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003955 size_t numConverted = 0;
3956
Karl Schultze4dc75c2016-02-02 15:37:51 -07003957 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3958 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003959 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003960 }
3961 }
3962 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003963 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003964 argv = NULL;
3965 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003966
3967 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003968
3969 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003970 if (argc > 0 && argv != NULL) {
3971 for (int iii = 0; iii < argc; iii++) {
3972 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003973 free(argv[iii]);
3974 }
3975 }
3976 free(argv);
3977 }
3978
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003979 demo.connection = hInstance;
Lenny Komowffe446c2018-11-19 17:08:04 -07003980 strncpy(demo.name, "Vulkan Cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003981 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003982 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003983
3984 demo_prepare(&demo);
3985
Dave Houlton5fa47912018-02-16 11:02:26 -07003986 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003987
3988 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003989 while (!done) {
Petr Krausd88e4e52019-01-23 18:45:04 +01003990 if (demo.pause) {
3991 const BOOL succ = WaitMessage();
3992
3993 if (!succ) {
3994 struct demo *tmp = &demo;
3995 struct demo *demo = tmp;
3996 ERR_EXIT("WaitMessage() failed on paused demo", "event loop error");
3997 }
3998 }
Ian Elliotta748eaf2015-04-28 15:50:36 -06003999 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07004000 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06004001 {
Dave Houlton5fa47912018-02-16 11:02:26 -07004002 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07004003 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06004004 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07004005 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06004006 DispatchMessage(&msg);
4007 }
Tony Barbourc8a59892015-10-20 12:49:46 -06004008 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06004009 }
4010
4011 demo_cleanup(&demo);
4012
Karl Schultze4dc75c2016-02-02 15:37:51 -07004013 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06004014}
Bill Hollingsf4352142017-02-14 22:58:56 -05004015
Bill Hollings0a0625a2019-07-15 17:39:18 -04004016#elif defined(VK_USE_PLATFORM_METAL_EXT)
4017static void demo_main(struct demo *demo, void *caMetalLayer, int argc, const char *argv[]) {
Dave Houlton5fa47912018-02-16 11:02:26 -07004018 demo_init(demo, argc, (char **)argv);
Bill Hollings0a0625a2019-07-15 17:39:18 -04004019 demo->caMetalLayer = caMetalLayer;
Tony Barbourc65cd752017-03-13 15:29:35 -06004020 demo_init_vk_swapchain(demo);
4021 demo_prepare(demo);
4022 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05004023}
4024
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004025#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
4026#include <android/log.h>
4027#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004028#include "android_util.h"
4029
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004030static bool initialized = false;
4031static bool active = false;
4032struct demo demo;
4033
Dave Houlton5fa47912018-02-16 11:02:26 -07004034static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004035
Dave Houlton5fa47912018-02-16 11:02:26 -07004036static void processCommand(struct android_app *app, int32_t cmd) {
4037 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004038 case APP_CMD_INIT_WINDOW: {
4039 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06004040 // We're getting a new window. If the app is starting up, we
4041 // need to initialize. If the app has already been
4042 // initialized, that means that we lost our previous window,
4043 // which means that we have a lot of work to do. At a minimum,
4044 // we need to destroy the swapchain and surface associated with
4045 // the old window, and create a new surface and swapchain.
4046 // However, since there are a lot of other objects/state that
4047 // is tied to the swapchain, it's easiest to simply cleanup and
4048 // start over (i.e. use a brute-force approach of re-starting
4049 // the app)
4050 if (demo.prepared) {
4051 demo_cleanup(&demo);
4052 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004053
4054 // Parse Intents into argc, argv
4055 // Use the following key to send arguments, i.e.
4056 // --es args "--validate"
4057 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07004058 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004059 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07004060 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004061
4062 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07004063 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 -06004064
4065 demo_init(&demo, argc, argv);
4066
4067 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07004068 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004069
Dave Houlton5fa47912018-02-16 11:02:26 -07004070 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004071 demo_init_vk_swapchain(&demo);
4072 demo_prepare(&demo);
4073 initialized = true;
4074 }
4075 break;
4076 }
4077 case APP_CMD_GAINED_FOCUS: {
4078 active = true;
4079 break;
4080 }
4081 case APP_CMD_LOST_FOCUS: {
4082 active = false;
4083 break;
4084 }
4085 }
4086}
4087
Dave Houlton5fa47912018-02-16 11:02:26 -07004088void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06004089#ifdef ANDROID
4090 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07004091 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06004092#endif
4093
Ian Elliottf05d5d12016-05-17 12:03:35 -06004094 demo.prepared = false;
4095
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004096 app->onAppCmd = processCommand;
4097 app->onInputEvent = processInput;
4098
Dave Houlton5fa47912018-02-16 11:02:26 -07004099 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004100 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07004101 struct android_poll_source *source;
4102 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004103 if (source) {
4104 source->process(app, source);
4105 }
4106
4107 if (app->destroyRequested != 0) {
4108 demo_cleanup(&demo);
4109 return;
4110 }
4111 }
4112 if (initialized && active) {
4113 demo_run(&demo);
4114 }
4115 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004116}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004117#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004118int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004119 struct demo demo;
4120
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004121 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004122#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004123 demo_create_xcb_window(&demo);
4124#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4125 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004126#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4127 demo_create_window(&demo);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02004128#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
4129 demo_create_directfb_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004130#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004131
Tony Barbourcc69f452015-10-08 13:59:42 -06004132 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004133
4134 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004135
Tony Barbour153cb062016-12-07 13:43:36 -07004136#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004137 demo_run_xcb(&demo);
4138#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4139 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004140#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4141 demo_run(&demo);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02004142#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
4143 demo_run_directfb(&demo);
Damien Leone600c3052017-01-31 10:26:07 -07004144#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4145 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004146#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004147
4148 demo_cleanup(&demo);
4149
Karl Schultz0218c002016-03-22 17:06:13 -06004150 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004151}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004152#endif