blob: 5382cd0e927cae41c19d7b8a99e578b71cc67a27 [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>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -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)); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -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
115#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;
lenny-lunargfbe22392016-06-06 11:07:53 -0600532 if (!demo->suppress_popups)
533 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600534 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600535
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600536#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600537
Mike Schuchardt837d5162018-03-08 12:57:02 -0700538 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600539 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700540 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600541 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700542 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600543 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700544 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
545 __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600546 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600547 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600548 }
Cody Northropaf28a532016-09-27 10:50:57 -0600549
lenny-lunargfbe22392016-06-06 11:07:53 -0600550#else
Cody Northropaf28a532016-09-27 10:50:57 -0600551
lenny-lunargfbe22392016-06-06 11:07:53 -0600552 printf("%s\n", message);
553 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600554
lenny-lunargfbe22392016-06-06 11:07:53 -0600555#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600556
lenny-lunargfbe22392016-06-06 11:07:53 -0600557 free(message);
558
Mark Young66510e52017-11-10 10:05:14 -0700559 // Don't bail out, but keep going.
lenny-lunargfbe22392016-06-06 11:07:53 -0600560 return false;
561}
562
Ian Elliottd940ca22017-03-22 08:31:27 -0600563bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
564 // The desired time was the earliest time that the present should have
565 // occured. In almost every case, the actual time should be later than the
566 // desired time. We should only consider the actual time "late" if it is
567 // after "desired + rdur".
568 if (actual <= desired) {
569 // The actual time was before or equal to the desired time. This will
570 // probably never happen, but in case it does, return false since the
571 // present was obviously NOT late.
572 return false;
573 }
Liam Middlebrook881fe392018-05-03 15:52:32 -0700574 uint64_t deadline = desired + rdur;
Ian Elliottd940ca22017-03-22 08:31:27 -0600575 if (actual > deadline) {
576 return true;
577 } else {
578 return false;
579 }
580}
Dave Houlton5fa47912018-02-16 11:02:26 -0700581bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600582 if (earliest < actual) {
583 // Consider whether this present could have occured earlier. Make sure
584 // that earliest time was at least 2msec earlier than actual time, and
585 // that the margin was at least 2msec:
586 uint64_t diff = actual - earliest;
587 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
588 // This present could have occured earlier because both: 1) the
589 // earliest time was at least 2 msec before actual time, and 2) the
590 // margin was at least 2msec.
591 return true;
592 }
593 }
594 return false;
595}
596
Tony-LunarG6cebf142019-09-11 14:32:23 -0600597// Forward declarations:
Ian Elliottcf074d32015-10-16 18:02:43 -0600598static void demo_resize(struct demo *demo);
Tony-LunarG6cebf142019-09-11 14:32:23 -0600599static void demo_create_surface(struct demo *demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600600
Dave Houlton5fa47912018-02-16 11:02:26 -0700601static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700602 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600603 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700604 if ((typeBits & 1) == 1) {
605 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700606 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700607 *typeIndex = i;
608 return true;
609 }
610 }
611 typeBits >>= 1;
612 }
613 // No memory types matched, return failure
614 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600615}
616
Karl Schultze4dc75c2016-02-02 15:37:51 -0700617static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600618 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600619
Tony Barbource7524e2016-07-28 12:01:45 -0600620 // This function could get called twice if the texture uses a staging buffer
621 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700622 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600623
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600624 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600625 assert(!err);
626
Tony Barbource7524e2016-07-28 12:01:45 -0600627 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700628 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700629 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
630 assert(!err);
631
Karl Schultze4dc75c2016-02-02 15:37:51 -0700632 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700633 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
634 .pNext = NULL,
635 .waitSemaphoreCount = 0,
636 .pWaitSemaphores = NULL,
637 .pWaitDstStageMask = NULL,
638 .commandBufferCount = 1,
639 .pCommandBuffers = cmd_bufs,
640 .signalSemaphoreCount = 0,
641 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600642
Tony Barbource7524e2016-07-28 12:01:45 -0600643 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600644 assert(!err);
645
Tony Barbource7524e2016-07-28 12:01:45 -0600646 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600647 assert(!err);
648
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600649 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600650 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600651 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600652}
653
Dave Houlton5fa47912018-02-16 11:02:26 -0700654static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
655 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600656 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600657 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600658
Dave Houlton5fa47912018-02-16 11:02:26 -0700659 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
660 .pNext = NULL,
661 .srcAccessMask = srcAccessMask,
662 .dstAccessMask = 0,
Tony-LunarGa141b962018-05-30 11:33:19 -0600663 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
664 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Dave Houlton5fa47912018-02-16 11:02:26 -0700665 .oldLayout = old_image_layout,
666 .newLayout = new_image_layout,
667 .image = image,
668 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600669
Tony Barboureb5077b2016-10-19 15:23:36 -0600670 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700671 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
672 /* Make sure anything that was copying from this image has completed */
673 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
674 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600675
Dave Houlton5fa47912018-02-16 11:02:26 -0700676 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
677 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
678 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700679
Dave Houlton5fa47912018-02-16 11:02:26 -0700680 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
681 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
682 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700683
Dave Houlton5fa47912018-02-16 11:02:26 -0700684 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
685 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
686 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600687
Dave Houlton5fa47912018-02-16 11:02:26 -0700688 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
689 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
690 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600691
Dave Houlton5fa47912018-02-16 11:02:26 -0700692 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
693 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
694 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600695
Dave Houlton5fa47912018-02-16 11:02:26 -0700696 default:
697 image_memory_barrier.dstAccessMask = 0;
698 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600699 }
700
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600701 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600702
Dave Houlton5fa47912018-02-16 11:02:26 -0700703 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600704}
705
Karl Schultze4dc75c2016-02-02 15:37:51 -0700706static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Lenny Komowe830b592018-02-23 16:18:36 -0700707 VkDebugUtilsLabelEXT label;
708 memset(&label, 0, sizeof(label));
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700709 const VkCommandBufferBeginInfo cmd_buf_info = {
710 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
711 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600712 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700713 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700714 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800715 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700716 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
717 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800718 };
719 const VkRenderPassBeginInfo rp_begin = {
720 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
721 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800722 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700723 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800724 .renderArea.offset.x = 0,
725 .renderArea.offset.y = 0,
726 .renderArea.extent.width = demo->width,
727 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600728 .clearValueCount = 2,
729 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700730 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800731 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600732
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600733 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Mark Young66510e52017-11-10 10:05:14 -0700734
735 if (demo->validate) {
736 // Set a name for the command buffer
737 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
738 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
739 .pNext = NULL,
740 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
741 .objectHandle = (uint64_t)cmd_buf,
742 .pObjectName = "CubeDrawCommandBuf",
743 };
744 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
745
746 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
747 label.pNext = NULL;
748 label.pLabelName = "DrawBegin";
749 label.color[0] = 0.4f;
750 label.color[1] = 0.3f;
751 label.color[2] = 0.2f;
752 label.color[3] = 0.1f;
753 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
754 }
755
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600756 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800757 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Mark Young66510e52017-11-10 10:05:14 -0700758
759 if (demo->validate) {
760 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
761 label.pNext = NULL;
762 label.pLabelName = "InsideRenderPass";
763 label.color[0] = 8.4f;
764 label.color[1] = 7.3f;
765 label.color[2] = 6.2f;
766 label.color[3] = 7.1f;
767 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
768 }
769
Karl Schultze4dc75c2016-02-02 15:37:51 -0700770 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700771 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
772 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600773 VkViewport viewport;
774 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700775 viewport.height = (float)demo->height;
776 viewport.width = (float)demo->width;
777 viewport.minDepth = (float)0.0f;
778 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700779 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600780
781 VkRect2D scissor;
782 memset(&scissor, 0, sizeof(scissor));
783 scissor.extent.width = demo->width;
784 scissor.extent.height = demo->height;
785 scissor.offset.x = 0;
786 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700787 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Mark Young66510e52017-11-10 10:05:14 -0700788
789 if (demo->validate) {
790 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
791 label.pNext = NULL;
792 label.pLabelName = "ActualDraw";
Mike Schuchardt4cf3aa42018-02-23 12:44:05 -0700793 label.color[0] = -0.4f;
794 label.color[1] = -0.3f;
795 label.color[2] = -0.2f;
796 label.color[3] = -0.1f;
Mark Young66510e52017-11-10 10:05:14 -0700797 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
798 }
799
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600800 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Mark Young66510e52017-11-10 10:05:14 -0700801 if (demo->validate) {
802 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
803 }
804
Tony Barbour98390392016-07-28 10:50:13 -0600805 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600806 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800807 vkCmdEndRenderPass(cmd_buf);
Mark Young66510e52017-11-10 10:05:14 -0700808 if (demo->validate) {
809 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
810 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600811
Tony Barbour22e06272016-09-07 16:07:56 -0600812 if (demo->separate_present_queue) {
813 // We have to transfer ownership from the graphics queue family to the
814 // present queue family to be able to present. Note that we don't have
815 // to transfer from present queue family back to graphics queue family at
816 // the start of the next frame because we don't care about the image's
817 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700818 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
819 .pNext = NULL,
820 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600821 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700822 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
823 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
824 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
825 .dstQueueFamilyIndex = demo->present_queue_family_index,
826 .image = demo->swapchain_image_resources[demo->current_buffer].image,
827 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600828
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600829 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
830 NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600831 }
Mark Young66510e52017-11-10 10:05:14 -0700832 if (demo->validate) {
833 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
834 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600835 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600836 assert(!err);
837}
838
Tony Barbour22e06272016-09-07 16:07:56 -0600839void demo_build_image_ownership_cmd(struct demo *demo, int i) {
840 VkResult U_ASSERT_ONLY err;
841
842 const VkCommandBufferBeginInfo cmd_buf_info = {
843 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
844 .pNext = NULL,
845 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
846 .pInheritanceInfo = NULL,
847 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700848 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600849 assert(!err);
850
Dave Houlton5fa47912018-02-16 11:02:26 -0700851 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
852 .pNext = NULL,
853 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600854 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700855 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
856 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
857 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
858 .dstQueueFamilyIndex = demo->present_queue_family_index,
859 .image = demo->swapchain_image_resources[i].image,
860 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600861
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600862 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
863 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700864 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600865 assert(!err);
866}
867
Karl Schultze4dc75c2016-02-02 15:37:51 -0700868void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600869 mat4x4 MVP, Model, VP;
870 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600871 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600872 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600873
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600874 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600875
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700876 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600877 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700878 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600879 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600880
Dave Houlton5fa47912018-02-16 11:02:26 -0700881 err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0,
882 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600883 assert(!err);
884
Karl Schultze4dc75c2016-02-02 15:37:51 -0700885 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600886
Tony Barboura72d9492017-01-11 13:35:09 -0700887 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600888}
889
Ian Elliottd940ca22017-03-22 08:31:27 -0600890void DemoUpdateTargetIPD(struct demo *demo) {
891 // Look at what happened to previous presents, and make appropriate
892 // adjustments in timing:
893 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700894 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600895 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600896
Dave Houlton5fa47912018-02-16 11:02:26 -0700897 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600898 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600899 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700900 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600901 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700902 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600903 assert(!err);
904
905 bool early = false;
906 bool late = false;
907 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700908 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600909 if (!demo->syncd_with_actual_presents) {
910 // This is the first time that we've received an
911 // actualPresentTime for this swapchain. In order to not
912 // perceive these early frames as "late", we need to sync-up
913 // our future desiredPresentTime's with the
914 // actualPresentTime(s) that we're receiving now.
915 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600916
Ian Elliottd940ca22017-03-22 08:31:27 -0600917 // So that we don't suspect any pending presents as late,
918 // record them all as suspected-late presents:
919 demo->last_late_id = demo->next_present_id - 1;
920 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600921 demo->syncd_with_actual_presents = true;
922 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700923 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600924 demo->refresh_duration)) {
925 // This image could have been presented earlier. We don't want
926 // to decrease the target_IPD until we've seen early presents
927 // for at least two seconds.
928 if (demo->last_early_id == past[i].presentID) {
929 // We've now seen two seconds worth of early presents.
930 // Flag it as such, and reset the counter:
931 early = true;
932 demo->last_early_id = 0;
933 } else if (demo->last_early_id == 0) {
934 // This is the first early present we've seen.
935 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700936 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
937 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600938 demo->last_early_id = past[i].presentID + howManyPresents;
939 } else {
940 // We are in the midst of a set of early images,
941 // and so we won't do anything.
942 }
943 late = false;
944 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700945 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600946 // This image was presented after its desired time. Since
947 // there's a delay between calling vkQueuePresentKHR and when
948 // we get the timing data, several presents may have been late.
949 // Thus, we need to threat all of the outstanding presents as
950 // being likely late, so that we only increase the target_IPD
951 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700952 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600953 late = true;
954 // Record the last suspected-late present:
955 demo->last_late_id = demo->next_present_id - 1;
956 } else {
957 // We are in the midst of a set of likely-late images,
958 // and so we won't do anything.
959 }
960 early = false;
961 demo->last_early_id = 0;
962 } else {
963 // Since this image was not presented early or late, reset
964 // any sets of early or late presentIDs:
965 early = false;
966 late = false;
967 calibrate_next = true;
968 demo->last_early_id = 0;
969 demo->last_late_id = 0;
970 }
971 }
972
973 if (early) {
974 // Since we've seen at least two-seconds worth of presnts that
975 // could have occured earlier than desired, let's decrease the
976 // target_IPD (i.e. increase the frame rate):
977 //
978 // TODO(ianelliott): Try to calculate a better target_IPD based
979 // on the most recently-seen present (this is overly-simplistic).
980 demo->refresh_duration_multiplier--;
981 if (demo->refresh_duration_multiplier == 0) {
982 // This should never happen, but in case it does, don't
983 // try to go faster.
984 demo->refresh_duration_multiplier = 1;
985 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700986 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600987 }
988 if (late) {
989 // Since we found a new instance of a late present, we want to
990 // increase the target_IPD (i.e. decrease the frame rate):
991 //
992 // TODO(ianelliott): Try to calculate a better target_IPD based
993 // on the most recently-seen present (this is overly-simplistic).
994 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -0700995 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600996 }
997
998 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700999 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
1000 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -06001001 }
Karl Schultza33771f2018-05-28 16:16:47 -06001002 free(past);
Ian Elliottd940ca22017-03-22 08:31:27 -06001003 }
Ian Elliottd940ca22017-03-22 08:31:27 -06001004}
1005
Karl Schultze4dc75c2016-02-02 15:37:51 -07001006static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001007 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001008
Damien Leonec336d822017-04-14 16:10:14 -07001009 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +03001010 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1011 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001012
Tony Barbour8b7c9112017-06-29 13:34:41 -06001013 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -06001014 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -07001015 err =
1016 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1017 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001018
Tony Barbour6914e6d2017-06-02 12:11:29 -06001019 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1020 // demo->swapchain is out of date (e.g. the window was resized) and
1021 // must be recreated:
1022 demo_resize(demo);
1023 } else if (err == VK_SUBOPTIMAL_KHR) {
1024 // demo->swapchain is not as optimal as it could be, but the platform's
1025 // presentation engine will still present the image correctly.
1026 break;
Tony-LunarG6cebf142019-09-11 14:32:23 -06001027 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1028 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1029 demo_create_surface(demo);
1030 demo_resize(demo);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001031 } else {
1032 assert(!err);
1033 }
Tony Barbour8b7c9112017-06-29 13:34:41 -06001034 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001035
Tony Barbour3eafe1f2017-06-14 11:59:55 -06001036 demo_update_data_buffer(demo);
1037
Ian Elliottd940ca22017-03-22 08:31:27 -06001038 if (demo->VK_GOOGLE_display_timing_enabled) {
1039 // Look at what happened to previous presents, and make appropriate
1040 // adjustments in timing:
1041 DemoUpdateTargetIPD(demo);
1042
1043 // Note: a real application would position its geometry to that it's in
1044 // the correct locatoin for when the next image is presented. It might
1045 // also wait, so that there's less latency between any input and when
1046 // the next image is rendered/presented. This demo program is so
1047 // simple that it doesn't do either of those.
1048 }
1049
Tony Barbource7524e2016-07-28 12:01:45 -06001050 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001051 // that the image won't be rendered to until the presentation
1052 // engine has fully released ownership to the application, and it is
1053 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001054 VkPipelineStageFlags pipe_stage_flags;
1055 VkSubmitInfo submit_info;
1056 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1057 submit_info.pNext = NULL;
1058 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1059 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1060 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001061 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001062 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001063 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001064 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001065 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -07001066 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001067 assert(!err);
1068
Tony Barbour22e06272016-09-07 16:07:56 -06001069 if (demo->separate_present_queue) {
1070 // If we are using separate queues, change image ownership to the
1071 // present queue before presenting, waiting for the draw complete
1072 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001073 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001074 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1075 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001076 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001077 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001078 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001079 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001080 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001081 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1082 assert(!err);
1083 }
1084
1085 // If we are using separate queues we have to wait for image ownership,
1086 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001087 VkPresentInfoKHR present = {
1088 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001089 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001090 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -07001091 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1092 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001093 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001094 .pSwapchains = &demo->swapchain,
1095 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001096 };
1097
Dave Airlie48a68f92019-04-12 17:04:48 +10001098 VkRectLayerKHR rect;
1099 VkPresentRegionKHR region;
1100 VkPresentRegionsKHR regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001101 if (demo->VK_KHR_incremental_present_enabled) {
1102 // If using VK_KHR_incremental_present, we provide a hint of the region
1103 // that contains changed content relative to the previously-presented
1104 // image. The implementation can use this hint in order to save
1105 // work/power (by only copying the region in the hint). The
1106 // implementation is free to ignore the hint though, and so we must
1107 // ensure that the entire image has the correctly-drawn content.
1108 uint32_t eighthOfWidth = demo->width / 8;
1109 uint32_t eighthOfHeight = demo->height / 8;
Dave Airlie48a68f92019-04-12 17:04:48 +10001110
1111 rect.offset.x = eighthOfWidth;
1112 rect.offset.y = eighthOfHeight;
1113 rect.extent.width = eighthOfWidth * 6;
1114 rect.extent.height = eighthOfHeight * 6;
1115 rect.layer = 0;
1116
1117 region.rectangleCount = 1;
1118 region.pRectangles = &rect;
1119
1120 regions.sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR;
1121 regions.pNext = present.pNext;
1122 regions.swapchainCount = present.swapchainCount;
1123 regions.pRegions = &region;
Ian Elliott53622a72017-03-28 11:06:33 -06001124 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001125 }
1126
Ian Elliottd940ca22017-03-22 08:31:27 -06001127 if (demo->VK_GOOGLE_display_timing_enabled) {
1128 VkPresentTimeGOOGLE ptime;
1129 if (demo->prev_desired_present_time == 0) {
1130 // This must be the first present for this swapchain.
1131 //
1132 // We don't know where we are relative to the presentation engine's
1133 // display's refresh cycle. We also don't know how long rendering
1134 // takes. Let's make a grossly-simplified assumption that the
1135 // desiredPresentTime should be half way between now and
1136 // now+target_IPD. We will adjust over time.
1137 uint64_t curtime = getTimeInNanoseconds();
1138 if (curtime == 0) {
1139 // Since we didn't find out the current time, don't give a
1140 // desiredPresentTime:
1141 ptime.desiredPresentTime = 0;
1142 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001143 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1144 }
1145 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001146 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001147 }
1148 ptime.presentID = demo->next_present_id++;
1149 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001150
1151 VkPresentTimesInfoGOOGLE present_time = {
1152 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1153 .pNext = present.pNext,
1154 .swapchainCount = present.swapchainCount,
1155 .pTimes = &ptime,
1156 };
1157 if (demo->VK_GOOGLE_display_timing_enabled) {
1158 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001159 }
1160 }
1161
Tony Barboura2a67812016-07-18 13:21:06 -06001162 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001163 demo->frame_index += 1;
1164 demo->frame_index %= FRAME_LAG;
1165
Ian Elliottcf074d32015-10-16 18:02:43 -06001166 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1167 // demo->swapchain is out of date (e.g. the window was resized) and
1168 // must be recreated:
1169 demo_resize(demo);
1170 } else if (err == VK_SUBOPTIMAL_KHR) {
1171 // demo->swapchain is not as optimal as it could be, but the platform's
1172 // presentation engine will still present the image correctly.
Tony-LunarG6cebf142019-09-11 14:32:23 -06001173 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1174 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1175 demo_create_surface(demo);
1176 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06001177 } else {
1178 assert(!err);
1179 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001180}
1181
Karl Schultze4dc75c2016-02-02 15:37:51 -07001182static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001183 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001184 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001185
Ian Elliottb08e0d92015-11-20 11:55:46 -07001186 // Check the surface capabilities and formats
1187 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001188 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001189 assert(!err);
1190
Ian Elliott8528eff2015-08-07 15:56:59 -06001191 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001192 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001193 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001194 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001195 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001196 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001197 assert(!err);
1198
Ian Elliotta0781972015-08-21 15:09:33 -06001199 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001200 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1201 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1202 // If the surface size is undefined, the size is set to the size
1203 // of the images requested, which must fit within the minimum and
1204 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001205 swapchainExtent.width = demo->width;
1206 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001207
1208 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1209 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1210 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1211 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1212 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001213
Ian Elliottcf7f7482016-08-19 03:46:58 -06001214 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1215 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1216 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1217 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1218 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001219 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001220 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001221 swapchainExtent = surfCapabilities.currentExtent;
1222 demo->width = surfCapabilities.currentExtent.width;
1223 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001224 }
1225
Jeremy Kniager602e4182018-01-19 10:52:34 -07001226 if (demo->width == 0 || demo->height == 0) {
1227 demo->is_minimized = true;
1228 return;
1229 } else {
1230 demo->is_minimized = false;
1231 }
1232
Tony Barbour66a56c32016-09-21 13:10:56 -06001233 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001234 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001235 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001236
Ian Elliottb18fdde2016-10-03 12:11:12 -06001237 // There are times when you may wish to use another present mode. The
1238 // following code shows how to select them, and the comments provide some
1239 // reasons you may wish to use them.
1240 //
1241 // It should be noted that Vulkan 1.0 doesn't provide a method for
1242 // synchronizing rendering with the presentation engine's display. There
1243 // is a method provided for throttling rendering with the display, but
1244 // there are some presentation engines for which this method will not work.
1245 // If an application doesn't throttle its rendering, and if it renders much
1246 // faster than the refresh rate of the display, this can waste power on
1247 // mobile devices. That is because power is being spent rendering images
1248 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001249
Ian Elliottb18fdde2016-10-03 12:11:12 -06001250 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1251 // tearing, or have some way of synchronizing their rendering with the
1252 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001253 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1254 // generally render a new presentable image every refresh cycle, but are
1255 // occasionally early. In this case, the application wants the new image
1256 // to be displayed instead of the previously-queued-for-presentation image
1257 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001258 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1259 // render a new presentable image every refresh cycle, but are occasionally
1260 // late. In this case (perhaps because of stuttering/latency concerns),
1261 // the application wants the late image to be immediately displayed, even
1262 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001263
Dave Houlton5fa47912018-02-16 11:02:26 -07001264 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001265 for (size_t i = 0; i < presentModeCount; ++i) {
1266 if (presentModes[i] == demo->presentMode) {
1267 swapchainPresentMode = demo->presentMode;
1268 break;
1269 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001270 }
1271 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001272 if (swapchainPresentMode != demo->presentMode) {
1273 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1274 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001275
Tony Barbour84376fe2017-01-06 15:54:22 -07001276 // Determine the number of VkImages to use in the swap chain.
1277 // Application desires to acquire 3 images at a time for triple
1278 // buffering
1279 uint32_t desiredNumOfSwapchainImages = 3;
1280 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1281 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1282 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001283 // If maxImageCount is 0, we can ask for as many images as we want;
1284 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001285 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001286 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001287 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001288 }
1289
Tony Barbour9fd6d352015-09-16 15:01:32 -06001290 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001291 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001292 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001293 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001294 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001295 }
1296
Tony Barbour820b55b2017-03-14 08:55:46 -06001297 // Find a supported composite alpha mode - one of these is guaranteed to be set
1298 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1299 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1300 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1301 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1302 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1303 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1304 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001305 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001306 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1307 compositeAlpha = compositeAlphaFlags[i];
1308 break;
1309 }
1310 }
1311
Tony Barboura2a67812016-07-18 13:21:06 -06001312 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001313 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001314 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001315 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001316 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001317 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001318 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001319 .imageExtent =
1320 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001321 .width = swapchainExtent.width,
1322 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001323 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001324 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001325 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001326 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001327 .imageArrayLayers = 1,
1328 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1329 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001330 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001331 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001332 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001333 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001334 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001335 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001336 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001337 assert(!err);
1338
Ian Elliottcf074d32015-10-16 18:02:43 -06001339 // If we just re-created an existing swapchain, we should destroy the old
1340 // swapchain at this point.
1341 // Note: destroying the swapchain also cleans up all its associated
1342 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001343 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001344 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001345 }
1346
Dave Houlton5fa47912018-02-16 11:02:26 -07001347 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001348 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001349
Dave Houlton5fa47912018-02-16 11:02:26 -07001350 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001351 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001352 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001353 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001354
Dave Houlton5fa47912018-02-16 11:02:26 -07001355 demo->swapchain_image_resources =
1356 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001357 assert(demo->swapchain_image_resources);
1358
Ian Elliotta0781972015-08-21 15:09:33 -06001359 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001360 VkImageViewCreateInfo color_image_view = {
1361 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362 .pNext = NULL,
1363 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001364 .components =
1365 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001366 .r = VK_COMPONENT_SWIZZLE_R,
1367 .g = VK_COMPONENT_SWIZZLE_G,
1368 .b = VK_COMPONENT_SWIZZLE_B,
1369 .a = VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001370 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001371 .subresourceRange =
1372 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001373 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1374 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001375 };
1376
Tony Barboura72d9492017-01-11 13:35:09 -07001377 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001378
Tony Barboura72d9492017-01-11 13:35:09 -07001379 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380
Dave Houlton5fa47912018-02-16 11:02:26 -07001381 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001382 assert(!err);
1383 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001384
Ian Elliottd940ca22017-03-22 08:31:27 -06001385 if (demo->VK_GOOGLE_display_timing_enabled) {
1386 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001387 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001388 assert(!err);
1389 demo->refresh_duration = rc_dur.refreshDuration;
1390
1391 demo->syncd_with_actual_presents = false;
1392 // Initially target 1X the refresh duration:
1393 demo->target_IPD = demo->refresh_duration;
1394 demo->refresh_duration_multiplier = 1;
1395 demo->prev_desired_present_time = 0;
1396 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001397 }
1398
Jason Chen5d4fee92019-04-04 12:45:29 +08001399 if (NULL != swapchainImages) {
1400 free(swapchainImages);
1401 }
1402
Mark Young04fd3d82016-01-25 14:43:50 -07001403 if (NULL != presentModes) {
1404 free(presentModes);
1405 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406}
1407
Karl Schultze4dc75c2016-02-02 15:37:51 -07001408static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001409 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001410 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001411 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001413 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001415 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001417 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001418 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001419 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001420 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421 .flags = 0,
1422 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001423
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001424 VkImageViewCreateInfo view = {
1425 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001426 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001427 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001428 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001429 .subresourceRange =
1430 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001431 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001432 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001433 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001434
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001435 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001436 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001437 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438
1439 demo->depth.format = depth_format;
1440
1441 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001442 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443 assert(!err);
1444
Karl Schultze4dc75c2016-02-02 15:37:51 -07001445 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001446 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001447
Chia-I Wuf48700b2015-11-10 16:21:09 +08001448 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001449 demo->depth.mem_alloc.pNext = NULL;
1450 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1451 demo->depth.mem_alloc.memoryTypeIndex = 0;
1452
Dave Houlton5fa47912018-02-16 11:02:26 -07001453 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001454 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001455 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001456
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001457 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001458 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001459 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001460
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001461 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001462 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001463 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001464
1465 /* create image view */
1466 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001467 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001469}
1470
Karl Schultzb7940402018-05-29 13:09:22 -06001471/* Convert ppm image data from header file into RGBA texture image */
1472#include "lunarg.ppm.h"
Dave Houlton5fa47912018-02-16 11:02:26 -07001473bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Karl Schultzb7940402018-05-29 13:09:22 -06001474 (void)filename;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001475 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001476 cPtr = (char *)lunarg_ppm;
1477 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001478 return false;
1479 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001480 while (strncmp(cPtr++, "\n", 1))
1481 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001482 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001483 if (rgba_data == NULL) {
1484 return true;
1485 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001486 while (strncmp(cPtr++, "\n", 1))
1487 ;
1488 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001489 return false;
1490 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001491 while (strncmp(cPtr++, "\n", 1))
1492 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001493 for (int y = 0; y < *height; y++) {
1494 uint8_t *rowPtr = rgba_data;
1495 for (int x = 0; x < *width; x++) {
1496 memcpy(rowPtr, cPtr, 3);
1497 rowPtr[3] = 255; /* Alpha of 1 */
1498 rowPtr += 4;
1499 cPtr += 3;
1500 }
1501 rgba_data += layout->rowPitch;
1502 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001503 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001504}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001505
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001506static void demo_prepare_texture_buffer(struct demo *demo, const char *filename, struct texture_object *tex_obj) {
1507 int32_t tex_width;
1508 int32_t tex_height;
1509 VkResult U_ASSERT_ONLY err;
1510 bool U_ASSERT_ONLY pass;
1511
1512 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
1513 ERR_EXIT("Failed to load textures", "Load Texture Failure");
1514 }
1515
1516 tex_obj->tex_width = tex_width;
1517 tex_obj->tex_height = tex_height;
1518
1519 const VkBufferCreateInfo buffer_create_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
1520 .pNext = NULL,
1521 .flags = 0,
1522 .size = tex_width * tex_height * 4,
1523 .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1524 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1525 .queueFamilyIndexCount = 0,
1526 .pQueueFamilyIndices = NULL};
1527
1528 err = vkCreateBuffer(demo->device, &buffer_create_info, NULL, &tex_obj->buffer);
1529 assert(!err);
1530
1531 VkMemoryRequirements mem_reqs;
1532 vkGetBufferMemoryRequirements(demo->device, tex_obj->buffer, &mem_reqs);
1533
1534 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1535 tex_obj->mem_alloc.pNext = NULL;
1536 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1537 tex_obj->mem_alloc.memoryTypeIndex = 0;
1538
1539 VkFlags requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1540 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, requirements, &tex_obj->mem_alloc.memoryTypeIndex);
1541 assert(pass);
1542
1543 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
1544 assert(!err);
1545
1546 /* bind memory */
1547 err = vkBindBufferMemory(demo->device, tex_obj->buffer, tex_obj->mem, 0);
1548 assert(!err);
1549
1550 VkSubresourceLayout layout;
1551 memset(&layout, 0, sizeof(layout));
1552 layout.rowPitch = tex_width * 4;
1553
1554 void *data;
1555 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
1556 assert(!err);
1557
1558 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1559 fprintf(stderr, "Error loading texture: %s\n", filename);
1560 }
1561
1562 vkUnmapMemory(demo->device, tex_obj->mem);
1563}
1564
Dave Houlton5fa47912018-02-16 11:02:26 -07001565static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1566 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001567 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001568 int32_t tex_width;
1569 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001570 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001571 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001572
Karl Schultze4dc75c2016-02-02 15:37:51 -07001573 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001574 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001575 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001576
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001577 tex_obj->tex_width = tex_width;
1578 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001579
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001580 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001581 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001582 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001583 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001584 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001585 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001586 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001587 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001588 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001589 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001590 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001591 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001592 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001593 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001594
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001595 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001596
Dave Houlton5fa47912018-02-16 11:02:26 -07001597 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001598 assert(!err);
1599
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001600 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001601
Chia-I Wuf48700b2015-11-10 16:21:09 +08001602 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001603 tex_obj->mem_alloc.pNext = NULL;
1604 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1605 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001606
Dave Houlton5fa47912018-02-16 11:02:26 -07001607 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001608 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001609
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001610 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001611 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001612 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001613
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001614 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001615 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001616 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001617
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001618 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001619 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001620 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001621 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001622 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001623 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001624 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001625 void *data;
1626
Dave Houlton5fa47912018-02-16 11:02:26 -07001627 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001628
Dave Houlton5fa47912018-02-16 11:02:26 -07001629 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001630 assert(!err);
1631
1632 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1633 fprintf(stderr, "Error loading texture: %s\n", filename);
1634 }
1635
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001636 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001637 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001638
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001639 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001640}
1641
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001642static void demo_destroy_texture(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001643 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001644 vkFreeMemory(demo->device, tex_objs->mem, NULL);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001645 if (tex_objs->image) vkDestroyImage(demo->device, tex_objs->image, NULL);
1646 if (tex_objs->buffer) vkDestroyBuffer(demo->device, tex_objs->buffer, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001647}
1648
Karl Schultze4dc75c2016-02-02 15:37:51 -07001649static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001650 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001651 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001652 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001653
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001654 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001655
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001656 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001657 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001658
Dave Houlton5fa47912018-02-16 11:02:26 -07001659 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001660 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001661 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1662 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001663 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1664 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001665 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1666 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001667 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001668 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001669 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001670 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001671
Tony Barbour16156cb2016-10-19 14:15:49 -06001672 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001673 demo_prepare_texture_buffer(demo, tex_files[i], &demo->staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001674
Dave Houlton5fa47912018-02-16 11:02:26 -07001675 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1676 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1677 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001678
Dave Houlton5fa47912018-02-16 11:02:26 -07001679 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1680 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001681 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001682
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001683 VkBufferImageCopy copy_region = {
1684 .bufferOffset = 0,
1685 .bufferRowLength = demo->staging_texture.tex_width,
1686 .bufferImageHeight = demo->staging_texture.tex_height,
1687 .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1688 .imageOffset = {0, 0, 0},
1689 .imageExtent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001690 };
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001691
1692 vkCmdCopyBufferToImage(demo->cmd, demo->staging_texture.buffer, demo->textures[i].image,
1693 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001694
Dave Houlton5fa47912018-02-16 11:02:26 -07001695 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1696 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001697 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001698
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001699 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001700 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1701 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001702 }
1703
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001704 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001705 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001706 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001707 .magFilter = VK_FILTER_NEAREST,
1708 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001709 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001710 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1711 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1712 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001713 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001714 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001715 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001716 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001717 .minLod = 0.0f,
1718 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001719 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001720 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001721 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001722
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001723 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001724 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001725 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001726 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001727 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001728 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001729 .components =
1730 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001731 VK_COMPONENT_SWIZZLE_R,
1732 VK_COMPONENT_SWIZZLE_G,
1733 VK_COMPONENT_SWIZZLE_B,
1734 VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001735 },
1736 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001737 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001738 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001739
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001740 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001741 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742 assert(!err);
1743
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744 /* create image view */
1745 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001746 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001747 assert(!err);
1748 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001749}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001750
Tony Barboura72d9492017-01-11 13:35:09 -07001751void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001752 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001753 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001754 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001755 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001756 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001757 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001758 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001759 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001760
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001761 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001762 mat4x4_mul(MVP, VP, demo->model_matrix);
1763 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001764 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001765
Karl Schultza2615462017-01-20 13:19:20 -07001766 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001767 data.position[i][0] = g_vertex_buffer_data[i * 3];
1768 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1769 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001770 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001771 data.attr[i][0] = g_uv_buffer_data[2 * i];
1772 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001773 data.attr[i][2] = 0;
1774 data.attr[i][3] = 0;
1775 }
1776
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001777 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001778 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001779 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001780 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001781
Tony Barboura72d9492017-01-11 13:35:09 -07001782 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001783 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001784 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001785
Dave Houlton5fa47912018-02-16 11:02:26 -07001786 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001787
Tony Barboura72d9492017-01-11 13:35:09 -07001788 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1789 mem_alloc.pNext = NULL;
1790 mem_alloc.allocationSize = mem_reqs.size;
1791 mem_alloc.memoryTypeIndex = 0;
1792
Dave Houlton5fa47912018-02-16 11:02:26 -07001793 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1794 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1795 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001796 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001797
Dave Houlton5fa47912018-02-16 11:02:26 -07001798 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001799 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001800
Dave Houlton5fa47912018-02-16 11:02:26 -07001801 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 -07001802 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001803
Tony Barboura72d9492017-01-11 13:35:09 -07001804 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001805
Tony Barboura72d9492017-01-11 13:35:09 -07001806 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001807
Tony Barboura72d9492017-01-11 13:35:09 -07001808 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001809 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001810 assert(!err);
1811 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001812}
1813
Karl Schultze4dc75c2016-02-02 15:37:51 -07001814static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001815 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001816 [0] =
1817 {
1818 .binding = 0,
1819 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1820 .descriptorCount = 1,
1821 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1822 .pImmutableSamplers = NULL,
1823 },
1824 [1] =
1825 {
1826 .binding = 1,
1827 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1828 .descriptorCount = DEMO_TEXTURE_COUNT,
1829 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1830 .pImmutableSamplers = NULL,
1831 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001832 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001833 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001834 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001835 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001836 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001837 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001838 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001839 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001840
Dave Houlton5fa47912018-02-16 11:02:26 -07001841 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001842 assert(!err);
1843
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001844 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001845 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1846 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001847 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001848 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001849 };
1850
Dave Houlton5fa47912018-02-16 11:02:26 -07001851 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001853}
1854
Karl Schultze4dc75c2016-02-02 15:37:51 -07001855static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001856 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1857 // because at the start of the renderpass, we don't care about their contents.
1858 // At the start of the subpass, the color attachment's layout will be transitioned
1859 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1860 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1861 // the renderpass, the color attachment's layout will be transitioned to
1862 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1863 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001864 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001865 [0] =
1866 {
1867 .format = demo->format,
1868 .flags = 0,
1869 .samples = VK_SAMPLE_COUNT_1_BIT,
1870 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1871 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1872 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1873 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1874 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1875 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1876 },
1877 [1] =
1878 {
1879 .format = demo->depth.format,
1880 .flags = 0,
1881 .samples = VK_SAMPLE_COUNT_1_BIT,
1882 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1883 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1884 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1885 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1886 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1887 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1888 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001889 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001890 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001891 .attachment = 0,
1892 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001893 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001894 const VkAttachmentReference depth_reference = {
1895 .attachment = 1,
1896 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1897 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001898 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001899 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1900 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001901 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001902 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001903 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001904 .pColorAttachments = &color_reference,
1905 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001906 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001907 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001908 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001909 };
Tony-LunarG495604b2019-06-13 15:32:05 -06001910
1911 VkSubpassDependency attachmentDependencies[2] = {
1912 [0] =
1913 {
1914 // Depth buffer is shared between swapchain images
1915 .srcSubpass = VK_SUBPASS_EXTERNAL,
1916 .dstSubpass = 0,
1917 .srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1918 .dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1919 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1920 .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1921 .dependencyFlags = 0,
1922 },
1923 [1] =
1924 {
1925 // Image Layout Transition
1926 .srcSubpass = VK_SUBPASS_EXTERNAL,
1927 .dstSubpass = 0,
1928 .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1929 .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1930 .srcAccessMask = 0,
1931 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
1932 .dependencyFlags = 0,
1933 },
1934 };
1935
Chia-I Wuf973e322015-07-08 13:34:24 +08001936 const VkRenderPassCreateInfo rp_info = {
1937 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1938 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001939 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001940 .attachmentCount = 2,
1941 .pAttachments = attachments,
1942 .subpassCount = 1,
1943 .pSubpasses = &subpass,
Tony-LunarG495604b2019-06-13 15:32:05 -06001944 .dependencyCount = 2,
1945 .pDependencies = attachmentDependencies,
Chia-I Wuf973e322015-07-08 13:34:24 +08001946 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001947 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001948
Chia-I Wu63636062015-10-26 21:10:41 +08001949 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001950 assert(!err);
1951}
1952
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001953static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001954 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001955 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001956 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001957
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001958 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1959 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001960 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001961 moduleCreateInfo.codeSize = size;
1962 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001963
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001964 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1965 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001966
1967 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001968}
1969
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001970static void demo_prepare_vs(struct demo *demo) {
1971 const uint32_t vs_code[] = {
1972#include "cube.vert.inc"
1973 };
1974 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001975}
1976
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001977static void demo_prepare_fs(struct demo *demo) {
1978 const uint32_t fs_code[] = {
1979#include "cube.frag.inc"
1980 };
1981 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982}
1983
Karl Schultze4dc75c2016-02-02 15:37:51 -07001984static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001985 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001986 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001987 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001988 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001989 VkPipelineRasterizationStateCreateInfo rs;
1990 VkPipelineColorBlendStateCreateInfo cb;
1991 VkPipelineDepthStencilStateCreateInfo ds;
1992 VkPipelineViewportStateCreateInfo vp;
1993 VkPipelineMultisampleStateCreateInfo ms;
1994 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1995 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001996 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001997
Piers Daniellba839d12015-09-29 13:01:09 -06001998 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1999 memset(&dynamicState, 0, sizeof dynamicState);
2000 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2001 dynamicState.pDynamicStates = dynamicStateEnables;
2002
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002003 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002004 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002005 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002006
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002007 memset(&vi, 0, sizeof(vi));
2008 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2009
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002010 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002011 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002012 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002013
2014 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002015 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2016 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002017 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002018 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002019 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002020 rs.rasterizerDiscardEnable = VK_FALSE;
2021 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002022 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002023
2024 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002025 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2026 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002027 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002028 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002029 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002030 cb.attachmentCount = 1;
2031 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002032
Tony Barbour29645d02015-01-16 14:27:35 -07002033 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002034 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002035 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002036 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002037 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002038 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002039
2040 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002041 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002042 ds.depthTestEnable = VK_TRUE;
2043 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002044 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002045 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002046 ds.back.failOp = VK_STENCIL_OP_KEEP;
2047 ds.back.passOp = VK_STENCIL_OP_KEEP;
2048 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002049 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002050 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002051
Tony Barbour29645d02015-01-16 14:27:35 -07002052 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002053 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002054 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002055 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002056
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002057 demo_prepare_vs(demo);
2058 demo_prepare_fs(demo);
2059
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002060 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002061 VkPipelineShaderStageCreateInfo shaderStages[2];
2062 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2063
Karl Schultze4dc75c2016-02-02 15:37:51 -07002064 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2065 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002066 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002067 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002068
Karl Schultze4dc75c2016-02-02 15:37:51 -07002069 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2070 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002071 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002072 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002073
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002074 memset(&pipelineCache, 0, sizeof(pipelineCache));
2075 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002076
Dave Houlton5fa47912018-02-16 11:02:26 -07002077 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002078 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002079
Karl Schultze4dc75c2016-02-02 15:37:51 -07002080 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002081 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002082 pipeline.pRasterizationState = &rs;
2083 pipeline.pColorBlendState = &cb;
2084 pipeline.pMultisampleState = &ms;
2085 pipeline.pViewportState = &vp;
2086 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002087 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002088 pipeline.pStages = shaderStages;
2089 pipeline.renderPass = demo->render_pass;
2090 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002091
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002092 pipeline.renderPass = demo->render_pass;
2093
Dave Houlton5fa47912018-02-16 11:02:26 -07002094 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002095 assert(!err);
2096
Chia-I Wu63636062015-10-26 21:10:41 +08002097 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2098 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002099}
2100
Karl Schultze4dc75c2016-02-02 15:37:51 -07002101static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002102 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07002103 [0] =
2104 {
2105 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2106 .descriptorCount = demo->swapchainImageCount,
2107 },
2108 [1] =
2109 {
2110 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2111 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2112 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002113 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002114 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002115 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002116 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002117 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002118 .poolSizeCount = 2,
2119 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002120 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002121 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002122
Dave Houlton5fa47912018-02-16 11:02:26 -07002123 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002124 assert(!err);
2125}
2126
Karl Schultze4dc75c2016-02-02 15:37:51 -07002127static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002128 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002129 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002130 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002131
Dave Houlton5fa47912018-02-16 11:02:26 -07002132 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2133 .pNext = NULL,
2134 .descriptorPool = demo->desc_pool,
2135 .descriptorSetCount = 1,
2136 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002137
2138 VkDescriptorBufferInfo buffer_info;
2139 buffer_info.offset = 0;
2140 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002141
Chia-I Wuae721ba2015-05-25 16:27:55 +08002142 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002143 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002144 tex_descs[i].sampler = demo->textures[i].sampler;
2145 tex_descs[i].imageView = demo->textures[i].view;
Karl Schultze88bc842018-09-11 16:23:14 -06002146 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002147 }
2148
2149 memset(&writes, 0, sizeof(writes));
2150
2151 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002152 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002153 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002154 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002155
2156 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002157 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002158 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002159 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002160 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002161
Tony Barboura72d9492017-01-11 13:35:09 -07002162 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2163 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2164 assert(!err);
2165 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2166 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2167 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2168 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2169 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002170}
2171
Karl Schultze4dc75c2016-02-02 15:37:51 -07002172static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002173 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002174 attachments[1] = demo->depth.view;
2175
Chia-I Wuf973e322015-07-08 13:34:24 +08002176 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002177 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2178 .pNext = NULL,
2179 .renderPass = demo->render_pass,
2180 .attachmentCount = 2,
2181 .pAttachments = attachments,
2182 .width = demo->width,
2183 .height = demo->height,
2184 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002185 };
2186 VkResult U_ASSERT_ONLY err;
2187 uint32_t i;
2188
Tony Barbourd8e504f2015-10-08 14:26:24 -06002189 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002190 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002191 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002192 assert(!err);
2193 }
2194}
2195
Karl Schultze4dc75c2016-02-02 15:37:51 -07002196static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002197 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002198 if (demo->cmd_pool == VK_NULL_HANDLE) {
2199 const VkCommandPoolCreateInfo cmd_pool_info = {
2200 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2201 .pNext = NULL,
2202 .queueFamilyIndex = demo->graphics_queue_family_index,
2203 .flags = 0,
2204 };
2205 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2206 assert(!err);
2207 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002208
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002209 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002210 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002211 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002212 .commandPool = demo->cmd_pool,
2213 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002214 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002215 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002216 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2217 assert(!err);
2218 VkCommandBufferBeginInfo cmd_buf_info = {
2219 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2220 .pNext = NULL,
2221 .flags = 0,
2222 .pInheritanceInfo = NULL,
2223 };
2224 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2225 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002226
2227 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002228
2229 if (demo->is_minimized) {
2230 demo->prepared = false;
2231 return;
2232 }
2233
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002234 demo_prepare_depth(demo);
2235 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002236 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002237
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002238 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002239 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002240 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002241
Ian Elliotta0781972015-08-21 15:09:33 -06002242 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002243 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002244 assert(!err);
2245 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002246
Tony Barbour22e06272016-09-07 16:07:56 -06002247 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002248 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002249 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2250 .pNext = NULL,
2251 .queueFamilyIndex = demo->present_queue_family_index,
2252 .flags = 0,
2253 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002254 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002255 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002256 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002257 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2258 .pNext = NULL,
2259 .commandPool = demo->present_cmd_pool,
2260 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2261 .commandBufferCount = 1,
2262 };
2263 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002264 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2265 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002266 assert(!err);
2267 demo_build_image_ownership_cmd(demo, i);
2268 }
2269 }
2270
Chia-I Wu63ea9262015-03-26 13:14:16 +08002271 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002272 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002273
Chia-I Wuf973e322015-07-08 13:34:24 +08002274 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002275
Ian Elliotta0781972015-08-21 15:09:33 -06002276 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002277 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002278 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002279 }
2280
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002281 /*
2282 * Prepare functions above may generate pipeline commands
2283 * that need to be flushed before beginning the render loop.
2284 */
2285 demo_flush_init_cmd(demo);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06002286 if (demo->staging_texture.buffer) {
2287 demo_destroy_texture(demo, &demo->staging_texture);
Tony Barbour16156cb2016-10-19 14:15:49 -06002288 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002289
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002290 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002291 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002292}
2293
Karl Schultze4dc75c2016-02-02 15:37:51 -07002294static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002295 uint32_t i;
2296
2297 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002298 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002299
Tony Barbour66a56c32016-09-21 13:10:56 -06002300 // Wait for fences from present operations
2301 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002302 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002303 vkDestroyFence(demo->device, demo->fences[i], NULL);
2304 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2305 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2306 if (demo->separate_present_queue) {
2307 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2308 }
2309 }
2310
Jeremy Kniager602e4182018-01-19 10:52:34 -07002311 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2312 if (!demo->is_minimized) {
2313 for (i = 0; i < demo->swapchainImageCount; i++) {
2314 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2315 }
2316 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002317
Jeremy Kniager602e4182018-01-19 10:52:34 -07002318 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2319 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2320 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2321 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2322 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002323
Jeremy Kniager602e4182018-01-19 10:52:34 -07002324 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2325 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2326 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2327 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2328 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2329 }
2330 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002331
Jeremy Kniager602e4182018-01-19 10:52:34 -07002332 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2333 vkDestroyImage(demo->device, demo->depth.image, NULL);
2334 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002335
Jeremy Kniager602e4182018-01-19 10:52:34 -07002336 for (i = 0; i < demo->swapchainImageCount; i++) {
2337 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2338 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2339 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2340 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2341 }
2342 free(demo->swapchain_image_resources);
2343 free(demo->queue_props);
2344 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002345
Jeremy Kniager602e4182018-01-19 10:52:34 -07002346 if (demo->separate_present_queue) {
2347 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2348 }
Tony Barbour22e06272016-09-07 16:07:56 -06002349 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002350 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002351 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002352 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07002353 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002354 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002355 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002356
Tony Barbour153cb062016-12-07 13:43:36 -07002357#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002358 XDestroyWindow(demo->display, demo->xlib_window);
2359 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002360#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002361 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002362 xcb_disconnect(demo->connection);
2363 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002364#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002365 wl_keyboard_destroy(demo->keyboard);
2366 wl_pointer_destroy(demo->pointer);
2367 wl_seat_destroy(demo->seat);
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002368 xdg_toplevel_destroy(demo->xdg_toplevel);
2369 xdg_surface_destroy(demo->xdg_surface);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002370 wl_surface_destroy(demo->window);
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002371 xdg_wm_base_destroy(demo->xdg_wm_base);
2372 if (demo->xdg_decoration_mgr) {
2373 zxdg_toplevel_decoration_v1_destroy(demo->toplevel_decoration);
2374 zxdg_decoration_manager_v1_destroy(demo->xdg_decoration_mgr);
2375 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002376 wl_compositor_destroy(demo->compositor);
2377 wl_registry_destroy(demo->registry);
2378 wl_display_disconnect(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002379#endif
Karl Schultz86429112017-06-20 11:27:59 -06002380
2381 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002382}
2383
Karl Schultze4dc75c2016-02-02 15:37:51 -07002384static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002385 uint32_t i;
2386
Mike Stroyanbb60b382015-10-28 09:46:57 -06002387 // Don't react to resize until after first initialization.
2388 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002389 if (demo->is_minimized) {
2390 demo_prepare(demo);
2391 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002392 return;
2393 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002394 // In order to properly resize the window, we must re-create the swapchain
2395 // AND redo the command buffers, etc.
2396 //
2397 // First, perform part of the demo_cleanup() function:
2398 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002399 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002400
2401 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002402 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002403 }
Chia-I Wu63636062015-10-26 21:10:41 +08002404 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002405
Chia-I Wu63636062015-10-26 21:10:41 +08002406 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2407 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2408 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2409 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2410 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002411
2412 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002413 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2414 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2415 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2416 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002417 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002418
Chia-I Wu63636062015-10-26 21:10:41 +08002419 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2420 vkDestroyImage(demo->device, demo->depth.image, NULL);
2421 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002422
Ian Elliottcf074d32015-10-16 18:02:43 -06002423 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002424 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002425 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002426 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2427 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002428 }
Chia-I Wu63636062015-10-26 21:10:41 +08002429 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002430 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002431 if (demo->separate_present_queue) {
2432 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2433 }
Tony Barboura72d9492017-01-11 13:35:09 -07002434 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002435
Ian Elliottcf074d32015-10-16 18:02:43 -06002436 // Second, re-perform the demo_prepare() function, which will re-create the
2437 // swapchain:
2438 demo_prepare(demo);
2439}
2440
David Pinedo2bb7c932015-06-18 17:03:14 -06002441// On MS-Windows, make this a global, so it's available to WndProc()
2442struct demo demo;
2443
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002444#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002445static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002446 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002447
Ian Elliott639ca472015-04-16 15:23:05 -06002448 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002449 demo->curFrame++;
Petr Krausd88e4e52019-01-23 18:45:04 +01002450 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002451 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002452 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002453}
Ian Elliott639ca472015-04-16 15:23:05 -06002454
2455// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002456LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2457 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002458 case WM_CLOSE:
2459 PostQuitMessage(validation_error);
2460 break;
2461 case WM_PAINT:
2462 // The validation callback calls MessageBox which can generate paint
2463 // events - don't make more Vulkan calls if we got here from the
2464 // callback
2465 if (!in_callback) {
2466 demo_run(&demo);
2467 }
2468 break;
2469 case WM_GETMINMAXINFO: // set window's minimum size
2470 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2471 return 0;
Aaron Hagan500b9c32018-10-03 21:56:29 -04002472 case WM_ERASEBKGND:
2473 return 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002474 case WM_SIZE:
2475 // Resize the application to the new window size, except when
2476 // it was minimized. Vulkan doesn't support images or swapchains
2477 // with width=0 and height=0.
2478 if (wParam != SIZE_MINIMIZED) {
2479 demo.width = lParam & 0xffff;
2480 demo.height = (lParam & 0xffff0000) >> 16;
2481 demo_resize(&demo);
2482 }
2483 break;
Petr Krausd88e4e52019-01-23 18:45:04 +01002484 case WM_KEYDOWN:
2485 switch (wParam) {
2486 case VK_ESCAPE:
2487 PostQuitMessage(validation_error);
2488 break;
2489 case VK_LEFT:
2490 demo.spin_angle -= demo.spin_increment;
2491 break;
2492 case VK_RIGHT:
2493 demo.spin_angle += demo.spin_increment;
2494 break;
2495 case VK_SPACE:
2496 demo.pause = !demo.pause;
2497 break;
2498 }
2499 return 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002500 default:
2501 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002502 }
2503 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2504}
2505
Karl Schultze4dc75c2016-02-02 15:37:51 -07002506static void demo_create_window(struct demo *demo) {
2507 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002508
2509 // Initialize the window class structure:
2510 win_class.cbSize = sizeof(WNDCLASSEX);
2511 win_class.style = CS_HREDRAW | CS_VREDRAW;
2512 win_class.lpfnWndProc = WndProc;
2513 win_class.cbClsExtra = 0;
2514 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002515 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002516 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2517 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2518 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2519 win_class.lpszMenuName = NULL;
2520 win_class.lpszClassName = demo->name;
2521 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2522 // Register window class:
2523 if (!RegisterClassEx(&win_class)) {
2524 // It didn't work, so try to give a useful error:
2525 printf("Unexpected error trying to start the application!\n");
2526 fflush(stdout);
2527 exit(1);
2528 }
2529 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002530 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002531 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002532 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002533 demo->name, // class name
2534 demo->name, // app name
2535 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002536 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002537 100, 100, // x/y coords
2538 wr.right - wr.left, // width
2539 wr.bottom - wr.top, // height
2540 NULL, // handle to parent
2541 NULL, // handle to menu
2542 demo->connection, // hInstance
2543 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002544 if (!demo->window) {
2545 // It didn't work, so try to give a useful error:
2546 printf("Cannot create a window in which to draw!\n");
2547 fflush(stdout);
2548 exit(1);
2549 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002550 // Window client area size must be at least 1 pixel high, to prevent crash.
2551 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002552 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002553}
Tony Barbour8295ac42016-08-08 11:04:17 -06002554#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002555static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002556 const char *display_envar = getenv("DISPLAY");
2557 if (display_envar == NULL || display_envar[0] == '\0') {
2558 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2559 fflush(stdout);
2560 exit(1);
2561 }
Tony Barbour2593b182016-04-19 10:57:58 -06002562
Tony Barbouree9cd372017-01-31 16:09:58 -07002563 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002564 demo->display = XOpenDisplay(NULL);
2565 long visualMask = VisualScreenMask;
2566 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002567 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002568 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002569 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002570
Dave Houlton5fa47912018-02-16 11:02:26 -07002571 Colormap colormap =
2572 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002573
Dave Houlton5fa47912018-02-16 11:02:26 -07002574 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002575 windowAttributes.colormap = colormap;
2576 windowAttributes.background_pixel = 0xFFFFFFFF;
2577 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002578 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002579
Dave Houlton5fa47912018-02-16 11:02:26 -07002580 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2581 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2582 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002583
2584 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2585 XMapWindow(demo->display, demo->xlib_window);
2586 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002587 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002588}
2589static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002590 switch (event->type) {
2591 case ClientMessage:
2592 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002593 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002594 case KeyPress:
2595 switch (event->xkey.keycode) {
2596 case 0x9: // Escape
2597 demo->quit = true;
2598 break;
2599 case 0x71: // left arrow key
2600 demo->spin_angle -= demo->spin_increment;
2601 break;
2602 case 0x72: // right arrow key
2603 demo->spin_angle += demo->spin_increment;
2604 break;
2605 case 0x41: // space bar
2606 demo->pause = !demo->pause;
2607 break;
2608 }
Tony Barbour2593b182016-04-19 10:57:58 -06002609 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002610 case ConfigureNotify:
2611 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2612 demo->width = event->xconfigure.width;
2613 demo->height = event->xconfigure.height;
2614 demo_resize(demo);
2615 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002616 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002617 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002618 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002619 }
Tony Barbour2593b182016-04-19 10:57:58 -06002620}
2621
2622static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002623 while (!demo->quit) {
2624 XEvent event;
2625
2626 if (demo->pause) {
2627 XNextEvent(demo->display, &event);
2628 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002629 }
2630 while (XPending(demo->display) > 0) {
2631 XNextEvent(demo->display, &event);
2632 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002633 }
2634
Tony Barbour2593b182016-04-19 10:57:58 -06002635 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002636 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002637 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002638 }
2639}
Tony Barbour153cb062016-12-07 13:43:36 -07002640#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002641static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002642 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002643 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002644 case XCB_EXPOSE:
2645 // TODO: Resize window
2646 break;
2647 case XCB_CLIENT_MESSAGE:
2648 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2649 demo->quit = true;
2650 }
2651 break;
2652 case XCB_KEY_RELEASE: {
2653 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002654
Dave Houlton5fa47912018-02-16 11:02:26 -07002655 switch (key->detail) {
2656 case 0x9: // Escape
2657 demo->quit = true;
2658 break;
2659 case 0x71: // left arrow key
2660 demo->spin_angle -= demo->spin_increment;
2661 break;
2662 case 0x72: // right arrow key
2663 demo->spin_angle += demo->spin_increment;
2664 break;
2665 case 0x41: // space bar
2666 demo->pause = !demo->pause;
2667 break;
2668 }
2669 } break;
2670 case XCB_CONFIGURE_NOTIFY: {
2671 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2672 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2673 demo->width = cfg->width;
2674 demo->height = cfg->height;
2675 demo_resize(demo);
2676 }
2677 } break;
2678 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002679 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002680 }
2681}
2682
Tony Barbour2593b182016-04-19 10:57:58 -06002683static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002684 xcb_flush(demo->connection);
2685
2686 while (!demo->quit) {
2687 xcb_generic_event_t *event;
2688
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002689 if (demo->pause) {
2690 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002691 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002692 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002693 }
2694 while (event) {
2695 demo_handle_xcb_event(demo, event);
2696 free(event);
2697 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002698 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002699
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002700 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002701 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002702 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002703 }
2704}
2705
Tony Barbour2593b182016-04-19 10:57:58 -06002706static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002707 uint32_t value_mask, value_list[32];
2708
Tony Barbour2593b182016-04-19 10:57:58 -06002709 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002710
2711 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2712 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002713 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002714
Dave Houlton5fa47912018-02-16 11:02:26 -07002715 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2716 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002717
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002718 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002719 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2720 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002721
Dave Houlton5fa47912018-02-16 11:02:26 -07002722 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2723 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002724
Dave Houlton5fa47912018-02-16 11:02:26 -07002725 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002726 &(*demo->atom_wm_delete_window).atom);
2727 free(reply);
2728
Tony Barbour2593b182016-04-19 10:57:58 -06002729 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002730
Karl Schultze4dc75c2016-02-02 15:37:51 -07002731 // Force the x/y coordinates to 100,100 results are identical in consecutive
2732 // runs
2733 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002734 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002735}
Tony Barbour6b131662016-08-25 13:14:45 -06002736// VK_USE_PLATFORM_XCB_KHR
2737#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002738static void demo_run(struct demo *demo) {
2739 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002740 if (demo->pause) {
2741 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002742 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002743 wl_display_dispatch_pending(demo->display); // don't block
2744 demo_draw(demo);
2745 demo->curFrame++;
2746 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2747 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002748 }
2749}
2750
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002751static void handle_surface_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) {
2752 struct demo *demo = (struct demo *)data;
2753 xdg_surface_ack_configure(xdg_surface, serial);
2754 if (demo->xdg_surface_has_been_configured) {
2755 demo_resize(demo);
2756 }
2757 demo->xdg_surface_has_been_configured = 1;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002758}
2759
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002760static const struct xdg_surface_listener xdg_surface_listener = {handle_surface_configure};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002761
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002762static void handle_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel UNUSED, int32_t width, int32_t height,
2763 struct wl_array *states UNUSED) {
2764 struct demo *demo = (struct demo *)data;
2765 demo->width = width;
2766 demo->height = height;
2767 /* This should be followed by a surface configure */
2768}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002769
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002770static void handle_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel UNUSED) {
2771 struct demo *demo = (struct demo *)data;
2772 demo->quit = true;
2773}
2774
2775static const struct xdg_toplevel_listener xdg_toplevel_listener = {handle_toplevel_configure, handle_toplevel_close};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002776
2777static void demo_create_window(struct demo *demo) {
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002778 if (!demo->xdg_wm_base) {
2779 printf("Compositor did not provide the standard protocol xdg-wm-base\n");
2780 fflush(stdout);
2781 exit(1);
2782 }
2783
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002784 demo->window = wl_compositor_create_surface(demo->compositor);
2785 if (!demo->window) {
2786 printf("Can not create wayland_surface from compositor!\n");
2787 fflush(stdout);
2788 exit(1);
2789 }
2790
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002791 demo->xdg_surface = xdg_wm_base_get_xdg_surface(demo->xdg_wm_base, demo->window);
2792 if (!demo->xdg_surface) {
2793 printf("Can not get xdg_surface from wayland_surface!\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002794 fflush(stdout);
2795 exit(1);
2796 }
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04002797 demo->xdg_toplevel = xdg_surface_get_toplevel(demo->xdg_surface);
2798 if (!demo->xdg_toplevel) {
2799 printf("Can not allocate xdg_toplevel for xdg_surface!\n");
2800 fflush(stdout);
2801 exit(1);
2802 }
2803 xdg_surface_add_listener(demo->xdg_surface, &xdg_surface_listener, demo);
2804 xdg_toplevel_add_listener(demo->xdg_toplevel, &xdg_toplevel_listener, demo);
2805 xdg_toplevel_set_title(demo->xdg_toplevel, APP_SHORT_NAME);
2806 if (demo->xdg_decoration_mgr) {
2807 // if supported, let the compositor render titlebars for us
2808 demo->toplevel_decoration =
2809 zxdg_decoration_manager_v1_get_toplevel_decoration(demo->xdg_decoration_mgr, demo->xdg_toplevel);
2810 zxdg_toplevel_decoration_v1_set_mode(demo->toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
2811 }
2812
2813 wl_surface_commit(demo->window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002814}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002815#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2816static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002817 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002818
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002819 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002820 demo->curFrame++;
2821}
Karl Schultz206b1c52018-04-13 18:02:07 -06002822#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2823static void demo_run(struct demo *demo) {
2824 demo_draw(demo);
2825 demo->curFrame++;
2826 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2827 demo->quit = TRUE;
2828 }
2829}
Damien Leone600c3052017-01-31 10:26:07 -07002830#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2831static VkResult demo_create_display_surface(struct demo *demo) {
2832 VkResult U_ASSERT_ONLY err;
2833 uint32_t display_count;
2834 uint32_t mode_count;
2835 uint32_t plane_count;
2836 VkDisplayPropertiesKHR display_props;
2837 VkDisplayKHR display;
2838 VkDisplayModePropertiesKHR mode_props;
2839 VkDisplayPlanePropertiesKHR *plane_props;
2840 VkBool32 found_plane = VK_FALSE;
2841 uint32_t plane_index;
2842 VkExtent2D image_extent;
2843 VkDisplaySurfaceCreateInfoKHR create_info;
2844
2845 // Get the first display
2846 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2847 assert(!err);
2848
2849 if (display_count == 0) {
2850 printf("Cannot find any display!\n");
2851 fflush(stdout);
2852 exit(1);
2853 }
2854
2855 display_count = 1;
2856 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2857 assert(!err || (err == VK_INCOMPLETE));
2858
2859 display = display_props.display;
2860
2861 // Get the first mode of the display
2862 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2863 assert(!err);
2864
2865 if (mode_count == 0) {
2866 printf("Cannot find any mode for the display!\n");
2867 fflush(stdout);
2868 exit(1);
2869 }
2870
2871 mode_count = 1;
2872 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2873 assert(!err || (err == VK_INCOMPLETE));
2874
2875 // Get the list of planes
2876 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2877 assert(!err);
2878
2879 if (plane_count == 0) {
2880 printf("Cannot find any plane!\n");
2881 fflush(stdout);
2882 exit(1);
2883 }
2884
2885 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2886 assert(plane_props);
2887
2888 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2889 assert(!err);
2890
2891 // Find a plane compatible with the display
2892 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2893 uint32_t supported_count;
2894 VkDisplayKHR *supported_displays;
2895
2896 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002897 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002898 continue;
2899 }
2900
2901 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2902 assert(!err);
2903
2904 if (supported_count == 0) {
2905 continue;
2906 }
2907
2908 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2909 assert(supported_displays);
2910
2911 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2912 assert(!err);
2913
2914 for (uint32_t i = 0; i < supported_count; i++) {
2915 if (supported_displays[i] == display) {
2916 found_plane = VK_TRUE;
2917 break;
2918 }
2919 }
2920
2921 free(supported_displays);
2922
2923 if (found_plane) {
2924 break;
2925 }
2926 }
2927
2928 if (!found_plane) {
2929 printf("Cannot find a plane compatible with the display!\n");
2930 fflush(stdout);
2931 exit(1);
2932 }
2933
2934 free(plane_props);
2935
Tony Barbour820b55b2017-03-14 08:55:46 -06002936 VkDisplayPlaneCapabilitiesKHR planeCaps;
2937 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2938 // Find a supported alpha mode
Mark Young66510e52017-11-10 10:05:14 -07002939 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2940 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002941 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2942 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2943 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2944 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2945 };
2946 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2947 if (planeCaps.supportedAlpha & alphaModes[i]) {
2948 alphaMode = alphaModes[i];
2949 break;
2950 }
2951 }
Damien Leone600c3052017-01-31 10:26:07 -07002952 image_extent.width = mode_props.parameters.visibleRegion.width;
2953 image_extent.height = mode_props.parameters.visibleRegion.height;
2954
2955 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2956 create_info.pNext = NULL;
2957 create_info.flags = 0;
2958 create_info.displayMode = mode_props.displayMode;
2959 create_info.planeIndex = plane_index;
2960 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2961 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002962 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002963 create_info.globalAlpha = 1.0f;
2964 create_info.imageExtent = image_extent;
2965
2966 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2967}
2968
Dave Houlton5fa47912018-02-16 11:02:26 -07002969static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07002970 while (!demo->quit) {
2971 demo_draw(demo);
2972 demo->curFrame++;
2973
2974 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2975 demo->quit = true;
2976 }
2977 }
2978}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002979#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002980
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002981/*
2982 * Return 1 (true) if all layer names specified in check_names
2983 * can be found in given layer properties.
2984 */
Dave Houlton5fa47912018-02-16 11:02:26 -07002985static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002986 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002987 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002988 for (uint32_t j = 0; j < layer_count; j++) {
2989 if (!strcmp(check_names[i], layers[j].layerName)) {
2990 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002991 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002992 }
2993 }
2994 if (!found) {
2995 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2996 return 0;
2997 }
2998 }
2999 return 1;
3000}
3001
Karl Schultze4dc75c2016-02-02 15:37:51 -07003002static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003003 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003004 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003005 uint32_t instance_layer_count = 0;
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003006 char *instance_validation_layers[] = {"VK_LAYER_KHRONOS_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06003007 demo->enabled_extension_count = 0;
3008 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07003009 demo->is_minimized = false;
3010 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003011
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003012 // Look for validation layers
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003013 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003014 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003015 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07003016 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003017
Karl Schultz7c50ad62016-04-01 12:07:03 -06003018 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003019 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
3020 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06003021 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003022
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003023 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
Dave Houlton5fa47912018-02-16 11:02:26 -07003024 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06003025 if (validation_found) {
Mark Lobodzinski96f2ff32019-03-27 12:37:30 -06003026 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
3027 demo->enabled_layers[0] = "VK_LAYER_KHRONOS_validation";
Karl Schultz7c50ad62016-04-01 12:07:03 -06003028 }
3029 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003030 }
Dustin Graves7c287352016-01-27 13:48:06 -07003031
Karl Schultz7c50ad62016-04-01 12:07:03 -06003032 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003033 ERR_EXIT(
3034 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
3035 "Please look at the Getting Started guide for additional information.\n",
3036 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06003037 }
Dustin Graves7c287352016-01-27 13:48:06 -07003038 }
3039
3040 /* Look for instance extensions */
3041 VkBool32 surfaceExtFound = 0;
3042 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003043 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003044
Dave Houlton5fa47912018-02-16 11:02:26 -07003045 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003046 assert(!err);
3047
Dustin Graves7c287352016-01-27 13:48:06 -07003048 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003049 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3050 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003051 assert(!err);
3052 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003053 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003054 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003055 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003056 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003057#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003058 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003059 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003060 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003061 }
Tony Barbour153cb062016-12-07 13:43:36 -07003062#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003063 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06003064 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003065 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06003066 }
Tony Barbour153cb062016-12-07 13:43:36 -07003067#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003068 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003069 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003070 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003071 }
Tony Barbour153cb062016-12-07 13:43:36 -07003072#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003073 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003074 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003075 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003076 }
Damien Leone600c3052017-01-31 10:26:07 -07003077#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003078 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07003079 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003080 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07003081 }
Tony Barbour153cb062016-12-07 13:43:36 -07003082#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003083 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003084 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003085 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003086 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003087#elif defined(VK_USE_PLATFORM_IOS_MVK)
3088 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3089 platformSurfaceExtFound = 1;
3090 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3091 }
3092#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3093 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3094 platformSurfaceExtFound = 1;
3095 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3096 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003097#endif
Mark Young66510e52017-11-10 10:05:14 -07003098 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3099 if (demo->validate) {
3100 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3101 }
3102 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003103 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003104 }
Dustin Graves7c287352016-01-27 13:48:06 -07003105
3106 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003107 }
Dustin Graves7c287352016-01-27 13:48:06 -07003108
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003109 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003110 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3111 " extension.\n\n"
3112 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3113 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06003114 "vkCreateInstance Failure");
3115 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003116 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003117#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003118 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3119 " extension.\n\n"
3120 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3121 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003122 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003123#elif defined(VK_USE_PLATFORM_IOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003124 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_IOS_SURFACE_EXTENSION_NAME
3125 " extension.\n\n"
3126 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3127 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003128 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003129#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003130 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_MACOS_SURFACE_EXTENSION_NAME
3131 " extension.\n\n"
3132 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3133 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003134 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003135#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003136 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3137 " extension.\n\n"
3138 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3139 "Please look at the Getting Started guide for additional information.\n",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003140 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003141#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003142 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3143 " extension.\n\n"
3144 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3145 "Please look at the Getting Started guide for additional information.\n",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003146 "vkCreateInstance Failure");
Damien Leone600c3052017-01-31 10:26:07 -07003147#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003148 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3149 " extension.\n\n"
3150 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3151 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07003152 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003153#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003154 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3155 " extension.\n\n"
3156 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3157 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003158 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003159#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003160 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3161 " extension.\n\n"
3162 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3163 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06003164 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003165#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003166 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003167 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003168 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003169 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003170 .pApplicationName = APP_SHORT_NAME,
3171 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003172 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003173 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003174 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003175 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003176 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003177 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003178 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003179 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003180 .enabledLayerCount = demo->enabled_layer_count,
3181 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3182 .enabledExtensionCount = demo->enabled_extension_count,
3183 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003184 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003185
3186 /*
3187 * This is info for a temp callback to use during CreateInstance.
3188 * After the instance is created, we use the instance-based
3189 * function to register the final callback.
3190 */
Mark Young66510e52017-11-10 10:05:14 -07003191 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003192 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003193 // VK_EXT_debug_utils style
3194 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3195 dbg_messenger_create_info.pNext = NULL;
3196 dbg_messenger_create_info.flags = 0;
3197 dbg_messenger_create_info.messageSeverity =
3198 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3199 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3200 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3201 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3202 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3203 dbg_messenger_create_info.pUserData = demo;
3204 inst_info.pNext = &dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003205 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003206
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003207 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003208
Chia-I Wu63636062015-10-26 21:10:41 +08003209 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003210 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003211 ERR_EXIT(
3212 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3213 "Please look at the Getting Started guide for additional information.\n",
3214 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003215 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003216 ERR_EXIT(
3217 "Cannot find a specified extension library.\n"
3218 "Make sure your layers path is set appropriately.\n",
3219 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003220 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003221 ERR_EXIT(
3222 "vkCreateInstance failed.\n\n"
3223 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3224 "Please look at the Getting Started guide for additional information.\n",
3225 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003226 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003227
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003228 /* Make initial call to query gpu_count, then second call for gpu info*/
3229 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
Petr Kraus2f1dbdb2018-04-14 14:45:17 +02003230 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003231
3232 if (gpu_count > 0) {
3233 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3234 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3235 assert(!err);
3236 /* For cube demo we just grab the first physical device */
3237 demo->gpu = physical_devices[0];
3238 free(physical_devices);
3239 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003240 ERR_EXIT(
3241 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3242 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3243 "Please look at the Getting Started guide for additional information.\n",
3244 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003245 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003246
Dustin Graves7c287352016-01-27 13:48:06 -07003247 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003248 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003249 VkBool32 swapchainExtFound = 0;
3250 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003251 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003252
Dave Houlton5fa47912018-02-16 11:02:26 -07003253 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003254 assert(!err);
3255
Dustin Graves7c287352016-01-27 13:48:06 -07003256 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003257 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3258 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003259 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003260
Dustin Graves7c287352016-01-27 13:48:06 -07003261 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003262 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003263 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003264 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003265 }
3266 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003267 }
Dustin Graves7c287352016-01-27 13:48:06 -07003268
Ian Elliott53622a72017-03-28 11:06:33 -06003269 if (demo->VK_KHR_incremental_present_enabled) {
3270 // Even though the user "enabled" the extension via the command
3271 // line, we must make sure that it's enumerated for use with the
3272 // device. Therefore, disable it here, and re-enable it again if
3273 // enumerated.
3274 demo->VK_KHR_incremental_present_enabled = false;
3275 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003276 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3277 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003278 demo->VK_KHR_incremental_present_enabled = true;
3279 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3280 }
3281 assert(demo->enabled_extension_count < 64);
3282 }
3283 if (!demo->VK_KHR_incremental_present_enabled) {
3284 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3285 }
3286 }
3287
Ian Elliottd940ca22017-03-22 08:31:27 -06003288 if (demo->VK_GOOGLE_display_timing_enabled) {
3289 // Even though the user "enabled" the extension via the command
3290 // line, we must make sure that it's enumerated for use with the
3291 // device. Therefore, disable it here, and re-enable it again if
3292 // enumerated.
3293 demo->VK_GOOGLE_display_timing_enabled = false;
3294 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003295 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3296 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003297 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003298 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003299 }
3300 assert(demo->enabled_extension_count < 64);
3301 }
3302 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003303 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003304 }
3305 }
3306
Dustin Graves7c287352016-01-27 13:48:06 -07003307 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003308 }
Dustin Graves7c287352016-01-27 13:48:06 -07003309
Tony Barbourcc69f452015-10-08 13:59:42 -06003310 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003311 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3312 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3313 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003314 "vkCreateInstance Failure");
3315 }
3316
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003317 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003318 // Setup VK_EXT_debug_utils function pointers always (we use them for
3319 // debug labels and names).
3320 demo->CreateDebugUtilsMessengerEXT =
3321 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3322 demo->DestroyDebugUtilsMessengerEXT =
3323 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3324 demo->SubmitDebugUtilsMessageEXT =
3325 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3326 demo->CmdBeginDebugUtilsLabelEXT =
3327 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3328 demo->CmdEndDebugUtilsLabelEXT =
3329 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3330 demo->CmdInsertDebugUtilsLabelEXT =
3331 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3332 demo->SetDebugUtilsObjectNameEXT =
3333 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3334 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3335 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3336 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3337 NULL == demo->SetDebugUtilsObjectNameEXT) {
3338 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003339 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003340
Mark Young66510e52017-11-10 10:05:14 -07003341 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003342 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003343 case VK_SUCCESS:
3344 break;
3345 case VK_ERROR_OUT_OF_HOST_MEMORY:
Mark Young66510e52017-11-10 10:05:14 -07003346 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003347 break;
3348 default:
Mark Young66510e52017-11-10 10:05:14 -07003349 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003350 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003351 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003352 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003353 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003354
3355 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003356 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003357 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003358
Dave Houlton5fa47912018-02-16 11:02:26 -07003359 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3360 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003361
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003362 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003363 // If app has specific feature requirements it should check supported
3364 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003365 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003366 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003367
Tony Barbourda2bad52016-01-22 14:36:40 -07003368 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3369 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3370 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3371 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3372 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3373}
3374
Karl Schultze4dc75c2016-02-02 15:37:51 -07003375static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003376 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003377 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003378 VkDeviceQueueCreateInfo queues[2];
3379 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3380 queues[0].pNext = NULL;
3381 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3382 queues[0].queueCount = 1;
3383 queues[0].pQueuePriorities = queue_priorities;
3384 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003385
3386 VkDeviceCreateInfo device = {
3387 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3388 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003389 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003390 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003391 .enabledLayerCount = 0,
3392 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003393 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003394 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003395 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003396 };
Tony Barbour22e06272016-09-07 16:07:56 -06003397 if (demo->separate_present_queue) {
3398 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3399 queues[1].pNext = NULL;
3400 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3401 queues[1].queueCount = 1;
3402 queues[1].pQueuePriorities = queue_priorities;
3403 queues[1].flags = 0;
3404 device.queueCreateInfoCount = 2;
3405 }
Chia-I Wu63636062015-10-26 21:10:41 +08003406 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003407 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003408}
3409
Tony-LunarG6cebf142019-09-11 14:32:23 -06003410static void demo_create_surface(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003411 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003412
Karl Schultze4dc75c2016-02-02 15:37:51 -07003413// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003414#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003415 VkWin32SurfaceCreateInfoKHR createInfo;
3416 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3417 createInfo.pNext = NULL;
3418 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003419 createInfo.hinstance = demo->connection;
3420 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003421
Dave Houlton5fa47912018-02-16 11:02:26 -07003422 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003423#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003424 VkWaylandSurfaceCreateInfoKHR createInfo;
3425 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3426 createInfo.pNext = NULL;
3427 createInfo.flags = 0;
3428 createInfo.display = demo->display;
3429 createInfo.surface = demo->window;
3430
Dave Houlton5fa47912018-02-16 11:02:26 -07003431 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003432#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3433 VkAndroidSurfaceCreateInfoKHR createInfo;
3434 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3435 createInfo.pNext = NULL;
3436 createInfo.flags = 0;
Mike Schuchardt837d5162018-03-08 12:57:02 -07003437 createInfo.window = (struct ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003438
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003439 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003440#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3441 VkXlibSurfaceCreateInfoKHR createInfo;
3442 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3443 createInfo.pNext = NULL;
3444 createInfo.flags = 0;
3445 createInfo.dpy = demo->display;
3446 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003447
Dave Houlton5fa47912018-02-16 11:02:26 -07003448 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003449#elif defined(VK_USE_PLATFORM_XCB_KHR)
3450 VkXcbSurfaceCreateInfoKHR createInfo;
3451 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3452 createInfo.pNext = NULL;
3453 createInfo.flags = 0;
3454 createInfo.connection = demo->connection;
3455 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003456
Tony Barbour153cb062016-12-07 13:43:36 -07003457 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003458#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3459 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003460#elif defined(VK_USE_PLATFORM_IOS_MVK)
3461 VkIOSSurfaceCreateInfoMVK surface;
3462 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3463 surface.pNext = NULL;
3464 surface.flags = 0;
3465 surface.pView = demo->window;
3466
3467 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3468#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3469 VkMacOSSurfaceCreateInfoMVK surface;
3470 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3471 surface.pNext = NULL;
3472 surface.flags = 0;
3473 surface.pView = demo->window;
3474
3475 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003476#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003477 assert(!err);
Tony-LunarG6cebf142019-09-11 14:32:23 -06003478}
3479
3480static void demo_init_vk_swapchain(struct demo *demo) {
3481 VkResult U_ASSERT_ONLY err;
3482
3483 demo_create_surface(demo);
Ian Elliottaaae5352015-07-06 14:27:58 -06003484
Tony Barbourcc69f452015-10-08 13:59:42 -06003485 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003486 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003487 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003488 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003489 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003490
3491 // Search for a graphics and a present queue in the array of queue
3492 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003493 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3494 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003495 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003496 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3497 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3498 graphicsQueueFamilyIndex = i;
3499 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003500
Tony Barbourab2a0362016-09-06 11:40:12 -06003501 if (supportsPresent[i] == VK_TRUE) {
3502 graphicsQueueFamilyIndex = i;
3503 presentQueueFamilyIndex = i;
3504 break;
3505 }
3506 }
3507 }
3508
3509 if (presentQueueFamilyIndex == UINT32_MAX) {
3510 // If didn't find a queue that supports both graphics and present, then
3511 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003512 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003513 if (supportsPresent[i] == VK_TRUE) {
3514 presentQueueFamilyIndex = i;
3515 break;
3516 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003517 }
3518 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003519
3520 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003521 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3522 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003523 }
3524
Tony Barbourab2a0362016-09-06 11:40:12 -06003525 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3526 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003527 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003528 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003529
Tony Barbourda2bad52016-01-22 14:36:40 -07003530 demo_create_device(demo);
3531
3532 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3533 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3534 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3535 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3536 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003537 if (demo->VK_GOOGLE_display_timing_enabled) {
3538 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3539 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3540 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003541
Dave Houlton5fa47912018-02-16 11:02:26 -07003542 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003543
Tony Barbour22e06272016-09-07 16:07:56 -06003544 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003545 demo->present_queue = demo->graphics_queue;
3546 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003547 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003548 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003549
Ian Elliottaaae5352015-07-06 14:27:58 -06003550 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003551 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003552 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003553 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003554 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3555 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003556 assert(!err);
3557 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3558 // the surface has no preferred format. Otherwise, at least one
3559 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003560 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003561 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003562 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003563 assert(formatCount >= 1);
3564 demo->format = surfFormats[0].format;
3565 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003566 demo->color_space = surfFormats[0].colorSpace;
Jason Chen5d4fee92019-04-04 12:45:29 +08003567 free(surfFormats);
Ian Elliotte4602cd2015-04-21 16:41:02 -06003568
David Pinedo2bb7c932015-06-18 17:03:14 -06003569 demo->quit = false;
3570 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003571
Tony Barbource7524e2016-07-28 12:01:45 -06003572 // Create semaphores to synchronize acquiring presentable buffers before
3573 // rendering and waiting for drawing to be complete before presenting
3574 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3575 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3576 .pNext = NULL,
3577 .flags = 0,
3578 };
Tony Barbource7524e2016-07-28 12:01:45 -06003579
Tony Barbour66a56c32016-09-21 13:10:56 -06003580 // Create fences that we can use to throttle if we get too far
3581 // ahead of the image presents
3582 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003583 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003584 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003585 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3586 assert(!err);
3587
Dave Houlton5fa47912018-02-16 11:02:26 -07003588 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003589 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003590
Dave Houlton5fa47912018-02-16 11:02:26 -07003591 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003592 assert(!err);
3593
3594 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003595 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003596 assert(!err);
3597 }
Tony Barbour22e06272016-09-07 16:07:56 -06003598 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003599 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003600
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003601 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003602 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003603}
3604
Tony Barbour153cb062016-12-07 13:43:36 -07003605#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003606static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3607 wl_fixed_t sy) {}
3608
3609static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3610
3611static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3612
3613static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3614 uint32_t state) {
3615 struct demo *demo = data;
3616 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003617 xdg_toplevel_move(demo->xdg_toplevel, demo->seat, serial);
Joey Bzdek15eb0702017-06-07 09:40:36 -06003618 }
3619}
3620
3621static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3622
3623static const struct wl_pointer_listener pointer_listener = {
3624 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3625};
3626
3627static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3628
3629static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3630 struct wl_array *keys) {}
3631
3632static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3633
3634static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3635 uint32_t state) {
3636 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3637 struct demo *demo = data;
3638 switch (key) {
3639 case KEY_ESC: // Escape
3640 demo->quit = true;
3641 break;
3642 case KEY_LEFT: // left arrow key
3643 demo->spin_angle -= demo->spin_increment;
3644 break;
3645 case KEY_RIGHT: // right arrow key
3646 demo->spin_angle += demo->spin_increment;
3647 break;
3648 case KEY_SPACE: // space bar
3649 demo->pause = !demo->pause;
3650 break;
3651 }
3652}
3653
3654static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3655 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3656
3657static const struct wl_keyboard_listener keyboard_listener = {
3658 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3659};
3660
3661static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3662 // Subscribe to pointer events
3663 struct demo *demo = data;
3664 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3665 demo->pointer = wl_seat_get_pointer(seat);
3666 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3667 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3668 wl_pointer_destroy(demo->pointer);
3669 demo->pointer = NULL;
3670 }
3671 // Subscribe to keyboard events
3672 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3673 demo->keyboard = wl_seat_get_keyboard(seat);
3674 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3675 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3676 wl_keyboard_destroy(demo->keyboard);
3677 demo->keyboard = NULL;
3678 }
3679}
3680
3681static const struct wl_seat_listener seat_listener = {
3682 seat_handle_capabilities,
3683};
3684
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003685static void wm_base_ping(void *data UNUSED, struct xdg_wm_base *xdg_wm_base, uint32_t serial) {
3686 xdg_wm_base_pong(xdg_wm_base, serial);
3687}
3688
3689static const struct xdg_wm_base_listener wm_base_listener = {wm_base_ping};
3690
3691static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
3692 uint32_t version UNUSED) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003693 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003694 // pickup wayland objects when they appear
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003695 if (strcmp(interface, wl_compositor_interface.name) == 0) {
Georges Basile Stavracas Neto7da3ea72019-10-05 11:53:39 -03003696 uint32_t minVersion = version < 4 ? version : 4;
3697 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, minVersion);
3698 if (demo->VK_KHR_incremental_present_enabled && minVersion < 4) {
3699 fprintf(stderr, "Wayland compositor doesn't support VK_KHR_incremental_present, disabling.\n");
3700 demo->VK_KHR_incremental_present_enabled = false;
3701 }
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003702 } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
3703 demo->xdg_wm_base = wl_registry_bind(registry, id, &xdg_wm_base_interface, 1);
3704 xdg_wm_base_add_listener(demo->xdg_wm_base, &wm_base_listener, NULL);
3705 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003706 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3707 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Manuel Stoeckl6f6e3372019-06-26 20:55:53 -04003708 } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
3709 demo->xdg_decoration_mgr = wl_registry_bind(registry, id, &zxdg_decoration_manager_v1_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003710 }
3711}
3712
Dave Houlton5fa47912018-02-16 11:02:26 -07003713static 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 +09003714
Dave Houlton5fa47912018-02-16 11:02:26 -07003715static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003716#endif
3717
Karl Schultze4dc75c2016-02-02 15:37:51 -07003718static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003719#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003720 const xcb_setup_t *setup;
3721 xcb_screen_iterator_t iter;
3722 int scr;
3723
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003724 const char *display_envar = getenv("DISPLAY");
3725 if (display_envar == NULL || display_envar[0] == '\0') {
3726 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3727 fflush(stdout);
3728 exit(1);
3729 }
3730
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003731 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003732 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003733 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003734 fflush(stdout);
3735 exit(1);
3736 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003737
3738 setup = xcb_get_setup(demo->connection);
3739 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003740 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003741
3742 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003743#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3744 demo->display = wl_display_connect(NULL);
3745
3746 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003747 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003748 fflush(stdout);
3749 exit(1);
3750 }
3751
3752 demo->registry = wl_display_get_registry(demo->display);
3753 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3754 wl_display_dispatch(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003755#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003756}
3757
Karl Schultze4dc75c2016-02-02 15:37:51 -07003758static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003759 vec3 eye = {0.0f, 3.0f, 5.0f};
3760 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003761 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003762
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003763 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003764 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003765 demo->frameCount = INT32_MAX;
Mike Schuchardtaed62462019-07-26 07:56:30 -07003766
Piers Daniell735ee532015-02-23 16:23:13 -07003767 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003768 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003769 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003770 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003771 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003772 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3773 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003774 i++;
3775 continue;
3776 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003777 if (strcmp(argv[i], "--break") == 0) {
3778 demo->use_break = true;
3779 continue;
3780 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003781 if (strcmp(argv[i], "--validate") == 0) {
3782 demo->validate = true;
3783 continue;
3784 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003785 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3786 demo->validate = true;
3787 demo->validate_checks_disabled = true;
3788 continue;
3789 }
Tony Barbour2593b182016-04-19 10:57:58 -06003790 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003791 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003792 continue;
3793 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003794 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3795 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003796 i++;
3797 continue;
3798 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003799 if (strcmp(argv[i], "--suppress_popups") == 0) {
3800 demo->suppress_popups = true;
3801 continue;
3802 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003803 if (strcmp(argv[i], "--display_timing") == 0) {
3804 demo->VK_GOOGLE_display_timing_enabled = true;
3805 continue;
3806 }
Ian Elliott53622a72017-03-28 11:06:33 -06003807 if (strcmp(argv[i], "--incremental_present") == 0) {
3808 demo->VK_KHR_incremental_present_enabled = true;
3809 continue;
3810 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003811
Cody Northropaf28a532016-09-27 10:50:57 -06003812#if defined(ANDROID)
Lenny Komowffe446c2018-11-19 17:08:04 -07003813 ERR_EXIT("Usage: vkcube [--validate]\n", "Usage");
Cody Northropaf28a532016-09-27 10:50:57 -06003814#else
Tony-LunarGd74734f2019-06-04 14:11:43 -06003815 char *message =
3816 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled]\n"
3817 "\t[--break] [--c <framecount>] [--suppress_popups]\n"
3818 "\t[--incremental_present] [--display_timing]\n"
3819 "\t[--present_mode <present mode enum>]\n"
3820 "\t<present_mode_enum>\n"
3821 "\t\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3822 "\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3823 "\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
3824 "\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n";
3825 int length = snprintf(NULL, 0, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3826 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3827 char *usage = (char *)malloc(length + 1);
3828 if (!usage) {
3829 exit(1);
3830 }
3831 snprintf(usage, length + 1, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3832 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
3833#if defined(_WIN32)
3834 if (!demo->suppress_popups) MessageBox(NULL, usage, "Usage Error", MB_OK);
3835#else
Tony-LunarG122ac062019-06-13 16:21:57 -06003836 fprintf(stderr, "%s", usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003837 fflush(stderr);
Tony-LunarGd74734f2019-06-04 14:11:43 -06003838#endif
3839 free(usage);
Ian Elliott639ca472015-04-16 15:23:05 -06003840 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003841#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003842 }
3843
Tony Barbour153cb062016-12-07 13:43:36 -07003844 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003845
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003846 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003847
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003848 demo->width = 500;
3849 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003850
Tony Barbour66a56c32016-09-21 13:10:56 -06003851 demo->spin_angle = 4.0f;
3852 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003853 demo->pause = false;
3854
Dave Houlton5fa47912018-02-16 11:02:26 -07003855 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003856 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3857 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003858
Dave Houlton5fa47912018-02-16 11:02:26 -07003859 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003860}
3861
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003862#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003863// Include header required for parsing the command line options.
3864#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003865
Dave Houlton5fa47912018-02-16 11:02:26 -07003866int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3867 MSG msg; // message
3868 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003869 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003870 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003871
Jamie Madillb2ff6502017-03-15 16:17:46 -04003872 // Ensure wParam is initialized.
3873 msg.wParam = 0;
3874
Karl Schultze4dc75c2016-02-02 15:37:51 -07003875 // Use the CommandLine functions to get the command line arguments.
3876 // Unfortunately, Microsoft outputs
3877 // this information as wide characters for Unicode, and we simply want the
3878 // Ascii version to be compatible
3879 // with the non-Windows side. So, we have to convert the information to
3880 // Ascii character strings.
3881 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3882 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003883 argc = 0;
3884 }
3885
Karl Schultze4dc75c2016-02-02 15:37:51 -07003886 if (argc > 0) {
3887 argv = (char **)malloc(sizeof(char *) * argc);
3888 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003889 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003890 } else {
3891 for (int iii = 0; iii < argc; iii++) {
3892 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003893 size_t numConverted = 0;
3894
Karl Schultze4dc75c2016-02-02 15:37:51 -07003895 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3896 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003897 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003898 }
3899 }
3900 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003901 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003902 argv = NULL;
3903 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003904
3905 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003906
3907 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003908 if (argc > 0 && argv != NULL) {
3909 for (int iii = 0; iii < argc; iii++) {
3910 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003911 free(argv[iii]);
3912 }
3913 }
3914 free(argv);
3915 }
3916
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003917 demo.connection = hInstance;
Lenny Komowffe446c2018-11-19 17:08:04 -07003918 strncpy(demo.name, "Vulkan Cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003919 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003920 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003921
3922 demo_prepare(&demo);
3923
Dave Houlton5fa47912018-02-16 11:02:26 -07003924 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003925
3926 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003927 while (!done) {
Petr Krausd88e4e52019-01-23 18:45:04 +01003928 if (demo.pause) {
3929 const BOOL succ = WaitMessage();
3930
3931 if (!succ) {
3932 struct demo *tmp = &demo;
3933 struct demo *demo = tmp;
3934 ERR_EXIT("WaitMessage() failed on paused demo", "event loop error");
3935 }
3936 }
Ian Elliotta748eaf2015-04-28 15:50:36 -06003937 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003938 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003939 {
Dave Houlton5fa47912018-02-16 11:02:26 -07003940 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07003941 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003942 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003943 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003944 DispatchMessage(&msg);
3945 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003946 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003947 }
3948
3949 demo_cleanup(&demo);
3950
Karl Schultze4dc75c2016-02-02 15:37:51 -07003951 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003952}
Bill Hollingsf4352142017-02-14 22:58:56 -05003953
3954#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
Karl Schultz206b1c52018-04-13 18:02:07 -06003955static void demo_main(struct demo *demo, void *view, int argc, const char *argv[]) {
Bill Hollingsf4352142017-02-14 22:58:56 -05003956
Dave Houlton5fa47912018-02-16 11:02:26 -07003957 demo_init(demo, argc, (char **)argv);
Tony Barbourc65cd752017-03-13 15:29:35 -06003958 demo->window = view;
3959 demo_init_vk_swapchain(demo);
3960 demo_prepare(demo);
3961 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003962}
3963
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003964#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3965#include <android/log.h>
3966#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003967#include "android_util.h"
3968
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003969static bool initialized = false;
3970static bool active = false;
3971struct demo demo;
3972
Dave Houlton5fa47912018-02-16 11:02:26 -07003973static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003974
Dave Houlton5fa47912018-02-16 11:02:26 -07003975static void processCommand(struct android_app *app, int32_t cmd) {
3976 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003977 case APP_CMD_INIT_WINDOW: {
3978 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003979 // We're getting a new window. If the app is starting up, we
3980 // need to initialize. If the app has already been
3981 // initialized, that means that we lost our previous window,
3982 // which means that we have a lot of work to do. At a minimum,
3983 // we need to destroy the swapchain and surface associated with
3984 // the old window, and create a new surface and swapchain.
3985 // However, since there are a lot of other objects/state that
3986 // is tied to the swapchain, it's easiest to simply cleanup and
3987 // start over (i.e. use a brute-force approach of re-starting
3988 // the app)
3989 if (demo.prepared) {
3990 demo_cleanup(&demo);
3991 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003992
3993 // Parse Intents into argc, argv
3994 // Use the following key to send arguments, i.e.
3995 // --es args "--validate"
3996 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07003997 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003998 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003999 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004000
4001 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07004002 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 -06004003
4004 demo_init(&demo, argc, argv);
4005
4006 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07004007 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004008
Dave Houlton5fa47912018-02-16 11:02:26 -07004009 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004010 demo_init_vk_swapchain(&demo);
4011 demo_prepare(&demo);
4012 initialized = true;
4013 }
4014 break;
4015 }
4016 case APP_CMD_GAINED_FOCUS: {
4017 active = true;
4018 break;
4019 }
4020 case APP_CMD_LOST_FOCUS: {
4021 active = false;
4022 break;
4023 }
4024 }
4025}
4026
Dave Houlton5fa47912018-02-16 11:02:26 -07004027void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06004028#ifdef ANDROID
4029 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07004030 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06004031#endif
4032
Ian Elliottf05d5d12016-05-17 12:03:35 -06004033 demo.prepared = false;
4034
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004035 app->onAppCmd = processCommand;
4036 app->onInputEvent = processInput;
4037
Dave Houlton5fa47912018-02-16 11:02:26 -07004038 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004039 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07004040 struct android_poll_source *source;
4041 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004042 if (source) {
4043 source->process(app, source);
4044 }
4045
4046 if (app->destroyRequested != 0) {
4047 demo_cleanup(&demo);
4048 return;
4049 }
4050 }
4051 if (initialized && active) {
4052 demo_run(&demo);
4053 }
4054 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004055}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004056#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004057int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004058 struct demo demo;
4059
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004060 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004061#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004062 demo_create_xcb_window(&demo);
4063#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4064 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004065#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4066 demo_create_window(&demo);
4067#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004068
Tony Barbourcc69f452015-10-08 13:59:42 -06004069 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004070
4071 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004072
Tony Barbour153cb062016-12-07 13:43:36 -07004073#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004074 demo_run_xcb(&demo);
4075#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4076 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004077#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4078 demo_run(&demo);
Damien Leone600c3052017-01-31 10:26:07 -07004079#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4080 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004081#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004082
4083 demo_cleanup(&demo);
4084
Karl Schultz0218c002016-03-22 17:06:13 -06004085 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004086}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004087#endif