blob: 9516fca1b1deb1c5ff09c6218b074181c9c0fa79 [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
Tony-LunarG495604b2019-06-13 15:32:05 -06002 * Copyright (c) 2015-2019 The Khronos Group Inc.
3 * Copyright (c) 2015-2019 Valve Corporation
4 * Copyright (c) 2015-2019 LunarG, Inc.
Ian Elliottd940ca22017-03-22 08:31:27 -06005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Chia-I Wu <olv@lunarg.com>
19 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20 * Author: Ian Elliott <ian@LunarG.com>
21 * Author: Ian Elliott <ianelliott@google.com>
22 * Author: Jon Ashburn <jon@lunarg.com>
23 * Author: Gwan-gyeong Mun <elongbug@gmail.com>
24 * Author: Tony Barbour <tony@LunarG.com>
25 * Author: Bill Hollings <bill.hollings@brenwill.com>
26 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070027
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060029#include <stdio.h>
Ian Elliottd940ca22017-03-22 08:31:27 -060030#include <stdarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060031#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070035#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090036#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060037#include <X11/Xutil.h>
Joey Bzdek15eb0702017-06-07 09:40:36 -060038#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
39#include <linux/input.h>
Tony Barbour2593b182016-04-19 10:57:58 -060040#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060041
Ian Elliott639ca472015-04-16 15:23:05 -060042#ifdef _WIN32
Biswapriyo Nathffe404f2020-08-25 01:47:41 +053043#ifdef _MSC_VER
Ian Elliott639ca472015-04-16 15:23:05 -060044#pragma comment(linker, "/subsystem:windows")
Biswapriyo Nathc472fa22020-08-25 23:49:34 +053045#endif // MSVC
Ian Elliott639ca472015-04-16 15:23:05 -060046#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070047#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060048
Cody Northrop8707a6e2016-04-26 19:59:19 -060049#ifdef ANDROID
50#include "vulkan_wrapper.h"
51#else
David Pinedoc5193c12015-11-06 12:54:48 -070052#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060053#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070054
David Pinedo541526f2015-11-24 09:00:24 -070055#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060056#include "linmath.h"
Mark Lobodzinski0edf1382018-04-10 15:40:04 -060057#include "object_type_string_helper.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060058
Ian Elliottd940ca22017-03-22 08:31:27 -060059#include "gettime.h"
60#include "inttypes.h"
61#define MILLION 1000000L
62#define BILLION 1000000000L
63
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060064#define DEMO_TEXTURE_COUNT 1
Lenny Komowffe446c2018-11-19 17:08:04 -070065#define APP_SHORT_NAME "vkcube"
66#define APP_LONG_NAME "Vulkan Cube"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060067
Tony Barbour66a56c32016-09-21 13:10:56 -060068// Allow a maximum of two outstanding presentation operations.
69#define FRAME_LAG 2
70
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060071#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
72
Tony Barbourfdc2d352015-04-22 09:02:32 -060073#if defined(NDEBUG) && defined(__GNUC__)
74#define U_ASSERT_ONLY __attribute__((unused))
75#else
76#define U_ASSERT_ONLY
77#endif
78
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090079#if defined(__GNUC__)
80#define UNUSED __attribute__((unused))
81#else
82#define UNUSED
83#endif
84
Ian Elliott07264132015-04-28 11:35:02 -060085#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060086bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070087#define ERR_EXIT(err_msg, err_class) \
88 do { \
89 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
90 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070091 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -060092void DbgMsg(char *fmt, ...) {
93 va_list va;
94 va_start(va, fmt);
Mike Weiblene9847ff2019-06-27 15:18:32 -060095 vprintf(fmt, va);
Ian Elliottd940ca22017-03-22 08:31:27 -060096 va_end(va);
Mike Weiblene9847ff2019-06-27 15:18:32 -060097 fflush(stdout);
Ian Elliottd940ca22017-03-22 08:31:27 -060098}
Ian Elliott07264132015-04-28 11:35:02 -060099
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500100#elif defined __ANDROID__
101#include <android/log.h>
Jeremy Kniager979b5312019-11-22 14:55:57 -0700102#define ERR_EXIT(err_msg, err_class) \
103 do { \
Lenny Komowffe446c2018-11-19 17:08:04 -0700104 ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", err_msg)); \
Jeremy Kniager979b5312019-11-22 14:55:57 -0700105 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500106 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600107#ifdef VARARGS_WORKS_ON_ANDROID
108void DbgMsg(const char *fmt, ...) {
109 va_list va;
110 va_start(va, fmt);
Lenny Komowffe446c2018-11-19 17:08:04 -0700111 __android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, va);
Ian Elliottd940ca22017-03-22 08:31:27 -0600112 va_end(va);
113}
114#else // VARARGS_WORKS_ON_ANDROID
Jeremy Kniager979b5312019-11-22 14:55:57 -0700115#define DbgMsg(fmt, ...) \
116 do { \
Lenny Komowffe446c2018-11-19 17:08:04 -0700117 ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, ##__VA_ARGS__)); \
Ian Elliottd940ca22017-03-22 08:31:27 -0600118 } while (0)
119#endif // VARARGS_WORKS_ON_ANDROID
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500120#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700121#define ERR_EXIT(err_msg, err_class) \
122 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -0800123 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700124 fflush(stdout); \
125 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700126 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600127void DbgMsg(char *fmt, ...) {
128 va_list va;
129 va_start(va, fmt);
Mike Weiblene9847ff2019-06-27 15:18:32 -0600130 vprintf(fmt, va);
Ian Elliottd940ca22017-03-22 08:31:27 -0600131 va_end(va);
Mike Weiblene9847ff2019-06-27 15:18:32 -0600132 fflush(stdout);
Ian Elliottd940ca22017-03-22 08:31:27 -0600133}
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500134#endif
Ian Elliott07264132015-04-28 11:35:02 -0600135
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700136#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
137 { \
138 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
139 if (demo->fp##entrypoint == NULL) { \
140 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
141 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700142 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600143
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600144static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
145
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700146#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
147 { \
148 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
149 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
150 if (demo->fp##entrypoint == NULL) { \
151 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
152 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700153 }
Ian Elliott673898b2015-06-22 15:07:49 -0600154
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600155/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700156 * structure to track all objects related to a texture.
157 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600158struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600159 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700160
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600161 VkImage image;
Tony-LunarGbc9fc052018-09-21 13:47:06 -0600162 VkBuffer buffer;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600163 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600164
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800165 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500166 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600167 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700168 int32_t tex_width, tex_height;
169};
170
Karl Schultze4dc75c2016-02-02 15:37:51 -0700171static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172
Karl Schultz0218c002016-03-22 17:06:13 -0600173static int validation_error = 0;
174
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600175struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600176 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700177 float mvp[4][4];
178 float position[12 * 3][4];
179 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600180};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600181
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600182//--------------------------------------------------------------------------------------
183// Mesh and VertexFormat Data
184//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700185// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600186static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 -1.0f,-1.0f, 1.0f,
189 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800190 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600191 -1.0f, 1.0f,-1.0f,
192 -1.0f,-1.0f,-1.0f,
193
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800194 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600195 1.0f, 1.0f,-1.0f,
196 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600198 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600199 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600200
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800201 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600202 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600203 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800204 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600205 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600206 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600207
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800208 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600209 -1.0f, 1.0f, 1.0f,
210 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800211 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600213 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800215 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600216 1.0f, 1.0f, 1.0f,
217 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800218 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219 1.0f,-1.0f,-1.0f,
220 1.0f, 1.0f,-1.0f,
221
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800222 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600224 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800225 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600226 1.0f,-1.0f, 1.0f,
227 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600228};
229
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600230static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700231 0.0f, 1.0f, // -X side
232 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600235 0.0f, 0.0f,
236 0.0f, 1.0f,
237
Rene Lindsay76867e82016-07-29 10:49:11 -0700238 1.0f, 1.0f, // -Z side
239 0.0f, 0.0f,
240 0.0f, 1.0f,
241 1.0f, 1.0f,
242 1.0f, 0.0f,
243 0.0f, 0.0f,
244
245 1.0f, 0.0f, // -Y side
246 1.0f, 1.0f,
247 0.0f, 1.0f,
248 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600249 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800250 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600251
Rene Lindsay76867e82016-07-29 10:49:11 -0700252 1.0f, 0.0f, // +Y side
253 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800254 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600255 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700256 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600257 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600258
Rene Lindsay76867e82016-07-29 10:49:11 -0700259 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800260 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700261 0.0f, 1.0f,
262 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600263 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800264 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700265
266 0.0f, 0.0f, // +Z side
267 0.0f, 1.0f,
268 1.0f, 0.0f,
269 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600270 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700271 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600272};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700273// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600274
Karl Schultze4dc75c2016-02-02 15:37:51 -0700275void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600276 int i;
277
278 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700279 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600280 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
281 }
282 printf("\n");
283 fflush(stdout);
284}
285
Karl Schultze4dc75c2016-02-02 15:37:51 -0700286void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600287 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700288 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600289 printf("\n");
290 fflush(stdout);
291}
292
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600293typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600294 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800295 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600296 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600297 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700298 VkBuffer uniform_buffer;
299 VkDeviceMemory uniform_memory;
Tony-LunarG37af49f2019-12-19 12:04:19 -0700300 void *uniform_memory_ptr;
Tony Barboura72d9492017-01-11 13:35:09 -0700301 VkFramebuffer framebuffer;
302 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700303} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600304
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600305struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500306#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600307#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700308 HINSTANCE connection; // hInstance - Windows Instance
309 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
310 HWND window; // hWnd - window handle
311 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700312#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700313 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600314 Window xlib_window;
315 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700316#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700317 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318 xcb_connection_t *connection;
319 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600320 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800321 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900322#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
323 struct wl_display *display;
324 struct wl_registry *registry;
325 struct wl_compositor *compositor;
326 struct wl_surface *window;
Tony-LunarG5ceb7be2019-12-05 16:05:40 -0700327 struct wl_shell *shell;
328 struct wl_shell_surface *shell_surface;
Joey Bzdek15eb0702017-06-07 09:40:36 -0600329 struct wl_seat *seat;
330 struct wl_pointer *pointer;
331 struct wl_keyboard *keyboard;
Nicolas Caramellic8be8c82020-07-13 17:22:09 +0200332#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
333 IDirectFB *dfb;
334 IDirectFBSurface *window;
335 IDirectFBEventBuffer *event_buffer;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500336#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mike Schuchardt837d5162018-03-08 12:57:02 -0700337 struct ANativeWindow *window;
Bill Hollings0a0625a2019-07-15 17:39:18 -0400338#elif defined(VK_USE_PLATFORM_METAL_EXT)
339 void *caMetalLayer;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500340#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700341 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600342 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700343 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600344 bool separate_present_queue;
Jeremy Kniager602e4182018-01-19 10:52:34 -0700345 bool is_minimized;
Tony-LunarG50e737c2020-07-16 14:26:03 -0600346 uint32_t gpu_number;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600347
Ian Elliott53622a72017-03-28 11:06:33 -0600348 bool VK_KHR_incremental_present_enabled;
349
Ian Elliottd940ca22017-03-22 08:31:27 -0600350 bool VK_GOOGLE_display_timing_enabled;
351 bool syncd_with_actual_presents;
352 uint64_t refresh_duration;
353 uint64_t refresh_duration_multiplier;
354 uint64_t target_IPD; // image present duration (inverse of frame rate)
355 uint64_t prev_desired_present_time;
356 uint32_t next_present_id;
357 uint32_t last_early_id; // 0 if no early images
358 uint32_t last_late_id; // 0 if no late images
359
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600361 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600362 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600363 VkQueue graphics_queue;
364 VkQueue present_queue;
365 uint32_t graphics_queue_family_index;
366 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600367 VkSemaphore image_acquired_semaphores[FRAME_LAG];
368 VkSemaphore draw_complete_semaphores[FRAME_LAG];
369 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600370 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600371 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600372 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Tony Barbourda2bad52016-01-22 14:36:40 -0700374 uint32_t enabled_extension_count;
375 uint32_t enabled_layer_count;
376 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600377 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700378
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600381 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600382
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700383 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
384 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
385 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
386 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600387 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
388 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
389 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
390 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
391 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600392 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
393 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600394 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600395 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700396 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600397 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600398 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600399 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800401 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600402 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403
404 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600405 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600406
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800408 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500409 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600410 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600411 } depth;
412
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600414 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600415
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700416 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500417 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600418 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600419 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800420 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600421 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600422
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600423 mat4x4 projection_matrix;
424 mat4x4 view_matrix;
425 mat4x4 model_matrix;
426
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600427 float spin_angle;
428 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600429 bool pause;
430
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600431 VkShaderModule vert_shader_module;
432 VkShaderModule frag_shader_module;
433
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600434 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800435
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600436 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600437 int32_t curFrame;
438 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600439 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600440 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600441 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600442 bool suppress_popups;
Mark Young66510e52017-11-10 10:05:14 -0700443
444 PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
445 PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
446 PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
447 PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
448 PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
449 PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
450 PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
451 VkDebugUtilsMessengerEXT dbg_messenger;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600452
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600453 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600454 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600455};
456
Mark Young66510e52017-11-10 10:05:14 -0700457VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
458 VkDebugUtilsMessageTypeFlagsEXT messageType,
459 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
460 void *pUserData) {
461 char prefix[64] = "";
462 char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000);
lenny-lunargfbe22392016-06-06 11:07:53 -0600463 assert(message);
Mark Young66510e52017-11-10 10:05:14 -0700464 struct demo *demo = (struct demo *)pUserData;
lenny-lunargfbe22392016-06-06 11:07:53 -0600465
Mark Young66510e52017-11-10 10:05:14 -0700466 if (demo->use_break) {
467#ifndef WIN32
468 raise(SIGTRAP);
469#else
470 DebugBreak();
471#endif
472 }
473
474 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
475 strcat(prefix, "VERBOSE : ");
476 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
477 strcat(prefix, "INFO : ");
478 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
479 strcat(prefix, "WARNING : ");
480 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
481 strcat(prefix, "ERROR : ");
482 }
483
484 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
485 strcat(prefix, "GENERAL");
lenny-lunargfbe22392016-06-06 11:07:53 -0600486 } else {
Mark Young66510e52017-11-10 10:05:14 -0700487 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
488 strcat(prefix, "VALIDATION");
489 validation_error = 1;
490 }
491 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
492 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
493 strcat(prefix, "|");
494 }
495 strcat(prefix, "PERFORMANCE");
496 }
497 }
498
499 sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber,
500 pCallbackData->pMessageIdName, pCallbackData->pMessage);
501 if (pCallbackData->objectCount > 0) {
502 char tmp_message[500];
503 sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount);
504 strcat(message, tmp_message);
505 for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
506 if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
507 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object,
Mark Young44212682018-02-21 15:29:41 -0700508 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700509 (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName);
510 } else {
511 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object,
Mark Young44212682018-02-21 15:29:41 -0700512 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700513 (void *)(pCallbackData->pObjects[object].objectHandle));
514 }
515 strcat(message, tmp_message);
516 }
517 }
518 if (pCallbackData->cmdBufLabelCount > 0) {
519 char tmp_message[500];
520 sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount);
521 strcat(message, tmp_message);
522 for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
523 sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label,
524 pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0],
525 pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2],
526 pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]);
527 strcat(message, tmp_message);
528 }
lenny-lunargfbe22392016-06-06 11:07:53 -0600529 }
530
531#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600532
Tony Barbour602ca4f2016-09-12 14:02:43 -0600533 in_callback = true;
Jeremy Kniager979b5312019-11-22 14:55:57 -0700534 if (!demo->suppress_popups) MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600535 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600536
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600537#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600538
Mike Schuchardt837d5162018-03-08 12:57:02 -0700539 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
Jeremy Kniager979b5312019-11-22 14:55:57 -0700540 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700541 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
Jeremy Kniager979b5312019-11-22 14:55:57 -0700542 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700543 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600544 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700545 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
546 __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600547 } else {
Jeremy Kniager979b5312019-11-22 14:55:57 -0700548 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600549 }
Cody Northropaf28a532016-09-27 10:50:57 -0600550
lenny-lunargfbe22392016-06-06 11:07:53 -0600551#else
Cody Northropaf28a532016-09-27 10:50:57 -0600552
lenny-lunargfbe22392016-06-06 11:07:53 -0600553 printf("%s\n", message);
554 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600555
lenny-lunargfbe22392016-06-06 11:07:53 -0600556#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600557
lenny-lunargfbe22392016-06-06 11:07:53 -0600558 free(message);
559
Mark Young66510e52017-11-10 10:05:14 -0700560 // Don't bail out, but keep going.
lenny-lunargfbe22392016-06-06 11:07:53 -0600561 return false;
562}
563
Ian Elliottd940ca22017-03-22 08:31:27 -0600564bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
565 // The desired time was the earliest time that the present should have
566 // occured. In almost every case, the actual time should be later than the
567 // desired time. We should only consider the actual time "late" if it is
568 // after "desired + rdur".
569 if (actual <= desired) {
570 // The actual time was before or equal to the desired time. This will
571 // probably never happen, but in case it does, return false since the
572 // present was obviously NOT late.
573 return false;
574 }
Liam Middlebrook881fe392018-05-03 15:52:32 -0700575 uint64_t deadline = desired + rdur;
Ian Elliottd940ca22017-03-22 08:31:27 -0600576 if (actual > deadline) {
577 return true;
578 } else {
579 return false;
580 }
581}
Dave Houlton5fa47912018-02-16 11:02:26 -0700582bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600583 if (earliest < actual) {
584 // Consider whether this present could have occured earlier. Make sure
585 // that earliest time was at least 2msec earlier than actual time, and
586 // that the margin was at least 2msec:
587 uint64_t diff = actual - earliest;
588 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
589 // This present could have occured earlier because both: 1) the
590 // earliest time was at least 2 msec before actual time, and 2) the
591 // margin was at least 2msec.
592 return true;
593 }
594 }
595 return false;
596}
597
Tony-LunarG6cebf142019-09-11 14:32:23 -0600598// Forward declarations:
Ian Elliottcf074d32015-10-16 18:02:43 -0600599static void demo_resize(struct demo *demo);
Tony-LunarG6cebf142019-09-11 14:32:23 -0600600static void demo_create_surface(struct demo *demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600601
Dave Houlton5fa47912018-02-16 11:02:26 -0700602static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700603 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600604 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700605 if ((typeBits & 1) == 1) {
606 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700607 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700608 *typeIndex = i;
609 return true;
610 }
611 }
612 typeBits >>= 1;
613 }
614 // No memory types matched, return failure
615 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600616}
617
Karl Schultze4dc75c2016-02-02 15:37:51 -0700618static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600619 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600620
Tony Barbource7524e2016-07-28 12:01:45 -0600621 // This function could get called twice if the texture uses a staging buffer
622 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700623 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600624
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600625 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600626 assert(!err);
627
Tony Barbource7524e2016-07-28 12:01:45 -0600628 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700629 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700630 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
631 assert(!err);
632
Karl Schultze4dc75c2016-02-02 15:37:51 -0700633 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700634 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
635 .pNext = NULL,
636 .waitSemaphoreCount = 0,
637 .pWaitSemaphores = NULL,
638 .pWaitDstStageMask = NULL,
639 .commandBufferCount = 1,
640 .pCommandBuffers = cmd_bufs,
641 .signalSemaphoreCount = 0,
642 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600643
Tony Barbource7524e2016-07-28 12:01:45 -0600644 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600645 assert(!err);
646
Tony Barbource7524e2016-07-28 12:01:45 -0600647 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600648 assert(!err);
649
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600650 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600651 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600652 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600653}
654
Dave Houlton5fa47912018-02-16 11:02:26 -0700655static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
656 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600657 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600658 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600659
Dave Houlton5fa47912018-02-16 11:02:26 -0700660 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
661 .pNext = NULL,
662 .srcAccessMask = srcAccessMask,
663 .dstAccessMask = 0,
Tony-LunarGa141b962018-05-30 11:33:19 -0600664 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
665 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Dave Houlton5fa47912018-02-16 11:02:26 -0700666 .oldLayout = old_image_layout,
667 .newLayout = new_image_layout,
668 .image = image,
669 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600670
Tony Barboureb5077b2016-10-19 15:23:36 -0600671 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700672 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
673 /* Make sure anything that was copying from this image has completed */
674 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
675 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600676
Dave Houlton5fa47912018-02-16 11:02:26 -0700677 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
678 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
679 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700680
Dave Houlton5fa47912018-02-16 11:02:26 -0700681 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
682 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
683 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700684
Dave Houlton5fa47912018-02-16 11:02:26 -0700685 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
686 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
687 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600688
Dave Houlton5fa47912018-02-16 11:02:26 -0700689 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
690 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
691 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600692
Dave Houlton5fa47912018-02-16 11:02:26 -0700693 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
694 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
695 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600696
Dave Houlton5fa47912018-02-16 11:02:26 -0700697 default:
698 image_memory_barrier.dstAccessMask = 0;
699 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600700 }
701
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600702 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600703
Dave Houlton5fa47912018-02-16 11:02:26 -0700704 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600705}
706
Karl Schultze4dc75c2016-02-02 15:37:51 -0700707static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Lenny Komowe830b592018-02-23 16:18:36 -0700708 VkDebugUtilsLabelEXT label;
709 memset(&label, 0, sizeof(label));
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700710 const VkCommandBufferBeginInfo cmd_buf_info = {
711 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
712 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600713 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700714 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700715 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800716 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700717 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
718 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800719 };
720 const VkRenderPassBeginInfo rp_begin = {
721 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
722 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800723 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700724 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800725 .renderArea.offset.x = 0,
726 .renderArea.offset.y = 0,
727 .renderArea.extent.width = demo->width,
728 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600729 .clearValueCount = 2,
730 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700731 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800732 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600734 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Mark Young66510e52017-11-10 10:05:14 -0700735
736 if (demo->validate) {
737 // Set a name for the command buffer
738 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
739 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
740 .pNext = NULL,
741 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
742 .objectHandle = (uint64_t)cmd_buf,
743 .pObjectName = "CubeDrawCommandBuf",
744 };
745 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
746
747 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
748 label.pNext = NULL;
749 label.pLabelName = "DrawBegin";
750 label.color[0] = 0.4f;
751 label.color[1] = 0.3f;
752 label.color[2] = 0.2f;
753 label.color[3] = 0.1f;
754 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
755 }
756
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600757 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800758 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Mark Young66510e52017-11-10 10:05:14 -0700759
760 if (demo->validate) {
761 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
762 label.pNext = NULL;
763 label.pLabelName = "InsideRenderPass";
764 label.color[0] = 8.4f;
765 label.color[1] = 7.3f;
766 label.color[2] = 6.2f;
767 label.color[3] = 7.1f;
768 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
769 }
770
Karl Schultze4dc75c2016-02-02 15:37:51 -0700771 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700772 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
773 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600774 VkViewport viewport;
775 memset(&viewport, 0, sizeof(viewport));
Jeremy Kniager979b5312019-11-22 14:55:57 -0700776 float viewport_dimension;
777 if (demo->width < demo->height) {
778 viewport_dimension = (float)demo->width;
779 viewport.y = (demo->height - demo->width) / 2.0f;
780 } else {
781 viewport_dimension = (float)demo->height;
782 viewport.x = (demo->width - demo->height) / 2.0f;
783 }
784 viewport.height = viewport_dimension;
785 viewport.width = viewport_dimension;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700786 viewport.minDepth = (float)0.0f;
787 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700788 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600789
790 VkRect2D scissor;
791 memset(&scissor, 0, sizeof(scissor));
792 scissor.extent.width = demo->width;
793 scissor.extent.height = demo->height;
794 scissor.offset.x = 0;
795 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700796 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Mark Young66510e52017-11-10 10:05:14 -0700797
798 if (demo->validate) {
799 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
800 label.pNext = NULL;
801 label.pLabelName = "ActualDraw";
Mike Schuchardt4cf3aa42018-02-23 12:44:05 -0700802 label.color[0] = -0.4f;
803 label.color[1] = -0.3f;
804 label.color[2] = -0.2f;
805 label.color[3] = -0.1f;
Mark Young66510e52017-11-10 10:05:14 -0700806 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
807 }
808
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600809 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Mark Young66510e52017-11-10 10:05:14 -0700810 if (demo->validate) {
811 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
812 }
813
Tony Barbour98390392016-07-28 10:50:13 -0600814 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600815 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800816 vkCmdEndRenderPass(cmd_buf);
Mark Young66510e52017-11-10 10:05:14 -0700817 if (demo->validate) {
818 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
819 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600820
Tony Barbour22e06272016-09-07 16:07:56 -0600821 if (demo->separate_present_queue) {
822 // We have to transfer ownership from the graphics queue family to the
823 // present queue family to be able to present. Note that we don't have
824 // to transfer from present queue family back to graphics queue family at
825 // the start of the next frame because we don't care about the image's
826 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700827 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
828 .pNext = NULL,
829 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600830 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700831 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
832 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
833 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
834 .dstQueueFamilyIndex = demo->present_queue_family_index,
835 .image = demo->swapchain_image_resources[demo->current_buffer].image,
836 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600837
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600838 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
839 NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600840 }
Mark Young66510e52017-11-10 10:05:14 -0700841 if (demo->validate) {
842 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
843 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600844 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600845 assert(!err);
846}
847
Tony Barbour22e06272016-09-07 16:07:56 -0600848void demo_build_image_ownership_cmd(struct demo *demo, int i) {
849 VkResult U_ASSERT_ONLY err;
850
851 const VkCommandBufferBeginInfo cmd_buf_info = {
852 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
853 .pNext = NULL,
854 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
855 .pInheritanceInfo = NULL,
856 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700857 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600858 assert(!err);
859
Dave Houlton5fa47912018-02-16 11:02:26 -0700860 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
861 .pNext = NULL,
862 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600863 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700864 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
865 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
866 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
867 .dstQueueFamilyIndex = demo->present_queue_family_index,
868 .image = demo->swapchain_image_resources[i].image,
869 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600870
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600871 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
872 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700873 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600874 assert(!err);
875}
876
Karl Schultze4dc75c2016-02-02 15:37:51 -0700877void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600878 mat4x4 MVP, Model, VP;
879 int matrixSize = sizeof(MVP);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600880
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600881 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600882
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700883 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600884 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700885 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600886 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600887
Tony-LunarG37af49f2019-12-19 12:04:19 -0700888 memcpy(demo->swapchain_image_resources[demo->current_buffer].uniform_memory_ptr, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600889}
890
Ian Elliottd940ca22017-03-22 08:31:27 -0600891void DemoUpdateTargetIPD(struct demo *demo) {
892 // Look at what happened to previous presents, and make appropriate
893 // adjustments in timing:
894 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700895 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600896 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600897
Dave Houlton5fa47912018-02-16 11:02:26 -0700898 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600899 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600900 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700901 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600902 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700903 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600904 assert(!err);
905
906 bool early = false;
907 bool late = false;
908 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700909 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600910 if (!demo->syncd_with_actual_presents) {
911 // This is the first time that we've received an
912 // actualPresentTime for this swapchain. In order to not
913 // perceive these early frames as "late", we need to sync-up
914 // our future desiredPresentTime's with the
915 // actualPresentTime(s) that we're receiving now.
916 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600917
Ian Elliottd940ca22017-03-22 08:31:27 -0600918 // So that we don't suspect any pending presents as late,
919 // record them all as suspected-late presents:
920 demo->last_late_id = demo->next_present_id - 1;
921 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600922 demo->syncd_with_actual_presents = true;
923 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700924 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600925 demo->refresh_duration)) {
926 // This image could have been presented earlier. We don't want
927 // to decrease the target_IPD until we've seen early presents
928 // for at least two seconds.
929 if (demo->last_early_id == past[i].presentID) {
930 // We've now seen two seconds worth of early presents.
931 // Flag it as such, and reset the counter:
932 early = true;
933 demo->last_early_id = 0;
934 } else if (demo->last_early_id == 0) {
935 // This is the first early present we've seen.
936 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700937 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
938 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600939 demo->last_early_id = past[i].presentID + howManyPresents;
940 } else {
941 // We are in the midst of a set of early images,
942 // and so we won't do anything.
943 }
944 late = false;
945 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700946 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600947 // This image was presented after its desired time. Since
948 // there's a delay between calling vkQueuePresentKHR and when
949 // we get the timing data, several presents may have been late.
950 // Thus, we need to threat all of the outstanding presents as
951 // being likely late, so that we only increase the target_IPD
952 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700953 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600954 late = true;
955 // Record the last suspected-late present:
956 demo->last_late_id = demo->next_present_id - 1;
957 } else {
958 // We are in the midst of a set of likely-late images,
959 // and so we won't do anything.
960 }
961 early = false;
962 demo->last_early_id = 0;
963 } else {
964 // Since this image was not presented early or late, reset
965 // any sets of early or late presentIDs:
966 early = false;
967 late = false;
968 calibrate_next = true;
969 demo->last_early_id = 0;
970 demo->last_late_id = 0;
971 }
972 }
973
974 if (early) {
975 // Since we've seen at least two-seconds worth of presnts that
976 // could have occured earlier than desired, let's decrease the
977 // target_IPD (i.e. increase the frame rate):
978 //
979 // TODO(ianelliott): Try to calculate a better target_IPD based
980 // on the most recently-seen present (this is overly-simplistic).
981 demo->refresh_duration_multiplier--;
982 if (demo->refresh_duration_multiplier == 0) {
983 // This should never happen, but in case it does, don't
984 // try to go faster.
985 demo->refresh_duration_multiplier = 1;
986 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700987 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600988 }
989 if (late) {
990 // Since we found a new instance of a late present, we want to
991 // increase the target_IPD (i.e. decrease the frame rate):
992 //
993 // TODO(ianelliott): Try to calculate a better target_IPD based
994 // on the most recently-seen present (this is overly-simplistic).
995 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -0700996 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600997 }
998
999 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001000 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
1001 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -06001002 }
Karl Schultza33771f2018-05-28 16:16:47 -06001003 free(past);
Ian Elliottd940ca22017-03-22 08:31:27 -06001004 }
Ian Elliottd940ca22017-03-22 08:31:27 -06001005}
1006
Karl Schultze4dc75c2016-02-02 15:37:51 -07001007static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001008 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001009
Damien Leonec336d822017-04-14 16:10:14 -07001010 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +03001011 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1012 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001013
Tony Barbour8b7c9112017-06-29 13:34:41 -06001014 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -06001015 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -07001016 err =
1017 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1018 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001019
Tony Barbour6914e6d2017-06-02 12:11:29 -06001020 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1021 // demo->swapchain is out of date (e.g. the window was resized) and
1022 // must be recreated:
1023 demo_resize(demo);
1024 } else if (err == VK_SUBOPTIMAL_KHR) {
1025 // demo->swapchain is not as optimal as it could be, but the platform's
1026 // presentation engine will still present the image correctly.
1027 break;
Tony-LunarG6cebf142019-09-11 14:32:23 -06001028 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1029 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1030 demo_create_surface(demo);
1031 demo_resize(demo);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001032 } else {
1033 assert(!err);
1034 }
Tony Barbour8b7c9112017-06-29 13:34:41 -06001035 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001036
Tony Barbour3eafe1f2017-06-14 11:59:55 -06001037 demo_update_data_buffer(demo);
1038
Ian Elliottd940ca22017-03-22 08:31:27 -06001039 if (demo->VK_GOOGLE_display_timing_enabled) {
1040 // Look at what happened to previous presents, and make appropriate
1041 // adjustments in timing:
1042 DemoUpdateTargetIPD(demo);
1043
1044 // Note: a real application would position its geometry to that it's in
1045 // the correct locatoin for when the next image is presented. It might
1046 // also wait, so that there's less latency between any input and when
1047 // the next image is rendered/presented. This demo program is so
1048 // simple that it doesn't do either of those.
1049 }
1050
Tony Barbource7524e2016-07-28 12:01:45 -06001051 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001052 // that the image won't be rendered to until the presentation
1053 // engine has fully released ownership to the application, and it is
1054 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001055 VkPipelineStageFlags pipe_stage_flags;
1056 VkSubmitInfo submit_info;
1057 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1058 submit_info.pNext = NULL;
1059 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1060 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1061 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001062 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001063 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001064 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001065 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001066 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -07001067 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001068 assert(!err);
1069
Tony Barbour22e06272016-09-07 16:07:56 -06001070 if (demo->separate_present_queue) {
1071 // If we are using separate queues, change image ownership to the
1072 // present queue before presenting, waiting for the draw complete
1073 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001074 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001075 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1076 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001077 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001078 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001079 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001080 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001081 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001082 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1083 assert(!err);
1084 }
1085
1086 // If we are using separate queues we have to wait for image ownership,
1087 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001088 VkPresentInfoKHR present = {
1089 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001090 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001091 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -07001092 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1093 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001094 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001095 .pSwapchains = &demo->swapchain,
1096 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001097 };
1098
Dave Airlie48a68f92019-04-12 17:04:48 +10001099 VkRectLayerKHR rect;
1100 VkPresentRegionKHR region;
1101 VkPresentRegionsKHR regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001102 if (demo->VK_KHR_incremental_present_enabled) {
1103 // If using VK_KHR_incremental_present, we provide a hint of the region
1104 // that contains changed content relative to the previously-presented
1105 // image. The implementation can use this hint in order to save
1106 // work/power (by only copying the region in the hint). The
1107 // implementation is free to ignore the hint though, and so we must
1108 // ensure that the entire image has the correctly-drawn content.
1109 uint32_t eighthOfWidth = demo->width / 8;
1110 uint32_t eighthOfHeight = demo->height / 8;
Dave Airlie48a68f92019-04-12 17:04:48 +10001111
1112 rect.offset.x = eighthOfWidth;
1113 rect.offset.y = eighthOfHeight;
1114 rect.extent.width = eighthOfWidth * 6;
1115 rect.extent.height = eighthOfHeight * 6;
1116 rect.layer = 0;
1117
1118 region.rectangleCount = 1;
1119 region.pRectangles = &rect;
1120
1121 regions.sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR;
1122 regions.pNext = present.pNext;
1123 regions.swapchainCount = present.swapchainCount;
1124 regions.pRegions = &region;
Ian Elliott53622a72017-03-28 11:06:33 -06001125 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001126 }
1127
Ian Elliottd940ca22017-03-22 08:31:27 -06001128 if (demo->VK_GOOGLE_display_timing_enabled) {
1129 VkPresentTimeGOOGLE ptime;
1130 if (demo->prev_desired_present_time == 0) {
1131 // This must be the first present for this swapchain.
1132 //
1133 // We don't know where we are relative to the presentation engine's
1134 // display's refresh cycle. We also don't know how long rendering
1135 // takes. Let's make a grossly-simplified assumption that the
1136 // desiredPresentTime should be half way between now and
1137 // now+target_IPD. We will adjust over time.
1138 uint64_t curtime = getTimeInNanoseconds();
1139 if (curtime == 0) {
1140 // Since we didn't find out the current time, don't give a
1141 // desiredPresentTime:
1142 ptime.desiredPresentTime = 0;
1143 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001144 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1145 }
1146 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001147 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001148 }
1149 ptime.presentID = demo->next_present_id++;
1150 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001151
1152 VkPresentTimesInfoGOOGLE present_time = {
1153 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1154 .pNext = present.pNext,
1155 .swapchainCount = present.swapchainCount,
1156 .pTimes = &ptime,
1157 };
1158 if (demo->VK_GOOGLE_display_timing_enabled) {
1159 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001160 }
1161 }
1162
Tony Barboura2a67812016-07-18 13:21:06 -06001163 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001164 demo->frame_index += 1;
1165 demo->frame_index %= FRAME_LAG;
1166
Ian Elliottcf074d32015-10-16 18:02:43 -06001167 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1168 // demo->swapchain is out of date (e.g. the window was resized) and
1169 // must be recreated:
1170 demo_resize(demo);
1171 } else if (err == VK_SUBOPTIMAL_KHR) {
1172 // demo->swapchain is not as optimal as it could be, but the platform's
1173 // presentation engine will still present the image correctly.
Tony-LunarG6cebf142019-09-11 14:32:23 -06001174 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1175 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1176 demo_create_surface(demo);
1177 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06001178 } else {
1179 assert(!err);
1180 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001181}
1182
Karl Schultze4dc75c2016-02-02 15:37:51 -07001183static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001184 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001185 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001186
Ian Elliottb08e0d92015-11-20 11:55:46 -07001187 // Check the surface capabilities and formats
1188 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001189 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001190 assert(!err);
1191
Ian Elliott8528eff2015-08-07 15:56:59 -06001192 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001193 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001194 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001195 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001196 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001197 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001198 assert(!err);
1199
Ian Elliotta0781972015-08-21 15:09:33 -06001200 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001201 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1202 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1203 // If the surface size is undefined, the size is set to the size
1204 // of the images requested, which must fit within the minimum and
1205 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001206 swapchainExtent.width = demo->width;
1207 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001208
1209 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1210 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1211 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1212 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1213 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001214
Ian Elliottcf7f7482016-08-19 03:46:58 -06001215 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1216 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1217 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1218 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1219 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001220 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001221 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001222 swapchainExtent = surfCapabilities.currentExtent;
1223 demo->width = surfCapabilities.currentExtent.width;
1224 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001225 }
1226
Jeremy Kniager602e4182018-01-19 10:52:34 -07001227 if (demo->width == 0 || demo->height == 0) {
1228 demo->is_minimized = true;
1229 return;
1230 } else {
1231 demo->is_minimized = false;
1232 }
1233
Tony Barbour66a56c32016-09-21 13:10:56 -06001234 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001235 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001236 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001237
Ian Elliottb18fdde2016-10-03 12:11:12 -06001238 // There are times when you may wish to use another present mode. The
1239 // following code shows how to select them, and the comments provide some
1240 // reasons you may wish to use them.
1241 //
1242 // It should be noted that Vulkan 1.0 doesn't provide a method for
1243 // synchronizing rendering with the presentation engine's display. There
1244 // is a method provided for throttling rendering with the display, but
1245 // there are some presentation engines for which this method will not work.
1246 // If an application doesn't throttle its rendering, and if it renders much
1247 // faster than the refresh rate of the display, this can waste power on
1248 // mobile devices. That is because power is being spent rendering images
1249 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001250
Ian Elliottb18fdde2016-10-03 12:11:12 -06001251 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1252 // tearing, or have some way of synchronizing their rendering with the
1253 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001254 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1255 // generally render a new presentable image every refresh cycle, but are
1256 // occasionally early. In this case, the application wants the new image
1257 // to be displayed instead of the previously-queued-for-presentation image
1258 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001259 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1260 // render a new presentable image every refresh cycle, but are occasionally
1261 // late. In this case (perhaps because of stuttering/latency concerns),
1262 // the application wants the late image to be immediately displayed, even
1263 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001264
Dave Houlton5fa47912018-02-16 11:02:26 -07001265 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001266 for (size_t i = 0; i < presentModeCount; ++i) {
1267 if (presentModes[i] == demo->presentMode) {
1268 swapchainPresentMode = demo->presentMode;
1269 break;
1270 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001271 }
1272 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001273 if (swapchainPresentMode != demo->presentMode) {
1274 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1275 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001276
Tony Barbour84376fe2017-01-06 15:54:22 -07001277 // Determine the number of VkImages to use in the swap chain.
1278 // Application desires to acquire 3 images at a time for triple
1279 // buffering
1280 uint32_t desiredNumOfSwapchainImages = 3;
1281 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1282 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1283 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001284 // If maxImageCount is 0, we can ask for as many images as we want;
1285 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001286 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001287 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001288 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001289 }
1290
Tony Barbour9fd6d352015-09-16 15:01:32 -06001291 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001292 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001293 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001294 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001295 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001296 }
1297
Tony Barbour820b55b2017-03-14 08:55:46 -06001298 // Find a supported composite alpha mode - one of these is guaranteed to be set
1299 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1300 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1301 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1302 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1303 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1304 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1305 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001306 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001307 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1308 compositeAlpha = compositeAlphaFlags[i];
1309 break;
1310 }
1311 }
1312
Tony Barboura2a67812016-07-18 13:21:06 -06001313 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001314 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001315 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001316 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001317 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001318 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001319 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001320 .imageExtent =
1321 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001322 .width = swapchainExtent.width,
1323 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001324 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001325 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001326 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001327 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001328 .imageArrayLayers = 1,
1329 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1330 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001331 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001332 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001333 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001334 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001336 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001337 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001338 assert(!err);
1339
Ian Elliottcf074d32015-10-16 18:02:43 -06001340 // If we just re-created an existing swapchain, we should destroy the old
1341 // swapchain at this point.
1342 // Note: destroying the swapchain also cleans up all its associated
1343 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001344 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001345 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001346 }
1347
Dave Houlton5fa47912018-02-16 11:02:26 -07001348 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001349 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001350
Dave Houlton5fa47912018-02-16 11:02:26 -07001351 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001352 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001353 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001354 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001355
Dave Houlton5fa47912018-02-16 11:02:26 -07001356 demo->swapchain_image_resources =
1357 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001358 assert(demo->swapchain_image_resources);
1359
Ian Elliotta0781972015-08-21 15:09:33 -06001360 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001361 VkImageViewCreateInfo color_image_view = {
1362 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001363 .pNext = NULL,
1364 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001365 .components =
1366 {
Richard S. Wright Jrca51bc72021-01-06 13:03:18 -05001367 .r = VK_COMPONENT_SWIZZLE_IDENTITY,
1368 .g = VK_COMPONENT_SWIZZLE_IDENTITY,
1369 .b = VK_COMPONENT_SWIZZLE_IDENTITY,
1370 .a = VK_COMPONENT_SWIZZLE_IDENTITY,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001371 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001372 .subresourceRange =
1373 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001374 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1375 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001376 };
1377
Tony Barboura72d9492017-01-11 13:35:09 -07001378 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001379
Tony Barboura72d9492017-01-11 13:35:09 -07001380 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001381
Dave Houlton5fa47912018-02-16 11:02:26 -07001382 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001383 assert(!err);
1384 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001385
Ian Elliottd940ca22017-03-22 08:31:27 -06001386 if (demo->VK_GOOGLE_display_timing_enabled) {
1387 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001388 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001389 assert(!err);
1390 demo->refresh_duration = rc_dur.refreshDuration;
1391
1392 demo->syncd_with_actual_presents = false;
1393 // Initially target 1X the refresh duration:
1394 demo->target_IPD = demo->refresh_duration;
1395 demo->refresh_duration_multiplier = 1;
1396 demo->prev_desired_present_time = 0;
1397 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001398 }
1399
Jason Chen5d4fee92019-04-04 12:45:29 +08001400 if (NULL != swapchainImages) {
1401 free(swapchainImages);
1402 }
1403
Mark Young04fd3d82016-01-25 14:43:50 -07001404 if (NULL != presentModes) {
1405 free(presentModes);
1406 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407}
1408
Karl Schultze4dc75c2016-02-02 15:37:51 -07001409static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001410 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001411 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001412 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001414 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001416 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001417 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001418 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001419 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001420 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001421 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001422 .flags = 0,
1423 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001424
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001425 VkImageViewCreateInfo view = {
1426 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001428 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001429 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001430 .subresourceRange =
1431 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001433 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001434 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001435
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001436 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001437 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001438 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001439
1440 demo->depth.format = depth_format;
1441
1442 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001443 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001444 assert(!err);
1445
Karl Schultze4dc75c2016-02-02 15:37:51 -07001446 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001447 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001448
Chia-I Wuf48700b2015-11-10 16:21:09 +08001449 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001450 demo->depth.mem_alloc.pNext = NULL;
1451 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1452 demo->depth.mem_alloc.memoryTypeIndex = 0;
1453
Dave Houlton5fa47912018-02-16 11:02:26 -07001454 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001455 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001456 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001457
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001458 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001459 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001460 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001461
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001462 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001463 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001464 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001465
1466 /* create image view */
1467 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001468 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001469 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001470}
1471
Karl Schultzb7940402018-05-29 13:09:22 -06001472/* Convert ppm image data from header file into RGBA texture image */
1473#include "lunarg.ppm.h"
Dave Houlton5fa47912018-02-16 11:02:26 -07001474bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Karl Schultzb7940402018-05-29 13:09:22 -06001475 (void)filename;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001476 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001477 cPtr = (char *)lunarg_ppm;
1478 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001479 return false;
1480 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001481 while (strncmp(cPtr++, "\n", 1))
1482 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001483 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001484 if (rgba_data == NULL) {
1485 return true;
1486 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001487 while (strncmp(cPtr++, "\n", 1))
1488 ;
1489 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001490 return false;
1491 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001492 while (strncmp(cPtr++, "\n", 1))
1493 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001494 for (int y = 0; y < *height; y++) {
1495 uint8_t *rowPtr = rgba_data;
1496 for (int x = 0; x < *width; x++) {
1497 memcpy(rowPtr, cPtr, 3);
1498 rowPtr[3] = 255; /* Alpha of 1 */
1499 rowPtr += 4;
1500 cPtr += 3;
1501 }
1502 rgba_data += layout->rowPitch;
1503 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001504 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001505}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001506
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001507static void demo_prepare_texture_buffer(struct demo *demo, const char *filename, struct texture_object *tex_obj) {
1508 int32_t tex_width;
1509 int32_t tex_height;
1510 VkResult U_ASSERT_ONLY err;
1511 bool U_ASSERT_ONLY pass;
1512
1513 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
1514 ERR_EXIT("Failed to load textures", "Load Texture Failure");
1515 }
1516
1517 tex_obj->tex_width = tex_width;
1518 tex_obj->tex_height = tex_height;
1519
1520 const VkBufferCreateInfo buffer_create_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
1521 .pNext = NULL,
1522 .flags = 0,
1523 .size = tex_width * tex_height * 4,
1524 .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1525 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1526 .queueFamilyIndexCount = 0,
1527 .pQueueFamilyIndices = NULL};
1528
1529 err = vkCreateBuffer(demo->device, &buffer_create_info, NULL, &tex_obj->buffer);
1530 assert(!err);
1531
1532 VkMemoryRequirements mem_reqs;
1533 vkGetBufferMemoryRequirements(demo->device, tex_obj->buffer, &mem_reqs);
1534
1535 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1536 tex_obj->mem_alloc.pNext = NULL;
1537 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1538 tex_obj->mem_alloc.memoryTypeIndex = 0;
1539
1540 VkFlags requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1541 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, requirements, &tex_obj->mem_alloc.memoryTypeIndex);
1542 assert(pass);
1543
1544 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
1545 assert(!err);
1546
1547 /* bind memory */
1548 err = vkBindBufferMemory(demo->device, tex_obj->buffer, tex_obj->mem, 0);
1549 assert(!err);
1550
1551 VkSubresourceLayout layout;
1552 memset(&layout, 0, sizeof(layout));
1553 layout.rowPitch = tex_width * 4;
1554
1555 void *data;
1556 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
1557 assert(!err);
1558
1559 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1560 fprintf(stderr, "Error loading texture: %s\n", filename);
1561 }
1562
1563 vkUnmapMemory(demo->device, tex_obj->mem);
1564}
1565
Dave Houlton5fa47912018-02-16 11:02:26 -07001566static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1567 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001568 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001569 int32_t tex_width;
1570 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001571 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001572 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001573
Karl Schultze4dc75c2016-02-02 15:37:51 -07001574 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001575 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001576 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001577
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001578 tex_obj->tex_width = tex_width;
1579 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001580
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001581 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001582 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001583 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001584 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001585 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001586 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001587 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001588 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001589 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001590 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001591 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001592 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001593 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001594 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001595
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001596 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001597
Dave Houlton5fa47912018-02-16 11:02:26 -07001598 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001599 assert(!err);
1600
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001601 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001602
Chia-I Wuf48700b2015-11-10 16:21:09 +08001603 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001604 tex_obj->mem_alloc.pNext = NULL;
1605 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1606 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001607
Dave Houlton5fa47912018-02-16 11:02:26 -07001608 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001609 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001610
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001611 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001612 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001613 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001614
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001615 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001616 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001617 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001618
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001619 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001620 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001621 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001622 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001623 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001624 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001625 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001626 void *data;
1627
Dave Houlton5fa47912018-02-16 11:02:26 -07001628 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001629
Dave Houlton5fa47912018-02-16 11:02:26 -07001630 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001631 assert(!err);
1632
1633 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1634 fprintf(stderr, "Error loading texture: %s\n", filename);
1635 }
1636
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001637 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001638 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001639
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001640 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001641}
1642
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001643static void demo_destroy_texture(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001644 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001645 vkFreeMemory(demo->device, tex_objs->mem, NULL);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001646 if (tex_objs->image) vkDestroyImage(demo->device, tex_objs->image, NULL);
1647 if (tex_objs->buffer) vkDestroyBuffer(demo->device, tex_objs->buffer, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001648}
1649
Karl Schultze4dc75c2016-02-02 15:37:51 -07001650static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001651 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001652 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001653 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001654
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001655 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001656
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001657 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001658 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001659
Dave Houlton5fa47912018-02-16 11:02:26 -07001660 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001661 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001662 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1663 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001664 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1665 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001666 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1667 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001668 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001669 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001670 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001671 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001672
Tony Barbour16156cb2016-10-19 14:15:49 -06001673 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001674 demo_prepare_texture_buffer(demo, tex_files[i], &demo->staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001675
Dave Houlton5fa47912018-02-16 11:02:26 -07001676 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1677 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1678 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001679
Dave Houlton5fa47912018-02-16 11:02:26 -07001680 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1681 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001682 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001683
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001684 VkBufferImageCopy copy_region = {
1685 .bufferOffset = 0,
1686 .bufferRowLength = demo->staging_texture.tex_width,
1687 .bufferImageHeight = demo->staging_texture.tex_height,
1688 .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1689 .imageOffset = {0, 0, 0},
1690 .imageExtent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001691 };
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001692
1693 vkCmdCopyBufferToImage(demo->cmd, demo->staging_texture.buffer, demo->textures[i].image,
1694 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001695
Dave Houlton5fa47912018-02-16 11:02:26 -07001696 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1697 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001698 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001699
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001700 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001701 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1702 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001703 }
1704
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001705 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001706 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001708 .magFilter = VK_FILTER_NEAREST,
1709 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001710 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001711 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1712 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1713 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001714 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001715 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001716 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001717 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001718 .minLod = 0.0f,
1719 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001720 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001721 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001722 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001723
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001724 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001725 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001726 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001727 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001728 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001729 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001730 .components =
1731 {
Richard S. Wright Jrca51bc72021-01-06 13:03:18 -05001732 VK_COMPONENT_SWIZZLE_IDENTITY,
1733 VK_COMPONENT_SWIZZLE_IDENTITY,
1734 VK_COMPONENT_SWIZZLE_IDENTITY,
1735 VK_COMPONENT_SWIZZLE_IDENTITY,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001736 },
1737 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001738 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001739 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001740
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001741 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001742 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001743 assert(!err);
1744
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745 /* create image view */
1746 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001747 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001748 assert(!err);
1749 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001750}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751
Tony Barboura72d9492017-01-11 13:35:09 -07001752void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001753 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001754 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001755 VkMemoryAllocateInfo mem_alloc;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001756 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001757 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001758 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001759 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001760
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001761 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001762 mat4x4_mul(MVP, VP, demo->model_matrix);
1763 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001764 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001765
Karl Schultza2615462017-01-20 13:19:20 -07001766 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001767 data.position[i][0] = g_vertex_buffer_data[i * 3];
1768 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1769 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001770 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001771 data.attr[i][0] = g_uv_buffer_data[2 * i];
1772 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001773 data.attr[i][2] = 0;
1774 data.attr[i][3] = 0;
1775 }
1776
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001777 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001778 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001779 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001780 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001781
Tony Barboura72d9492017-01-11 13:35:09 -07001782 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001783 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001784 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001785
Dave Houlton5fa47912018-02-16 11:02:26 -07001786 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001787
Tony Barboura72d9492017-01-11 13:35:09 -07001788 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1789 mem_alloc.pNext = NULL;
1790 mem_alloc.allocationSize = mem_reqs.size;
1791 mem_alloc.memoryTypeIndex = 0;
1792
Dave Houlton5fa47912018-02-16 11:02:26 -07001793 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1794 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1795 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001796 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001797
Dave Houlton5fa47912018-02-16 11:02:26 -07001798 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001799 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001800
Tony-LunarG37af49f2019-12-19 12:04:19 -07001801 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, 0,
1802 &demo->swapchain_image_resources[i].uniform_memory_ptr);
Tony Barboura72d9492017-01-11 13:35:09 -07001803 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001804
Tony-LunarG37af49f2019-12-19 12:04:19 -07001805 memcpy(demo->swapchain_image_resources[i].uniform_memory_ptr, &data, sizeof data);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001806
Tony Barboura72d9492017-01-11 13:35:09 -07001807 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001808 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001809 assert(!err);
1810 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001811}
1812
Karl Schultze4dc75c2016-02-02 15:37:51 -07001813static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001814 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001815 [0] =
1816 {
1817 .binding = 0,
1818 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1819 .descriptorCount = 1,
1820 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1821 .pImmutableSamplers = NULL,
1822 },
1823 [1] =
1824 {
1825 .binding = 1,
1826 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1827 .descriptorCount = DEMO_TEXTURE_COUNT,
1828 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1829 .pImmutableSamplers = NULL,
1830 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001831 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001832 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001833 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001834 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001835 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001836 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001837 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001838 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001839
Dave Houlton5fa47912018-02-16 11:02:26 -07001840 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001841 assert(!err);
1842
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001843 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001844 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1845 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001846 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001847 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001848 };
1849
Dave Houlton5fa47912018-02-16 11:02:26 -07001850 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001851 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852}
1853
Karl Schultze4dc75c2016-02-02 15:37:51 -07001854static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001855 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1856 // because at the start of the renderpass, we don't care about their contents.
1857 // At the start of the subpass, the color attachment's layout will be transitioned
1858 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1859 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1860 // the renderpass, the color attachment's layout will be transitioned to
1861 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1862 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001863 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001864 [0] =
1865 {
1866 .format = demo->format,
1867 .flags = 0,
1868 .samples = VK_SAMPLE_COUNT_1_BIT,
1869 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1870 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1871 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1872 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1873 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1874 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1875 },
1876 [1] =
1877 {
1878 .format = demo->depth.format,
1879 .flags = 0,
1880 .samples = VK_SAMPLE_COUNT_1_BIT,
1881 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1882 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1883 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1884 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1885 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1886 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1887 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001888 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001889 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001890 .attachment = 0,
1891 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001892 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001893 const VkAttachmentReference depth_reference = {
1894 .attachment = 1,
1895 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1896 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001897 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001898 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1899 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001900 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001901 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001902 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001903 .pColorAttachments = &color_reference,
1904 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001905 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001906 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001907 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001908 };
Tony-LunarG495604b2019-06-13 15:32:05 -06001909
1910 VkSubpassDependency attachmentDependencies[2] = {
1911 [0] =
1912 {
1913 // Depth buffer is shared between swapchain images
1914 .srcSubpass = VK_SUBPASS_EXTERNAL,
1915 .dstSubpass = 0,
1916 .srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1917 .dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1918 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1919 .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1920 .dependencyFlags = 0,
1921 },
1922 [1] =
1923 {
1924 // Image Layout Transition
1925 .srcSubpass = VK_SUBPASS_EXTERNAL,
1926 .dstSubpass = 0,
1927 .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1928 .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1929 .srcAccessMask = 0,
1930 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
1931 .dependencyFlags = 0,
1932 },
1933 };
1934
Chia-I Wuf973e322015-07-08 13:34:24 +08001935 const VkRenderPassCreateInfo rp_info = {
1936 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1937 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001938 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001939 .attachmentCount = 2,
1940 .pAttachments = attachments,
1941 .subpassCount = 1,
1942 .pSubpasses = &subpass,
Tony-LunarG495604b2019-06-13 15:32:05 -06001943 .dependencyCount = 2,
1944 .pDependencies = attachmentDependencies,
Chia-I Wuf973e322015-07-08 13:34:24 +08001945 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001946 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001947
Chia-I Wu63636062015-10-26 21:10:41 +08001948 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001949 assert(!err);
1950}
1951
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001952static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001953 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001954 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001955 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001956
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001957 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1958 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001959 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001960 moduleCreateInfo.codeSize = size;
1961 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001962
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001963 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1964 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001965
1966 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001967}
1968
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001969static void demo_prepare_vs(struct demo *demo) {
1970 const uint32_t vs_code[] = {
1971#include "cube.vert.inc"
1972 };
1973 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001974}
1975
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001976static void demo_prepare_fs(struct demo *demo) {
1977 const uint32_t fs_code[] = {
1978#include "cube.frag.inc"
1979 };
1980 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001981}
1982
Karl Schultze4dc75c2016-02-02 15:37:51 -07001983static void demo_prepare_pipeline(struct demo *demo) {
Tony-LunarG8ee48dc2020-05-07 10:02:58 -06001984#define NUM_DYNAMIC_STATES 2 /*Viewport + Scissor*/
1985
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001986 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001987 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001988 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001989 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001990 VkPipelineRasterizationStateCreateInfo rs;
1991 VkPipelineColorBlendStateCreateInfo cb;
1992 VkPipelineDepthStencilStateCreateInfo ds;
1993 VkPipelineViewportStateCreateInfo vp;
1994 VkPipelineMultisampleStateCreateInfo ms;
Tony-LunarG8ee48dc2020-05-07 10:02:58 -06001995 VkDynamicState dynamicStateEnables[NUM_DYNAMIC_STATES];
Karl Schultze4dc75c2016-02-02 15:37:51 -07001996 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001997 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001998
Piers Daniellba839d12015-09-29 13:01:09 -06001999 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2000 memset(&dynamicState, 0, sizeof dynamicState);
2001 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2002 dynamicState.pDynamicStates = dynamicStateEnables;
2003
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002004 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002005 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002006 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002007
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002008 memset(&vi, 0, sizeof(vi));
2009 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2010
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002011 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002012 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002013 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002014
2015 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002016 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2017 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002018 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002019 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002020 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002021 rs.rasterizerDiscardEnable = VK_FALSE;
2022 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002023 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002024
2025 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002026 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2027 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002028 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002029 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002030 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002031 cb.attachmentCount = 1;
2032 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002033
Tony Barbour29645d02015-01-16 14:27:35 -07002034 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002035 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002036 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002037 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002038 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002039 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002040
2041 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002042 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002043 ds.depthTestEnable = VK_TRUE;
2044 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002045 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002046 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002047 ds.back.failOp = VK_STENCIL_OP_KEEP;
2048 ds.back.passOp = VK_STENCIL_OP_KEEP;
2049 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002050 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002051 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002052
Tony Barbour29645d02015-01-16 14:27:35 -07002053 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002054 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002055 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002056 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002057
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002058 demo_prepare_vs(demo);
2059 demo_prepare_fs(demo);
2060
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002061 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002062 VkPipelineShaderStageCreateInfo shaderStages[2];
2063 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2064
Karl Schultze4dc75c2016-02-02 15:37:51 -07002065 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2066 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002067 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002068 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002069
Karl Schultze4dc75c2016-02-02 15:37:51 -07002070 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2071 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002072 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002073 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002074
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002075 memset(&pipelineCache, 0, sizeof(pipelineCache));
2076 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002077
Dave Houlton5fa47912018-02-16 11:02:26 -07002078 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002079 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002080
Karl Schultze4dc75c2016-02-02 15:37:51 -07002081 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002082 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002083 pipeline.pRasterizationState = &rs;
2084 pipeline.pColorBlendState = &cb;
2085 pipeline.pMultisampleState = &ms;
2086 pipeline.pViewportState = &vp;
2087 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002088 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002089 pipeline.pStages = shaderStages;
2090 pipeline.renderPass = demo->render_pass;
2091 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002092
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002093 pipeline.renderPass = demo->render_pass;
2094
Dave Houlton5fa47912018-02-16 11:02:26 -07002095 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002096 assert(!err);
2097
Chia-I Wu63636062015-10-26 21:10:41 +08002098 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2099 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002100}
2101
Karl Schultze4dc75c2016-02-02 15:37:51 -07002102static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002103 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07002104 [0] =
2105 {
2106 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2107 .descriptorCount = demo->swapchainImageCount,
2108 },
2109 [1] =
2110 {
2111 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2112 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2113 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002114 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002115 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002116 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002117 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002118 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002119 .poolSizeCount = 2,
2120 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002121 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002122 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002123
Dave Houlton5fa47912018-02-16 11:02:26 -07002124 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002125 assert(!err);
2126}
2127
Karl Schultze4dc75c2016-02-02 15:37:51 -07002128static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002129 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002130 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002131 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002132
Dave Houlton5fa47912018-02-16 11:02:26 -07002133 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2134 .pNext = NULL,
2135 .descriptorPool = demo->desc_pool,
2136 .descriptorSetCount = 1,
2137 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002138
2139 VkDescriptorBufferInfo buffer_info;
2140 buffer_info.offset = 0;
2141 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002142
Chia-I Wuae721ba2015-05-25 16:27:55 +08002143 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002144 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002145 tex_descs[i].sampler = demo->textures[i].sampler;
2146 tex_descs[i].imageView = demo->textures[i].view;
Karl Schultze88bc842018-09-11 16:23:14 -06002147 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002148 }
2149
2150 memset(&writes, 0, sizeof(writes));
2151
2152 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002153 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002154 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002155 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002156
2157 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002158 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002159 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002160 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002161 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002162
Tony Barboura72d9492017-01-11 13:35:09 -07002163 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2164 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2165 assert(!err);
2166 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2167 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2168 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2169 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2170 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002171}
2172
Karl Schultze4dc75c2016-02-02 15:37:51 -07002173static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002174 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002175 attachments[1] = demo->depth.view;
2176
Chia-I Wuf973e322015-07-08 13:34:24 +08002177 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002178 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2179 .pNext = NULL,
2180 .renderPass = demo->render_pass,
2181 .attachmentCount = 2,
2182 .pAttachments = attachments,
2183 .width = demo->width,
2184 .height = demo->height,
2185 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002186 };
2187 VkResult U_ASSERT_ONLY err;
2188 uint32_t i;
2189
Tony Barbourd8e504f2015-10-08 14:26:24 -06002190 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002191 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002192 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002193 assert(!err);
2194 }
2195}
2196
Karl Schultze4dc75c2016-02-02 15:37:51 -07002197static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002198 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002199 if (demo->cmd_pool == VK_NULL_HANDLE) {
2200 const VkCommandPoolCreateInfo cmd_pool_info = {
2201 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2202 .pNext = NULL,
2203 .queueFamilyIndex = demo->graphics_queue_family_index,
2204 .flags = 0,
2205 };
2206 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2207 assert(!err);
2208 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002209
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002210 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002211 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002212 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002213 .commandPool = demo->cmd_pool,
2214 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002215 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002216 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002217 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2218 assert(!err);
2219 VkCommandBufferBeginInfo cmd_buf_info = {
2220 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2221 .pNext = NULL,
2222 .flags = 0,
2223 .pInheritanceInfo = NULL,
2224 };
2225 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2226 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002227
2228 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002229
2230 if (demo->is_minimized) {
2231 demo->prepared = false;
2232 return;
2233 }
2234
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002235 demo_prepare_depth(demo);
2236 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002237 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002238
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002239 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002240 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002241 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002242
Ian Elliotta0781972015-08-21 15:09:33 -06002243 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002244 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002245 assert(!err);
2246 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002247
Tony Barbour22e06272016-09-07 16:07:56 -06002248 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002249 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002250 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2251 .pNext = NULL,
2252 .queueFamilyIndex = demo->present_queue_family_index,
2253 .flags = 0,
2254 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002255 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002256 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002257 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002258 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2259 .pNext = NULL,
2260 .commandPool = demo->present_cmd_pool,
2261 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2262 .commandBufferCount = 1,
2263 };
2264 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002265 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2266 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002267 assert(!err);
2268 demo_build_image_ownership_cmd(demo, i);
2269 }
2270 }
2271
Chia-I Wu63ea9262015-03-26 13:14:16 +08002272 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002273 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002274
Chia-I Wuf973e322015-07-08 13:34:24 +08002275 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002276
Ian Elliotta0781972015-08-21 15:09:33 -06002277 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002278 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002279 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002280 }
2281
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002282 /*
2283 * Prepare functions above may generate pipeline commands
2284 * that need to be flushed before beginning the render loop.
2285 */
2286 demo_flush_init_cmd(demo);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06002287 if (demo->staging_texture.buffer) {
2288 demo_destroy_texture(demo, &demo->staging_texture);
Tony Barbour16156cb2016-10-19 14:15:49 -06002289 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002290
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002291 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002292 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002293}
2294
Karl Schultze4dc75c2016-02-02 15:37:51 -07002295static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002296 uint32_t i;
2297
2298 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002299 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002300
Tony Barbour66a56c32016-09-21 13:10:56 -06002301 // Wait for fences from present operations
2302 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002303 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002304 vkDestroyFence(demo->device, demo->fences[i], NULL);
2305 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2306 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2307 if (demo->separate_present_queue) {
2308 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2309 }
2310 }
2311
Jeremy Kniager602e4182018-01-19 10:52:34 -07002312 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2313 if (!demo->is_minimized) {
2314 for (i = 0; i < demo->swapchainImageCount; i++) {
2315 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2316 }
2317 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002318
Jeremy Kniager602e4182018-01-19 10:52:34 -07002319 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2320 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2321 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2322 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2323 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002324
Jeremy Kniager602e4182018-01-19 10:52:34 -07002325 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2326 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2327 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2328 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2329 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2330 }
2331 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002332
Jeremy Kniager602e4182018-01-19 10:52:34 -07002333 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2334 vkDestroyImage(demo->device, demo->depth.image, NULL);
2335 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002336
Jeremy Kniager602e4182018-01-19 10:52:34 -07002337 for (i = 0; i < demo->swapchainImageCount; i++) {
2338 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2339 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2340 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
Tony-LunarG37af49f2019-12-19 12:04:19 -07002341 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002342 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2343 }
2344 free(demo->swapchain_image_resources);
2345 free(demo->queue_props);
2346 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002347
Jeremy Kniager602e4182018-01-19 10:52:34 -07002348 if (demo->separate_present_queue) {
2349 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2350 }
Tony Barbour22e06272016-09-07 16:07:56 -06002351 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002352 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002353 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002354 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07002355 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002356 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002357 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002358
Tony Barbour153cb062016-12-07 13:43:36 -07002359#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002360 XDestroyWindow(demo->display, demo->xlib_window);
2361 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002362#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002363 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002364 xcb_disconnect(demo->connection);
2365 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002366#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002367 wl_keyboard_destroy(demo->keyboard);
2368 wl_pointer_destroy(demo->pointer);
2369 wl_seat_destroy(demo->seat);
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002370 wl_shell_surface_destroy(demo->shell_surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002371 wl_surface_destroy(demo->window);
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002372 wl_shell_destroy(demo->shell);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002373 wl_compositor_destroy(demo->compositor);
2374 wl_registry_destroy(demo->registry);
2375 wl_display_disconnect(demo->display);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02002376#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
2377 demo->event_buffer->Release(demo->event_buffer);
2378 demo->window->Release(demo->window);
2379 demo->dfb->Release(demo->dfb);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002380#endif
Karl Schultz86429112017-06-20 11:27:59 -06002381
2382 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002383}
2384
Karl Schultze4dc75c2016-02-02 15:37:51 -07002385static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002386 uint32_t i;
2387
Mike Stroyanbb60b382015-10-28 09:46:57 -06002388 // Don't react to resize until after first initialization.
2389 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002390 if (demo->is_minimized) {
2391 demo_prepare(demo);
2392 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002393 return;
2394 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002395 // In order to properly resize the window, we must re-create the swapchain
2396 // AND redo the command buffers, etc.
2397 //
2398 // First, perform part of the demo_cleanup() function:
2399 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002400 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002401
2402 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002403 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002404 }
Chia-I Wu63636062015-10-26 21:10:41 +08002405 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002406
Chia-I Wu63636062015-10-26 21:10:41 +08002407 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2408 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2409 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2410 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2411 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002412
2413 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002414 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2415 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2416 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2417 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002418 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002419
Chia-I Wu63636062015-10-26 21:10:41 +08002420 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2421 vkDestroyImage(demo->device, demo->depth.image, NULL);
2422 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002423
Ian Elliottcf074d32015-10-16 18:02:43 -06002424 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002425 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002426 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002427 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
Tony-LunarG37af49f2019-12-19 12:04:19 -07002428 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07002429 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002430 }
Chia-I Wu63636062015-10-26 21:10:41 +08002431 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002432 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002433 if (demo->separate_present_queue) {
2434 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2435 }
Tony Barboura72d9492017-01-11 13:35:09 -07002436 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002437
Ian Elliottcf074d32015-10-16 18:02:43 -06002438 // Second, re-perform the demo_prepare() function, which will re-create the
2439 // swapchain:
2440 demo_prepare(demo);
2441}
2442
David Pinedo2bb7c932015-06-18 17:03:14 -06002443// On MS-Windows, make this a global, so it's available to WndProc()
2444struct demo demo;
2445
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002446#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002447static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002448 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002449
Ian Elliott639ca472015-04-16 15:23:05 -06002450 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002451 demo->curFrame++;
Petr Krausd88e4e52019-01-23 18:45:04 +01002452 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002453 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002454 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002455}
Ian Elliott639ca472015-04-16 15:23:05 -06002456
2457// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002458LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2459 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002460 case WM_CLOSE:
2461 PostQuitMessage(validation_error);
2462 break;
2463 case WM_PAINT:
2464 // The validation callback calls MessageBox which can generate paint
2465 // events - don't make more Vulkan calls if we got here from the
2466 // callback
2467 if (!in_callback) {
2468 demo_run(&demo);
2469 }
2470 break;
2471 case WM_GETMINMAXINFO: // set window's minimum size
2472 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2473 return 0;
Aaron Hagan500b9c32018-10-03 21:56:29 -04002474 case WM_ERASEBKGND:
2475 return 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002476 case WM_SIZE:
2477 // Resize the application to the new window size, except when
2478 // it was minimized. Vulkan doesn't support images or swapchains
2479 // with width=0 and height=0.
2480 if (wParam != SIZE_MINIMIZED) {
2481 demo.width = lParam & 0xffff;
2482 demo.height = (lParam & 0xffff0000) >> 16;
2483 demo_resize(&demo);
2484 }
2485 break;
Petr Krausd88e4e52019-01-23 18:45:04 +01002486 case WM_KEYDOWN:
2487 switch (wParam) {
2488 case VK_ESCAPE:
2489 PostQuitMessage(validation_error);
2490 break;
2491 case VK_LEFT:
2492 demo.spin_angle -= demo.spin_increment;
2493 break;
2494 case VK_RIGHT:
2495 demo.spin_angle += demo.spin_increment;
2496 break;
2497 case VK_SPACE:
2498 demo.pause = !demo.pause;
2499 break;
2500 }
2501 return 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002502 default:
2503 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002504 }
2505 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2506}
2507
Karl Schultze4dc75c2016-02-02 15:37:51 -07002508static void demo_create_window(struct demo *demo) {
2509 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002510
2511 // Initialize the window class structure:
2512 win_class.cbSize = sizeof(WNDCLASSEX);
2513 win_class.style = CS_HREDRAW | CS_VREDRAW;
2514 win_class.lpfnWndProc = WndProc;
2515 win_class.cbClsExtra = 0;
2516 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002517 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002518 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2519 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2520 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2521 win_class.lpszMenuName = NULL;
2522 win_class.lpszClassName = demo->name;
2523 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2524 // Register window class:
2525 if (!RegisterClassEx(&win_class)) {
2526 // It didn't work, so try to give a useful error:
2527 printf("Unexpected error trying to start the application!\n");
2528 fflush(stdout);
2529 exit(1);
2530 }
2531 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002532 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002533 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002534 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002535 demo->name, // class name
2536 demo->name, // app name
2537 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002538 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002539 100, 100, // x/y coords
2540 wr.right - wr.left, // width
2541 wr.bottom - wr.top, // height
2542 NULL, // handle to parent
2543 NULL, // handle to menu
2544 demo->connection, // hInstance
2545 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002546 if (!demo->window) {
2547 // It didn't work, so try to give a useful error:
2548 printf("Cannot create a window in which to draw!\n");
2549 fflush(stdout);
2550 exit(1);
2551 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002552 // Window client area size must be at least 1 pixel high, to prevent crash.
2553 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002554 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002555}
Tony Barbour8295ac42016-08-08 11:04:17 -06002556#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002557static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002558 const char *display_envar = getenv("DISPLAY");
2559 if (display_envar == NULL || display_envar[0] == '\0') {
2560 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2561 fflush(stdout);
2562 exit(1);
2563 }
Tony Barbour2593b182016-04-19 10:57:58 -06002564
Tony Barbouree9cd372017-01-31 16:09:58 -07002565 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002566 demo->display = XOpenDisplay(NULL);
2567 long visualMask = VisualScreenMask;
2568 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002569 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002570 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002571 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002572
Dave Houlton5fa47912018-02-16 11:02:26 -07002573 Colormap colormap =
2574 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002575
Dave Houlton5fa47912018-02-16 11:02:26 -07002576 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002577 windowAttributes.colormap = colormap;
2578 windowAttributes.background_pixel = 0xFFFFFFFF;
2579 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002580 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002581
Dave Houlton5fa47912018-02-16 11:02:26 -07002582 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2583 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2584 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002585
2586 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2587 XMapWindow(demo->display, demo->xlib_window);
2588 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002589 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002590}
2591static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002592 switch (event->type) {
2593 case ClientMessage:
2594 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002595 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002596 case KeyPress:
2597 switch (event->xkey.keycode) {
2598 case 0x9: // Escape
2599 demo->quit = true;
2600 break;
2601 case 0x71: // left arrow key
2602 demo->spin_angle -= demo->spin_increment;
2603 break;
2604 case 0x72: // right arrow key
2605 demo->spin_angle += demo->spin_increment;
2606 break;
2607 case 0x41: // space bar
2608 demo->pause = !demo->pause;
2609 break;
2610 }
Tony Barbour2593b182016-04-19 10:57:58 -06002611 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002612 case ConfigureNotify:
2613 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2614 demo->width = event->xconfigure.width;
2615 demo->height = event->xconfigure.height;
2616 demo_resize(demo);
2617 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002618 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002619 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002620 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002621 }
Tony Barbour2593b182016-04-19 10:57:58 -06002622}
2623
2624static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002625 while (!demo->quit) {
2626 XEvent event;
2627
2628 if (demo->pause) {
2629 XNextEvent(demo->display, &event);
2630 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002631 }
2632 while (XPending(demo->display) > 0) {
2633 XNextEvent(demo->display, &event);
2634 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002635 }
2636
Tony Barbour2593b182016-04-19 10:57:58 -06002637 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002638 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002639 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002640 }
2641}
Tony Barbour153cb062016-12-07 13:43:36 -07002642#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002643static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002644 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002645 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002646 case XCB_EXPOSE:
2647 // TODO: Resize window
2648 break;
2649 case XCB_CLIENT_MESSAGE:
2650 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2651 demo->quit = true;
2652 }
2653 break;
2654 case XCB_KEY_RELEASE: {
2655 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002656
Dave Houlton5fa47912018-02-16 11:02:26 -07002657 switch (key->detail) {
2658 case 0x9: // Escape
2659 demo->quit = true;
2660 break;
2661 case 0x71: // left arrow key
2662 demo->spin_angle -= demo->spin_increment;
2663 break;
2664 case 0x72: // right arrow key
2665 demo->spin_angle += demo->spin_increment;
2666 break;
2667 case 0x41: // space bar
2668 demo->pause = !demo->pause;
2669 break;
2670 }
2671 } break;
2672 case XCB_CONFIGURE_NOTIFY: {
2673 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2674 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2675 demo->width = cfg->width;
2676 demo->height = cfg->height;
2677 demo_resize(demo);
2678 }
2679 } break;
2680 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002681 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002682 }
2683}
2684
Tony Barbour2593b182016-04-19 10:57:58 -06002685static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002686 xcb_flush(demo->connection);
2687
2688 while (!demo->quit) {
2689 xcb_generic_event_t *event;
2690
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002691 if (demo->pause) {
2692 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002693 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002694 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002695 }
2696 while (event) {
2697 demo_handle_xcb_event(demo, event);
2698 free(event);
2699 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002700 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002701
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002702 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002703 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002704 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002705 }
2706}
2707
Tony Barbour2593b182016-04-19 10:57:58 -06002708static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002709 uint32_t value_mask, value_list[32];
2710
Tony Barbour2593b182016-04-19 10:57:58 -06002711 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002712
2713 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2714 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002715 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002716
Dave Houlton5fa47912018-02-16 11:02:26 -07002717 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2718 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002719
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002720 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002721 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2722 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002723
Dave Houlton5fa47912018-02-16 11:02:26 -07002724 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2725 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002726
Dave Houlton5fa47912018-02-16 11:02:26 -07002727 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002728 &(*demo->atom_wm_delete_window).atom);
2729 free(reply);
2730
Tony Barbour2593b182016-04-19 10:57:58 -06002731 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002732
Karl Schultze4dc75c2016-02-02 15:37:51 -07002733 // Force the x/y coordinates to 100,100 results are identical in consecutive
2734 // runs
2735 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002736 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002737}
Tony Barbour6b131662016-08-25 13:14:45 -06002738// VK_USE_PLATFORM_XCB_KHR
2739#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002740static void demo_run(struct demo *demo) {
2741 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002742 if (demo->pause) {
2743 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002744 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002745 wl_display_dispatch_pending(demo->display); // don't block
2746 demo_draw(demo);
2747 demo->curFrame++;
2748 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2749 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002750 }
2751}
2752
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002753static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) {
2754 wl_shell_surface_pong(shell_surface, serial);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002755}
2756
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002757static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED,
2758 int32_t width UNUSED, int32_t height UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002759
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002760static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002761
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002762static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002763
2764static void demo_create_window(struct demo *demo) {
2765 demo->window = wl_compositor_create_surface(demo->compositor);
2766 if (!demo->window) {
2767 printf("Can not create wayland_surface from compositor!\n");
2768 fflush(stdout);
2769 exit(1);
2770 }
2771
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002772 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2773 if (!demo->shell_surface) {
2774 printf("Can not get shell_surface from wayland_surface!\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002775 fflush(stdout);
2776 exit(1);
2777 }
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07002778 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo);
2779 wl_shell_surface_set_toplevel(demo->shell_surface);
2780 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002781}
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02002782#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
2783static void demo_create_directfb_window(struct demo *demo) {
2784 DFBResult ret;
2785
2786 ret = DirectFBInit(NULL, NULL);
2787 if (ret) {
2788 printf("DirectFBInit failed to initialize DirectFB!\n");
2789 fflush(stdout);
2790 exit(1);
2791 }
2792
2793 ret = DirectFBCreate(&demo->dfb);
2794 if (ret) {
2795 printf("DirectFBCreate failed to create main interface of DirectFB!\n");
2796 fflush(stdout);
2797 exit(1);
2798 }
2799
2800 DFBSurfaceDescription desc;
2801 desc.flags = DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT;
2802 desc.caps = DSCAPS_PRIMARY;
2803 desc.width = demo->width;
2804 desc.height = demo->height;
2805 ret = demo->dfb->CreateSurface(demo->dfb, &desc, &demo->window);
2806 if (ret) {
2807 printf("CreateSurface failed to create DirectFB surface interface!\n");
2808 fflush(stdout);
2809 exit(1);
2810 }
2811
2812 ret = demo->dfb->CreateInputEventBuffer(demo->dfb, DICAPS_KEYS, DFB_FALSE, &demo->event_buffer);
2813 if (ret) {
2814 printf("CreateInputEventBuffer failed to create DirectFB event buffer interface!\n");
2815 fflush(stdout);
2816 exit(1);
2817 }
2818}
2819
2820static void demo_handle_directfb_event(struct demo *demo, const DFBInputEvent *event) {
2821 if (event->type != DIET_KEYPRESS) return;
2822 switch (event->key_symbol) {
2823 case DIKS_ESCAPE: // Escape
2824 demo->quit = true;
2825 break;
2826 case DIKS_CURSOR_LEFT: // left arrow key
2827 demo->spin_angle -= demo->spin_increment;
2828 break;
2829 case DIKS_CURSOR_RIGHT: // right arrow key
2830 demo->spin_angle += demo->spin_increment;
2831 break;
2832 case DIKS_SPACE: // space bar
2833 demo->pause = !demo->pause;
2834 break;
2835 default:
2836 break;
2837 }
2838}
2839
2840static void demo_run_directfb(struct demo *demo) {
2841 while (!demo->quit) {
2842 DFBInputEvent event;
2843
2844 if (demo->pause) {
2845 demo->event_buffer->WaitForEvent(demo->event_buffer);
Nicolas Caramelli6d4f1c62020-07-13 18:10:07 +02002846 if (!demo->event_buffer->GetEvent(demo->event_buffer, DFB_EVENT(&event))) demo_handle_directfb_event(demo, &event);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02002847 } else {
Nicolas Caramelli6d4f1c62020-07-13 18:10:07 +02002848 if (!demo->event_buffer->GetEvent(demo->event_buffer, DFB_EVENT(&event))) demo_handle_directfb_event(demo, &event);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02002849
2850 demo_draw(demo);
2851 demo->curFrame++;
2852 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2853 }
2854 }
2855}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002856#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2857static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002858 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002859
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002860 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002861 demo->curFrame++;
2862}
Bill Hollings0a0625a2019-07-15 17:39:18 -04002863#elif defined(VK_USE_PLATFORM_METAL_EXT)
Karl Schultz206b1c52018-04-13 18:02:07 -06002864static void demo_run(struct demo *demo) {
2865 demo_draw(demo);
2866 demo->curFrame++;
2867 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2868 demo->quit = TRUE;
2869 }
2870}
Damien Leone600c3052017-01-31 10:26:07 -07002871#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2872static VkResult demo_create_display_surface(struct demo *demo) {
2873 VkResult U_ASSERT_ONLY err;
2874 uint32_t display_count;
2875 uint32_t mode_count;
2876 uint32_t plane_count;
2877 VkDisplayPropertiesKHR display_props;
2878 VkDisplayKHR display;
2879 VkDisplayModePropertiesKHR mode_props;
2880 VkDisplayPlanePropertiesKHR *plane_props;
2881 VkBool32 found_plane = VK_FALSE;
2882 uint32_t plane_index;
2883 VkExtent2D image_extent;
2884 VkDisplaySurfaceCreateInfoKHR create_info;
2885
2886 // Get the first display
2887 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2888 assert(!err);
2889
2890 if (display_count == 0) {
2891 printf("Cannot find any display!\n");
2892 fflush(stdout);
2893 exit(1);
2894 }
2895
2896 display_count = 1;
2897 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2898 assert(!err || (err == VK_INCOMPLETE));
2899
2900 display = display_props.display;
2901
2902 // Get the first mode of the display
2903 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2904 assert(!err);
2905
2906 if (mode_count == 0) {
2907 printf("Cannot find any mode for the display!\n");
2908 fflush(stdout);
2909 exit(1);
2910 }
2911
2912 mode_count = 1;
2913 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2914 assert(!err || (err == VK_INCOMPLETE));
2915
2916 // Get the list of planes
2917 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2918 assert(!err);
2919
2920 if (plane_count == 0) {
2921 printf("Cannot find any plane!\n");
2922 fflush(stdout);
2923 exit(1);
2924 }
2925
2926 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2927 assert(plane_props);
2928
2929 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2930 assert(!err);
2931
2932 // Find a plane compatible with the display
2933 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2934 uint32_t supported_count;
2935 VkDisplayKHR *supported_displays;
2936
2937 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002938 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002939 continue;
2940 }
2941
2942 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2943 assert(!err);
2944
2945 if (supported_count == 0) {
2946 continue;
2947 }
2948
2949 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2950 assert(supported_displays);
2951
2952 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2953 assert(!err);
2954
2955 for (uint32_t i = 0; i < supported_count; i++) {
2956 if (supported_displays[i] == display) {
2957 found_plane = VK_TRUE;
2958 break;
2959 }
2960 }
2961
2962 free(supported_displays);
2963
2964 if (found_plane) {
2965 break;
2966 }
2967 }
2968
2969 if (!found_plane) {
2970 printf("Cannot find a plane compatible with the display!\n");
2971 fflush(stdout);
2972 exit(1);
2973 }
2974
2975 free(plane_props);
2976
Tony Barbour820b55b2017-03-14 08:55:46 -06002977 VkDisplayPlaneCapabilitiesKHR planeCaps;
2978 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2979 // Find a supported alpha mode
Mark Young66510e52017-11-10 10:05:14 -07002980 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2981 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002982 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2983 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2984 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2985 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2986 };
2987 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2988 if (planeCaps.supportedAlpha & alphaModes[i]) {
2989 alphaMode = alphaModes[i];
2990 break;
2991 }
2992 }
Damien Leone600c3052017-01-31 10:26:07 -07002993 image_extent.width = mode_props.parameters.visibleRegion.width;
2994 image_extent.height = mode_props.parameters.visibleRegion.height;
2995
2996 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2997 create_info.pNext = NULL;
2998 create_info.flags = 0;
2999 create_info.displayMode = mode_props.displayMode;
3000 create_info.planeIndex = plane_index;
3001 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
3002 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06003003 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07003004 create_info.globalAlpha = 1.0f;
3005 create_info.imageExtent = image_extent;
3006
3007 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
3008}
3009
Dave Houlton5fa47912018-02-16 11:02:26 -07003010static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07003011 while (!demo->quit) {
3012 demo_draw(demo);
3013 demo->curFrame++;
3014
3015 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
3016 demo->quit = true;
3017 }
3018 }
3019}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003020#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003021
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003022/*
3023 * Return 1 (true) if all layer names specified in check_names
3024 * can be found in given layer properties.
3025 */
Dave Houlton5fa47912018-02-16 11:02:26 -07003026static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003027 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003028 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003029 for (uint32_t j = 0; j < layer_count; j++) {
3030 if (!strcmp(check_names[i], layers[j].layerName)) {
3031 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07003032 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003033 }
3034 }
3035 if (!found) {
3036 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
3037 return 0;
3038 }
3039 }
3040 return 1;
3041}
3042
Karl Schultze4dc75c2016-02-02 15:37:51 -07003043static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003044 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003045 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003046 uint32_t instance_layer_count = 0;
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003047 char *instance_validation_layers[] = {"VK_LAYER_KHRONOS_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06003048 demo->enabled_extension_count = 0;
3049 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07003050 demo->is_minimized = false;
3051 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003052
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003053 // Look for validation layers
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003054 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003055 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003056 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07003057 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003058
Karl Schultz7c50ad62016-04-01 12:07:03 -06003059 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003060 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
3061 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06003062 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003063
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003064 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
Dave Houlton5fa47912018-02-16 11:02:26 -07003065 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06003066 if (validation_found) {
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003067 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
3068 demo->enabled_layers[0] = "VK_LAYER_KHRONOS_validation";
Karl Schultz7c50ad62016-04-01 12:07:03 -06003069 }
3070 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003071 }
Dustin Graves7c287352016-01-27 13:48:06 -07003072
Karl Schultz7c50ad62016-04-01 12:07:03 -06003073 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003074 ERR_EXIT(
3075 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
3076 "Please look at the Getting Started guide for additional information.\n",
3077 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06003078 }
Dustin Graves7c287352016-01-27 13:48:06 -07003079 }
3080
3081 /* Look for instance extensions */
3082 VkBool32 surfaceExtFound = 0;
3083 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003084 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003085
Dave Houlton5fa47912018-02-16 11:02:26 -07003086 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003087 assert(!err);
3088
Dustin Graves7c287352016-01-27 13:48:06 -07003089 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003090 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3091 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003092 assert(!err);
3093 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003094 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003095 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003096 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003097 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003098#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003099 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003100 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003101 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003102 }
Tony Barbour153cb062016-12-07 13:43:36 -07003103#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003104 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06003105 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003106 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06003107 }
Tony Barbour153cb062016-12-07 13:43:36 -07003108#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003109 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003110 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003111 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003112 }
Tony Barbour153cb062016-12-07 13:43:36 -07003113#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003114 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003115 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003116 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003117 }
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02003118#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
3119 if (!strcmp(VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3120 platformSurfaceExtFound = 1;
3121 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME;
3122 }
Damien Leone600c3052017-01-31 10:26:07 -07003123#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003124 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07003125 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003126 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07003127 }
Tony Barbour153cb062016-12-07 13:43:36 -07003128#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003129 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003130 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003131 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003132 }
Bill Hollings0a0625a2019-07-15 17:39:18 -04003133#elif defined(VK_USE_PLATFORM_METAL_EXT)
3134 if (!strcmp(VK_EXT_METAL_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Bill Hollingsf4352142017-02-14 22:58:56 -05003135 platformSurfaceExtFound = 1;
Bill Hollings0a0625a2019-07-15 17:39:18 -04003136 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_METAL_SURFACE_EXTENSION_NAME;
Bill Hollingsf4352142017-02-14 22:58:56 -05003137 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003138#endif
Richard S. Wright Jrca51bc72021-01-06 13:03:18 -05003139 if (!strcmp(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3140 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME;
3141 }
Mark Young66510e52017-11-10 10:05:14 -07003142 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3143 if (demo->validate) {
3144 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3145 }
3146 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003147 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003148 }
Dustin Graves7c287352016-01-27 13:48:06 -07003149
3150 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003151 }
Dustin Graves7c287352016-01-27 13:48:06 -07003152
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003153 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003154 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3155 " extension.\n\n"
3156 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3157 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06003158 "vkCreateInstance Failure");
3159 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003160 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003161#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003162 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3163 " extension.\n\n"
3164 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3165 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003166 "vkCreateInstance Failure");
Bill Hollings0a0625a2019-07-15 17:39:18 -04003167#elif defined(VK_USE_PLATFORM_METAL_EXT)
3168 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_METAL_SURFACE_EXTENSION_NAME
Dave Houlton5fa47912018-02-16 11:02:26 -07003169 " extension.\n\n"
3170 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3171 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003172 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003173#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003174 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3175 " extension.\n\n"
3176 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3177 "Please look at the Getting Started guide for additional information.\n",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003178 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003179#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003180 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3181 " extension.\n\n"
3182 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3183 "Please look at the Getting Started guide for additional information.\n",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003184 "vkCreateInstance Failure");
Damien Leone600c3052017-01-31 10:26:07 -07003185#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003186 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3187 " extension.\n\n"
3188 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3189 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07003190 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003191#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003192 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3193 " extension.\n\n"
3194 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3195 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003196 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003197#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003198 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3199 " extension.\n\n"
3200 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3201 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06003202 "vkCreateInstance Failure");
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02003203#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
3204 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME
3205 " extension.\n\n"
3206 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3207 "Please look at the Getting Started guide for additional information.\n",
3208 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003209#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003210 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003211 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003212 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003213 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003214 .pApplicationName = APP_SHORT_NAME,
3215 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003216 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003217 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003218 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003219 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003220 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003221 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003222 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003223 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003224 .enabledLayerCount = demo->enabled_layer_count,
3225 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3226 .enabledExtensionCount = demo->enabled_extension_count,
3227 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003228 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003229
3230 /*
3231 * This is info for a temp callback to use during CreateInstance.
3232 * After the instance is created, we use the instance-based
3233 * function to register the final callback.
3234 */
Mark Young66510e52017-11-10 10:05:14 -07003235 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003236 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003237 // VK_EXT_debug_utils style
3238 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3239 dbg_messenger_create_info.pNext = NULL;
3240 dbg_messenger_create_info.flags = 0;
3241 dbg_messenger_create_info.messageSeverity =
3242 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3243 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3244 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3245 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3246 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3247 dbg_messenger_create_info.pUserData = demo;
3248 inst_info.pNext = &dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003249 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003250
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003251 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003252
Chia-I Wu63636062015-10-26 21:10:41 +08003253 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003254 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003255 ERR_EXIT(
3256 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3257 "Please look at the Getting Started guide for additional information.\n",
3258 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003259 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003260 ERR_EXIT(
3261 "Cannot find a specified extension library.\n"
3262 "Make sure your layers path is set appropriately.\n",
3263 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003264 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003265 ERR_EXIT(
3266 "vkCreateInstance failed.\n\n"
3267 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3268 "Please look at the Getting Started guide for additional information.\n",
3269 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003270 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003271
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003272 /* Make initial call to query gpu_count, then second call for gpu info*/
3273 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
Petr Kraus2f1dbdb2018-04-14 14:45:17 +02003274 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003275
3276 if (gpu_count > 0) {
3277 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3278 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3279 assert(!err);
Tony-LunarG50e737c2020-07-16 14:26:03 -06003280 if (demo->gpu_number > gpu_count - 1) {
3281 fprintf(stderr, "Gpu %u specified is not present, gpu count = %u\n", demo->gpu_number, gpu_count);
3282 fprintf(stderr, "Continuing with gpu 0\n");
3283 demo->gpu_number = 0;
3284 }
3285 demo->gpu = physical_devices[demo->gpu_number];
Dustin Graves7c287352016-01-27 13:48:06 -07003286 free(physical_devices);
3287 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003288 ERR_EXIT(
3289 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3290 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3291 "Please look at the Getting Started guide for additional information.\n",
3292 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003293 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003294
Dustin Graves7c287352016-01-27 13:48:06 -07003295 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003296 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003297 VkBool32 swapchainExtFound = 0;
3298 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003299 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003300
Dave Houlton5fa47912018-02-16 11:02:26 -07003301 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003302 assert(!err);
3303
Dustin Graves7c287352016-01-27 13:48:06 -07003304 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003305 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3306 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003307 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003308
Dustin Graves7c287352016-01-27 13:48:06 -07003309 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003310 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003311 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003312 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003313 }
Richard S. Wright Jra7825742021-01-06 16:02:12 -05003314 if (!strcmp("VK_KHR_portability_subset", device_extensions[i].extensionName)) {
3315 demo->extension_names[demo->enabled_extension_count++] = "VK_KHR_portability_subset";
Richard S. Wright Jrca51bc72021-01-06 13:03:18 -05003316 }
Dustin Graves7c287352016-01-27 13:48:06 -07003317 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003318 }
Dustin Graves7c287352016-01-27 13:48:06 -07003319
Ian Elliott53622a72017-03-28 11:06:33 -06003320 if (demo->VK_KHR_incremental_present_enabled) {
3321 // Even though the user "enabled" the extension via the command
3322 // line, we must make sure that it's enumerated for use with the
3323 // device. Therefore, disable it here, and re-enable it again if
3324 // enumerated.
3325 demo->VK_KHR_incremental_present_enabled = false;
3326 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003327 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3328 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003329 demo->VK_KHR_incremental_present_enabled = true;
3330 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3331 }
3332 assert(demo->enabled_extension_count < 64);
3333 }
3334 if (!demo->VK_KHR_incremental_present_enabled) {
3335 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3336 }
3337 }
3338
Ian Elliottd940ca22017-03-22 08:31:27 -06003339 if (demo->VK_GOOGLE_display_timing_enabled) {
3340 // Even though the user "enabled" the extension via the command
3341 // line, we must make sure that it's enumerated for use with the
3342 // device. Therefore, disable it here, and re-enable it again if
3343 // enumerated.
3344 demo->VK_GOOGLE_display_timing_enabled = false;
3345 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003346 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3347 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003348 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003349 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003350 }
3351 assert(demo->enabled_extension_count < 64);
3352 }
3353 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003354 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003355 }
3356 }
3357
Dustin Graves7c287352016-01-27 13:48:06 -07003358 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003359 }
Dustin Graves7c287352016-01-27 13:48:06 -07003360
Tony Barbourcc69f452015-10-08 13:59:42 -06003361 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003362 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3363 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3364 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003365 "vkCreateInstance Failure");
3366 }
3367
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003368 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003369 // Setup VK_EXT_debug_utils function pointers always (we use them for
3370 // debug labels and names).
3371 demo->CreateDebugUtilsMessengerEXT =
3372 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3373 demo->DestroyDebugUtilsMessengerEXT =
3374 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3375 demo->SubmitDebugUtilsMessageEXT =
3376 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3377 demo->CmdBeginDebugUtilsLabelEXT =
3378 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3379 demo->CmdEndDebugUtilsLabelEXT =
3380 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3381 demo->CmdInsertDebugUtilsLabelEXT =
3382 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3383 demo->SetDebugUtilsObjectNameEXT =
3384 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3385 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3386 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3387 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3388 NULL == demo->SetDebugUtilsObjectNameEXT) {
3389 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003390 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003391
Mark Young66510e52017-11-10 10:05:14 -07003392 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003393 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003394 case VK_SUCCESS:
3395 break;
3396 case VK_ERROR_OUT_OF_HOST_MEMORY:
Mark Young66510e52017-11-10 10:05:14 -07003397 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003398 break;
3399 default:
Mark Young66510e52017-11-10 10:05:14 -07003400 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003401 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003402 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003403 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003404 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003405
3406 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003407 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003408 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003409
Dave Houlton5fa47912018-02-16 11:02:26 -07003410 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3411 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003412
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003413 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003414 // If app has specific feature requirements it should check supported
3415 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003416 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003417 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003418
Tony Barbourda2bad52016-01-22 14:36:40 -07003419 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3420 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3421 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3422 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3423 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3424}
3425
Karl Schultze4dc75c2016-02-02 15:37:51 -07003426static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003427 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003428 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003429 VkDeviceQueueCreateInfo queues[2];
3430 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3431 queues[0].pNext = NULL;
3432 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3433 queues[0].queueCount = 1;
3434 queues[0].pQueuePriorities = queue_priorities;
3435 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003436
3437 VkDeviceCreateInfo device = {
3438 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3439 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003440 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003441 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003442 .enabledLayerCount = 0,
3443 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003444 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003445 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003446 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003447 };
Tony Barbour22e06272016-09-07 16:07:56 -06003448 if (demo->separate_present_queue) {
3449 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3450 queues[1].pNext = NULL;
3451 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3452 queues[1].queueCount = 1;
3453 queues[1].pQueuePriorities = queue_priorities;
3454 queues[1].flags = 0;
3455 device.queueCreateInfoCount = 2;
3456 }
Chia-I Wu63636062015-10-26 21:10:41 +08003457 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003458 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003459}
3460
Tony-LunarG6cebf142019-09-11 14:32:23 -06003461static void demo_create_surface(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003462 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003463
Karl Schultze4dc75c2016-02-02 15:37:51 -07003464// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003465#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003466 VkWin32SurfaceCreateInfoKHR createInfo;
3467 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3468 createInfo.pNext = NULL;
3469 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003470 createInfo.hinstance = demo->connection;
3471 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003472
Dave Houlton5fa47912018-02-16 11:02:26 -07003473 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003474#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003475 VkWaylandSurfaceCreateInfoKHR createInfo;
3476 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3477 createInfo.pNext = NULL;
3478 createInfo.flags = 0;
3479 createInfo.display = demo->display;
3480 createInfo.surface = demo->window;
3481
Dave Houlton5fa47912018-02-16 11:02:26 -07003482 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003483#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3484 VkAndroidSurfaceCreateInfoKHR createInfo;
3485 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3486 createInfo.pNext = NULL;
3487 createInfo.flags = 0;
Mike Schuchardt837d5162018-03-08 12:57:02 -07003488 createInfo.window = (struct ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003489
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003490 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003491#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3492 VkXlibSurfaceCreateInfoKHR createInfo;
3493 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3494 createInfo.pNext = NULL;
3495 createInfo.flags = 0;
3496 createInfo.dpy = demo->display;
3497 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003498
Dave Houlton5fa47912018-02-16 11:02:26 -07003499 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003500#elif defined(VK_USE_PLATFORM_XCB_KHR)
3501 VkXcbSurfaceCreateInfoKHR createInfo;
3502 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3503 createInfo.pNext = NULL;
3504 createInfo.flags = 0;
3505 createInfo.connection = demo->connection;
3506 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003507
Tony Barbour153cb062016-12-07 13:43:36 -07003508 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02003509#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
3510 VkDirectFBSurfaceCreateInfoEXT createInfo;
3511 createInfo.sType = VK_STRUCTURE_TYPE_DIRECTFB_SURFACE_CREATE_INFO_EXT;
3512 createInfo.pNext = NULL;
3513 createInfo.flags = 0;
3514 createInfo.dfb = demo->dfb;
3515 createInfo.surface = demo->window;
3516
3517 err = vkCreateDirectFBSurfaceEXT(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003518#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3519 err = demo_create_display_surface(demo);
Bill Hollings0a0625a2019-07-15 17:39:18 -04003520#elif defined(VK_USE_PLATFORM_METAL_EXT)
3521 VkMetalSurfaceCreateInfoEXT surface;
3522 surface.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
Bill Hollingsf4352142017-02-14 22:58:56 -05003523 surface.pNext = NULL;
3524 surface.flags = 0;
Bill Hollings0a0625a2019-07-15 17:39:18 -04003525 surface.pLayer = demo->caMetalLayer;
Bill Hollingsf4352142017-02-14 22:58:56 -05003526
Bill Hollings0a0625a2019-07-15 17:39:18 -04003527 err = vkCreateMetalSurfaceEXT(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003528#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003529 assert(!err);
Tony-LunarG6cebf142019-09-11 14:32:23 -06003530}
3531
Joshua Ashton1fbefe02020-04-02 06:45:48 +01003532static VkSurfaceFormatKHR pick_surface_format(const VkSurfaceFormatKHR *surfaceFormats, uint32_t count) {
3533 // Prefer non-SRGB formats...
3534 for (uint32_t i = 0; i < count; i++) {
3535 const VkFormat format = surfaceFormats[i].format;
3536
3537 if (format == VK_FORMAT_R8G8B8A8_UNORM || format == VK_FORMAT_B8G8R8A8_UNORM ||
3538 format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 || format == VK_FORMAT_A2R10G10B10_UNORM_PACK32 ||
3539 format == VK_FORMAT_R16G16B16A16_SFLOAT) {
3540 return surfaceFormats[i];
3541 }
3542 }
3543
3544 printf("Can't find our preferred formats... Falling back to first exposed format. Rendering may be incorrect.\n");
3545
3546 assert(count >= 1);
3547 return surfaceFormats[0];
3548}
3549
Tony-LunarG6cebf142019-09-11 14:32:23 -06003550static void demo_init_vk_swapchain(struct demo *demo) {
3551 VkResult U_ASSERT_ONLY err;
3552
3553 demo_create_surface(demo);
Ian Elliottaaae5352015-07-06 14:27:58 -06003554
Tony Barbourcc69f452015-10-08 13:59:42 -06003555 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003556 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003557 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003558 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003559 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003560
3561 // Search for a graphics and a present queue in the array of queue
3562 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003563 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3564 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003565 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003566 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3567 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3568 graphicsQueueFamilyIndex = i;
3569 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003570
Tony Barbourab2a0362016-09-06 11:40:12 -06003571 if (supportsPresent[i] == VK_TRUE) {
3572 graphicsQueueFamilyIndex = i;
3573 presentQueueFamilyIndex = i;
3574 break;
3575 }
3576 }
3577 }
3578
3579 if (presentQueueFamilyIndex == UINT32_MAX) {
3580 // If didn't find a queue that supports both graphics and present, then
3581 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003582 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003583 if (supportsPresent[i] == VK_TRUE) {
3584 presentQueueFamilyIndex = i;
3585 break;
3586 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003587 }
3588 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003589
3590 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003591 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3592 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003593 }
3594
Tony Barbourab2a0362016-09-06 11:40:12 -06003595 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3596 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003597 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003598 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003599
Tony Barbourda2bad52016-01-22 14:36:40 -07003600 demo_create_device(demo);
3601
3602 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3603 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3604 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3605 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3606 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003607 if (demo->VK_GOOGLE_display_timing_enabled) {
3608 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3609 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3610 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003611
Dave Houlton5fa47912018-02-16 11:02:26 -07003612 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003613
Tony Barbour22e06272016-09-07 16:07:56 -06003614 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003615 demo->present_queue = demo->graphics_queue;
3616 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003617 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003618 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003619
Ian Elliottaaae5352015-07-06 14:27:58 -06003620 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003621 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003622 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003623 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003624 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3625 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003626 assert(!err);
Joshua Ashton1fbefe02020-04-02 06:45:48 +01003627 VkSurfaceFormatKHR surfaceFormat = pick_surface_format(surfFormats, formatCount);
3628 demo->format = surfaceFormat.format;
3629 demo->color_space = surfaceFormat.colorSpace;
Jason Chen5d4fee92019-04-04 12:45:29 +08003630 free(surfFormats);
Ian Elliotte4602cd2015-04-21 16:41:02 -06003631
David Pinedo2bb7c932015-06-18 17:03:14 -06003632 demo->quit = false;
3633 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003634
Tony Barbource7524e2016-07-28 12:01:45 -06003635 // Create semaphores to synchronize acquiring presentable buffers before
3636 // rendering and waiting for drawing to be complete before presenting
3637 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3638 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3639 .pNext = NULL,
3640 .flags = 0,
3641 };
Tony Barbource7524e2016-07-28 12:01:45 -06003642
Tony Barbour66a56c32016-09-21 13:10:56 -06003643 // Create fences that we can use to throttle if we get too far
3644 // ahead of the image presents
3645 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003646 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003647 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003648 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3649 assert(!err);
3650
Dave Houlton5fa47912018-02-16 11:02:26 -07003651 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003652 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003653
Dave Houlton5fa47912018-02-16 11:02:26 -07003654 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003655 assert(!err);
3656
3657 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003658 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003659 assert(!err);
3660 }
Tony Barbour22e06272016-09-07 16:07:56 -06003661 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003662 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003663
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003664 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003665 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003666}
3667
Tony Barbour153cb062016-12-07 13:43:36 -07003668#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003669static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3670 wl_fixed_t sy) {}
3671
3672static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3673
3674static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3675
3676static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3677 uint32_t state) {
3678 struct demo *demo = data;
3679 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07003680 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
Joey Bzdek15eb0702017-06-07 09:40:36 -06003681 }
3682}
3683
3684static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3685
3686static const struct wl_pointer_listener pointer_listener = {
3687 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3688};
3689
3690static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3691
3692static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3693 struct wl_array *keys) {}
3694
3695static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3696
3697static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3698 uint32_t state) {
3699 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3700 struct demo *demo = data;
3701 switch (key) {
3702 case KEY_ESC: // Escape
3703 demo->quit = true;
3704 break;
3705 case KEY_LEFT: // left arrow key
3706 demo->spin_angle -= demo->spin_increment;
3707 break;
3708 case KEY_RIGHT: // right arrow key
3709 demo->spin_angle += demo->spin_increment;
3710 break;
3711 case KEY_SPACE: // space bar
3712 demo->pause = !demo->pause;
3713 break;
3714 }
3715}
3716
3717static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3718 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3719
3720static const struct wl_keyboard_listener keyboard_listener = {
3721 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3722};
3723
3724static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3725 // Subscribe to pointer events
3726 struct demo *demo = data;
3727 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3728 demo->pointer = wl_seat_get_pointer(seat);
3729 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3730 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3731 wl_pointer_destroy(demo->pointer);
3732 demo->pointer = NULL;
3733 }
3734 // Subscribe to keyboard events
3735 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3736 demo->keyboard = wl_seat_get_keyboard(seat);
3737 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3738 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3739 wl_keyboard_destroy(demo->keyboard);
3740 demo->keyboard = NULL;
3741 }
3742}
3743
3744static const struct wl_seat_listener seat_listener = {
3745 seat_handle_capabilities,
3746};
3747
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07003748static 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 +09003749 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003750 // pickup wayland objects when they appear
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07003751 if (strcmp(interface, "wl_compositor") == 0) {
Georges Basile Stavracas Neto7da3ea72019-10-05 11:53:39 -03003752 uint32_t minVersion = version < 4 ? version : 4;
3753 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, minVersion);
3754 if (demo->VK_KHR_incremental_present_enabled && minVersion < 4) {
3755 fprintf(stderr, "Wayland compositor doesn't support VK_KHR_incremental_present, disabling.\n");
3756 demo->VK_KHR_incremental_present_enabled = false;
3757 }
Tony-LunarG5ceb7be2019-12-05 16:05:40 -07003758 } else if (strcmp(interface, "wl_shell") == 0) {
3759 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3760 } else if (strcmp(interface, "wl_seat") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003761 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3762 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003763 }
3764}
3765
Dave Houlton5fa47912018-02-16 11:02:26 -07003766static 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 +09003767
Dave Houlton5fa47912018-02-16 11:02:26 -07003768static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003769#endif
3770
Karl Schultze4dc75c2016-02-02 15:37:51 -07003771static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003772#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003773 const xcb_setup_t *setup;
3774 xcb_screen_iterator_t iter;
3775 int scr;
3776
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003777 const char *display_envar = getenv("DISPLAY");
3778 if (display_envar == NULL || display_envar[0] == '\0') {
3779 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3780 fflush(stdout);
3781 exit(1);
3782 }
3783
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003784 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003785 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003786 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003787 fflush(stdout);
3788 exit(1);
3789 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003790
3791 setup = xcb_get_setup(demo->connection);
3792 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003793 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003794
3795 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003796#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3797 demo->display = wl_display_connect(NULL);
3798
3799 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003800 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003801 fflush(stdout);
3802 exit(1);
3803 }
3804
3805 demo->registry = wl_display_get_registry(demo->display);
3806 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3807 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003808#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003809}
3810
Karl Schultze4dc75c2016-02-02 15:37:51 -07003811static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003812 vec3 eye = {0.0f, 3.0f, 5.0f};
3813 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003814 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003815
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003816 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003817 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003818 demo->frameCount = INT32_MAX;
Tony-LunarG50e737c2020-07-16 14:26:03 -06003819 /* For cube demo we just grab the first physical device by default */
3820 demo->gpu_number = 0;
Jeremy Kniager979b5312019-11-22 14:55:57 -07003821
Piers Daniell735ee532015-02-23 16:23:13 -07003822 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003823 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003824 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003825 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003826 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003827 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3828 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003829 i++;
3830 continue;
3831 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003832 if (strcmp(argv[i], "--break") == 0) {
3833 demo->use_break = true;
3834 continue;
3835 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003836 if (strcmp(argv[i], "--validate") == 0) {
3837 demo->validate = true;
3838 continue;
3839 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003840 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3841 demo->validate = true;
3842 demo->validate_checks_disabled = true;
3843 continue;
3844 }
Tony Barbour2593b182016-04-19 10:57:58 -06003845 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003846 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003847 continue;
3848 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003849 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3850 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003851 i++;
3852 continue;
3853 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003854 if (strcmp(argv[i], "--suppress_popups") == 0) {
3855 demo->suppress_popups = true;
3856 continue;
3857 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003858 if (strcmp(argv[i], "--display_timing") == 0) {
3859 demo->VK_GOOGLE_display_timing_enabled = true;
3860 continue;
3861 }
Ian Elliott53622a72017-03-28 11:06:33 -06003862 if (strcmp(argv[i], "--incremental_present") == 0) {
3863 demo->VK_KHR_incremental_present_enabled = true;
3864 continue;
3865 }
Tony-LunarG50e737c2020-07-16 14:26:03 -06003866 if ((strcmp(argv[i], "--gpu_number") == 0) && (i < argc - 1)) {
3867 demo->gpu_number = atoi(argv[i + 1]);
3868 i++;
3869 continue;
3870 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003871
Cody Northropaf28a532016-09-27 10:50:57 -06003872#if defined(ANDROID)
Lenny Komowffe446c2018-11-19 17:08:04 -07003873 ERR_EXIT("Usage: vkcube [--validate]\n", "Usage");
Cody Northropaf28a532016-09-27 10:50:57 -06003874#else
Tony-LunarGd74734f2019-06-04 14:11:43 -06003875 char *message =
3876 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled]\n"
3877 "\t[--break] [--c <framecount>] [--suppress_popups]\n"
3878 "\t[--incremental_present] [--display_timing]\n"
Tony-LunarG50e737c2020-07-16 14:26:03 -06003879 "\t[--gpu_number <index of physical device>]\n"
Tony-LunarGd74734f2019-06-04 14:11:43 -06003880 "\t[--present_mode <present mode enum>]\n"
3881 "\t<present_mode_enum>\n"
3882 "\t\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3883 "\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3884 "\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
3885 "\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n";
3886 int length = snprintf(NULL, 0, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3887 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3888 char *usage = (char *)malloc(length + 1);
3889 if (!usage) {
3890 exit(1);
3891 }
3892 snprintf(usage, length + 1, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3893 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3894#if defined(_WIN32)
3895 if (!demo->suppress_popups) MessageBox(NULL, usage, "Usage Error", MB_OK);
3896#else
Tony-LunarG122ac062019-06-13 16:21:57 -06003897 fprintf(stderr, "%s", usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003898 fflush(stderr);
Tony-LunarGd74734f2019-06-04 14:11:43 -06003899#endif
3900 free(usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003901 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003902#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003903 }
3904
Tony Barbour153cb062016-12-07 13:43:36 -07003905 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003906
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003907 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003908
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003909 demo->width = 500;
3910 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003911
Tony Barbour66a56c32016-09-21 13:10:56 -06003912 demo->spin_angle = 4.0f;
3913 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003914 demo->pause = false;
3915
Dave Houlton5fa47912018-02-16 11:02:26 -07003916 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003917 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3918 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003919
Dave Houlton5fa47912018-02-16 11:02:26 -07003920 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003921}
3922
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003923#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003924// Include header required for parsing the command line options.
3925#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003926
Dave Houlton5fa47912018-02-16 11:02:26 -07003927int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3928 MSG msg; // message
3929 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003930 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003931 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003932
Jamie Madillb2ff6502017-03-15 16:17:46 -04003933 // Ensure wParam is initialized.
3934 msg.wParam = 0;
3935
Karl Schultze4dc75c2016-02-02 15:37:51 -07003936 // Use the CommandLine functions to get the command line arguments.
3937 // Unfortunately, Microsoft outputs
3938 // this information as wide characters for Unicode, and we simply want the
3939 // Ascii version to be compatible
3940 // with the non-Windows side. So, we have to convert the information to
3941 // Ascii character strings.
3942 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3943 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003944 argc = 0;
3945 }
3946
Karl Schultze4dc75c2016-02-02 15:37:51 -07003947 if (argc > 0) {
3948 argv = (char **)malloc(sizeof(char *) * argc);
3949 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003950 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003951 } else {
3952 for (int iii = 0; iii < argc; iii++) {
3953 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003954 size_t numConverted = 0;
3955
Karl Schultze4dc75c2016-02-02 15:37:51 -07003956 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3957 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003958 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003959 }
3960 }
3961 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003962 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003963 argv = NULL;
3964 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003965
3966 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003967
3968 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003969 if (argc > 0 && argv != NULL) {
3970 for (int iii = 0; iii < argc; iii++) {
3971 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003972 free(argv[iii]);
3973 }
3974 }
3975 free(argv);
3976 }
3977
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003978 demo.connection = hInstance;
Lenny Komowffe446c2018-11-19 17:08:04 -07003979 strncpy(demo.name, "Vulkan Cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003980 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003981 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003982
3983 demo_prepare(&demo);
3984
Dave Houlton5fa47912018-02-16 11:02:26 -07003985 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003986
3987 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003988 while (!done) {
Petr Krausd88e4e52019-01-23 18:45:04 +01003989 if (demo.pause) {
3990 const BOOL succ = WaitMessage();
3991
3992 if (!succ) {
3993 struct demo *tmp = &demo;
3994 struct demo *demo = tmp;
3995 ERR_EXIT("WaitMessage() failed on paused demo", "event loop error");
3996 }
3997 }
Ian Elliotta748eaf2015-04-28 15:50:36 -06003998 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003999 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06004000 {
Dave Houlton5fa47912018-02-16 11:02:26 -07004001 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07004002 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06004003 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07004004 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06004005 DispatchMessage(&msg);
4006 }
Tony Barbourc8a59892015-10-20 12:49:46 -06004007 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06004008 }
4009
4010 demo_cleanup(&demo);
4011
Karl Schultze4dc75c2016-02-02 15:37:51 -07004012 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06004013}
Bill Hollingsf4352142017-02-14 22:58:56 -05004014
Bill Hollings0a0625a2019-07-15 17:39:18 -04004015#elif defined(VK_USE_PLATFORM_METAL_EXT)
4016static void demo_main(struct demo *demo, void *caMetalLayer, int argc, const char *argv[]) {
Dave Houlton5fa47912018-02-16 11:02:26 -07004017 demo_init(demo, argc, (char **)argv);
Bill Hollings0a0625a2019-07-15 17:39:18 -04004018 demo->caMetalLayer = caMetalLayer;
Tony Barbourc65cd752017-03-13 15:29:35 -06004019 demo_init_vk_swapchain(demo);
4020 demo_prepare(demo);
4021 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05004022}
4023
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004024#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
4025#include <android/log.h>
4026#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004027#include "android_util.h"
4028
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004029static bool initialized = false;
4030static bool active = false;
4031struct demo demo;
4032
Dave Houlton5fa47912018-02-16 11:02:26 -07004033static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004034
Dave Houlton5fa47912018-02-16 11:02:26 -07004035static void processCommand(struct android_app *app, int32_t cmd) {
4036 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004037 case APP_CMD_INIT_WINDOW: {
4038 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06004039 // We're getting a new window. If the app is starting up, we
4040 // need to initialize. If the app has already been
4041 // initialized, that means that we lost our previous window,
4042 // which means that we have a lot of work to do. At a minimum,
4043 // we need to destroy the swapchain and surface associated with
4044 // the old window, and create a new surface and swapchain.
4045 // However, since there are a lot of other objects/state that
4046 // is tied to the swapchain, it's easiest to simply cleanup and
4047 // start over (i.e. use a brute-force approach of re-starting
4048 // the app)
4049 if (demo.prepared) {
4050 demo_cleanup(&demo);
4051 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004052
4053 // Parse Intents into argc, argv
4054 // Use the following key to send arguments, i.e.
4055 // --es args "--validate"
4056 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07004057 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004058 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07004059 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004060
4061 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07004062 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 -06004063
4064 demo_init(&demo, argc, argv);
4065
4066 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07004067 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004068
Dave Houlton5fa47912018-02-16 11:02:26 -07004069 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004070 demo_init_vk_swapchain(&demo);
4071 demo_prepare(&demo);
4072 initialized = true;
4073 }
4074 break;
4075 }
4076 case APP_CMD_GAINED_FOCUS: {
4077 active = true;
4078 break;
4079 }
4080 case APP_CMD_LOST_FOCUS: {
4081 active = false;
4082 break;
4083 }
4084 }
4085}
4086
Dave Houlton5fa47912018-02-16 11:02:26 -07004087void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06004088#ifdef ANDROID
4089 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07004090 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06004091#endif
4092
Ian Elliottf05d5d12016-05-17 12:03:35 -06004093 demo.prepared = false;
4094
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004095 app->onAppCmd = processCommand;
4096 app->onInputEvent = processInput;
4097
Dave Houlton5fa47912018-02-16 11:02:26 -07004098 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004099 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07004100 struct android_poll_source *source;
4101 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004102 if (source) {
4103 source->process(app, source);
4104 }
4105
4106 if (app->destroyRequested != 0) {
4107 demo_cleanup(&demo);
4108 return;
4109 }
4110 }
4111 if (initialized && active) {
4112 demo_run(&demo);
4113 }
4114 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004115}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004116#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004117int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004118 struct demo demo;
4119
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004120 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004121#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004122 demo_create_xcb_window(&demo);
4123#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4124 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004125#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4126 demo_create_window(&demo);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02004127#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
4128 demo_create_directfb_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004129#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004130
Tony Barbourcc69f452015-10-08 13:59:42 -06004131 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004132
4133 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004134
Tony Barbour153cb062016-12-07 13:43:36 -07004135#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004136 demo_run_xcb(&demo);
4137#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4138 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004139#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4140 demo_run(&demo);
Nicolas Caramellic8be8c82020-07-13 17:22:09 +02004141#elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
4142 demo_run_directfb(&demo);
Damien Leone600c3052017-01-31 10:26:07 -07004143#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4144 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004145#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004146
4147 demo_cleanup(&demo);
4148
Karl Schultz0218c002016-03-22 17:06:13 -06004149 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004150}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004151#endif