blob: 9cbc45e5ecd90255343802d87cacbda95fd0f085 [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>
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -040040#include "xdg-shell-client-header.h"
41#include "xdg-decoration-client-header.h"
Tony Barbour2593b182016-04-19 10:57:58 -060042#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060043
Ian Elliott639ca472015-04-16 15:23:05 -060044#ifdef _WIN32
45#pragma comment(linker, "/subsystem:windows")
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;
300 VkFramebuffer framebuffer;
301 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700302} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600303
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600304struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500305#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600306#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700307 HINSTANCE connection; // hInstance - Windows Instance
308 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
309 HWND window; // hWnd - window handle
310 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700311#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700312 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600313 Window xlib_window;
314 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700315#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700316 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600317 xcb_connection_t *connection;
318 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600319 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800320 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900321#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
322 struct wl_display *display;
323 struct wl_registry *registry;
324 struct wl_compositor *compositor;
325 struct wl_surface *window;
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -0400326 struct xdg_wm_base *xdg_wm_base;
327 struct zxdg_decoration_manager_v1 *xdg_decoration_mgr;
328 struct zxdg_toplevel_decoration_v1 *toplevel_decoration;
329 struct xdg_surface *xdg_surface;
330 int xdg_surface_has_been_configured;
331 struct xdg_toplevel *xdg_toplevel;
Joey Bzdek15eb0702017-06-07 09:40:36 -0600332 struct wl_seat *seat;
333 struct wl_pointer *pointer;
334 struct wl_keyboard *keyboard;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500335#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mike Schuchardt837d5162018-03-08 12:57:02 -0700336 struct ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500337#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
338 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500339#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700340 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600341 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700342 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600343 bool separate_present_queue;
Jeremy Kniager602e4182018-01-19 10:52:34 -0700344 bool is_minimized;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600345
Ian Elliott53622a72017-03-28 11:06:33 -0600346 bool VK_KHR_incremental_present_enabled;
347
Ian Elliottd940ca22017-03-22 08:31:27 -0600348 bool VK_GOOGLE_display_timing_enabled;
349 bool syncd_with_actual_presents;
350 uint64_t refresh_duration;
351 uint64_t refresh_duration_multiplier;
352 uint64_t target_IPD; // image present duration (inverse of frame rate)
353 uint64_t prev_desired_present_time;
354 uint32_t next_present_id;
355 uint32_t last_early_id; // 0 if no early images
356 uint32_t last_late_id; // 0 if no late images
357
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600359 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600361 VkQueue graphics_queue;
362 VkQueue present_queue;
363 uint32_t graphics_queue_family_index;
364 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600365 VkSemaphore image_acquired_semaphores[FRAME_LAG];
366 VkSemaphore draw_complete_semaphores[FRAME_LAG];
367 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600368 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600369 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600370 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371
Tony Barbourda2bad52016-01-22 14:36:40 -0700372 uint32_t enabled_extension_count;
373 uint32_t enabled_layer_count;
374 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600375 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700376
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600377 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600378 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600379 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700381 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
382 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
383 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
384 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600385 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
386 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
387 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
388 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
389 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600390 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
391 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600392 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600393 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700394 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600395 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600396 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600397 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600398
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800399 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600400 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600401
402 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600403 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600405 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800406 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500407 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600408 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600409 } depth;
410
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600411 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600412 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600413
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700414 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500415 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600416 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600417 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800418 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600419 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600420
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600421 mat4x4 projection_matrix;
422 mat4x4 view_matrix;
423 mat4x4 model_matrix;
424
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600425 float spin_angle;
426 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600427 bool pause;
428
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600429 VkShaderModule vert_shader_module;
430 VkShaderModule frag_shader_module;
431
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600432 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800433
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600434 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600435 int32_t curFrame;
436 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600437 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600438 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600439 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600440 bool suppress_popups;
Mark Young66510e52017-11-10 10:05:14 -0700441
442 PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
443 PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
444 PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
445 PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
446 PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
447 PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
448 PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
449 VkDebugUtilsMessengerEXT dbg_messenger;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600450
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600451 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600452 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600453};
454
Mark Young66510e52017-11-10 10:05:14 -0700455VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
456 VkDebugUtilsMessageTypeFlagsEXT messageType,
457 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
458 void *pUserData) {
459 char prefix[64] = "";
460 char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000);
lenny-lunargfbe22392016-06-06 11:07:53 -0600461 assert(message);
Mark Young66510e52017-11-10 10:05:14 -0700462 struct demo *demo = (struct demo *)pUserData;
lenny-lunargfbe22392016-06-06 11:07:53 -0600463
Mark Young66510e52017-11-10 10:05:14 -0700464 if (demo->use_break) {
465#ifndef WIN32
466 raise(SIGTRAP);
467#else
468 DebugBreak();
469#endif
470 }
471
472 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
473 strcat(prefix, "VERBOSE : ");
474 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
475 strcat(prefix, "INFO : ");
476 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
477 strcat(prefix, "WARNING : ");
478 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
479 strcat(prefix, "ERROR : ");
480 }
481
482 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
483 strcat(prefix, "GENERAL");
lenny-lunargfbe22392016-06-06 11:07:53 -0600484 } else {
Mark Young66510e52017-11-10 10:05:14 -0700485 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
486 strcat(prefix, "VALIDATION");
487 validation_error = 1;
488 }
489 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
490 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
491 strcat(prefix, "|");
492 }
493 strcat(prefix, "PERFORMANCE");
494 }
495 }
496
497 sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber,
498 pCallbackData->pMessageIdName, pCallbackData->pMessage);
499 if (pCallbackData->objectCount > 0) {
500 char tmp_message[500];
501 sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount);
502 strcat(message, tmp_message);
503 for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
504 if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
505 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object,
Mark Young44212682018-02-21 15:29:41 -0700506 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700507 (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName);
508 } else {
509 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object,
Mark Young44212682018-02-21 15:29:41 -0700510 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700511 (void *)(pCallbackData->pObjects[object].objectHandle));
512 }
513 strcat(message, tmp_message);
514 }
515 }
516 if (pCallbackData->cmdBufLabelCount > 0) {
517 char tmp_message[500];
518 sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount);
519 strcat(message, tmp_message);
520 for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
521 sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label,
522 pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0],
523 pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2],
524 pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]);
525 strcat(message, tmp_message);
526 }
lenny-lunargfbe22392016-06-06 11:07:53 -0600527 }
528
529#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600530
Tony Barbour602ca4f2016-09-12 14:02:43 -0600531 in_callback = true;
Jeremy Kniager979b5312019-11-22 14:55:57 -0700532 if (!demo->suppress_popups) MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600533 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600534
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600535#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600536
Mike Schuchardt837d5162018-03-08 12:57:02 -0700537 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
Jeremy Kniager979b5312019-11-22 14:55:57 -0700538 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700539 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
Jeremy Kniager979b5312019-11-22 14:55:57 -0700540 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700541 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600542 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700543 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
544 __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600545 } else {
Jeremy Kniager979b5312019-11-22 14:55:57 -0700546 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600547 }
Cody Northropaf28a532016-09-27 10:50:57 -0600548
lenny-lunargfbe22392016-06-06 11:07:53 -0600549#else
Cody Northropaf28a532016-09-27 10:50:57 -0600550
lenny-lunargfbe22392016-06-06 11:07:53 -0600551 printf("%s\n", message);
552 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600553
lenny-lunargfbe22392016-06-06 11:07:53 -0600554#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600555
lenny-lunargfbe22392016-06-06 11:07:53 -0600556 free(message);
557
Mark Young66510e52017-11-10 10:05:14 -0700558 // Don't bail out, but keep going.
lenny-lunargfbe22392016-06-06 11:07:53 -0600559 return false;
560}
561
Ian Elliottd940ca22017-03-22 08:31:27 -0600562bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
563 // The desired time was the earliest time that the present should have
564 // occured. In almost every case, the actual time should be later than the
565 // desired time. We should only consider the actual time "late" if it is
566 // after "desired + rdur".
567 if (actual <= desired) {
568 // The actual time was before or equal to the desired time. This will
569 // probably never happen, but in case it does, return false since the
570 // present was obviously NOT late.
571 return false;
572 }
Liam Middlebrook881fe392018-05-03 15:52:32 -0700573 uint64_t deadline = desired + rdur;
Ian Elliottd940ca22017-03-22 08:31:27 -0600574 if (actual > deadline) {
575 return true;
576 } else {
577 return false;
578 }
579}
Dave Houlton5fa47912018-02-16 11:02:26 -0700580bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600581 if (earliest < actual) {
582 // Consider whether this present could have occured earlier. Make sure
583 // that earliest time was at least 2msec earlier than actual time, and
584 // that the margin was at least 2msec:
585 uint64_t diff = actual - earliest;
586 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
587 // This present could have occured earlier because both: 1) the
588 // earliest time was at least 2 msec before actual time, and 2) the
589 // margin was at least 2msec.
590 return true;
591 }
592 }
593 return false;
594}
595
Tony-LunarG6cebf142019-09-11 14:32:23 -0600596// Forward declarations:
Ian Elliottcf074d32015-10-16 18:02:43 -0600597static void demo_resize(struct demo *demo);
Tony-LunarG6cebf142019-09-11 14:32:23 -0600598static void demo_create_surface(struct demo *demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600599
Dave Houlton5fa47912018-02-16 11:02:26 -0700600static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700601 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600602 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700603 if ((typeBits & 1) == 1) {
604 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700605 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700606 *typeIndex = i;
607 return true;
608 }
609 }
610 typeBits >>= 1;
611 }
612 // No memory types matched, return failure
613 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600614}
615
Karl Schultze4dc75c2016-02-02 15:37:51 -0700616static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600617 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600618
Tony Barbource7524e2016-07-28 12:01:45 -0600619 // This function could get called twice if the texture uses a staging buffer
620 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700621 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600622
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600623 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600624 assert(!err);
625
Tony Barbource7524e2016-07-28 12:01:45 -0600626 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700627 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700628 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
629 assert(!err);
630
Karl Schultze4dc75c2016-02-02 15:37:51 -0700631 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700632 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
633 .pNext = NULL,
634 .waitSemaphoreCount = 0,
635 .pWaitSemaphores = NULL,
636 .pWaitDstStageMask = NULL,
637 .commandBufferCount = 1,
638 .pCommandBuffers = cmd_bufs,
639 .signalSemaphoreCount = 0,
640 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600641
Tony Barbource7524e2016-07-28 12:01:45 -0600642 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600643 assert(!err);
644
Tony Barbource7524e2016-07-28 12:01:45 -0600645 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600646 assert(!err);
647
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600648 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600649 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600650 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600651}
652
Dave Houlton5fa47912018-02-16 11:02:26 -0700653static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
654 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600655 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600656 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600657
Dave Houlton5fa47912018-02-16 11:02:26 -0700658 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
659 .pNext = NULL,
660 .srcAccessMask = srcAccessMask,
661 .dstAccessMask = 0,
Tony-LunarGa141b962018-05-30 11:33:19 -0600662 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
663 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Dave Houlton5fa47912018-02-16 11:02:26 -0700664 .oldLayout = old_image_layout,
665 .newLayout = new_image_layout,
666 .image = image,
667 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600668
Tony Barboureb5077b2016-10-19 15:23:36 -0600669 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700670 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
671 /* Make sure anything that was copying from this image has completed */
672 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
673 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600674
Dave Houlton5fa47912018-02-16 11:02:26 -0700675 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
676 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
677 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700678
Dave Houlton5fa47912018-02-16 11:02:26 -0700679 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
680 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
681 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700682
Dave Houlton5fa47912018-02-16 11:02:26 -0700683 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
684 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
685 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600686
Dave Houlton5fa47912018-02-16 11:02:26 -0700687 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
688 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
689 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600690
Dave Houlton5fa47912018-02-16 11:02:26 -0700691 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
692 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
693 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600694
Dave Houlton5fa47912018-02-16 11:02:26 -0700695 default:
696 image_memory_barrier.dstAccessMask = 0;
697 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600698 }
699
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600700 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600701
Dave Houlton5fa47912018-02-16 11:02:26 -0700702 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600703}
704
Karl Schultze4dc75c2016-02-02 15:37:51 -0700705static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Lenny Komowe830b592018-02-23 16:18:36 -0700706 VkDebugUtilsLabelEXT label;
707 memset(&label, 0, sizeof(label));
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700708 const VkCommandBufferBeginInfo cmd_buf_info = {
709 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
710 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600711 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700712 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700713 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800714 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700715 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
716 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800717 };
718 const VkRenderPassBeginInfo rp_begin = {
719 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
720 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800721 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700722 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800723 .renderArea.offset.x = 0,
724 .renderArea.offset.y = 0,
725 .renderArea.extent.width = demo->width,
726 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600727 .clearValueCount = 2,
728 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700729 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800730 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600731
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600732 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Mark Young66510e52017-11-10 10:05:14 -0700733
734 if (demo->validate) {
735 // Set a name for the command buffer
736 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
737 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
738 .pNext = NULL,
739 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
740 .objectHandle = (uint64_t)cmd_buf,
741 .pObjectName = "CubeDrawCommandBuf",
742 };
743 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
744
745 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
746 label.pNext = NULL;
747 label.pLabelName = "DrawBegin";
748 label.color[0] = 0.4f;
749 label.color[1] = 0.3f;
750 label.color[2] = 0.2f;
751 label.color[3] = 0.1f;
752 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
753 }
754
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600755 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800756 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Mark Young66510e52017-11-10 10:05:14 -0700757
758 if (demo->validate) {
759 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
760 label.pNext = NULL;
761 label.pLabelName = "InsideRenderPass";
762 label.color[0] = 8.4f;
763 label.color[1] = 7.3f;
764 label.color[2] = 6.2f;
765 label.color[3] = 7.1f;
766 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
767 }
768
Karl Schultze4dc75c2016-02-02 15:37:51 -0700769 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700770 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
771 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600772 VkViewport viewport;
773 memset(&viewport, 0, sizeof(viewport));
Jeremy Kniager979b5312019-11-22 14:55:57 -0700774 float viewport_dimension;
775 if (demo->width < demo->height) {
776 viewport_dimension = (float)demo->width;
777 viewport.y = (demo->height - demo->width) / 2.0f;
778 } else {
779 viewport_dimension = (float)demo->height;
780 viewport.x = (demo->width - demo->height) / 2.0f;
781 }
782 viewport.height = viewport_dimension;
783 viewport.width = viewport_dimension;
Karl Schultze4dc75c2016-02-02 15:37:51 -0700784 viewport.minDepth = (float)0.0f;
785 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700786 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600787
788 VkRect2D scissor;
789 memset(&scissor, 0, sizeof(scissor));
790 scissor.extent.width = demo->width;
791 scissor.extent.height = demo->height;
792 scissor.offset.x = 0;
793 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700794 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Mark Young66510e52017-11-10 10:05:14 -0700795
796 if (demo->validate) {
797 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
798 label.pNext = NULL;
799 label.pLabelName = "ActualDraw";
Mike Schuchardt4cf3aa42018-02-23 12:44:05 -0700800 label.color[0] = -0.4f;
801 label.color[1] = -0.3f;
802 label.color[2] = -0.2f;
803 label.color[3] = -0.1f;
Mark Young66510e52017-11-10 10:05:14 -0700804 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
805 }
806
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600807 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Mark Young66510e52017-11-10 10:05:14 -0700808 if (demo->validate) {
809 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
810 }
811
Tony Barbour98390392016-07-28 10:50:13 -0600812 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600813 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800814 vkCmdEndRenderPass(cmd_buf);
Mark Young66510e52017-11-10 10:05:14 -0700815 if (demo->validate) {
816 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
817 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600818
Tony Barbour22e06272016-09-07 16:07:56 -0600819 if (demo->separate_present_queue) {
820 // We have to transfer ownership from the graphics queue family to the
821 // present queue family to be able to present. Note that we don't have
822 // to transfer from present queue family back to graphics queue family at
823 // the start of the next frame because we don't care about the image's
824 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700825 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
826 .pNext = NULL,
827 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600828 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700829 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
830 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
831 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
832 .dstQueueFamilyIndex = demo->present_queue_family_index,
833 .image = demo->swapchain_image_resources[demo->current_buffer].image,
834 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600835
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600836 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
837 NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600838 }
Mark Young66510e52017-11-10 10:05:14 -0700839 if (demo->validate) {
840 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
841 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600842 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600843 assert(!err);
844}
845
Tony Barbour22e06272016-09-07 16:07:56 -0600846void demo_build_image_ownership_cmd(struct demo *demo, int i) {
847 VkResult U_ASSERT_ONLY err;
848
849 const VkCommandBufferBeginInfo cmd_buf_info = {
850 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
851 .pNext = NULL,
852 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
853 .pInheritanceInfo = NULL,
854 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700855 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600856 assert(!err);
857
Dave Houlton5fa47912018-02-16 11:02:26 -0700858 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
859 .pNext = NULL,
860 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600861 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700862 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
863 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
864 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
865 .dstQueueFamilyIndex = demo->present_queue_family_index,
866 .image = demo->swapchain_image_resources[i].image,
867 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600868
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600869 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
870 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700871 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600872 assert(!err);
873}
874
Karl Schultze4dc75c2016-02-02 15:37:51 -0700875void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600876 mat4x4 MVP, Model, VP;
877 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600878 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600879 VkResult U_ASSERT_ONLY err;
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
Dave Houlton5fa47912018-02-16 11:02:26 -0700888 err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0,
889 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600890 assert(!err);
891
Karl Schultze4dc75c2016-02-02 15:37:51 -0700892 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600893
Tony Barboura72d9492017-01-11 13:35:09 -0700894 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600895}
896
Ian Elliottd940ca22017-03-22 08:31:27 -0600897void DemoUpdateTargetIPD(struct demo *demo) {
898 // Look at what happened to previous presents, and make appropriate
899 // adjustments in timing:
900 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700901 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600902 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600903
Dave Houlton5fa47912018-02-16 11:02:26 -0700904 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600905 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600906 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700907 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600908 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700909 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600910 assert(!err);
911
912 bool early = false;
913 bool late = false;
914 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700915 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600916 if (!demo->syncd_with_actual_presents) {
917 // This is the first time that we've received an
918 // actualPresentTime for this swapchain. In order to not
919 // perceive these early frames as "late", we need to sync-up
920 // our future desiredPresentTime's with the
921 // actualPresentTime(s) that we're receiving now.
922 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600923
Ian Elliottd940ca22017-03-22 08:31:27 -0600924 // So that we don't suspect any pending presents as late,
925 // record them all as suspected-late presents:
926 demo->last_late_id = demo->next_present_id - 1;
927 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600928 demo->syncd_with_actual_presents = true;
929 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700930 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600931 demo->refresh_duration)) {
932 // This image could have been presented earlier. We don't want
933 // to decrease the target_IPD until we've seen early presents
934 // for at least two seconds.
935 if (demo->last_early_id == past[i].presentID) {
936 // We've now seen two seconds worth of early presents.
937 // Flag it as such, and reset the counter:
938 early = true;
939 demo->last_early_id = 0;
940 } else if (demo->last_early_id == 0) {
941 // This is the first early present we've seen.
942 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700943 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
944 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600945 demo->last_early_id = past[i].presentID + howManyPresents;
946 } else {
947 // We are in the midst of a set of early images,
948 // and so we won't do anything.
949 }
950 late = false;
951 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700952 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600953 // This image was presented after its desired time. Since
954 // there's a delay between calling vkQueuePresentKHR and when
955 // we get the timing data, several presents may have been late.
956 // Thus, we need to threat all of the outstanding presents as
957 // being likely late, so that we only increase the target_IPD
958 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700959 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600960 late = true;
961 // Record the last suspected-late present:
962 demo->last_late_id = demo->next_present_id - 1;
963 } else {
964 // We are in the midst of a set of likely-late images,
965 // and so we won't do anything.
966 }
967 early = false;
968 demo->last_early_id = 0;
969 } else {
970 // Since this image was not presented early or late, reset
971 // any sets of early or late presentIDs:
972 early = false;
973 late = false;
974 calibrate_next = true;
975 demo->last_early_id = 0;
976 demo->last_late_id = 0;
977 }
978 }
979
980 if (early) {
981 // Since we've seen at least two-seconds worth of presnts that
982 // could have occured earlier than desired, let's decrease the
983 // target_IPD (i.e. increase the frame rate):
984 //
985 // TODO(ianelliott): Try to calculate a better target_IPD based
986 // on the most recently-seen present (this is overly-simplistic).
987 demo->refresh_duration_multiplier--;
988 if (demo->refresh_duration_multiplier == 0) {
989 // This should never happen, but in case it does, don't
990 // try to go faster.
991 demo->refresh_duration_multiplier = 1;
992 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700993 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600994 }
995 if (late) {
996 // Since we found a new instance of a late present, we want to
997 // increase the target_IPD (i.e. decrease the frame rate):
998 //
999 // TODO(ianelliott): Try to calculate a better target_IPD based
1000 // on the most recently-seen present (this is overly-simplistic).
1001 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -07001002 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -06001003 }
1004
1005 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001006 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
1007 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -06001008 }
Karl Schultza33771f2018-05-28 16:16:47 -06001009 free(past);
Ian Elliottd940ca22017-03-22 08:31:27 -06001010 }
Ian Elliottd940ca22017-03-22 08:31:27 -06001011}
1012
Karl Schultze4dc75c2016-02-02 15:37:51 -07001013static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001014 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001015
Damien Leonec336d822017-04-14 16:10:14 -07001016 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +03001017 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1018 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001019
Tony Barbour8b7c9112017-06-29 13:34:41 -06001020 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -06001021 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -07001022 err =
1023 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1024 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001025
Tony Barbour6914e6d2017-06-02 12:11:29 -06001026 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1027 // demo->swapchain is out of date (e.g. the window was resized) and
1028 // must be recreated:
1029 demo_resize(demo);
1030 } else if (err == VK_SUBOPTIMAL_KHR) {
1031 // demo->swapchain is not as optimal as it could be, but the platform's
1032 // presentation engine will still present the image correctly.
1033 break;
Tony-LunarG6cebf142019-09-11 14:32:23 -06001034 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1035 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1036 demo_create_surface(demo);
1037 demo_resize(demo);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001038 } else {
1039 assert(!err);
1040 }
Tony Barbour8b7c9112017-06-29 13:34:41 -06001041 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001042
Tony Barbour3eafe1f2017-06-14 11:59:55 -06001043 demo_update_data_buffer(demo);
1044
Ian Elliottd940ca22017-03-22 08:31:27 -06001045 if (demo->VK_GOOGLE_display_timing_enabled) {
1046 // Look at what happened to previous presents, and make appropriate
1047 // adjustments in timing:
1048 DemoUpdateTargetIPD(demo);
1049
1050 // Note: a real application would position its geometry to that it's in
1051 // the correct locatoin for when the next image is presented. It might
1052 // also wait, so that there's less latency between any input and when
1053 // the next image is rendered/presented. This demo program is so
1054 // simple that it doesn't do either of those.
1055 }
1056
Tony Barbource7524e2016-07-28 12:01:45 -06001057 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001058 // that the image won't be rendered to until the presentation
1059 // engine has fully released ownership to the application, and it is
1060 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001061 VkPipelineStageFlags pipe_stage_flags;
1062 VkSubmitInfo submit_info;
1063 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1064 submit_info.pNext = NULL;
1065 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1066 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1067 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001068 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001069 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001070 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001071 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001072 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -07001073 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001074 assert(!err);
1075
Tony Barbour22e06272016-09-07 16:07:56 -06001076 if (demo->separate_present_queue) {
1077 // If we are using separate queues, change image ownership to the
1078 // present queue before presenting, waiting for the draw complete
1079 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001080 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001081 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1082 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001083 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001084 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001085 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001086 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001087 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001088 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1089 assert(!err);
1090 }
1091
1092 // If we are using separate queues we have to wait for image ownership,
1093 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001094 VkPresentInfoKHR present = {
1095 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001096 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001097 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -07001098 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1099 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001100 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001101 .pSwapchains = &demo->swapchain,
1102 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001103 };
1104
Dave Airlie48a68f92019-04-12 17:04:48 +10001105 VkRectLayerKHR rect;
1106 VkPresentRegionKHR region;
1107 VkPresentRegionsKHR regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001108 if (demo->VK_KHR_incremental_present_enabled) {
1109 // If using VK_KHR_incremental_present, we provide a hint of the region
1110 // that contains changed content relative to the previously-presented
1111 // image. The implementation can use this hint in order to save
1112 // work/power (by only copying the region in the hint). The
1113 // implementation is free to ignore the hint though, and so we must
1114 // ensure that the entire image has the correctly-drawn content.
1115 uint32_t eighthOfWidth = demo->width / 8;
1116 uint32_t eighthOfHeight = demo->height / 8;
Dave Airlie48a68f92019-04-12 17:04:48 +10001117
1118 rect.offset.x = eighthOfWidth;
1119 rect.offset.y = eighthOfHeight;
1120 rect.extent.width = eighthOfWidth * 6;
1121 rect.extent.height = eighthOfHeight * 6;
1122 rect.layer = 0;
1123
1124 region.rectangleCount = 1;
1125 region.pRectangles = &rect;
1126
1127 regions.sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR;
1128 regions.pNext = present.pNext;
1129 regions.swapchainCount = present.swapchainCount;
1130 regions.pRegions = &region;
Ian Elliott53622a72017-03-28 11:06:33 -06001131 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001132 }
1133
Ian Elliottd940ca22017-03-22 08:31:27 -06001134 if (demo->VK_GOOGLE_display_timing_enabled) {
1135 VkPresentTimeGOOGLE ptime;
1136 if (demo->prev_desired_present_time == 0) {
1137 // This must be the first present for this swapchain.
1138 //
1139 // We don't know where we are relative to the presentation engine's
1140 // display's refresh cycle. We also don't know how long rendering
1141 // takes. Let's make a grossly-simplified assumption that the
1142 // desiredPresentTime should be half way between now and
1143 // now+target_IPD. We will adjust over time.
1144 uint64_t curtime = getTimeInNanoseconds();
1145 if (curtime == 0) {
1146 // Since we didn't find out the current time, don't give a
1147 // desiredPresentTime:
1148 ptime.desiredPresentTime = 0;
1149 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001150 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1151 }
1152 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001153 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001154 }
1155 ptime.presentID = demo->next_present_id++;
1156 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001157
1158 VkPresentTimesInfoGOOGLE present_time = {
1159 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1160 .pNext = present.pNext,
1161 .swapchainCount = present.swapchainCount,
1162 .pTimes = &ptime,
1163 };
1164 if (demo->VK_GOOGLE_display_timing_enabled) {
1165 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001166 }
1167 }
1168
Tony Barboura2a67812016-07-18 13:21:06 -06001169 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001170 demo->frame_index += 1;
1171 demo->frame_index %= FRAME_LAG;
1172
Ian Elliottcf074d32015-10-16 18:02:43 -06001173 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1174 // demo->swapchain is out of date (e.g. the window was resized) and
1175 // must be recreated:
1176 demo_resize(demo);
1177 } else if (err == VK_SUBOPTIMAL_KHR) {
1178 // demo->swapchain is not as optimal as it could be, but the platform's
1179 // presentation engine will still present the image correctly.
Tony-LunarG6cebf142019-09-11 14:32:23 -06001180 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1181 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1182 demo_create_surface(demo);
1183 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06001184 } else {
1185 assert(!err);
1186 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001187}
1188
Karl Schultze4dc75c2016-02-02 15:37:51 -07001189static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001190 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001191 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001192
Ian Elliottb08e0d92015-11-20 11:55:46 -07001193 // Check the surface capabilities and formats
1194 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001195 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001196 assert(!err);
1197
Ian Elliott8528eff2015-08-07 15:56:59 -06001198 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001199 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001200 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001201 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001202 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001203 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001204 assert(!err);
1205
Ian Elliotta0781972015-08-21 15:09:33 -06001206 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001207 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1208 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1209 // If the surface size is undefined, the size is set to the size
1210 // of the images requested, which must fit within the minimum and
1211 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001212 swapchainExtent.width = demo->width;
1213 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001214
1215 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1216 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1217 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1218 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1219 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001220
Ian Elliottcf7f7482016-08-19 03:46:58 -06001221 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1222 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1223 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1224 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1225 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001226 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001227 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001228 swapchainExtent = surfCapabilities.currentExtent;
1229 demo->width = surfCapabilities.currentExtent.width;
1230 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001231 }
1232
Jeremy Kniager602e4182018-01-19 10:52:34 -07001233 if (demo->width == 0 || demo->height == 0) {
1234 demo->is_minimized = true;
1235 return;
1236 } else {
1237 demo->is_minimized = false;
1238 }
1239
Tony Barbour66a56c32016-09-21 13:10:56 -06001240 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001241 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001242 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001243
Ian Elliottb18fdde2016-10-03 12:11:12 -06001244 // There are times when you may wish to use another present mode. The
1245 // following code shows how to select them, and the comments provide some
1246 // reasons you may wish to use them.
1247 //
1248 // It should be noted that Vulkan 1.0 doesn't provide a method for
1249 // synchronizing rendering with the presentation engine's display. There
1250 // is a method provided for throttling rendering with the display, but
1251 // there are some presentation engines for which this method will not work.
1252 // If an application doesn't throttle its rendering, and if it renders much
1253 // faster than the refresh rate of the display, this can waste power on
1254 // mobile devices. That is because power is being spent rendering images
1255 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001256
Ian Elliottb18fdde2016-10-03 12:11:12 -06001257 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1258 // tearing, or have some way of synchronizing their rendering with the
1259 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001260 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1261 // generally render a new presentable image every refresh cycle, but are
1262 // occasionally early. In this case, the application wants the new image
1263 // to be displayed instead of the previously-queued-for-presentation image
1264 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001265 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1266 // render a new presentable image every refresh cycle, but are occasionally
1267 // late. In this case (perhaps because of stuttering/latency concerns),
1268 // the application wants the late image to be immediately displayed, even
1269 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001270
Dave Houlton5fa47912018-02-16 11:02:26 -07001271 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001272 for (size_t i = 0; i < presentModeCount; ++i) {
1273 if (presentModes[i] == demo->presentMode) {
1274 swapchainPresentMode = demo->presentMode;
1275 break;
1276 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001277 }
1278 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001279 if (swapchainPresentMode != demo->presentMode) {
1280 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1281 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001282
Tony Barbour84376fe2017-01-06 15:54:22 -07001283 // Determine the number of VkImages to use in the swap chain.
1284 // Application desires to acquire 3 images at a time for triple
1285 // buffering
1286 uint32_t desiredNumOfSwapchainImages = 3;
1287 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1288 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1289 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001290 // If maxImageCount is 0, we can ask for as many images as we want;
1291 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001292 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001293 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001294 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001295 }
1296
Tony Barbour9fd6d352015-09-16 15:01:32 -06001297 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001298 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001299 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001300 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001301 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001302 }
1303
Tony Barbour820b55b2017-03-14 08:55:46 -06001304 // Find a supported composite alpha mode - one of these is guaranteed to be set
1305 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1306 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1307 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1308 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1309 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1310 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1311 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001312 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001313 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1314 compositeAlpha = compositeAlphaFlags[i];
1315 break;
1316 }
1317 }
1318
Tony Barboura2a67812016-07-18 13:21:06 -06001319 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001320 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001321 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001322 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001323 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001324 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001325 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001326 .imageExtent =
1327 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001328 .width = swapchainExtent.width,
1329 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001330 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001331 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001332 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001333 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001334 .imageArrayLayers = 1,
1335 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1336 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001337 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001338 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001339 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001340 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001341 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001342 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001343 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001344 assert(!err);
1345
Ian Elliottcf074d32015-10-16 18:02:43 -06001346 // If we just re-created an existing swapchain, we should destroy the old
1347 // swapchain at this point.
1348 // Note: destroying the swapchain also cleans up all its associated
1349 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001350 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001351 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001352 }
1353
Dave Houlton5fa47912018-02-16 11:02:26 -07001354 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001355 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001356
Dave Houlton5fa47912018-02-16 11:02:26 -07001357 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001358 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001359 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001360 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001361
Dave Houlton5fa47912018-02-16 11:02:26 -07001362 demo->swapchain_image_resources =
1363 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001364 assert(demo->swapchain_image_resources);
1365
Ian Elliotta0781972015-08-21 15:09:33 -06001366 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001367 VkImageViewCreateInfo color_image_view = {
1368 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001369 .pNext = NULL,
1370 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001371 .components =
1372 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001373 .r = VK_COMPONENT_SWIZZLE_R,
1374 .g = VK_COMPONENT_SWIZZLE_G,
1375 .b = VK_COMPONENT_SWIZZLE_B,
1376 .a = VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001377 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001378 .subresourceRange =
1379 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001380 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1381 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001382 };
1383
Tony Barboura72d9492017-01-11 13:35:09 -07001384 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001385
Tony Barboura72d9492017-01-11 13:35:09 -07001386 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001387
Dave Houlton5fa47912018-02-16 11:02:26 -07001388 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389 assert(!err);
1390 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001391
Ian Elliottd940ca22017-03-22 08:31:27 -06001392 if (demo->VK_GOOGLE_display_timing_enabled) {
1393 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001394 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001395 assert(!err);
1396 demo->refresh_duration = rc_dur.refreshDuration;
1397
1398 demo->syncd_with_actual_presents = false;
1399 // Initially target 1X the refresh duration:
1400 demo->target_IPD = demo->refresh_duration;
1401 demo->refresh_duration_multiplier = 1;
1402 demo->prev_desired_present_time = 0;
1403 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001404 }
1405
Jason Chen5d4fee92019-04-04 12:45:29 +08001406 if (NULL != swapchainImages) {
1407 free(swapchainImages);
1408 }
1409
Mark Young04fd3d82016-01-25 14:43:50 -07001410 if (NULL != presentModes) {
1411 free(presentModes);
1412 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413}
1414
Karl Schultze4dc75c2016-02-02 15:37:51 -07001415static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001416 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001417 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001418 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001420 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001422 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001424 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001425 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001426 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001427 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428 .flags = 0,
1429 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001430
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001431 VkImageViewCreateInfo view = {
1432 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001433 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001434 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001435 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001436 .subresourceRange =
1437 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001439 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001440 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001441
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001442 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001443 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001444 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001445
1446 demo->depth.format = depth_format;
1447
1448 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001449 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001450 assert(!err);
1451
Karl Schultze4dc75c2016-02-02 15:37:51 -07001452 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001453 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001454
Chia-I Wuf48700b2015-11-10 16:21:09 +08001455 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001456 demo->depth.mem_alloc.pNext = NULL;
1457 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1458 demo->depth.mem_alloc.memoryTypeIndex = 0;
1459
Dave Houlton5fa47912018-02-16 11:02:26 -07001460 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001461 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001462 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001463
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001464 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001465 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001466 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001467
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001468 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001469 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001470 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001471
1472 /* create image view */
1473 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001474 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001475 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001476}
1477
Karl Schultzb7940402018-05-29 13:09:22 -06001478/* Convert ppm image data from header file into RGBA texture image */
1479#include "lunarg.ppm.h"
Dave Houlton5fa47912018-02-16 11:02:26 -07001480bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Karl Schultzb7940402018-05-29 13:09:22 -06001481 (void)filename;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001482 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001483 cPtr = (char *)lunarg_ppm;
1484 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001485 return false;
1486 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001487 while (strncmp(cPtr++, "\n", 1))
1488 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001489 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001490 if (rgba_data == NULL) {
1491 return true;
1492 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001493 while (strncmp(cPtr++, "\n", 1))
1494 ;
1495 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001496 return false;
1497 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001498 while (strncmp(cPtr++, "\n", 1))
1499 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001500 for (int y = 0; y < *height; y++) {
1501 uint8_t *rowPtr = rgba_data;
1502 for (int x = 0; x < *width; x++) {
1503 memcpy(rowPtr, cPtr, 3);
1504 rowPtr[3] = 255; /* Alpha of 1 */
1505 rowPtr += 4;
1506 cPtr += 3;
1507 }
1508 rgba_data += layout->rowPitch;
1509 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001510 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001511}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001512
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001513static void demo_prepare_texture_buffer(struct demo *demo, const char *filename, struct texture_object *tex_obj) {
1514 int32_t tex_width;
1515 int32_t tex_height;
1516 VkResult U_ASSERT_ONLY err;
1517 bool U_ASSERT_ONLY pass;
1518
1519 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
1520 ERR_EXIT("Failed to load textures", "Load Texture Failure");
1521 }
1522
1523 tex_obj->tex_width = tex_width;
1524 tex_obj->tex_height = tex_height;
1525
1526 const VkBufferCreateInfo buffer_create_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
1527 .pNext = NULL,
1528 .flags = 0,
1529 .size = tex_width * tex_height * 4,
1530 .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1531 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1532 .queueFamilyIndexCount = 0,
1533 .pQueueFamilyIndices = NULL};
1534
1535 err = vkCreateBuffer(demo->device, &buffer_create_info, NULL, &tex_obj->buffer);
1536 assert(!err);
1537
1538 VkMemoryRequirements mem_reqs;
1539 vkGetBufferMemoryRequirements(demo->device, tex_obj->buffer, &mem_reqs);
1540
1541 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1542 tex_obj->mem_alloc.pNext = NULL;
1543 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1544 tex_obj->mem_alloc.memoryTypeIndex = 0;
1545
1546 VkFlags requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1547 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, requirements, &tex_obj->mem_alloc.memoryTypeIndex);
1548 assert(pass);
1549
1550 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
1551 assert(!err);
1552
1553 /* bind memory */
1554 err = vkBindBufferMemory(demo->device, tex_obj->buffer, tex_obj->mem, 0);
1555 assert(!err);
1556
1557 VkSubresourceLayout layout;
1558 memset(&layout, 0, sizeof(layout));
1559 layout.rowPitch = tex_width * 4;
1560
1561 void *data;
1562 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
1563 assert(!err);
1564
1565 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1566 fprintf(stderr, "Error loading texture: %s\n", filename);
1567 }
1568
1569 vkUnmapMemory(demo->device, tex_obj->mem);
1570}
1571
Dave Houlton5fa47912018-02-16 11:02:26 -07001572static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1573 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001574 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001575 int32_t tex_width;
1576 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001577 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001578 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001579
Karl Schultze4dc75c2016-02-02 15:37:51 -07001580 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001581 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001582 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001583
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001584 tex_obj->tex_width = tex_width;
1585 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001586
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001587 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001588 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001589 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001590 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001591 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001592 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001593 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001594 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001595 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001596 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001597 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001598 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001599 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001600 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001601
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001602 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001603
Dave Houlton5fa47912018-02-16 11:02:26 -07001604 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001605 assert(!err);
1606
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001607 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001608
Chia-I Wuf48700b2015-11-10 16:21:09 +08001609 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001610 tex_obj->mem_alloc.pNext = NULL;
1611 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1612 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001613
Dave Houlton5fa47912018-02-16 11:02:26 -07001614 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001615 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001616
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001617 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001618 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001619 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001620
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001621 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001622 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001623 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001624
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001625 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001626 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001627 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001628 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001629 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001630 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001631 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001632 void *data;
1633
Dave Houlton5fa47912018-02-16 11:02:26 -07001634 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001635
Dave Houlton5fa47912018-02-16 11:02:26 -07001636 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001637 assert(!err);
1638
1639 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1640 fprintf(stderr, "Error loading texture: %s\n", filename);
1641 }
1642
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001643 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001644 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001645
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001646 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001647}
1648
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001649static void demo_destroy_texture(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001650 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001651 vkFreeMemory(demo->device, tex_objs->mem, NULL);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001652 if (tex_objs->image) vkDestroyImage(demo->device, tex_objs->image, NULL);
1653 if (tex_objs->buffer) vkDestroyBuffer(demo->device, tex_objs->buffer, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001654}
1655
Karl Schultze4dc75c2016-02-02 15:37:51 -07001656static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001657 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001658 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001659 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001660
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001661 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001662
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001663 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001664 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001665
Dave Houlton5fa47912018-02-16 11:02:26 -07001666 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001667 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001668 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1669 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001670 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1671 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001672 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1673 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001674 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001675 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001676 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001677 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001678
Tony Barbour16156cb2016-10-19 14:15:49 -06001679 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001680 demo_prepare_texture_buffer(demo, tex_files[i], &demo->staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001681
Dave Houlton5fa47912018-02-16 11:02:26 -07001682 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1683 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1684 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001685
Dave Houlton5fa47912018-02-16 11:02:26 -07001686 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1687 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001688 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001689
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001690 VkBufferImageCopy copy_region = {
1691 .bufferOffset = 0,
1692 .bufferRowLength = demo->staging_texture.tex_width,
1693 .bufferImageHeight = demo->staging_texture.tex_height,
1694 .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1695 .imageOffset = {0, 0, 0},
1696 .imageExtent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001697 };
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001698
1699 vkCmdCopyBufferToImage(demo->cmd, demo->staging_texture.buffer, demo->textures[i].image,
1700 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001701
Dave Houlton5fa47912018-02-16 11:02:26 -07001702 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1703 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001704 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001705
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001706 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001707 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1708 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001709 }
1710
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001711 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001712 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001713 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001714 .magFilter = VK_FILTER_NEAREST,
1715 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001716 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001717 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1718 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1719 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001720 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001721 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001722 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001723 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001724 .minLod = 0.0f,
1725 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001726 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001727 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001728 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001729
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001730 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001731 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001732 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001733 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001734 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001735 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001736 .components =
1737 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001738 VK_COMPONENT_SWIZZLE_R,
1739 VK_COMPONENT_SWIZZLE_G,
1740 VK_COMPONENT_SWIZZLE_B,
1741 VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001742 },
1743 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001744 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001746
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001748 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001749 assert(!err);
1750
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751 /* create image view */
1752 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001753 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001754 assert(!err);
1755 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001756}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001757
Tony Barboura72d9492017-01-11 13:35:09 -07001758void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001759 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001760 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001761 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001762 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001763 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001764 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001765 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001766 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001767
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001768 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001769 mat4x4_mul(MVP, VP, demo->model_matrix);
1770 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001771 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001772
Karl Schultza2615462017-01-20 13:19:20 -07001773 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001774 data.position[i][0] = g_vertex_buffer_data[i * 3];
1775 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1776 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001777 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001778 data.attr[i][0] = g_uv_buffer_data[2 * i];
1779 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001780 data.attr[i][2] = 0;
1781 data.attr[i][3] = 0;
1782 }
1783
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001784 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001785 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001786 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001787 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001788
Tony Barboura72d9492017-01-11 13:35:09 -07001789 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001790 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001791 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001792
Dave Houlton5fa47912018-02-16 11:02:26 -07001793 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001794
Tony Barboura72d9492017-01-11 13:35:09 -07001795 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1796 mem_alloc.pNext = NULL;
1797 mem_alloc.allocationSize = mem_reqs.size;
1798 mem_alloc.memoryTypeIndex = 0;
1799
Dave Houlton5fa47912018-02-16 11:02:26 -07001800 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1801 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1802 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001803 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001804
Dave Houlton5fa47912018-02-16 11:02:26 -07001805 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001806 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001807
Dave Houlton5fa47912018-02-16 11:02:26 -07001808 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, 0, (void **)&pData);
Tony Barboura72d9492017-01-11 13:35:09 -07001809 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001810
Tony Barboura72d9492017-01-11 13:35:09 -07001811 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001812
Tony Barboura72d9492017-01-11 13:35:09 -07001813 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001814
Tony Barboura72d9492017-01-11 13:35:09 -07001815 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001816 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001817 assert(!err);
1818 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001819}
1820
Karl Schultze4dc75c2016-02-02 15:37:51 -07001821static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001822 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001823 [0] =
1824 {
1825 .binding = 0,
1826 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1827 .descriptorCount = 1,
1828 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1829 .pImmutableSamplers = NULL,
1830 },
1831 [1] =
1832 {
1833 .binding = 1,
1834 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1835 .descriptorCount = DEMO_TEXTURE_COUNT,
1836 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1837 .pImmutableSamplers = NULL,
1838 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001839 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001840 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001841 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001842 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001843 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001844 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001845 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001846 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001847
Dave Houlton5fa47912018-02-16 11:02:26 -07001848 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001849 assert(!err);
1850
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001851 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001852 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1853 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001854 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001855 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001856 };
1857
Dave Houlton5fa47912018-02-16 11:02:26 -07001858 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001859 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001860}
1861
Karl Schultze4dc75c2016-02-02 15:37:51 -07001862static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001863 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1864 // because at the start of the renderpass, we don't care about their contents.
1865 // At the start of the subpass, the color attachment's layout will be transitioned
1866 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1867 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1868 // the renderpass, the color attachment's layout will be transitioned to
1869 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1870 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001871 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001872 [0] =
1873 {
1874 .format = demo->format,
1875 .flags = 0,
1876 .samples = VK_SAMPLE_COUNT_1_BIT,
1877 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1878 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1879 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1880 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1881 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1882 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1883 },
1884 [1] =
1885 {
1886 .format = demo->depth.format,
1887 .flags = 0,
1888 .samples = VK_SAMPLE_COUNT_1_BIT,
1889 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1890 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1891 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1892 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1893 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1894 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1895 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001896 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001897 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001898 .attachment = 0,
1899 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001900 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001901 const VkAttachmentReference depth_reference = {
1902 .attachment = 1,
1903 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1904 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001905 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001906 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1907 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001908 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001909 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001910 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001911 .pColorAttachments = &color_reference,
1912 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001913 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001914 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001915 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001916 };
Tony-LunarG495604b2019-06-13 15:32:05 -06001917
1918 VkSubpassDependency attachmentDependencies[2] = {
1919 [0] =
1920 {
1921 // Depth buffer is shared between swapchain images
1922 .srcSubpass = VK_SUBPASS_EXTERNAL,
1923 .dstSubpass = 0,
1924 .srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1925 .dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1926 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1927 .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1928 .dependencyFlags = 0,
1929 },
1930 [1] =
1931 {
1932 // Image Layout Transition
1933 .srcSubpass = VK_SUBPASS_EXTERNAL,
1934 .dstSubpass = 0,
1935 .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1936 .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1937 .srcAccessMask = 0,
1938 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
1939 .dependencyFlags = 0,
1940 },
1941 };
1942
Chia-I Wuf973e322015-07-08 13:34:24 +08001943 const VkRenderPassCreateInfo rp_info = {
1944 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1945 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001946 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001947 .attachmentCount = 2,
1948 .pAttachments = attachments,
1949 .subpassCount = 1,
1950 .pSubpasses = &subpass,
Tony-LunarG495604b2019-06-13 15:32:05 -06001951 .dependencyCount = 2,
1952 .pDependencies = attachmentDependencies,
Chia-I Wuf973e322015-07-08 13:34:24 +08001953 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001954 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001955
Chia-I Wu63636062015-10-26 21:10:41 +08001956 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001957 assert(!err);
1958}
1959
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001960static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001961 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001962 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001963 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001965 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1966 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001967 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001968 moduleCreateInfo.codeSize = size;
1969 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001970
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001971 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1972 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001973
1974 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001975}
1976
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001977static void demo_prepare_vs(struct demo *demo) {
1978 const uint32_t vs_code[] = {
1979#include "cube.vert.inc"
1980 };
1981 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982}
1983
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001984static void demo_prepare_fs(struct demo *demo) {
1985 const uint32_t fs_code[] = {
1986#include "cube.frag.inc"
1987 };
1988 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001989}
1990
Karl Schultze4dc75c2016-02-02 15:37:51 -07001991static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001992 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001993 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001994 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001995 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001996 VkPipelineRasterizationStateCreateInfo rs;
1997 VkPipelineColorBlendStateCreateInfo cb;
1998 VkPipelineDepthStencilStateCreateInfo ds;
1999 VkPipelineViewportStateCreateInfo vp;
2000 VkPipelineMultisampleStateCreateInfo ms;
2001 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
2002 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06002003 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002004
Piers Daniellba839d12015-09-29 13:01:09 -06002005 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2006 memset(&dynamicState, 0, sizeof dynamicState);
2007 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2008 dynamicState.pDynamicStates = dynamicStateEnables;
2009
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002010 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002011 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002012 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002013
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002014 memset(&vi, 0, sizeof(vi));
2015 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2016
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002017 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002018 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002019 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002020
2021 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002022 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2023 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002024 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002025 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002026 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002027 rs.rasterizerDiscardEnable = VK_FALSE;
2028 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002029 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030
2031 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002032 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2033 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002034 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002035 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002036 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002037 cb.attachmentCount = 1;
2038 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002039
Tony Barbour29645d02015-01-16 14:27:35 -07002040 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002041 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002042 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002043 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002044 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002045 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002046
2047 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002048 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002049 ds.depthTestEnable = VK_TRUE;
2050 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002051 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002052 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002053 ds.back.failOp = VK_STENCIL_OP_KEEP;
2054 ds.back.passOp = VK_STENCIL_OP_KEEP;
2055 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002056 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002057 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002058
Tony Barbour29645d02015-01-16 14:27:35 -07002059 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002060 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002061 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002062 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002063
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002064 demo_prepare_vs(demo);
2065 demo_prepare_fs(demo);
2066
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002067 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002068 VkPipelineShaderStageCreateInfo shaderStages[2];
2069 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2070
Karl Schultze4dc75c2016-02-02 15:37:51 -07002071 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2072 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002073 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002074 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002075
Karl Schultze4dc75c2016-02-02 15:37:51 -07002076 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2077 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002078 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002079 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002080
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002081 memset(&pipelineCache, 0, sizeof(pipelineCache));
2082 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002083
Dave Houlton5fa47912018-02-16 11:02:26 -07002084 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002085 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002086
Karl Schultze4dc75c2016-02-02 15:37:51 -07002087 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002088 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002089 pipeline.pRasterizationState = &rs;
2090 pipeline.pColorBlendState = &cb;
2091 pipeline.pMultisampleState = &ms;
2092 pipeline.pViewportState = &vp;
2093 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002094 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002095 pipeline.pStages = shaderStages;
2096 pipeline.renderPass = demo->render_pass;
2097 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002098
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002099 pipeline.renderPass = demo->render_pass;
2100
Dave Houlton5fa47912018-02-16 11:02:26 -07002101 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002102 assert(!err);
2103
Chia-I Wu63636062015-10-26 21:10:41 +08002104 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2105 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002106}
2107
Karl Schultze4dc75c2016-02-02 15:37:51 -07002108static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002109 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07002110 [0] =
2111 {
2112 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2113 .descriptorCount = demo->swapchainImageCount,
2114 },
2115 [1] =
2116 {
2117 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2118 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2119 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002120 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002121 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002122 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002123 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002124 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002125 .poolSizeCount = 2,
2126 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002127 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002128 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002129
Dave Houlton5fa47912018-02-16 11:02:26 -07002130 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002131 assert(!err);
2132}
2133
Karl Schultze4dc75c2016-02-02 15:37:51 -07002134static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002135 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002136 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002137 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002138
Dave Houlton5fa47912018-02-16 11:02:26 -07002139 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2140 .pNext = NULL,
2141 .descriptorPool = demo->desc_pool,
2142 .descriptorSetCount = 1,
2143 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002144
2145 VkDescriptorBufferInfo buffer_info;
2146 buffer_info.offset = 0;
2147 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002148
Chia-I Wuae721ba2015-05-25 16:27:55 +08002149 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002150 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002151 tex_descs[i].sampler = demo->textures[i].sampler;
2152 tex_descs[i].imageView = demo->textures[i].view;
Karl Schultze88bc842018-09-11 16:23:14 -06002153 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002154 }
2155
2156 memset(&writes, 0, sizeof(writes));
2157
2158 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002159 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002160 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002161 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002162
2163 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002164 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002165 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002166 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002167 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002168
Tony Barboura72d9492017-01-11 13:35:09 -07002169 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2170 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2171 assert(!err);
2172 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2173 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2174 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2175 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2176 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002177}
2178
Karl Schultze4dc75c2016-02-02 15:37:51 -07002179static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002180 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002181 attachments[1] = demo->depth.view;
2182
Chia-I Wuf973e322015-07-08 13:34:24 +08002183 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002184 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2185 .pNext = NULL,
2186 .renderPass = demo->render_pass,
2187 .attachmentCount = 2,
2188 .pAttachments = attachments,
2189 .width = demo->width,
2190 .height = demo->height,
2191 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002192 };
2193 VkResult U_ASSERT_ONLY err;
2194 uint32_t i;
2195
Tony Barbourd8e504f2015-10-08 14:26:24 -06002196 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002197 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002198 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002199 assert(!err);
2200 }
2201}
2202
Karl Schultze4dc75c2016-02-02 15:37:51 -07002203static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002204 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002205 if (demo->cmd_pool == VK_NULL_HANDLE) {
2206 const VkCommandPoolCreateInfo cmd_pool_info = {
2207 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2208 .pNext = NULL,
2209 .queueFamilyIndex = demo->graphics_queue_family_index,
2210 .flags = 0,
2211 };
2212 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2213 assert(!err);
2214 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002215
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002216 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002217 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002218 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002219 .commandPool = demo->cmd_pool,
2220 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002221 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002222 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002223 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2224 assert(!err);
2225 VkCommandBufferBeginInfo cmd_buf_info = {
2226 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2227 .pNext = NULL,
2228 .flags = 0,
2229 .pInheritanceInfo = NULL,
2230 };
2231 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2232 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002233
2234 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002235
2236 if (demo->is_minimized) {
2237 demo->prepared = false;
2238 return;
2239 }
2240
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002241 demo_prepare_depth(demo);
2242 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002243 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002244
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002245 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002246 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002247 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002248
Ian Elliotta0781972015-08-21 15:09:33 -06002249 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002250 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002251 assert(!err);
2252 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002253
Tony Barbour22e06272016-09-07 16:07:56 -06002254 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002255 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002256 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2257 .pNext = NULL,
2258 .queueFamilyIndex = demo->present_queue_family_index,
2259 .flags = 0,
2260 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002261 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002262 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002263 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002264 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2265 .pNext = NULL,
2266 .commandPool = demo->present_cmd_pool,
2267 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2268 .commandBufferCount = 1,
2269 };
2270 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002271 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2272 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002273 assert(!err);
2274 demo_build_image_ownership_cmd(demo, i);
2275 }
2276 }
2277
Chia-I Wu63ea9262015-03-26 13:14:16 +08002278 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002279 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002280
Chia-I Wuf973e322015-07-08 13:34:24 +08002281 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002282
Ian Elliotta0781972015-08-21 15:09:33 -06002283 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002284 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002285 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002286 }
2287
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002288 /*
2289 * Prepare functions above may generate pipeline commands
2290 * that need to be flushed before beginning the render loop.
2291 */
2292 demo_flush_init_cmd(demo);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06002293 if (demo->staging_texture.buffer) {
2294 demo_destroy_texture(demo, &demo->staging_texture);
Tony Barbour16156cb2016-10-19 14:15:49 -06002295 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002296
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002297 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002298 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002299}
2300
Karl Schultze4dc75c2016-02-02 15:37:51 -07002301static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002302 uint32_t i;
2303
2304 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002305 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002306
Tony Barbour66a56c32016-09-21 13:10:56 -06002307 // Wait for fences from present operations
2308 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002309 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002310 vkDestroyFence(demo->device, demo->fences[i], NULL);
2311 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2312 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2313 if (demo->separate_present_queue) {
2314 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2315 }
2316 }
2317
Jeremy Kniager602e4182018-01-19 10:52:34 -07002318 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2319 if (!demo->is_minimized) {
2320 for (i = 0; i < demo->swapchainImageCount; i++) {
2321 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2322 }
2323 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002324
Jeremy Kniager602e4182018-01-19 10:52:34 -07002325 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2326 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2327 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2328 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2329 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002330
Jeremy Kniager602e4182018-01-19 10:52:34 -07002331 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2332 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2333 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2334 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2335 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2336 }
2337 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002338
Jeremy Kniager602e4182018-01-19 10:52:34 -07002339 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2340 vkDestroyImage(demo->device, demo->depth.image, NULL);
2341 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002342
Jeremy Kniager602e4182018-01-19 10:52:34 -07002343 for (i = 0; i < demo->swapchainImageCount; i++) {
2344 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2345 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2346 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2347 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2348 }
2349 free(demo->swapchain_image_resources);
2350 free(demo->queue_props);
2351 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002352
Jeremy Kniager602e4182018-01-19 10:52:34 -07002353 if (demo->separate_present_queue) {
2354 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2355 }
Tony Barbour22e06272016-09-07 16:07:56 -06002356 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002357 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002358 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002359 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07002360 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002361 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002362 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002363
Tony Barbour153cb062016-12-07 13:43:36 -07002364#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002365 XDestroyWindow(demo->display, demo->xlib_window);
2366 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002367#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002368 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002369 xcb_disconnect(demo->connection);
2370 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002371#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002372 wl_keyboard_destroy(demo->keyboard);
2373 wl_pointer_destroy(demo->pointer);
2374 wl_seat_destroy(demo->seat);
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002375 xdg_toplevel_destroy(demo->xdg_toplevel);
2376 xdg_surface_destroy(demo->xdg_surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002377 wl_surface_destroy(demo->window);
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002378 xdg_wm_base_destroy(demo->xdg_wm_base);
2379 if (demo->xdg_decoration_mgr) {
2380 zxdg_toplevel_decoration_v1_destroy(demo->toplevel_decoration);
2381 zxdg_decoration_manager_v1_destroy(demo->xdg_decoration_mgr);
2382 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002383 wl_compositor_destroy(demo->compositor);
2384 wl_registry_destroy(demo->registry);
2385 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002386#endif
Karl Schultz86429112017-06-20 11:27:59 -06002387
2388 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002389}
2390
Karl Schultze4dc75c2016-02-02 15:37:51 -07002391static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002392 uint32_t i;
2393
Mike Stroyanbb60b382015-10-28 09:46:57 -06002394 // Don't react to resize until after first initialization.
2395 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002396 if (demo->is_minimized) {
2397 demo_prepare(demo);
2398 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002399 return;
2400 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002401 // In order to properly resize the window, we must re-create the swapchain
2402 // AND redo the command buffers, etc.
2403 //
2404 // First, perform part of the demo_cleanup() function:
2405 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002406 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002407
2408 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002409 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002410 }
Chia-I Wu63636062015-10-26 21:10:41 +08002411 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002412
Chia-I Wu63636062015-10-26 21:10:41 +08002413 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2414 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2415 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2416 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2417 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002418
2419 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002420 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2421 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2422 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2423 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002424 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002425
Chia-I Wu63636062015-10-26 21:10:41 +08002426 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2427 vkDestroyImage(demo->device, demo->depth.image, NULL);
2428 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002429
Ian Elliottcf074d32015-10-16 18:02:43 -06002430 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002431 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002432 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002433 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2434 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002435 }
Chia-I Wu63636062015-10-26 21:10:41 +08002436 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002437 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002438 if (demo->separate_present_queue) {
2439 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2440 }
Tony Barboura72d9492017-01-11 13:35:09 -07002441 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002442
Ian Elliottcf074d32015-10-16 18:02:43 -06002443 // Second, re-perform the demo_prepare() function, which will re-create the
2444 // swapchain:
2445 demo_prepare(demo);
2446}
2447
David Pinedo2bb7c932015-06-18 17:03:14 -06002448// On MS-Windows, make this a global, so it's available to WndProc()
2449struct demo demo;
2450
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002451#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002452static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002453 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002454
Ian Elliott639ca472015-04-16 15:23:05 -06002455 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002456 demo->curFrame++;
Petr Krausd88e4e52019-01-23 18:45:04 +01002457 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002458 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002459 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002460}
Ian Elliott639ca472015-04-16 15:23:05 -06002461
2462// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002463LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2464 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002465 case WM_CLOSE:
2466 PostQuitMessage(validation_error);
2467 break;
2468 case WM_PAINT:
2469 // The validation callback calls MessageBox which can generate paint
2470 // events - don't make more Vulkan calls if we got here from the
2471 // callback
2472 if (!in_callback) {
2473 demo_run(&demo);
2474 }
2475 break;
2476 case WM_GETMINMAXINFO: // set window's minimum size
2477 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2478 return 0;
Aaron Hagan500b9c32018-10-03 21:56:29 -04002479 case WM_ERASEBKGND:
2480 return 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002481 case WM_SIZE:
2482 // Resize the application to the new window size, except when
2483 // it was minimized. Vulkan doesn't support images or swapchains
2484 // with width=0 and height=0.
2485 if (wParam != SIZE_MINIMIZED) {
2486 demo.width = lParam & 0xffff;
2487 demo.height = (lParam & 0xffff0000) >> 16;
2488 demo_resize(&demo);
2489 }
2490 break;
Petr Krausd88e4e52019-01-23 18:45:04 +01002491 case WM_KEYDOWN:
2492 switch (wParam) {
2493 case VK_ESCAPE:
2494 PostQuitMessage(validation_error);
2495 break;
2496 case VK_LEFT:
2497 demo.spin_angle -= demo.spin_increment;
2498 break;
2499 case VK_RIGHT:
2500 demo.spin_angle += demo.spin_increment;
2501 break;
2502 case VK_SPACE:
2503 demo.pause = !demo.pause;
2504 break;
2505 }
2506 return 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002507 default:
2508 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002509 }
2510 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2511}
2512
Karl Schultze4dc75c2016-02-02 15:37:51 -07002513static void demo_create_window(struct demo *demo) {
2514 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002515
2516 // Initialize the window class structure:
2517 win_class.cbSize = sizeof(WNDCLASSEX);
2518 win_class.style = CS_HREDRAW | CS_VREDRAW;
2519 win_class.lpfnWndProc = WndProc;
2520 win_class.cbClsExtra = 0;
2521 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002522 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002523 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2524 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2525 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2526 win_class.lpszMenuName = NULL;
2527 win_class.lpszClassName = demo->name;
2528 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2529 // Register window class:
2530 if (!RegisterClassEx(&win_class)) {
2531 // It didn't work, so try to give a useful error:
2532 printf("Unexpected error trying to start the application!\n");
2533 fflush(stdout);
2534 exit(1);
2535 }
2536 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002537 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002538 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002539 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002540 demo->name, // class name
2541 demo->name, // app name
2542 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002543 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002544 100, 100, // x/y coords
2545 wr.right - wr.left, // width
2546 wr.bottom - wr.top, // height
2547 NULL, // handle to parent
2548 NULL, // handle to menu
2549 demo->connection, // hInstance
2550 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002551 if (!demo->window) {
2552 // It didn't work, so try to give a useful error:
2553 printf("Cannot create a window in which to draw!\n");
2554 fflush(stdout);
2555 exit(1);
2556 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002557 // Window client area size must be at least 1 pixel high, to prevent crash.
2558 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002559 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002560}
Tony Barbour8295ac42016-08-08 11:04:17 -06002561#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002562static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002563 const char *display_envar = getenv("DISPLAY");
2564 if (display_envar == NULL || display_envar[0] == '\0') {
2565 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2566 fflush(stdout);
2567 exit(1);
2568 }
Tony Barbour2593b182016-04-19 10:57:58 -06002569
Tony Barbouree9cd372017-01-31 16:09:58 -07002570 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002571 demo->display = XOpenDisplay(NULL);
2572 long visualMask = VisualScreenMask;
2573 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002574 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002575 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002576 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002577
Dave Houlton5fa47912018-02-16 11:02:26 -07002578 Colormap colormap =
2579 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002580
Dave Houlton5fa47912018-02-16 11:02:26 -07002581 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002582 windowAttributes.colormap = colormap;
2583 windowAttributes.background_pixel = 0xFFFFFFFF;
2584 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002585 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002586
Dave Houlton5fa47912018-02-16 11:02:26 -07002587 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2588 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2589 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002590
2591 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2592 XMapWindow(demo->display, demo->xlib_window);
2593 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002594 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002595}
2596static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002597 switch (event->type) {
2598 case ClientMessage:
2599 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002600 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002601 case KeyPress:
2602 switch (event->xkey.keycode) {
2603 case 0x9: // Escape
2604 demo->quit = true;
2605 break;
2606 case 0x71: // left arrow key
2607 demo->spin_angle -= demo->spin_increment;
2608 break;
2609 case 0x72: // right arrow key
2610 demo->spin_angle += demo->spin_increment;
2611 break;
2612 case 0x41: // space bar
2613 demo->pause = !demo->pause;
2614 break;
2615 }
Tony Barbour2593b182016-04-19 10:57:58 -06002616 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002617 case ConfigureNotify:
2618 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2619 demo->width = event->xconfigure.width;
2620 demo->height = event->xconfigure.height;
2621 demo_resize(demo);
2622 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002623 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002624 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002625 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002626 }
Tony Barbour2593b182016-04-19 10:57:58 -06002627}
2628
2629static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002630 while (!demo->quit) {
2631 XEvent event;
2632
2633 if (demo->pause) {
2634 XNextEvent(demo->display, &event);
2635 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002636 }
2637 while (XPending(demo->display) > 0) {
2638 XNextEvent(demo->display, &event);
2639 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002640 }
2641
Tony Barbour2593b182016-04-19 10:57:58 -06002642 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002643 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002644 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002645 }
2646}
Tony Barbour153cb062016-12-07 13:43:36 -07002647#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002648static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002649 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002650 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002651 case XCB_EXPOSE:
2652 // TODO: Resize window
2653 break;
2654 case XCB_CLIENT_MESSAGE:
2655 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2656 demo->quit = true;
2657 }
2658 break;
2659 case XCB_KEY_RELEASE: {
2660 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002661
Dave Houlton5fa47912018-02-16 11:02:26 -07002662 switch (key->detail) {
2663 case 0x9: // Escape
2664 demo->quit = true;
2665 break;
2666 case 0x71: // left arrow key
2667 demo->spin_angle -= demo->spin_increment;
2668 break;
2669 case 0x72: // right arrow key
2670 demo->spin_angle += demo->spin_increment;
2671 break;
2672 case 0x41: // space bar
2673 demo->pause = !demo->pause;
2674 break;
2675 }
2676 } break;
2677 case XCB_CONFIGURE_NOTIFY: {
2678 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2679 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2680 demo->width = cfg->width;
2681 demo->height = cfg->height;
2682 demo_resize(demo);
2683 }
2684 } break;
2685 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002686 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002687 }
2688}
2689
Tony Barbour2593b182016-04-19 10:57:58 -06002690static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002691 xcb_flush(demo->connection);
2692
2693 while (!demo->quit) {
2694 xcb_generic_event_t *event;
2695
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002696 if (demo->pause) {
2697 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002698 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002699 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002700 }
2701 while (event) {
2702 demo_handle_xcb_event(demo, event);
2703 free(event);
2704 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002705 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002706
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002707 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002708 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002709 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002710 }
2711}
2712
Tony Barbour2593b182016-04-19 10:57:58 -06002713static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002714 uint32_t value_mask, value_list[32];
2715
Tony Barbour2593b182016-04-19 10:57:58 -06002716 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002717
2718 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2719 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002720 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002721
Dave Houlton5fa47912018-02-16 11:02:26 -07002722 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2723 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002724
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002725 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002726 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2727 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002728
Dave Houlton5fa47912018-02-16 11:02:26 -07002729 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2730 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002731
Dave Houlton5fa47912018-02-16 11:02:26 -07002732 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002733 &(*demo->atom_wm_delete_window).atom);
2734 free(reply);
2735
Tony Barbour2593b182016-04-19 10:57:58 -06002736 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002737
Karl Schultze4dc75c2016-02-02 15:37:51 -07002738 // Force the x/y coordinates to 100,100 results are identical in consecutive
2739 // runs
2740 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002741 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002742}
Tony Barbour6b131662016-08-25 13:14:45 -06002743// VK_USE_PLATFORM_XCB_KHR
2744#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002745static void demo_run(struct demo *demo) {
2746 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002747 if (demo->pause) {
2748 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002749 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002750 wl_display_dispatch_pending(demo->display); // don't block
2751 demo_draw(demo);
2752 demo->curFrame++;
2753 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2754 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002755 }
2756}
2757
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002758static void handle_surface_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) {
2759 struct demo *demo = (struct demo *)data;
2760 xdg_surface_ack_configure(xdg_surface, serial);
2761 if (demo->xdg_surface_has_been_configured) {
2762 demo_resize(demo);
2763 }
2764 demo->xdg_surface_has_been_configured = 1;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002765}
2766
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002767static const struct xdg_surface_listener xdg_surface_listener = {handle_surface_configure};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002768
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002769static void handle_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel UNUSED, int32_t width, int32_t height,
2770 struct wl_array *states UNUSED) {
2771 struct demo *demo = (struct demo *)data;
2772 demo->width = width;
2773 demo->height = height;
2774 /* This should be followed by a surface configure */
2775}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002776
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002777static void handle_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel UNUSED) {
2778 struct demo *demo = (struct demo *)data;
2779 demo->quit = true;
2780}
2781
2782static const struct xdg_toplevel_listener xdg_toplevel_listener = {handle_toplevel_configure, handle_toplevel_close};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002783
2784static void demo_create_window(struct demo *demo) {
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002785 if (!demo->xdg_wm_base) {
2786 printf("Compositor did not provide the standard protocol xdg-wm-base\n");
2787 fflush(stdout);
2788 exit(1);
2789 }
2790
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002791 demo->window = wl_compositor_create_surface(demo->compositor);
2792 if (!demo->window) {
2793 printf("Can not create wayland_surface from compositor!\n");
2794 fflush(stdout);
2795 exit(1);
2796 }
2797
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002798 demo->xdg_surface = xdg_wm_base_get_xdg_surface(demo->xdg_wm_base, demo->window);
2799 if (!demo->xdg_surface) {
2800 printf("Can not get xdg_surface from wayland_surface!\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002801 fflush(stdout);
2802 exit(1);
2803 }
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002804 demo->xdg_toplevel = xdg_surface_get_toplevel(demo->xdg_surface);
2805 if (!demo->xdg_toplevel) {
2806 printf("Can not allocate xdg_toplevel for xdg_surface!\n");
2807 fflush(stdout);
2808 exit(1);
2809 }
2810 xdg_surface_add_listener(demo->xdg_surface, &xdg_surface_listener, demo);
2811 xdg_toplevel_add_listener(demo->xdg_toplevel, &xdg_toplevel_listener, demo);
2812 xdg_toplevel_set_title(demo->xdg_toplevel, APP_SHORT_NAME);
2813 if (demo->xdg_decoration_mgr) {
2814 // if supported, let the compositor render titlebars for us
2815 demo->toplevel_decoration =
2816 zxdg_decoration_manager_v1_get_toplevel_decoration(demo->xdg_decoration_mgr, demo->xdg_toplevel);
2817 zxdg_toplevel_decoration_v1_set_mode(demo->toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
2818 }
2819
2820 wl_surface_commit(demo->window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002821}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002822#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2823static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002824 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002825
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002826 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002827 demo->curFrame++;
2828}
Karl Schultz206b1c52018-04-13 18:02:07 -06002829#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2830static void demo_run(struct demo *demo) {
2831 demo_draw(demo);
2832 demo->curFrame++;
2833 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2834 demo->quit = TRUE;
2835 }
2836}
Damien Leone600c3052017-01-31 10:26:07 -07002837#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2838static VkResult demo_create_display_surface(struct demo *demo) {
2839 VkResult U_ASSERT_ONLY err;
2840 uint32_t display_count;
2841 uint32_t mode_count;
2842 uint32_t plane_count;
2843 VkDisplayPropertiesKHR display_props;
2844 VkDisplayKHR display;
2845 VkDisplayModePropertiesKHR mode_props;
2846 VkDisplayPlanePropertiesKHR *plane_props;
2847 VkBool32 found_plane = VK_FALSE;
2848 uint32_t plane_index;
2849 VkExtent2D image_extent;
2850 VkDisplaySurfaceCreateInfoKHR create_info;
2851
2852 // Get the first display
2853 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2854 assert(!err);
2855
2856 if (display_count == 0) {
2857 printf("Cannot find any display!\n");
2858 fflush(stdout);
2859 exit(1);
2860 }
2861
2862 display_count = 1;
2863 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2864 assert(!err || (err == VK_INCOMPLETE));
2865
2866 display = display_props.display;
2867
2868 // Get the first mode of the display
2869 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2870 assert(!err);
2871
2872 if (mode_count == 0) {
2873 printf("Cannot find any mode for the display!\n");
2874 fflush(stdout);
2875 exit(1);
2876 }
2877
2878 mode_count = 1;
2879 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2880 assert(!err || (err == VK_INCOMPLETE));
2881
2882 // Get the list of planes
2883 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2884 assert(!err);
2885
2886 if (plane_count == 0) {
2887 printf("Cannot find any plane!\n");
2888 fflush(stdout);
2889 exit(1);
2890 }
2891
2892 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2893 assert(plane_props);
2894
2895 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2896 assert(!err);
2897
2898 // Find a plane compatible with the display
2899 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2900 uint32_t supported_count;
2901 VkDisplayKHR *supported_displays;
2902
2903 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002904 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002905 continue;
2906 }
2907
2908 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2909 assert(!err);
2910
2911 if (supported_count == 0) {
2912 continue;
2913 }
2914
2915 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2916 assert(supported_displays);
2917
2918 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2919 assert(!err);
2920
2921 for (uint32_t i = 0; i < supported_count; i++) {
2922 if (supported_displays[i] == display) {
2923 found_plane = VK_TRUE;
2924 break;
2925 }
2926 }
2927
2928 free(supported_displays);
2929
2930 if (found_plane) {
2931 break;
2932 }
2933 }
2934
2935 if (!found_plane) {
2936 printf("Cannot find a plane compatible with the display!\n");
2937 fflush(stdout);
2938 exit(1);
2939 }
2940
2941 free(plane_props);
2942
Tony Barbour820b55b2017-03-14 08:55:46 -06002943 VkDisplayPlaneCapabilitiesKHR planeCaps;
2944 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2945 // Find a supported alpha mode
Mark Young66510e52017-11-10 10:05:14 -07002946 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2947 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002948 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2949 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2950 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2951 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2952 };
2953 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2954 if (planeCaps.supportedAlpha & alphaModes[i]) {
2955 alphaMode = alphaModes[i];
2956 break;
2957 }
2958 }
Damien Leone600c3052017-01-31 10:26:07 -07002959 image_extent.width = mode_props.parameters.visibleRegion.width;
2960 image_extent.height = mode_props.parameters.visibleRegion.height;
2961
2962 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2963 create_info.pNext = NULL;
2964 create_info.flags = 0;
2965 create_info.displayMode = mode_props.displayMode;
2966 create_info.planeIndex = plane_index;
2967 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2968 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002969 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002970 create_info.globalAlpha = 1.0f;
2971 create_info.imageExtent = image_extent;
2972
2973 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2974}
2975
Dave Houlton5fa47912018-02-16 11:02:26 -07002976static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07002977 while (!demo->quit) {
2978 demo_draw(demo);
2979 demo->curFrame++;
2980
2981 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2982 demo->quit = true;
2983 }
2984 }
2985}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002986#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002987
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002988/*
2989 * Return 1 (true) if all layer names specified in check_names
2990 * can be found in given layer properties.
2991 */
Dave Houlton5fa47912018-02-16 11:02:26 -07002992static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002993 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002994 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002995 for (uint32_t j = 0; j < layer_count; j++) {
2996 if (!strcmp(check_names[i], layers[j].layerName)) {
2997 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002998 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002999 }
3000 }
3001 if (!found) {
3002 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
3003 return 0;
3004 }
3005 }
3006 return 1;
3007}
3008
Karl Schultze4dc75c2016-02-02 15:37:51 -07003009static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003010 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003011 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003012 uint32_t instance_layer_count = 0;
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003013 char *instance_validation_layers[] = {"VK_LAYER_KHRONOS_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06003014 demo->enabled_extension_count = 0;
3015 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07003016 demo->is_minimized = false;
3017 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003018
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003019 // Look for validation layers
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003020 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003021 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003022 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07003023 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003024
Karl Schultz7c50ad62016-04-01 12:07:03 -06003025 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003026 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
3027 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06003028 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003029
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003030 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
Dave Houlton5fa47912018-02-16 11:02:26 -07003031 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06003032 if (validation_found) {
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003033 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
3034 demo->enabled_layers[0] = "VK_LAYER_KHRONOS_validation";
Karl Schultz7c50ad62016-04-01 12:07:03 -06003035 }
3036 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003037 }
Dustin Graves7c287352016-01-27 13:48:06 -07003038
Karl Schultz7c50ad62016-04-01 12:07:03 -06003039 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003040 ERR_EXIT(
3041 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
3042 "Please look at the Getting Started guide for additional information.\n",
3043 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06003044 }
Dustin Graves7c287352016-01-27 13:48:06 -07003045 }
3046
3047 /* Look for instance extensions */
3048 VkBool32 surfaceExtFound = 0;
3049 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003050 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003051
Dave Houlton5fa47912018-02-16 11:02:26 -07003052 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003053 assert(!err);
3054
Dustin Graves7c287352016-01-27 13:48:06 -07003055 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003056 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3057 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003058 assert(!err);
3059 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003060 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003061 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003062 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003063 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003064#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003065 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003066 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003067 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003068 }
Tony Barbour153cb062016-12-07 13:43:36 -07003069#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003070 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06003071 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003072 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06003073 }
Tony Barbour153cb062016-12-07 13:43:36 -07003074#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003075 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003076 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003077 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003078 }
Tony Barbour153cb062016-12-07 13:43:36 -07003079#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003080 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003081 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003082 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003083 }
Damien Leone600c3052017-01-31 10:26:07 -07003084#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003085 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07003086 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003087 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07003088 }
Tony Barbour153cb062016-12-07 13:43:36 -07003089#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003090 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003091 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003092 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003093 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003094#elif defined(VK_USE_PLATFORM_IOS_MVK)
3095 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3096 platformSurfaceExtFound = 1;
3097 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3098 }
3099#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3100 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3101 platformSurfaceExtFound = 1;
3102 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3103 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003104#endif
Mark Young66510e52017-11-10 10:05:14 -07003105 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3106 if (demo->validate) {
3107 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3108 }
3109 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003110 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003111 }
Dustin Graves7c287352016-01-27 13:48:06 -07003112
3113 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003114 }
Dustin Graves7c287352016-01-27 13:48:06 -07003115
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003116 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003117 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3118 " extension.\n\n"
3119 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3120 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06003121 "vkCreateInstance Failure");
3122 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003123 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003124#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003125 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3126 " extension.\n\n"
3127 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3128 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003129 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003130#elif defined(VK_USE_PLATFORM_IOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003131 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_IOS_SURFACE_EXTENSION_NAME
3132 " extension.\n\n"
3133 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3134 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003135 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003136#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003137 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_MACOS_SURFACE_EXTENSION_NAME
3138 " extension.\n\n"
3139 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3140 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003141 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003142#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003143 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3144 " extension.\n\n"
3145 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3146 "Please look at the Getting Started guide for additional information.\n",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003147 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003148#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003149 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3150 " extension.\n\n"
3151 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3152 "Please look at the Getting Started guide for additional information.\n",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003153 "vkCreateInstance Failure");
Damien Leone600c3052017-01-31 10:26:07 -07003154#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003155 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3156 " extension.\n\n"
3157 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3158 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07003159 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003160#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003161 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3162 " extension.\n\n"
3163 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3164 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003165 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003166#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003167 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3168 " extension.\n\n"
3169 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3170 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06003171 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003172#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003173 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003174 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003175 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003176 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003177 .pApplicationName = APP_SHORT_NAME,
3178 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003179 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003180 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003181 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003182 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003183 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003184 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003185 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003186 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003187 .enabledLayerCount = demo->enabled_layer_count,
3188 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3189 .enabledExtensionCount = demo->enabled_extension_count,
3190 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003191 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003192
3193 /*
3194 * This is info for a temp callback to use during CreateInstance.
3195 * After the instance is created, we use the instance-based
3196 * function to register the final callback.
3197 */
Mark Young66510e52017-11-10 10:05:14 -07003198 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003199 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003200 // VK_EXT_debug_utils style
3201 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3202 dbg_messenger_create_info.pNext = NULL;
3203 dbg_messenger_create_info.flags = 0;
3204 dbg_messenger_create_info.messageSeverity =
3205 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3206 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3207 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3208 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3209 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3210 dbg_messenger_create_info.pUserData = demo;
3211 inst_info.pNext = &dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003212 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003213
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003214 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003215
Chia-I Wu63636062015-10-26 21:10:41 +08003216 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003217 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003218 ERR_EXIT(
3219 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3220 "Please look at the Getting Started guide for additional information.\n",
3221 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003222 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003223 ERR_EXIT(
3224 "Cannot find a specified extension library.\n"
3225 "Make sure your layers path is set appropriately.\n",
3226 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003227 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003228 ERR_EXIT(
3229 "vkCreateInstance failed.\n\n"
3230 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3231 "Please look at the Getting Started guide for additional information.\n",
3232 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003233 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003234
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003235 /* Make initial call to query gpu_count, then second call for gpu info*/
3236 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
Petr Kraus2f1dbdb2018-04-14 14:45:17 +02003237 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003238
3239 if (gpu_count > 0) {
3240 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3241 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3242 assert(!err);
3243 /* For cube demo we just grab the first physical device */
3244 demo->gpu = physical_devices[0];
3245 free(physical_devices);
3246 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003247 ERR_EXIT(
3248 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3249 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3250 "Please look at the Getting Started guide for additional information.\n",
3251 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003252 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003253
Dustin Graves7c287352016-01-27 13:48:06 -07003254 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003255 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003256 VkBool32 swapchainExtFound = 0;
3257 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003258 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003259
Dave Houlton5fa47912018-02-16 11:02:26 -07003260 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003261 assert(!err);
3262
Dustin Graves7c287352016-01-27 13:48:06 -07003263 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003264 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3265 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003266 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003267
Dustin Graves7c287352016-01-27 13:48:06 -07003268 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003269 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003270 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003271 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003272 }
3273 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003274 }
Dustin Graves7c287352016-01-27 13:48:06 -07003275
Ian Elliott53622a72017-03-28 11:06:33 -06003276 if (demo->VK_KHR_incremental_present_enabled) {
3277 // Even though the user "enabled" the extension via the command
3278 // line, we must make sure that it's enumerated for use with the
3279 // device. Therefore, disable it here, and re-enable it again if
3280 // enumerated.
3281 demo->VK_KHR_incremental_present_enabled = false;
3282 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003283 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3284 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003285 demo->VK_KHR_incremental_present_enabled = true;
3286 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3287 }
3288 assert(demo->enabled_extension_count < 64);
3289 }
3290 if (!demo->VK_KHR_incremental_present_enabled) {
3291 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3292 }
3293 }
3294
Ian Elliottd940ca22017-03-22 08:31:27 -06003295 if (demo->VK_GOOGLE_display_timing_enabled) {
3296 // Even though the user "enabled" the extension via the command
3297 // line, we must make sure that it's enumerated for use with the
3298 // device. Therefore, disable it here, and re-enable it again if
3299 // enumerated.
3300 demo->VK_GOOGLE_display_timing_enabled = false;
3301 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003302 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3303 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003304 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003305 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003306 }
3307 assert(demo->enabled_extension_count < 64);
3308 }
3309 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003310 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003311 }
3312 }
3313
Dustin Graves7c287352016-01-27 13:48:06 -07003314 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003315 }
Dustin Graves7c287352016-01-27 13:48:06 -07003316
Tony Barbourcc69f452015-10-08 13:59:42 -06003317 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003318 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3319 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3320 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003321 "vkCreateInstance Failure");
3322 }
3323
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003324 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003325 // Setup VK_EXT_debug_utils function pointers always (we use them for
3326 // debug labels and names).
3327 demo->CreateDebugUtilsMessengerEXT =
3328 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3329 demo->DestroyDebugUtilsMessengerEXT =
3330 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3331 demo->SubmitDebugUtilsMessageEXT =
3332 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3333 demo->CmdBeginDebugUtilsLabelEXT =
3334 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3335 demo->CmdEndDebugUtilsLabelEXT =
3336 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3337 demo->CmdInsertDebugUtilsLabelEXT =
3338 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3339 demo->SetDebugUtilsObjectNameEXT =
3340 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3341 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3342 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3343 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3344 NULL == demo->SetDebugUtilsObjectNameEXT) {
3345 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003346 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003347
Mark Young66510e52017-11-10 10:05:14 -07003348 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003349 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003350 case VK_SUCCESS:
3351 break;
3352 case VK_ERROR_OUT_OF_HOST_MEMORY:
Mark Young66510e52017-11-10 10:05:14 -07003353 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003354 break;
3355 default:
Mark Young66510e52017-11-10 10:05:14 -07003356 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003357 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003358 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003359 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003360 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003361
3362 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003363 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003364 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003365
Dave Houlton5fa47912018-02-16 11:02:26 -07003366 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3367 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003368
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003369 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003370 // If app has specific feature requirements it should check supported
3371 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003372 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003373 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003374
Tony Barbourda2bad52016-01-22 14:36:40 -07003375 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3376 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3377 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3378 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3379 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3380}
3381
Karl Schultze4dc75c2016-02-02 15:37:51 -07003382static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003383 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003384 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003385 VkDeviceQueueCreateInfo queues[2];
3386 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3387 queues[0].pNext = NULL;
3388 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3389 queues[0].queueCount = 1;
3390 queues[0].pQueuePriorities = queue_priorities;
3391 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003392
3393 VkDeviceCreateInfo device = {
3394 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3395 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003396 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003397 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003398 .enabledLayerCount = 0,
3399 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003400 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003401 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003402 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003403 };
Tony Barbour22e06272016-09-07 16:07:56 -06003404 if (demo->separate_present_queue) {
3405 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3406 queues[1].pNext = NULL;
3407 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3408 queues[1].queueCount = 1;
3409 queues[1].pQueuePriorities = queue_priorities;
3410 queues[1].flags = 0;
3411 device.queueCreateInfoCount = 2;
3412 }
Chia-I Wu63636062015-10-26 21:10:41 +08003413 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003414 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003415}
3416
Tony-LunarG6cebf142019-09-11 14:32:23 -06003417static void demo_create_surface(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003418 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003419
Karl Schultze4dc75c2016-02-02 15:37:51 -07003420// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003421#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003422 VkWin32SurfaceCreateInfoKHR createInfo;
3423 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3424 createInfo.pNext = NULL;
3425 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003426 createInfo.hinstance = demo->connection;
3427 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003428
Dave Houlton5fa47912018-02-16 11:02:26 -07003429 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003430#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003431 VkWaylandSurfaceCreateInfoKHR createInfo;
3432 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3433 createInfo.pNext = NULL;
3434 createInfo.flags = 0;
3435 createInfo.display = demo->display;
3436 createInfo.surface = demo->window;
3437
Dave Houlton5fa47912018-02-16 11:02:26 -07003438 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003439#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3440 VkAndroidSurfaceCreateInfoKHR createInfo;
3441 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3442 createInfo.pNext = NULL;
3443 createInfo.flags = 0;
Mike Schuchardt837d5162018-03-08 12:57:02 -07003444 createInfo.window = (struct ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003445
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003446 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003447#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3448 VkXlibSurfaceCreateInfoKHR createInfo;
3449 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3450 createInfo.pNext = NULL;
3451 createInfo.flags = 0;
3452 createInfo.dpy = demo->display;
3453 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003454
Dave Houlton5fa47912018-02-16 11:02:26 -07003455 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003456#elif defined(VK_USE_PLATFORM_XCB_KHR)
3457 VkXcbSurfaceCreateInfoKHR createInfo;
3458 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3459 createInfo.pNext = NULL;
3460 createInfo.flags = 0;
3461 createInfo.connection = demo->connection;
3462 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003463
Tony Barbour153cb062016-12-07 13:43:36 -07003464 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003465#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3466 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003467#elif defined(VK_USE_PLATFORM_IOS_MVK)
3468 VkIOSSurfaceCreateInfoMVK surface;
3469 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3470 surface.pNext = NULL;
3471 surface.flags = 0;
3472 surface.pView = demo->window;
3473
3474 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3475#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3476 VkMacOSSurfaceCreateInfoMVK surface;
3477 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3478 surface.pNext = NULL;
3479 surface.flags = 0;
3480 surface.pView = demo->window;
3481
3482 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003483#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003484 assert(!err);
Tony-LunarG6cebf142019-09-11 14:32:23 -06003485}
3486
3487static void demo_init_vk_swapchain(struct demo *demo) {
3488 VkResult U_ASSERT_ONLY err;
3489
3490 demo_create_surface(demo);
Ian Elliottaaae5352015-07-06 14:27:58 -06003491
Tony Barbourcc69f452015-10-08 13:59:42 -06003492 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003493 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003494 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003495 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003496 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003497
3498 // Search for a graphics and a present queue in the array of queue
3499 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003500 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3501 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003502 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003503 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3504 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3505 graphicsQueueFamilyIndex = i;
3506 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003507
Tony Barbourab2a0362016-09-06 11:40:12 -06003508 if (supportsPresent[i] == VK_TRUE) {
3509 graphicsQueueFamilyIndex = i;
3510 presentQueueFamilyIndex = i;
3511 break;
3512 }
3513 }
3514 }
3515
3516 if (presentQueueFamilyIndex == UINT32_MAX) {
3517 // If didn't find a queue that supports both graphics and present, then
3518 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003519 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003520 if (supportsPresent[i] == VK_TRUE) {
3521 presentQueueFamilyIndex = i;
3522 break;
3523 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003524 }
3525 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003526
3527 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003528 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3529 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003530 }
3531
Tony Barbourab2a0362016-09-06 11:40:12 -06003532 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3533 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003534 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003535 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003536
Tony Barbourda2bad52016-01-22 14:36:40 -07003537 demo_create_device(demo);
3538
3539 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3540 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3541 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3542 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3543 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003544 if (demo->VK_GOOGLE_display_timing_enabled) {
3545 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3546 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3547 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003548
Dave Houlton5fa47912018-02-16 11:02:26 -07003549 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003550
Tony Barbour22e06272016-09-07 16:07:56 -06003551 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003552 demo->present_queue = demo->graphics_queue;
3553 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003554 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003555 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003556
Ian Elliottaaae5352015-07-06 14:27:58 -06003557 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003558 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003559 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003560 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003561 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3562 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003563 assert(!err);
3564 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3565 // the surface has no preferred format. Otherwise, at least one
3566 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003567 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003568 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003569 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003570 assert(formatCount >= 1);
3571 demo->format = surfFormats[0].format;
3572 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003573 demo->color_space = surfFormats[0].colorSpace;
Jason Chen5d4fee92019-04-04 12:45:29 +08003574 free(surfFormats);
Ian Elliotte4602cd2015-04-21 16:41:02 -06003575
David Pinedo2bb7c932015-06-18 17:03:14 -06003576 demo->quit = false;
3577 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003578
Tony Barbource7524e2016-07-28 12:01:45 -06003579 // Create semaphores to synchronize acquiring presentable buffers before
3580 // rendering and waiting for drawing to be complete before presenting
3581 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3582 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3583 .pNext = NULL,
3584 .flags = 0,
3585 };
Tony Barbource7524e2016-07-28 12:01:45 -06003586
Tony Barbour66a56c32016-09-21 13:10:56 -06003587 // Create fences that we can use to throttle if we get too far
3588 // ahead of the image presents
3589 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003590 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003591 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003592 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3593 assert(!err);
3594
Dave Houlton5fa47912018-02-16 11:02:26 -07003595 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003596 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003597
Dave Houlton5fa47912018-02-16 11:02:26 -07003598 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003599 assert(!err);
3600
3601 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003602 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003603 assert(!err);
3604 }
Tony Barbour22e06272016-09-07 16:07:56 -06003605 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003606 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003607
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003608 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003609 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003610}
3611
Tony Barbour153cb062016-12-07 13:43:36 -07003612#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003613static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3614 wl_fixed_t sy) {}
3615
3616static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3617
3618static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3619
3620static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3621 uint32_t state) {
3622 struct demo *demo = data;
3623 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003624 xdg_toplevel_move(demo->xdg_toplevel, demo->seat, serial);
Joey Bzdek15eb0702017-06-07 09:40:36 -06003625 }
3626}
3627
3628static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3629
3630static const struct wl_pointer_listener pointer_listener = {
3631 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3632};
3633
3634static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3635
3636static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3637 struct wl_array *keys) {}
3638
3639static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3640
3641static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3642 uint32_t state) {
3643 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3644 struct demo *demo = data;
3645 switch (key) {
3646 case KEY_ESC: // Escape
3647 demo->quit = true;
3648 break;
3649 case KEY_LEFT: // left arrow key
3650 demo->spin_angle -= demo->spin_increment;
3651 break;
3652 case KEY_RIGHT: // right arrow key
3653 demo->spin_angle += demo->spin_increment;
3654 break;
3655 case KEY_SPACE: // space bar
3656 demo->pause = !demo->pause;
3657 break;
3658 }
3659}
3660
3661static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3662 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3663
3664static const struct wl_keyboard_listener keyboard_listener = {
3665 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3666};
3667
3668static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3669 // Subscribe to pointer events
3670 struct demo *demo = data;
3671 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3672 demo->pointer = wl_seat_get_pointer(seat);
3673 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3674 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3675 wl_pointer_destroy(demo->pointer);
3676 demo->pointer = NULL;
3677 }
3678 // Subscribe to keyboard events
3679 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3680 demo->keyboard = wl_seat_get_keyboard(seat);
3681 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3682 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3683 wl_keyboard_destroy(demo->keyboard);
3684 demo->keyboard = NULL;
3685 }
3686}
3687
3688static const struct wl_seat_listener seat_listener = {
3689 seat_handle_capabilities,
3690};
3691
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003692static void wm_base_ping(void *data UNUSED, struct xdg_wm_base *xdg_wm_base, uint32_t serial) {
3693 xdg_wm_base_pong(xdg_wm_base, serial);
3694}
3695
3696static const struct xdg_wm_base_listener wm_base_listener = {wm_base_ping};
3697
3698static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
3699 uint32_t version UNUSED) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003700 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003701 // pickup wayland objects when they appear
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003702 if (strcmp(interface, wl_compositor_interface.name) == 0) {
Georges Basile Stavracas Neto7da3ea72019-10-05 11:53:39 -03003703 uint32_t minVersion = version < 4 ? version : 4;
3704 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, minVersion);
3705 if (demo->VK_KHR_incremental_present_enabled && minVersion < 4) {
3706 fprintf(stderr, "Wayland compositor doesn't support VK_KHR_incremental_present, disabling.\n");
3707 demo->VK_KHR_incremental_present_enabled = false;
3708 }
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003709 } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
3710 demo->xdg_wm_base = wl_registry_bind(registry, id, &xdg_wm_base_interface, 1);
3711 xdg_wm_base_add_listener(demo->xdg_wm_base, &wm_base_listener, NULL);
3712 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003713 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3714 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003715 } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
3716 demo->xdg_decoration_mgr = wl_registry_bind(registry, id, &zxdg_decoration_manager_v1_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003717 }
3718}
3719
Dave Houlton5fa47912018-02-16 11:02:26 -07003720static 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 +09003721
Dave Houlton5fa47912018-02-16 11:02:26 -07003722static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003723#endif
3724
Karl Schultze4dc75c2016-02-02 15:37:51 -07003725static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003726#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003727 const xcb_setup_t *setup;
3728 xcb_screen_iterator_t iter;
3729 int scr;
3730
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003731 const char *display_envar = getenv("DISPLAY");
3732 if (display_envar == NULL || display_envar[0] == '\0') {
3733 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3734 fflush(stdout);
3735 exit(1);
3736 }
3737
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003738 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003739 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003740 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003741 fflush(stdout);
3742 exit(1);
3743 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003744
3745 setup = xcb_get_setup(demo->connection);
3746 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003747 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003748
3749 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003750#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3751 demo->display = wl_display_connect(NULL);
3752
3753 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003754 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003755 fflush(stdout);
3756 exit(1);
3757 }
3758
3759 demo->registry = wl_display_get_registry(demo->display);
3760 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3761 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003762#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003763}
3764
Karl Schultze4dc75c2016-02-02 15:37:51 -07003765static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003766 vec3 eye = {0.0f, 3.0f, 5.0f};
3767 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003768 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003769
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003770 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003771 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003772 demo->frameCount = INT32_MAX;
Jeremy Kniager979b5312019-11-22 14:55:57 -07003773
Piers Daniell735ee532015-02-23 16:23:13 -07003774 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003775 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003776 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003777 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003778 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003779 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3780 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003781 i++;
3782 continue;
3783 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003784 if (strcmp(argv[i], "--break") == 0) {
3785 demo->use_break = true;
3786 continue;
3787 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003788 if (strcmp(argv[i], "--validate") == 0) {
3789 demo->validate = true;
3790 continue;
3791 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003792 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3793 demo->validate = true;
3794 demo->validate_checks_disabled = true;
3795 continue;
3796 }
Tony Barbour2593b182016-04-19 10:57:58 -06003797 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003798 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003799 continue;
3800 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003801 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3802 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003803 i++;
3804 continue;
3805 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003806 if (strcmp(argv[i], "--suppress_popups") == 0) {
3807 demo->suppress_popups = true;
3808 continue;
3809 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003810 if (strcmp(argv[i], "--display_timing") == 0) {
3811 demo->VK_GOOGLE_display_timing_enabled = true;
3812 continue;
3813 }
Ian Elliott53622a72017-03-28 11:06:33 -06003814 if (strcmp(argv[i], "--incremental_present") == 0) {
3815 demo->VK_KHR_incremental_present_enabled = true;
3816 continue;
3817 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003818
Cody Northropaf28a532016-09-27 10:50:57 -06003819#if defined(ANDROID)
Lenny Komowffe446c2018-11-19 17:08:04 -07003820 ERR_EXIT("Usage: vkcube [--validate]\n", "Usage");
Cody Northropaf28a532016-09-27 10:50:57 -06003821#else
Tony-LunarGd74734f2019-06-04 14:11:43 -06003822 char *message =
3823 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled]\n"
3824 "\t[--break] [--c <framecount>] [--suppress_popups]\n"
3825 "\t[--incremental_present] [--display_timing]\n"
3826 "\t[--present_mode <present mode enum>]\n"
3827 "\t<present_mode_enum>\n"
3828 "\t\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3829 "\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3830 "\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
3831 "\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n";
3832 int length = snprintf(NULL, 0, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3833 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3834 char *usage = (char *)malloc(length + 1);
3835 if (!usage) {
3836 exit(1);
3837 }
3838 snprintf(usage, length + 1, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3839 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3840#if defined(_WIN32)
3841 if (!demo->suppress_popups) MessageBox(NULL, usage, "Usage Error", MB_OK);
3842#else
Tony-LunarG122ac062019-06-13 16:21:57 -06003843 fprintf(stderr, "%s", usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003844 fflush(stderr);
Tony-LunarGd74734f2019-06-04 14:11:43 -06003845#endif
3846 free(usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003847 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003848#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003849 }
3850
Tony Barbour153cb062016-12-07 13:43:36 -07003851 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003852
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003853 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003854
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003855 demo->width = 500;
3856 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003857
Tony Barbour66a56c32016-09-21 13:10:56 -06003858 demo->spin_angle = 4.0f;
3859 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003860 demo->pause = false;
3861
Dave Houlton5fa47912018-02-16 11:02:26 -07003862 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003863 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3864 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003865
Dave Houlton5fa47912018-02-16 11:02:26 -07003866 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003867}
3868
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003869#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003870// Include header required for parsing the command line options.
3871#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003872
Dave Houlton5fa47912018-02-16 11:02:26 -07003873int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3874 MSG msg; // message
3875 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003876 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003877 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003878
Jamie Madillb2ff6502017-03-15 16:17:46 -04003879 // Ensure wParam is initialized.
3880 msg.wParam = 0;
3881
Karl Schultze4dc75c2016-02-02 15:37:51 -07003882 // Use the CommandLine functions to get the command line arguments.
3883 // Unfortunately, Microsoft outputs
3884 // this information as wide characters for Unicode, and we simply want the
3885 // Ascii version to be compatible
3886 // with the non-Windows side. So, we have to convert the information to
3887 // Ascii character strings.
3888 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3889 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003890 argc = 0;
3891 }
3892
Karl Schultze4dc75c2016-02-02 15:37:51 -07003893 if (argc > 0) {
3894 argv = (char **)malloc(sizeof(char *) * argc);
3895 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003896 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003897 } else {
3898 for (int iii = 0; iii < argc; iii++) {
3899 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003900 size_t numConverted = 0;
3901
Karl Schultze4dc75c2016-02-02 15:37:51 -07003902 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3903 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003904 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003905 }
3906 }
3907 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003908 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003909 argv = NULL;
3910 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003911
3912 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003913
3914 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003915 if (argc > 0 && argv != NULL) {
3916 for (int iii = 0; iii < argc; iii++) {
3917 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003918 free(argv[iii]);
3919 }
3920 }
3921 free(argv);
3922 }
3923
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003924 demo.connection = hInstance;
Lenny Komowffe446c2018-11-19 17:08:04 -07003925 strncpy(demo.name, "Vulkan Cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003926 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003927 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003928
3929 demo_prepare(&demo);
3930
Dave Houlton5fa47912018-02-16 11:02:26 -07003931 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003932
3933 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003934 while (!done) {
Petr Krausd88e4e52019-01-23 18:45:04 +01003935 if (demo.pause) {
3936 const BOOL succ = WaitMessage();
3937
3938 if (!succ) {
3939 struct demo *tmp = &demo;
3940 struct demo *demo = tmp;
3941 ERR_EXIT("WaitMessage() failed on paused demo", "event loop error");
3942 }
3943 }
Ian Elliotta748eaf2015-04-28 15:50:36 -06003944 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003945 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003946 {
Dave Houlton5fa47912018-02-16 11:02:26 -07003947 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07003948 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003949 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003950 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003951 DispatchMessage(&msg);
3952 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003953 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003954 }
3955
3956 demo_cleanup(&demo);
3957
Karl Schultze4dc75c2016-02-02 15:37:51 -07003958 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003959}
Bill Hollingsf4352142017-02-14 22:58:56 -05003960
3961#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
Karl Schultz206b1c52018-04-13 18:02:07 -06003962static void demo_main(struct demo *demo, void *view, int argc, const char *argv[]) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003963 demo_init(demo, argc, (char **)argv);
Tony Barbourc65cd752017-03-13 15:29:35 -06003964 demo->window = view;
3965 demo_init_vk_swapchain(demo);
3966 demo_prepare(demo);
3967 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003968}
3969
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003970#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3971#include <android/log.h>
3972#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003973#include "android_util.h"
3974
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003975static bool initialized = false;
3976static bool active = false;
3977struct demo demo;
3978
Dave Houlton5fa47912018-02-16 11:02:26 -07003979static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003980
Dave Houlton5fa47912018-02-16 11:02:26 -07003981static void processCommand(struct android_app *app, int32_t cmd) {
3982 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003983 case APP_CMD_INIT_WINDOW: {
3984 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003985 // We're getting a new window. If the app is starting up, we
3986 // need to initialize. If the app has already been
3987 // initialized, that means that we lost our previous window,
3988 // which means that we have a lot of work to do. At a minimum,
3989 // we need to destroy the swapchain and surface associated with
3990 // the old window, and create a new surface and swapchain.
3991 // However, since there are a lot of other objects/state that
3992 // is tied to the swapchain, it's easiest to simply cleanup and
3993 // start over (i.e. use a brute-force approach of re-starting
3994 // the app)
3995 if (demo.prepared) {
3996 demo_cleanup(&demo);
3997 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003998
3999 // Parse Intents into argc, argv
4000 // Use the following key to send arguments, i.e.
4001 // --es args "--validate"
4002 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07004003 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004004 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07004005 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004006
4007 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07004008 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 -06004009
4010 demo_init(&demo, argc, argv);
4011
4012 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07004013 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004014
Dave Houlton5fa47912018-02-16 11:02:26 -07004015 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004016 demo_init_vk_swapchain(&demo);
4017 demo_prepare(&demo);
4018 initialized = true;
4019 }
4020 break;
4021 }
4022 case APP_CMD_GAINED_FOCUS: {
4023 active = true;
4024 break;
4025 }
4026 case APP_CMD_LOST_FOCUS: {
4027 active = false;
4028 break;
4029 }
4030 }
4031}
4032
Dave Houlton5fa47912018-02-16 11:02:26 -07004033void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06004034#ifdef ANDROID
4035 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07004036 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06004037#endif
4038
Ian Elliottf05d5d12016-05-17 12:03:35 -06004039 demo.prepared = false;
4040
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004041 app->onAppCmd = processCommand;
4042 app->onInputEvent = processInput;
4043
Dave Houlton5fa47912018-02-16 11:02:26 -07004044 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004045 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07004046 struct android_poll_source *source;
4047 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004048 if (source) {
4049 source->process(app, source);
4050 }
4051
4052 if (app->destroyRequested != 0) {
4053 demo_cleanup(&demo);
4054 return;
4055 }
4056 }
4057 if (initialized && active) {
4058 demo_run(&demo);
4059 }
4060 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004061}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004062#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004063int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004064 struct demo demo;
4065
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004066 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004067#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004068 demo_create_xcb_window(&demo);
4069#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4070 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004071#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4072 demo_create_window(&demo);
4073#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004074
Tony Barbourcc69f452015-10-08 13:59:42 -06004075 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004076
4077 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004078
Tony Barbour153cb062016-12-07 13:43:36 -07004079#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004080 demo_run_xcb(&demo);
4081#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4082 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004083#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4084 demo_run(&demo);
Damien Leone600c3052017-01-31 10:26:07 -07004085#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4086 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004087#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004088
4089 demo_cleanup(&demo);
4090
Karl Schultz0218c002016-03-22 17:06:13 -06004091 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004092}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004093#endif