blob: a03234eabc32db15ed7e63924683ad4326c0fdf0 [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
Ian Elliottd940ca22017-03-22 08:31:27 -06002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Chia-I Wu <olv@lunarg.com>
19 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20 * Author: Ian Elliott <ian@LunarG.com>
21 * Author: Ian Elliott <ianelliott@google.com>
22 * Author: Jon Ashburn <jon@lunarg.com>
23 * Author: Gwan-gyeong Mun <elongbug@gmail.com>
24 * Author: Tony Barbour <tony@LunarG.com>
25 * Author: Bill Hollings <bill.hollings@brenwill.com>
26 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070027
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060029#include <stdio.h>
Ian Elliottd940ca22017-03-22 08:31:27 -060030#include <stdarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060031#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070035#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090036#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060037#include <X11/Xutil.h>
Joey Bzdek15eb0702017-06-07 09:40:36 -060038#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
39#include <linux/input.h>
Tony Barbour2593b182016-04-19 10:57:58 -060040#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060041
Ian Elliott639ca472015-04-16 15:23:05 -060042#ifdef _WIN32
43#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060044#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070045#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060046
Tony Barbourefd0c5a2016-12-07 14:45:12 -070047#if defined(VK_USE_PLATFORM_MIR_KHR)
48#warning "Cube does not have code for Mir at this time"
49#endif
50
Cody Northrop8707a6e2016-04-26 19:59:19 -060051#ifdef ANDROID
52#include "vulkan_wrapper.h"
53#else
David Pinedoc5193c12015-11-06 12:54:48 -070054#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060055#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070056
David Pinedo541526f2015-11-24 09:00:24 -070057#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060058#include "linmath.h"
Mark Young44212682018-02-21 15:29:41 -070059#include "vk_enum_string_helper.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060060
Ian Elliottd940ca22017-03-22 08:31:27 -060061#include "gettime.h"
62#include "inttypes.h"
63#define MILLION 1000000L
64#define BILLION 1000000000L
65
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060066#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060067#define APP_SHORT_NAME "cube"
68#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060069
Tony Barbour66a56c32016-09-21 13:10:56 -060070// Allow a maximum of two outstanding presentation operations.
71#define FRAME_LAG 2
72
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060073#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
74
Tony Barbourfdc2d352015-04-22 09:02:32 -060075#if defined(NDEBUG) && defined(__GNUC__)
76#define U_ASSERT_ONLY __attribute__((unused))
77#else
78#define U_ASSERT_ONLY
79#endif
80
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090081#if defined(__GNUC__)
82#define UNUSED __attribute__((unused))
83#else
84#define UNUSED
85#endif
86
Ian Elliott07264132015-04-28 11:35:02 -060087#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060088bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070089#define ERR_EXIT(err_msg, err_class) \
90 do { \
91 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
92 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070093 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -060094void DbgMsg(char *fmt, ...) {
95 va_list va;
96 va_start(va, fmt);
97 printf(fmt, va);
98 fflush(stdout);
99 va_end(va);
100}
Ian Elliott07264132015-04-28 11:35:02 -0600101
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500102#elif defined __ANDROID__
103#include <android/log.h>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700104#define ERR_EXIT(err_msg, err_class) \
105 do { \
106 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
107 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500108 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600109#ifdef VARARGS_WORKS_ON_ANDROID
110void DbgMsg(const char *fmt, ...) {
111 va_list va;
112 va_start(va, fmt);
113 __android_log_print(ANDROID_LOG_INFO, "Cube", fmt, va);
114 va_end(va);
115}
116#else // VARARGS_WORKS_ON_ANDROID
117#define DbgMsg(fmt, ...) \
118 do { \
119 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", fmt, ##__VA_ARGS__)); \
120 } while (0)
121#endif // VARARGS_WORKS_ON_ANDROID
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500122#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700123#define ERR_EXIT(err_msg, err_class) \
124 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -0800125 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700126 fflush(stdout); \
127 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700128 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600129void DbgMsg(char *fmt, ...) {
130 va_list va;
131 va_start(va, fmt);
132 printf(fmt, va);
133 fflush(stdout);
134 va_end(va);
135}
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500136#endif
Ian Elliott07264132015-04-28 11:35:02 -0600137
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700138#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
139 { \
140 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
141 if (demo->fp##entrypoint == NULL) { \
142 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
143 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700144 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600145
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600146static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
147
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700148#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
149 { \
150 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
151 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
152 if (demo->fp##entrypoint == NULL) { \
153 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
154 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700155 }
Ian Elliott673898b2015-06-22 15:07:49 -0600156
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600157/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700158 * structure to track all objects related to a texture.
159 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600160struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600161 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700162
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600163 VkImage image;
164 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600165
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800166 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500167 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600168 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700169 int32_t tex_width, tex_height;
170};
171
Karl Schultze4dc75c2016-02-02 15:37:51 -0700172static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173
Karl Schultz0218c002016-03-22 17:06:13 -0600174static int validation_error = 0;
175
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600176struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600177 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700178 float mvp[4][4];
179 float position[12 * 3][4];
180 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600181};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600182
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600183//--------------------------------------------------------------------------------------
184// Mesh and VertexFormat Data
185//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700186// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600187static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 -1.0f,-1.0f, 1.0f,
190 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 -1.0f, 1.0f,-1.0f,
193 -1.0f,-1.0f,-1.0f,
194
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 1.0f, 1.0f,-1.0f,
197 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600200 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600201
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800202 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600203 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600204 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800205 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600206 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600207 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800209 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 -1.0f, 1.0f, 1.0f,
211 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800212 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600213 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600214 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600215
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 1.0f, 1.0f, 1.0f,
218 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800219 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220 1.0f,-1.0f,-1.0f,
221 1.0f, 1.0f,-1.0f,
222
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800226 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227 1.0f,-1.0f, 1.0f,
228 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600229};
230
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600231static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700232 0.0f, 1.0f, // -X side
233 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 0.0f, 0.0f,
237 0.0f, 1.0f,
238
Rene Lindsay76867e82016-07-29 10:49:11 -0700239 1.0f, 1.0f, // -Z side
240 0.0f, 0.0f,
241 0.0f, 1.0f,
242 1.0f, 1.0f,
243 1.0f, 0.0f,
244 0.0f, 0.0f,
245
246 1.0f, 0.0f, // -Y side
247 1.0f, 1.0f,
248 0.0f, 1.0f,
249 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600250 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800251 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600252
Rene Lindsay76867e82016-07-29 10:49:11 -0700253 1.0f, 0.0f, // +Y side
254 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800255 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600256 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700257 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600258 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600259
Rene Lindsay76867e82016-07-29 10:49:11 -0700260 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800261 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700262 0.0f, 1.0f,
263 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600264 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800265 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700266
267 0.0f, 0.0f, // +Z side
268 0.0f, 1.0f,
269 1.0f, 0.0f,
270 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600271 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700272 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600273};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700274// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600275
Karl Schultze4dc75c2016-02-02 15:37:51 -0700276void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600277 int i;
278
279 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700280 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600281 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
282 }
283 printf("\n");
284 fflush(stdout);
285}
286
Karl Schultze4dc75c2016-02-02 15:37:51 -0700287void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600288 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700289 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600290 printf("\n");
291 fflush(stdout);
292}
293
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600294typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600295 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800296 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600297 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600298 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700299 VkBuffer uniform_buffer;
300 VkDeviceMemory uniform_memory;
301 VkFramebuffer framebuffer;
302 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700303} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600304
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600305struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500306#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600307#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700308 HINSTANCE connection; // hInstance - Windows Instance
309 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
310 HWND window; // hWnd - window handle
311 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700312#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700313 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600314 Window xlib_window;
315 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700316#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700317 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318 xcb_connection_t *connection;
319 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600320 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800321 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900322#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
323 struct wl_display *display;
324 struct wl_registry *registry;
325 struct wl_compositor *compositor;
326 struct wl_surface *window;
327 struct wl_shell *shell;
328 struct wl_shell_surface *shell_surface;
Joey Bzdek15eb0702017-06-07 09:40:36 -0600329 struct wl_seat *seat;
330 struct wl_pointer *pointer;
331 struct wl_keyboard *keyboard;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700332#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500333#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700334 ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500335#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
336 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500337#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700338 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600339 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700340 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600341 bool separate_present_queue;
Jeremy Kniager602e4182018-01-19 10:52:34 -0700342 bool is_minimized;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600343
Ian Elliott53622a72017-03-28 11:06:33 -0600344 bool VK_KHR_incremental_present_enabled;
345
Ian Elliottd940ca22017-03-22 08:31:27 -0600346 bool VK_GOOGLE_display_timing_enabled;
347 bool syncd_with_actual_presents;
348 uint64_t refresh_duration;
349 uint64_t refresh_duration_multiplier;
350 uint64_t target_IPD; // image present duration (inverse of frame rate)
351 uint64_t prev_desired_present_time;
352 uint32_t next_present_id;
353 uint32_t last_early_id; // 0 if no early images
354 uint32_t last_late_id; // 0 if no late images
355
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600357 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600359 VkQueue graphics_queue;
360 VkQueue present_queue;
361 uint32_t graphics_queue_family_index;
362 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600363 VkSemaphore image_acquired_semaphores[FRAME_LAG];
364 VkSemaphore draw_complete_semaphores[FRAME_LAG];
365 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600366 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600367 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600368 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369
Tony Barbourda2bad52016-01-22 14:36:40 -0700370 uint32_t enabled_extension_count;
371 uint32_t enabled_layer_count;
372 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600373 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700374
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600377 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600378
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700379 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
380 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
381 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
382 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600383 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
384 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
385 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
386 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
387 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600388 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
389 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600390 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600391 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700392 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600393 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600394 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600395 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600396
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800397 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600398 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600399
400 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600401 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600402
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600403 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800404 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500405 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600406 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600407 } depth;
408
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600409 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600410 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600411
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700412 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500413 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600414 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600415 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800416 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600417 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600418
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600419 mat4x4 projection_matrix;
420 mat4x4 view_matrix;
421 mat4x4 model_matrix;
422
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600423 float spin_angle;
424 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600425 bool pause;
426
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600427 VkShaderModule vert_shader_module;
428 VkShaderModule frag_shader_module;
429
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600430 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800431
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600432 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600433 int32_t curFrame;
434 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600435 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600436 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600437 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600438 bool suppress_popups;
Mark Young66510e52017-11-10 10:05:14 -0700439
440 PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
441 PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
442 PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
443 PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
444 PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
445 PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
446 PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
447 VkDebugUtilsMessengerEXT dbg_messenger;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600448
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600449 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600450 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600451};
452
Mark Young66510e52017-11-10 10:05:14 -0700453VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
454 VkDebugUtilsMessageTypeFlagsEXT messageType,
455 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
456 void *pUserData) {
457 char prefix[64] = "";
458 char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000);
lenny-lunargfbe22392016-06-06 11:07:53 -0600459 assert(message);
Mark Young66510e52017-11-10 10:05:14 -0700460 struct demo *demo = (struct demo *)pUserData;
lenny-lunargfbe22392016-06-06 11:07:53 -0600461
Mark Young66510e52017-11-10 10:05:14 -0700462 if (demo->use_break) {
463#ifndef WIN32
464 raise(SIGTRAP);
465#else
466 DebugBreak();
467#endif
468 }
469
470 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
471 strcat(prefix, "VERBOSE : ");
472 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
473 strcat(prefix, "INFO : ");
474 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
475 strcat(prefix, "WARNING : ");
476 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
477 strcat(prefix, "ERROR : ");
478 }
479
480 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
481 strcat(prefix, "GENERAL");
lenny-lunargfbe22392016-06-06 11:07:53 -0600482 } else {
Mark Young66510e52017-11-10 10:05:14 -0700483 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
484 strcat(prefix, "VALIDATION");
485 validation_error = 1;
486 }
487 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
488 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
489 strcat(prefix, "|");
490 }
491 strcat(prefix, "PERFORMANCE");
492 }
493 }
494
495 sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber,
496 pCallbackData->pMessageIdName, pCallbackData->pMessage);
497 if (pCallbackData->objectCount > 0) {
498 char tmp_message[500];
499 sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount);
500 strcat(message, tmp_message);
501 for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
502 if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
503 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object,
Mark Young44212682018-02-21 15:29:41 -0700504 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700505 (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName);
506 } else {
507 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object,
Mark Young44212682018-02-21 15:29:41 -0700508 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700509 (void *)(pCallbackData->pObjects[object].objectHandle));
510 }
511 strcat(message, tmp_message);
512 }
513 }
514 if (pCallbackData->cmdBufLabelCount > 0) {
515 char tmp_message[500];
516 sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount);
517 strcat(message, tmp_message);
518 for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
519 sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label,
520 pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0],
521 pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2],
522 pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]);
523 strcat(message, tmp_message);
524 }
lenny-lunargfbe22392016-06-06 11:07:53 -0600525 }
526
527#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600528
Tony Barbour602ca4f2016-09-12 14:02:43 -0600529 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600530 struct demo *demo = (struct demo*) pUserData;
531 if (!demo->suppress_popups)
532 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600533 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600534
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600535#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600536
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600537 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600538 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600539 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600540 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600541 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600542 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600543 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600544 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600545 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600546 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600547 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600548 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600549 }
Cody Northropaf28a532016-09-27 10:50:57 -0600550
lenny-lunargfbe22392016-06-06 11:07:53 -0600551#else
Cody Northropaf28a532016-09-27 10:50:57 -0600552
lenny-lunargfbe22392016-06-06 11:07:53 -0600553 printf("%s\n", message);
554 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600555
lenny-lunargfbe22392016-06-06 11:07:53 -0600556#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600557
lenny-lunargfbe22392016-06-06 11:07:53 -0600558 free(message);
559
Mark Young66510e52017-11-10 10:05:14 -0700560 // Don't bail out, but keep going.
lenny-lunargfbe22392016-06-06 11:07:53 -0600561 return false;
562}
563
Ian Elliottd940ca22017-03-22 08:31:27 -0600564bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
565 // The desired time was the earliest time that the present should have
566 // occured. In almost every case, the actual time should be later than the
567 // desired time. We should only consider the actual time "late" if it is
568 // after "desired + rdur".
569 if (actual <= desired) {
570 // The actual time was before or equal to the desired time. This will
571 // probably never happen, but in case it does, return false since the
572 // present was obviously NOT late.
573 return false;
574 }
575 uint64_t deadline = actual + rdur;
576 if (actual > deadline) {
577 return true;
578 } else {
579 return false;
580 }
581}
Dave Houlton5fa47912018-02-16 11:02:26 -0700582bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600583 if (earliest < actual) {
584 // Consider whether this present could have occured earlier. Make sure
585 // that earliest time was at least 2msec earlier than actual time, and
586 // that the margin was at least 2msec:
587 uint64_t diff = actual - earliest;
588 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
589 // This present could have occured earlier because both: 1) the
590 // earliest time was at least 2 msec before actual time, and 2) the
591 // margin was at least 2msec.
592 return true;
593 }
594 }
595 return false;
596}
597
Ian Elliottcf074d32015-10-16 18:02:43 -0600598// Forward declaration:
599static void demo_resize(struct demo *demo);
600
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,
663 .oldLayout = old_image_layout,
664 .newLayout = new_image_layout,
665 .image = image,
666 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600667
Tony Barboureb5077b2016-10-19 15:23:36 -0600668 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700669 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
670 /* Make sure anything that was copying from this image has completed */
671 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
672 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600673
Dave Houlton5fa47912018-02-16 11:02:26 -0700674 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
675 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
676 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700677
Dave Houlton5fa47912018-02-16 11:02:26 -0700678 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
679 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
680 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700681
Dave Houlton5fa47912018-02-16 11:02:26 -0700682 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
683 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
684 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600685
Dave Houlton5fa47912018-02-16 11:02:26 -0700686 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
687 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
688 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600689
Dave Houlton5fa47912018-02-16 11:02:26 -0700690 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
691 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
692 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600693
Dave Houlton5fa47912018-02-16 11:02:26 -0700694 default:
695 image_memory_barrier.dstAccessMask = 0;
696 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600697 }
698
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600699 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600700
Dave Houlton5fa47912018-02-16 11:02:26 -0700701 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600702}
703
Karl Schultze4dc75c2016-02-02 15:37:51 -0700704static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Mark Young66510e52017-11-10 10:05:14 -0700705 VkDebugUtilsLabelEXT label = {};
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700706 const VkCommandBufferBeginInfo cmd_buf_info = {
707 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
708 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600709 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700710 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700711 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800712 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700713 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
714 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800715 };
716 const VkRenderPassBeginInfo rp_begin = {
717 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
718 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800719 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700720 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800721 .renderArea.offset.x = 0,
722 .renderArea.offset.y = 0,
723 .renderArea.extent.width = demo->width,
724 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600725 .clearValueCount = 2,
726 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700727 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800728 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600729
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600730 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Mark Young66510e52017-11-10 10:05:14 -0700731
732 if (demo->validate) {
733 // Set a name for the command buffer
734 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
735 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
736 .pNext = NULL,
737 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
738 .objectHandle = (uint64_t)cmd_buf,
739 .pObjectName = "CubeDrawCommandBuf",
740 };
741 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
742
743 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
744 label.pNext = NULL;
745 label.pLabelName = "DrawBegin";
746 label.color[0] = 0.4f;
747 label.color[1] = 0.3f;
748 label.color[2] = 0.2f;
749 label.color[3] = 0.1f;
750 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
751 }
752
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600753 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800754 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Mark Young66510e52017-11-10 10:05:14 -0700755
756 if (demo->validate) {
757 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
758 label.pNext = NULL;
759 label.pLabelName = "InsideRenderPass";
760 label.color[0] = 8.4f;
761 label.color[1] = 7.3f;
762 label.color[2] = 6.2f;
763 label.color[3] = 7.1f;
764 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
765 }
766
Karl Schultze4dc75c2016-02-02 15:37:51 -0700767 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700768 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
769 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600770 VkViewport viewport;
771 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700772 viewport.height = (float)demo->height;
773 viewport.width = (float)demo->width;
774 viewport.minDepth = (float)0.0f;
775 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700776 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600777
778 VkRect2D scissor;
779 memset(&scissor, 0, sizeof(scissor));
780 scissor.extent.width = demo->width;
781 scissor.extent.height = demo->height;
782 scissor.offset.x = 0;
783 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700784 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Mark Young66510e52017-11-10 10:05:14 -0700785
786 if (demo->validate) {
787 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
788 label.pNext = NULL;
789 label.pLabelName = "ActualDraw";
Mark Young44212682018-02-21 15:29:41 -0700790 label.color = {-0.4f, -0.3f, -0.2f, -0.1f};
Mark Young66510e52017-11-10 10:05:14 -0700791 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
792 }
793
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600794 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Mark Young66510e52017-11-10 10:05:14 -0700795 if (demo->validate) {
796 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
797 }
798
Tony Barbour98390392016-07-28 10:50:13 -0600799 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600800 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800801 vkCmdEndRenderPass(cmd_buf);
Mark Young66510e52017-11-10 10:05:14 -0700802 if (demo->validate) {
803 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
804 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600805
Tony Barbour22e06272016-09-07 16:07:56 -0600806 if (demo->separate_present_queue) {
807 // We have to transfer ownership from the graphics queue family to the
808 // present queue family to be able to present. Note that we don't have
809 // to transfer from present queue family back to graphics queue family at
810 // the start of the next frame because we don't care about the image's
811 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700812 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
813 .pNext = NULL,
814 .srcAccessMask = 0,
815 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
816 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
817 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
818 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
819 .dstQueueFamilyIndex = demo->present_queue_family_index,
820 .image = demo->swapchain_image_resources[demo->current_buffer].image,
821 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600822
Dave Houlton5fa47912018-02-16 11:02:26 -0700823 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0,
824 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600825 }
Mark Young66510e52017-11-10 10:05:14 -0700826 if (demo->validate) {
827 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
828 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600829 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600830 assert(!err);
831}
832
Tony Barbour22e06272016-09-07 16:07:56 -0600833void demo_build_image_ownership_cmd(struct demo *demo, int i) {
834 VkResult U_ASSERT_ONLY err;
835
836 const VkCommandBufferBeginInfo cmd_buf_info = {
837 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
838 .pNext = NULL,
839 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
840 .pInheritanceInfo = NULL,
841 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700842 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600843 assert(!err);
844
Dave Houlton5fa47912018-02-16 11:02:26 -0700845 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
846 .pNext = NULL,
847 .srcAccessMask = 0,
848 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
849 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
850 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
851 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
852 .dstQueueFamilyIndex = demo->present_queue_family_index,
853 .image = demo->swapchain_image_resources[i].image,
854 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600855
Dave Houlton5fa47912018-02-16 11:02:26 -0700856 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
857 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700858 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600859 assert(!err);
860}
861
Karl Schultze4dc75c2016-02-02 15:37:51 -0700862void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600863 mat4x4 MVP, Model, VP;
864 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600865 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600866 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600867
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600868 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600869
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700870 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600871 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700872 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600873 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600874
Dave Houlton5fa47912018-02-16 11:02:26 -0700875 err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0,
876 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600877 assert(!err);
878
Karl Schultze4dc75c2016-02-02 15:37:51 -0700879 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600880
Tony Barboura72d9492017-01-11 13:35:09 -0700881 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600882}
883
Ian Elliottd940ca22017-03-22 08:31:27 -0600884void DemoUpdateTargetIPD(struct demo *demo) {
885 // Look at what happened to previous presents, and make appropriate
886 // adjustments in timing:
887 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700888 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600889 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600890
Dave Houlton5fa47912018-02-16 11:02:26 -0700891 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600892 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600893 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700894 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600895 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700896 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600897 assert(!err);
898
899 bool early = false;
900 bool late = false;
901 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700902 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600903 if (!demo->syncd_with_actual_presents) {
904 // This is the first time that we've received an
905 // actualPresentTime for this swapchain. In order to not
906 // perceive these early frames as "late", we need to sync-up
907 // our future desiredPresentTime's with the
908 // actualPresentTime(s) that we're receiving now.
909 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600910
Ian Elliottd940ca22017-03-22 08:31:27 -0600911 // So that we don't suspect any pending presents as late,
912 // record them all as suspected-late presents:
913 demo->last_late_id = demo->next_present_id - 1;
914 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600915 demo->syncd_with_actual_presents = true;
916 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700917 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600918 demo->refresh_duration)) {
919 // This image could have been presented earlier. We don't want
920 // to decrease the target_IPD until we've seen early presents
921 // for at least two seconds.
922 if (demo->last_early_id == past[i].presentID) {
923 // We've now seen two seconds worth of early presents.
924 // Flag it as such, and reset the counter:
925 early = true;
926 demo->last_early_id = 0;
927 } else if (demo->last_early_id == 0) {
928 // This is the first early present we've seen.
929 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700930 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
931 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600932 demo->last_early_id = past[i].presentID + howManyPresents;
933 } else {
934 // We are in the midst of a set of early images,
935 // and so we won't do anything.
936 }
937 late = false;
938 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700939 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600940 // This image was presented after its desired time. Since
941 // there's a delay between calling vkQueuePresentKHR and when
942 // we get the timing data, several presents may have been late.
943 // Thus, we need to threat all of the outstanding presents as
944 // being likely late, so that we only increase the target_IPD
945 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700946 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600947 late = true;
948 // Record the last suspected-late present:
949 demo->last_late_id = demo->next_present_id - 1;
950 } else {
951 // We are in the midst of a set of likely-late images,
952 // and so we won't do anything.
953 }
954 early = false;
955 demo->last_early_id = 0;
956 } else {
957 // Since this image was not presented early or late, reset
958 // any sets of early or late presentIDs:
959 early = false;
960 late = false;
961 calibrate_next = true;
962 demo->last_early_id = 0;
963 demo->last_late_id = 0;
964 }
965 }
966
967 if (early) {
968 // Since we've seen at least two-seconds worth of presnts that
969 // could have occured earlier than desired, let's decrease the
970 // target_IPD (i.e. increase the frame rate):
971 //
972 // TODO(ianelliott): Try to calculate a better target_IPD based
973 // on the most recently-seen present (this is overly-simplistic).
974 demo->refresh_duration_multiplier--;
975 if (demo->refresh_duration_multiplier == 0) {
976 // This should never happen, but in case it does, don't
977 // try to go faster.
978 demo->refresh_duration_multiplier = 1;
979 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700980 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600981 }
982 if (late) {
983 // Since we found a new instance of a late present, we want to
984 // increase the target_IPD (i.e. decrease the frame rate):
985 //
986 // TODO(ianelliott): Try to calculate a better target_IPD based
987 // on the most recently-seen present (this is overly-simplistic).
988 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -0700989 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600990 }
991
992 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700993 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
994 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600995 }
996 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600997}
998
Karl Schultze4dc75c2016-02-02 15:37:51 -0700999static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001000 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001001
Damien Leonec336d822017-04-14 16:10:14 -07001002 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +03001003 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1004 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001005
Tony Barbour8b7c9112017-06-29 13:34:41 -06001006 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -06001007 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -07001008 err =
1009 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1010 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001011
Tony Barbour6914e6d2017-06-02 12:11:29 -06001012 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1013 // demo->swapchain is out of date (e.g. the window was resized) and
1014 // must be recreated:
1015 demo_resize(demo);
1016 } else if (err == VK_SUBOPTIMAL_KHR) {
1017 // demo->swapchain is not as optimal as it could be, but the platform's
1018 // presentation engine will still present the image correctly.
1019 break;
1020 } else {
1021 assert(!err);
1022 }
Tony Barbour8b7c9112017-06-29 13:34:41 -06001023 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001024
Tony Barbour3eafe1f2017-06-14 11:59:55 -06001025 demo_update_data_buffer(demo);
1026
Ian Elliottd940ca22017-03-22 08:31:27 -06001027 if (demo->VK_GOOGLE_display_timing_enabled) {
1028 // Look at what happened to previous presents, and make appropriate
1029 // adjustments in timing:
1030 DemoUpdateTargetIPD(demo);
1031
1032 // Note: a real application would position its geometry to that it's in
1033 // the correct locatoin for when the next image is presented. It might
1034 // also wait, so that there's less latency between any input and when
1035 // the next image is rendered/presented. This demo program is so
1036 // simple that it doesn't do either of those.
1037 }
1038
Tony Barbource7524e2016-07-28 12:01:45 -06001039 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001040 // that the image won't be rendered to until the presentation
1041 // engine has fully released ownership to the application, and it is
1042 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001043 VkPipelineStageFlags pipe_stage_flags;
1044 VkSubmitInfo submit_info;
1045 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1046 submit_info.pNext = NULL;
1047 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1048 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1049 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001050 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001051 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001052 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001053 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001054 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -07001055 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001056 assert(!err);
1057
Tony Barbour22e06272016-09-07 16:07:56 -06001058 if (demo->separate_present_queue) {
1059 // If we are using separate queues, change image ownership to the
1060 // present queue before presenting, waiting for the draw complete
1061 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001062 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001063 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1064 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001065 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001066 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001067 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001068 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001069 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001070 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1071 assert(!err);
1072 }
1073
1074 // If we are using separate queues we have to wait for image ownership,
1075 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001076 VkPresentInfoKHR present = {
1077 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001078 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001079 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -07001080 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1081 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001082 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001083 .pSwapchains = &demo->swapchain,
1084 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001085 };
1086
Ian Elliott53622a72017-03-28 11:06:33 -06001087 if (demo->VK_KHR_incremental_present_enabled) {
1088 // If using VK_KHR_incremental_present, we provide a hint of the region
1089 // that contains changed content relative to the previously-presented
1090 // image. The implementation can use this hint in order to save
1091 // work/power (by only copying the region in the hint). The
1092 // implementation is free to ignore the hint though, and so we must
1093 // ensure that the entire image has the correctly-drawn content.
1094 uint32_t eighthOfWidth = demo->width / 8;
1095 uint32_t eighthOfHeight = demo->height / 8;
1096 VkRectLayerKHR rect = {
1097 .offset.x = eighthOfWidth,
1098 .offset.y = eighthOfHeight,
1099 .extent.width = eighthOfWidth * 6,
1100 .extent.height = eighthOfHeight * 6,
1101 .layer = 0,
1102 };
1103 VkPresentRegionKHR region = {
1104 .rectangleCount = 1,
1105 .pRectangles = &rect,
1106 };
1107 VkPresentRegionsKHR regions = {
1108 .sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1109 .pNext = present.pNext,
1110 .swapchainCount = present.swapchainCount,
1111 .pRegions = &region,
1112 };
1113 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001114 }
1115
Ian Elliottd940ca22017-03-22 08:31:27 -06001116 if (demo->VK_GOOGLE_display_timing_enabled) {
1117 VkPresentTimeGOOGLE ptime;
1118 if (demo->prev_desired_present_time == 0) {
1119 // This must be the first present for this swapchain.
1120 //
1121 // We don't know where we are relative to the presentation engine's
1122 // display's refresh cycle. We also don't know how long rendering
1123 // takes. Let's make a grossly-simplified assumption that the
1124 // desiredPresentTime should be half way between now and
1125 // now+target_IPD. We will adjust over time.
1126 uint64_t curtime = getTimeInNanoseconds();
1127 if (curtime == 0) {
1128 // Since we didn't find out the current time, don't give a
1129 // desiredPresentTime:
1130 ptime.desiredPresentTime = 0;
1131 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001132 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1133 }
1134 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001135 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001136 }
1137 ptime.presentID = demo->next_present_id++;
1138 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001139
1140 VkPresentTimesInfoGOOGLE present_time = {
1141 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1142 .pNext = present.pNext,
1143 .swapchainCount = present.swapchainCount,
1144 .pTimes = &ptime,
1145 };
1146 if (demo->VK_GOOGLE_display_timing_enabled) {
1147 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001148 }
1149 }
1150
Tony Barboura2a67812016-07-18 13:21:06 -06001151 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001152 demo->frame_index += 1;
1153 demo->frame_index %= FRAME_LAG;
1154
Ian Elliottcf074d32015-10-16 18:02:43 -06001155 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1156 // demo->swapchain is out of date (e.g. the window was resized) and
1157 // must be recreated:
1158 demo_resize(demo);
1159 } else if (err == VK_SUBOPTIMAL_KHR) {
1160 // demo->swapchain is not as optimal as it could be, but the platform's
1161 // presentation engine will still present the image correctly.
1162 } else {
1163 assert(!err);
1164 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001165}
1166
Karl Schultze4dc75c2016-02-02 15:37:51 -07001167static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001168 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001169 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001170
Ian Elliottb08e0d92015-11-20 11:55:46 -07001171 // Check the surface capabilities and formats
1172 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001173 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001174 assert(!err);
1175
Ian Elliott8528eff2015-08-07 15:56:59 -06001176 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001177 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001178 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001179 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001180 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001181 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001182 assert(!err);
1183
Ian Elliotta0781972015-08-21 15:09:33 -06001184 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001185 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1186 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1187 // If the surface size is undefined, the size is set to the size
1188 // of the images requested, which must fit within the minimum and
1189 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001190 swapchainExtent.width = demo->width;
1191 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001192
1193 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1194 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1195 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1196 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1197 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001198
Ian Elliottcf7f7482016-08-19 03:46:58 -06001199 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1200 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1201 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1202 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1203 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001204 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001205 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001206 swapchainExtent = surfCapabilities.currentExtent;
1207 demo->width = surfCapabilities.currentExtent.width;
1208 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001209 }
1210
Jeremy Kniager602e4182018-01-19 10:52:34 -07001211 if (demo->width == 0 || demo->height == 0) {
1212 demo->is_minimized = true;
1213 return;
1214 } else {
1215 demo->is_minimized = false;
1216 }
1217
Tony Barbour66a56c32016-09-21 13:10:56 -06001218 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001219 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001220 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001221
Ian Elliottb18fdde2016-10-03 12:11:12 -06001222 // There are times when you may wish to use another present mode. The
1223 // following code shows how to select them, and the comments provide some
1224 // reasons you may wish to use them.
1225 //
1226 // It should be noted that Vulkan 1.0 doesn't provide a method for
1227 // synchronizing rendering with the presentation engine's display. There
1228 // is a method provided for throttling rendering with the display, but
1229 // there are some presentation engines for which this method will not work.
1230 // If an application doesn't throttle its rendering, and if it renders much
1231 // faster than the refresh rate of the display, this can waste power on
1232 // mobile devices. That is because power is being spent rendering images
1233 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001234
Ian Elliottb18fdde2016-10-03 12:11:12 -06001235 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1236 // tearing, or have some way of synchronizing their rendering with the
1237 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001238 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1239 // generally render a new presentable image every refresh cycle, but are
1240 // occasionally early. In this case, the application wants the new image
1241 // to be displayed instead of the previously-queued-for-presentation image
1242 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001243 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1244 // render a new presentable image every refresh cycle, but are occasionally
1245 // late. In this case (perhaps because of stuttering/latency concerns),
1246 // the application wants the late image to be immediately displayed, even
1247 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001248
Dave Houlton5fa47912018-02-16 11:02:26 -07001249 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001250 for (size_t i = 0; i < presentModeCount; ++i) {
1251 if (presentModes[i] == demo->presentMode) {
1252 swapchainPresentMode = demo->presentMode;
1253 break;
1254 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001255 }
1256 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001257 if (swapchainPresentMode != demo->presentMode) {
1258 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1259 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001260
Tony Barbour84376fe2017-01-06 15:54:22 -07001261 // Determine the number of VkImages to use in the swap chain.
1262 // Application desires to acquire 3 images at a time for triple
1263 // buffering
1264 uint32_t desiredNumOfSwapchainImages = 3;
1265 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1266 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1267 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001268 // If maxImageCount is 0, we can ask for as many images as we want;
1269 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001270 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001271 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001272 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001273 }
1274
Tony Barbour9fd6d352015-09-16 15:01:32 -06001275 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001276 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001277 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001278 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001279 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001280 }
1281
Tony Barbour820b55b2017-03-14 08:55:46 -06001282 // Find a supported composite alpha mode - one of these is guaranteed to be set
1283 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1284 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1285 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1286 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1287 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1288 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1289 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001290 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001291 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1292 compositeAlpha = compositeAlphaFlags[i];
1293 break;
1294 }
1295 }
1296
Tony Barboura2a67812016-07-18 13:21:06 -06001297 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001298 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001299 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001300 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001301 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001302 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001303 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001304 .imageExtent =
1305 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001306 .width = swapchainExtent.width,
1307 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001308 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001309 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001310 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001311 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001312 .imageArrayLayers = 1,
1313 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1314 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001315 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001316 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001317 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001318 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001319 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001320 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001321 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001322 assert(!err);
1323
Ian Elliottcf074d32015-10-16 18:02:43 -06001324 // If we just re-created an existing swapchain, we should destroy the old
1325 // swapchain at this point.
1326 // Note: destroying the swapchain also cleans up all its associated
1327 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001328 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001329 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001330 }
1331
Dave Houlton5fa47912018-02-16 11:02:26 -07001332 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001333 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001334
Dave Houlton5fa47912018-02-16 11:02:26 -07001335 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001336 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001337 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001338 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001339
Dave Houlton5fa47912018-02-16 11:02:26 -07001340 demo->swapchain_image_resources =
1341 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001342 assert(demo->swapchain_image_resources);
1343
Ian Elliotta0781972015-08-21 15:09:33 -06001344 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001345 VkImageViewCreateInfo color_image_view = {
1346 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001347 .pNext = NULL,
1348 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001349 .components =
1350 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001351 .r = VK_COMPONENT_SWIZZLE_R,
1352 .g = VK_COMPONENT_SWIZZLE_G,
1353 .b = VK_COMPONENT_SWIZZLE_B,
1354 .a = VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001355 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001356 .subresourceRange =
1357 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001358 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1359 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001360 };
1361
Tony Barboura72d9492017-01-11 13:35:09 -07001362 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001363
Tony Barboura72d9492017-01-11 13:35:09 -07001364 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365
Dave Houlton5fa47912018-02-16 11:02:26 -07001366 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001367 assert(!err);
1368 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001369
Ian Elliottd940ca22017-03-22 08:31:27 -06001370 if (demo->VK_GOOGLE_display_timing_enabled) {
1371 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001372 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001373 assert(!err);
1374 demo->refresh_duration = rc_dur.refreshDuration;
1375
1376 demo->syncd_with_actual_presents = false;
1377 // Initially target 1X the refresh duration:
1378 demo->target_IPD = demo->refresh_duration;
1379 demo->refresh_duration_multiplier = 1;
1380 demo->prev_desired_present_time = 0;
1381 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001382 }
1383
Mark Young04fd3d82016-01-25 14:43:50 -07001384 if (NULL != presentModes) {
1385 free(presentModes);
1386 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001387}
1388
Karl Schultze4dc75c2016-02-02 15:37:51 -07001389static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001390 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001391 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001392 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001393 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001394 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001395 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001396 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001398 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001399 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001400 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001401 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402 .flags = 0,
1403 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001404
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001405 VkImageViewCreateInfo view = {
1406 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001408 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001409 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001410 .subresourceRange =
1411 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001413 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001415
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001416 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001417 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001418 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419
1420 demo->depth.format = depth_format;
1421
1422 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001423 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424 assert(!err);
1425
Karl Schultze4dc75c2016-02-02 15:37:51 -07001426 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001427 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001428
Chia-I Wuf48700b2015-11-10 16:21:09 +08001429 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001430 demo->depth.mem_alloc.pNext = NULL;
1431 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1432 demo->depth.mem_alloc.memoryTypeIndex = 0;
1433
Dave Houlton5fa47912018-02-16 11:02:26 -07001434 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001435 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001436 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001437
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001438 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001439 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001440 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001441
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001442 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001443 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001444 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001445
1446 /* create image view */
1447 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001448 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001449 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001450}
1451
Tony Barbour1a86d032015-09-21 15:17:33 -06001452/* Load a ppm file into memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001453bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001454#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Dave Houlton5fa47912018-02-16 11:02:26 -07001455 filename = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001456#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001457
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001458#ifdef __ANDROID__
1459#include <lunarg.ppm.h>
1460 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001461 cPtr = (char *)lunarg_ppm;
1462 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001463 return false;
1464 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001465 while (strncmp(cPtr++, "\n", 1))
1466 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001467 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001468 if (rgba_data == NULL) {
1469 return true;
1470 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001471 while (strncmp(cPtr++, "\n", 1))
1472 ;
1473 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001474 return false;
1475 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001476 while (strncmp(cPtr++, "\n", 1))
1477 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001478
1479 for (int y = 0; y < *height; y++) {
1480 uint8_t *rowPtr = rgba_data;
1481 for (int x = 0; x < *width; x++) {
1482 memcpy(rowPtr, cPtr, 3);
1483 rowPtr[3] = 255; /* Alpha of 1 */
1484 rowPtr += 4;
1485 cPtr += 3;
1486 }
1487 rgba_data += layout->rowPitch;
1488 }
1489
1490 return true;
1491#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001492 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001493 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001494
Dave Houlton5fa47912018-02-16 11:02:26 -07001495 if (!fPtr) return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001496
Dave Houlton5fa47912018-02-16 11:02:26 -07001497 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001498 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1499 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001500 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001501 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001502
Tony Barbour1a86d032015-09-21 15:17:33 -06001503 do {
1504 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001505 if (cPtr == NULL) {
1506 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001507 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001508 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001509 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001510
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001511 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001512 if (rgba_data == NULL) {
1513 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001514 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001515 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001516 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001517 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001518 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1519 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001520 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001521 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001522
Karl Schultze4dc75c2016-02-02 15:37:51 -07001523 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001524 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001525 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001526 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001527 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001528 rowPtr[3] = 255; /* Alpha of 1 */
1529 rowPtr += 4;
1530 }
1531 rgba_data += layout->rowPitch;
1532 }
1533 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001534 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001535#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001536}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001537
Dave Houlton5fa47912018-02-16 11:02:26 -07001538static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1539 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001540 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001541 int32_t tex_width;
1542 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001543 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001544 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001545
Karl Schultze4dc75c2016-02-02 15:37:51 -07001546 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001547 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001548 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001549
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001550 tex_obj->tex_width = tex_width;
1551 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001552
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001553 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001554 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001555 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001556 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001557 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001558 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001559 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001560 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001561 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001562 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001563 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001564 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001565 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001566 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001567
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001568 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001569
Dave Houlton5fa47912018-02-16 11:02:26 -07001570 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001571 assert(!err);
1572
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001573 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001574
Chia-I Wuf48700b2015-11-10 16:21:09 +08001575 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001576 tex_obj->mem_alloc.pNext = NULL;
1577 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1578 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001579
Dave Houlton5fa47912018-02-16 11:02:26 -07001580 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001581 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001582
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001583 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001584 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001585 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001586
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001587 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001588 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001589 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001590
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001591 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001592 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001593 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001594 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001595 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001596 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001597 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001598 void *data;
1599
Dave Houlton5fa47912018-02-16 11:02:26 -07001600 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001601
Dave Houlton5fa47912018-02-16 11:02:26 -07001602 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001603 assert(!err);
1604
1605 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1606 fprintf(stderr, "Error loading texture: %s\n", filename);
1607 }
1608
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001609 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001610 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001611
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001612 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001613}
1614
Dave Houlton5fa47912018-02-16 11:02:26 -07001615static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001616 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001617 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1618 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001619}
1620
Karl Schultze4dc75c2016-02-02 15:37:51 -07001621static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001622 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001623 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001624 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001625
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001626 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001627
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001628 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001629 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001630
Dave Houlton5fa47912018-02-16 11:02:26 -07001631 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001632 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001633 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1634 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001635 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1636 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001637 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1638 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001639 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001640 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001641 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001642 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001643
Tony Barbour16156cb2016-10-19 14:15:49 -06001644 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Dave Houlton5fa47912018-02-16 11:02:26 -07001645 demo_prepare_texture_image(demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
1646 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1647 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001648
Dave Houlton5fa47912018-02-16 11:02:26 -07001649 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1650 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1651 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001652
Dave Houlton5fa47912018-02-16 11:02:26 -07001653 demo_set_image_layout(demo, demo->staging_texture.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1654 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001655 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001656
Dave Houlton5fa47912018-02-16 11:02:26 -07001657 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1658 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001659 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001660
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001661 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001662 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1663 .srcOffset = {0, 0, 0},
1664 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1665 .dstOffset = {0, 0, 0},
Dave Houlton5fa47912018-02-16 11:02:26 -07001666 .extent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001667 };
Dave Houlton5fa47912018-02-16 11:02:26 -07001668 vkCmdCopyImage(demo->cmd, demo->staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1669 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001670
Dave Houlton5fa47912018-02-16 11:02:26 -07001671 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1672 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001673 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001674
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001675 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001676 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1677 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001678 }
1679
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001680 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001681 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001682 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001683 .magFilter = VK_FILTER_NEAREST,
1684 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001685 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001686 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1687 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1688 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001689 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001690 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001691 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001692 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001693 .minLod = 0.0f,
1694 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001695 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001696 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001697 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001698
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001699 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001700 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001701 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001702 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001703 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001704 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001705 .components =
1706 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001707 VK_COMPONENT_SWIZZLE_R,
1708 VK_COMPONENT_SWIZZLE_G,
1709 VK_COMPONENT_SWIZZLE_B,
1710 VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001711 },
1712 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001713 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001714 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001715
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001716 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001717 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001718 assert(!err);
1719
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001720 /* create image view */
1721 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001722 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001723 assert(!err);
1724 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001725}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001726
Tony Barboura72d9492017-01-11 13:35:09 -07001727void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001728 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001729 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001730 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001731 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001732 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001733 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001734 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001735 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001736
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001737 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001738 mat4x4_mul(MVP, VP, demo->model_matrix);
1739 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001740 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001741
Karl Schultza2615462017-01-20 13:19:20 -07001742 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001743 data.position[i][0] = g_vertex_buffer_data[i * 3];
1744 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1745 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001746 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001747 data.attr[i][0] = g_uv_buffer_data[2 * i];
1748 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001749 data.attr[i][2] = 0;
1750 data.attr[i][3] = 0;
1751 }
1752
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001753 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001754 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001755 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001756 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001757
Tony Barboura72d9492017-01-11 13:35:09 -07001758 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001759 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001760 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001761
Dave Houlton5fa47912018-02-16 11:02:26 -07001762 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001763
Tony Barboura72d9492017-01-11 13:35:09 -07001764 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1765 mem_alloc.pNext = NULL;
1766 mem_alloc.allocationSize = mem_reqs.size;
1767 mem_alloc.memoryTypeIndex = 0;
1768
Dave Houlton5fa47912018-02-16 11:02:26 -07001769 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1770 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1771 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001772 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001773
Dave Houlton5fa47912018-02-16 11:02:26 -07001774 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001775 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001776
Dave Houlton5fa47912018-02-16 11:02:26 -07001777 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 -07001778 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001779
Tony Barboura72d9492017-01-11 13:35:09 -07001780 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001781
Tony Barboura72d9492017-01-11 13:35:09 -07001782 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001783
Tony Barboura72d9492017-01-11 13:35:09 -07001784 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001785 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001786 assert(!err);
1787 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001788}
1789
Karl Schultze4dc75c2016-02-02 15:37:51 -07001790static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001791 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001792 [0] =
1793 {
1794 .binding = 0,
1795 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1796 .descriptorCount = 1,
1797 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1798 .pImmutableSamplers = NULL,
1799 },
1800 [1] =
1801 {
1802 .binding = 1,
1803 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1804 .descriptorCount = DEMO_TEXTURE_COUNT,
1805 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1806 .pImmutableSamplers = NULL,
1807 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001808 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001809 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001810 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001811 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001812 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001813 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001814 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001815 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001816
Dave Houlton5fa47912018-02-16 11:02:26 -07001817 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001818 assert(!err);
1819
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001820 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001821 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1822 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001823 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001824 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001825 };
1826
Dave Houlton5fa47912018-02-16 11:02:26 -07001827 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001828 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001829}
1830
Karl Schultze4dc75c2016-02-02 15:37:51 -07001831static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001832 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1833 // because at the start of the renderpass, we don't care about their contents.
1834 // At the start of the subpass, the color attachment's layout will be transitioned
1835 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1836 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1837 // the renderpass, the color attachment's layout will be transitioned to
1838 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1839 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001840 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001841 [0] =
1842 {
1843 .format = demo->format,
1844 .flags = 0,
1845 .samples = VK_SAMPLE_COUNT_1_BIT,
1846 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1847 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1848 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1849 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1850 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1851 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1852 },
1853 [1] =
1854 {
1855 .format = demo->depth.format,
1856 .flags = 0,
1857 .samples = VK_SAMPLE_COUNT_1_BIT,
1858 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1859 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1860 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1861 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1862 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1863 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1864 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001865 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001866 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001867 .attachment = 0,
1868 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001869 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001870 const VkAttachmentReference depth_reference = {
1871 .attachment = 1,
1872 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1873 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001874 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001875 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1876 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001877 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001878 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001879 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001880 .pColorAttachments = &color_reference,
1881 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001882 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001883 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001884 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001885 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001886 const VkRenderPassCreateInfo rp_info = {
1887 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1888 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001889 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001890 .attachmentCount = 2,
1891 .pAttachments = attachments,
1892 .subpassCount = 1,
1893 .pSubpasses = &subpass,
1894 .dependencyCount = 0,
1895 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001896 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001897 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001898
Chia-I Wu63636062015-10-26 21:10:41 +08001899 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001900 assert(!err);
1901}
1902
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001903static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001904 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001905 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001906 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001907
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001908 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1909 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001910 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001911 moduleCreateInfo.codeSize = size;
1912 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001913
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001914 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1915 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001916
1917 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001918}
1919
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001920static void demo_prepare_vs(struct demo *demo) {
1921 const uint32_t vs_code[] = {
1922#include "cube.vert.inc"
1923 };
1924 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001925}
1926
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001927static void demo_prepare_fs(struct demo *demo) {
1928 const uint32_t fs_code[] = {
1929#include "cube.frag.inc"
1930 };
1931 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001932}
1933
Karl Schultze4dc75c2016-02-02 15:37:51 -07001934static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001935 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001936 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001937 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001938 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001939 VkPipelineRasterizationStateCreateInfo rs;
1940 VkPipelineColorBlendStateCreateInfo cb;
1941 VkPipelineDepthStencilStateCreateInfo ds;
1942 VkPipelineViewportStateCreateInfo vp;
1943 VkPipelineMultisampleStateCreateInfo ms;
1944 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1945 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001946 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001947
Piers Daniellba839d12015-09-29 13:01:09 -06001948 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1949 memset(&dynamicState, 0, sizeof dynamicState);
1950 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1951 dynamicState.pDynamicStates = dynamicStateEnables;
1952
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001953 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001954 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001955 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001956
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001957 memset(&vi, 0, sizeof(vi));
1958 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1959
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001960 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001961 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001962 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001963
1964 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001965 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1966 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001967 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001968 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001969 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001970 rs.rasterizerDiscardEnable = VK_FALSE;
1971 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001972 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001973
1974 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001975 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1976 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001977 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001978 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001979 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001980 cb.attachmentCount = 1;
1981 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982
Tony Barbour29645d02015-01-16 14:27:35 -07001983 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001984 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001985 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001986 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001987 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001988 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001989
1990 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001991 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001992 ds.depthTestEnable = VK_TRUE;
1993 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001994 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001995 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001996 ds.back.failOp = VK_STENCIL_OP_KEEP;
1997 ds.back.passOp = VK_STENCIL_OP_KEEP;
1998 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001999 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002000 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002001
Tony Barbour29645d02015-01-16 14:27:35 -07002002 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002003 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002004 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002005 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002006
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002007 demo_prepare_vs(demo);
2008 demo_prepare_fs(demo);
2009
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002010 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002011 VkPipelineShaderStageCreateInfo shaderStages[2];
2012 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2013
Karl Schultze4dc75c2016-02-02 15:37:51 -07002014 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2015 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002016 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002017 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002018
Karl Schultze4dc75c2016-02-02 15:37:51 -07002019 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2020 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002021 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002022 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002023
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002024 memset(&pipelineCache, 0, sizeof(pipelineCache));
2025 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002026
Dave Houlton5fa47912018-02-16 11:02:26 -07002027 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002028 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002029
Karl Schultze4dc75c2016-02-02 15:37:51 -07002030 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002031 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002032 pipeline.pRasterizationState = &rs;
2033 pipeline.pColorBlendState = &cb;
2034 pipeline.pMultisampleState = &ms;
2035 pipeline.pViewportState = &vp;
2036 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002037 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002038 pipeline.pStages = shaderStages;
2039 pipeline.renderPass = demo->render_pass;
2040 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002041
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002042 pipeline.renderPass = demo->render_pass;
2043
Dave Houlton5fa47912018-02-16 11:02:26 -07002044 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002045 assert(!err);
2046
Chia-I Wu63636062015-10-26 21:10:41 +08002047 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2048 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002049}
2050
Karl Schultze4dc75c2016-02-02 15:37:51 -07002051static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002052 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07002053 [0] =
2054 {
2055 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2056 .descriptorCount = demo->swapchainImageCount,
2057 },
2058 [1] =
2059 {
2060 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2061 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2062 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002063 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002064 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002065 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002066 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002067 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002068 .poolSizeCount = 2,
2069 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002070 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002071 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002072
Dave Houlton5fa47912018-02-16 11:02:26 -07002073 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002074 assert(!err);
2075}
2076
Karl Schultze4dc75c2016-02-02 15:37:51 -07002077static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002078 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002079 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002080 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002081
Dave Houlton5fa47912018-02-16 11:02:26 -07002082 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2083 .pNext = NULL,
2084 .descriptorPool = demo->desc_pool,
2085 .descriptorSetCount = 1,
2086 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002087
2088 VkDescriptorBufferInfo buffer_info;
2089 buffer_info.offset = 0;
2090 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002091
Chia-I Wuae721ba2015-05-25 16:27:55 +08002092 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002093 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002094 tex_descs[i].sampler = demo->textures[i].sampler;
2095 tex_descs[i].imageView = demo->textures[i].view;
2096 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002097 }
2098
2099 memset(&writes, 0, sizeof(writes));
2100
2101 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002102 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002103 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002104 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002105
2106 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002107 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002108 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002109 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002110 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002111
Tony Barboura72d9492017-01-11 13:35:09 -07002112 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2113 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2114 assert(!err);
2115 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2116 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2117 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2118 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2119 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002120}
2121
Karl Schultze4dc75c2016-02-02 15:37:51 -07002122static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002123 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002124 attachments[1] = demo->depth.view;
2125
Chia-I Wuf973e322015-07-08 13:34:24 +08002126 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002127 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2128 .pNext = NULL,
2129 .renderPass = demo->render_pass,
2130 .attachmentCount = 2,
2131 .pAttachments = attachments,
2132 .width = demo->width,
2133 .height = demo->height,
2134 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002135 };
2136 VkResult U_ASSERT_ONLY err;
2137 uint32_t i;
2138
Tony Barbourd8e504f2015-10-08 14:26:24 -06002139 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002140 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002141 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002142 assert(!err);
2143 }
2144}
2145
Karl Schultze4dc75c2016-02-02 15:37:51 -07002146static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002147 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002148 if (demo->cmd_pool == VK_NULL_HANDLE) {
2149 const VkCommandPoolCreateInfo cmd_pool_info = {
2150 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2151 .pNext = NULL,
2152 .queueFamilyIndex = demo->graphics_queue_family_index,
2153 .flags = 0,
2154 };
2155 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2156 assert(!err);
2157 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002158
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002159 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002160 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002161 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002162 .commandPool = demo->cmd_pool,
2163 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002164 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002165 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002166 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2167 assert(!err);
2168 VkCommandBufferBeginInfo cmd_buf_info = {
2169 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2170 .pNext = NULL,
2171 .flags = 0,
2172 .pInheritanceInfo = NULL,
2173 };
2174 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2175 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002176
2177 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002178
2179 if (demo->is_minimized) {
2180 demo->prepared = false;
2181 return;
2182 }
2183
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002184 demo_prepare_depth(demo);
2185 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002186 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002187
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002188 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002189 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002190 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002191
Ian Elliotta0781972015-08-21 15:09:33 -06002192 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002193 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002194 assert(!err);
2195 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002196
Tony Barbour22e06272016-09-07 16:07:56 -06002197 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002198 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002199 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2200 .pNext = NULL,
2201 .queueFamilyIndex = demo->present_queue_family_index,
2202 .flags = 0,
2203 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002204 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002205 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002206 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002207 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2208 .pNext = NULL,
2209 .commandPool = demo->present_cmd_pool,
2210 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2211 .commandBufferCount = 1,
2212 };
2213 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002214 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2215 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002216 assert(!err);
2217 demo_build_image_ownership_cmd(demo, i);
2218 }
2219 }
2220
Chia-I Wu63ea9262015-03-26 13:14:16 +08002221 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002222 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002223
Chia-I Wuf973e322015-07-08 13:34:24 +08002224 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002225
Ian Elliotta0781972015-08-21 15:09:33 -06002226 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002227 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002228 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002229 }
2230
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002231 /*
2232 * Prepare functions above may generate pipeline commands
2233 * that need to be flushed before beginning the render loop.
2234 */
2235 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002236 if (demo->staging_texture.image) {
2237 demo_destroy_texture_image(demo, &demo->staging_texture);
2238 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002239
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002240 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002241 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002242}
2243
Karl Schultze4dc75c2016-02-02 15:37:51 -07002244static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002245 uint32_t i;
2246
2247 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002248 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002249
Tony Barbour66a56c32016-09-21 13:10:56 -06002250 // Wait for fences from present operations
2251 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002252 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002253 vkDestroyFence(demo->device, demo->fences[i], NULL);
2254 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2255 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2256 if (demo->separate_present_queue) {
2257 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2258 }
2259 }
2260
Jeremy Kniager602e4182018-01-19 10:52:34 -07002261 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2262 if (!demo->is_minimized) {
2263 for (i = 0; i < demo->swapchainImageCount; i++) {
2264 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2265 }
2266 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002267
Jeremy Kniager602e4182018-01-19 10:52:34 -07002268 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2269 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2270 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2271 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2272 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002273
Jeremy Kniager602e4182018-01-19 10:52:34 -07002274 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2275 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2276 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2277 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2278 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2279 }
2280 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002281
Jeremy Kniager602e4182018-01-19 10:52:34 -07002282 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2283 vkDestroyImage(demo->device, demo->depth.image, NULL);
2284 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002285
Jeremy Kniager602e4182018-01-19 10:52:34 -07002286 for (i = 0; i < demo->swapchainImageCount; i++) {
2287 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2288 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2289 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2290 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2291 }
2292 free(demo->swapchain_image_resources);
2293 free(demo->queue_props);
2294 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002295
Jeremy Kniager602e4182018-01-19 10:52:34 -07002296 if (demo->separate_present_queue) {
2297 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2298 }
Tony Barbour22e06272016-09-07 16:07:56 -06002299 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002300 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002301 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002302 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07002303 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002304 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002305 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002306
Tony Barbour153cb062016-12-07 13:43:36 -07002307#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002308 XDestroyWindow(demo->display, demo->xlib_window);
2309 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002310#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002311 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002312 xcb_disconnect(demo->connection);
2313 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002314#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002315 wl_keyboard_destroy(demo->keyboard);
2316 wl_pointer_destroy(demo->pointer);
2317 wl_seat_destroy(demo->seat);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002318 wl_shell_surface_destroy(demo->shell_surface);
2319 wl_surface_destroy(demo->window);
2320 wl_shell_destroy(demo->shell);
2321 wl_compositor_destroy(demo->compositor);
2322 wl_registry_destroy(demo->registry);
2323 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002324#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002325#endif
Karl Schultz86429112017-06-20 11:27:59 -06002326
2327 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002328}
2329
Karl Schultze4dc75c2016-02-02 15:37:51 -07002330static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002331 uint32_t i;
2332
Mike Stroyanbb60b382015-10-28 09:46:57 -06002333 // Don't react to resize until after first initialization.
2334 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002335 if (demo->is_minimized) {
2336 demo_prepare(demo);
2337 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002338 return;
2339 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002340 // In order to properly resize the window, we must re-create the swapchain
2341 // AND redo the command buffers, etc.
2342 //
2343 // First, perform part of the demo_cleanup() function:
2344 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002345 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002346
2347 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002348 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002349 }
Chia-I Wu63636062015-10-26 21:10:41 +08002350 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002351
Chia-I Wu63636062015-10-26 21:10:41 +08002352 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2353 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2354 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2355 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2356 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002357
2358 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002359 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2360 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2361 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2362 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002363 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002364
Chia-I Wu63636062015-10-26 21:10:41 +08002365 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2366 vkDestroyImage(demo->device, demo->depth.image, NULL);
2367 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002368
Ian Elliottcf074d32015-10-16 18:02:43 -06002369 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002370 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002371 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002372 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2373 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002374 }
Chia-I Wu63636062015-10-26 21:10:41 +08002375 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002376 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002377 if (demo->separate_present_queue) {
2378 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2379 }
Tony Barboura72d9492017-01-11 13:35:09 -07002380 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002381
Ian Elliottcf074d32015-10-16 18:02:43 -06002382 // Second, re-perform the demo_prepare() function, which will re-create the
2383 // swapchain:
2384 demo_prepare(demo);
2385}
2386
David Pinedo2bb7c932015-06-18 17:03:14 -06002387// On MS-Windows, make this a global, so it's available to WndProc()
2388struct demo demo;
2389
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002390#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002391static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002392 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002393
Ian Elliott639ca472015-04-16 15:23:05 -06002394 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002395 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002396 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002397 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002398 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002399}
Ian Elliott639ca472015-04-16 15:23:05 -06002400
2401// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002402LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2403 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002404 case WM_CLOSE:
2405 PostQuitMessage(validation_error);
2406 break;
2407 case WM_PAINT:
2408 // The validation callback calls MessageBox which can generate paint
2409 // events - don't make more Vulkan calls if we got here from the
2410 // callback
2411 if (!in_callback) {
2412 demo_run(&demo);
2413 }
2414 break;
2415 case WM_GETMINMAXINFO: // set window's minimum size
2416 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2417 return 0;
2418 case WM_SIZE:
2419 // Resize the application to the new window size, except when
2420 // it was minimized. Vulkan doesn't support images or swapchains
2421 // with width=0 and height=0.
2422 if (wParam != SIZE_MINIMIZED) {
2423 demo.width = lParam & 0xffff;
2424 demo.height = (lParam & 0xffff0000) >> 16;
2425 demo_resize(&demo);
2426 }
2427 break;
2428 default:
2429 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002430 }
2431 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2432}
2433
Karl Schultze4dc75c2016-02-02 15:37:51 -07002434static void demo_create_window(struct demo *demo) {
2435 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002436
2437 // Initialize the window class structure:
2438 win_class.cbSize = sizeof(WNDCLASSEX);
2439 win_class.style = CS_HREDRAW | CS_VREDRAW;
2440 win_class.lpfnWndProc = WndProc;
2441 win_class.cbClsExtra = 0;
2442 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002443 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002444 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2445 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2446 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2447 win_class.lpszMenuName = NULL;
2448 win_class.lpszClassName = demo->name;
2449 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2450 // Register window class:
2451 if (!RegisterClassEx(&win_class)) {
2452 // It didn't work, so try to give a useful error:
2453 printf("Unexpected error trying to start the application!\n");
2454 fflush(stdout);
2455 exit(1);
2456 }
2457 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002458 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002459 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002460 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002461 demo->name, // class name
2462 demo->name, // app name
2463 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002464 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002465 100, 100, // x/y coords
2466 wr.right - wr.left, // width
2467 wr.bottom - wr.top, // height
2468 NULL, // handle to parent
2469 NULL, // handle to menu
2470 demo->connection, // hInstance
2471 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002472 if (!demo->window) {
2473 // It didn't work, so try to give a useful error:
2474 printf("Cannot create a window in which to draw!\n");
2475 fflush(stdout);
2476 exit(1);
2477 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002478 // Window client area size must be at least 1 pixel high, to prevent crash.
2479 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002480 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002481}
Tony Barbour8295ac42016-08-08 11:04:17 -06002482#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002483static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002484 const char *display_envar = getenv("DISPLAY");
2485 if (display_envar == NULL || display_envar[0] == '\0') {
2486 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2487 fflush(stdout);
2488 exit(1);
2489 }
Tony Barbour2593b182016-04-19 10:57:58 -06002490
Tony Barbouree9cd372017-01-31 16:09:58 -07002491 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002492 demo->display = XOpenDisplay(NULL);
2493 long visualMask = VisualScreenMask;
2494 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002495 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002496 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002497 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002498
Dave Houlton5fa47912018-02-16 11:02:26 -07002499 Colormap colormap =
2500 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002501
Dave Houlton5fa47912018-02-16 11:02:26 -07002502 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002503 windowAttributes.colormap = colormap;
2504 windowAttributes.background_pixel = 0xFFFFFFFF;
2505 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002506 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002507
Dave Houlton5fa47912018-02-16 11:02:26 -07002508 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2509 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2510 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002511
2512 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2513 XMapWindow(demo->display, demo->xlib_window);
2514 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002515 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002516}
2517static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002518 switch (event->type) {
2519 case ClientMessage:
2520 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002521 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002522 case KeyPress:
2523 switch (event->xkey.keycode) {
2524 case 0x9: // Escape
2525 demo->quit = true;
2526 break;
2527 case 0x71: // left arrow key
2528 demo->spin_angle -= demo->spin_increment;
2529 break;
2530 case 0x72: // right arrow key
2531 demo->spin_angle += demo->spin_increment;
2532 break;
2533 case 0x41: // space bar
2534 demo->pause = !demo->pause;
2535 break;
2536 }
Tony Barbour2593b182016-04-19 10:57:58 -06002537 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002538 case ConfigureNotify:
2539 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2540 demo->width = event->xconfigure.width;
2541 demo->height = event->xconfigure.height;
2542 demo_resize(demo);
2543 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002544 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002545 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002546 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002547 }
Tony Barbour2593b182016-04-19 10:57:58 -06002548}
2549
2550static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002551 while (!demo->quit) {
2552 XEvent event;
2553
2554 if (demo->pause) {
2555 XNextEvent(demo->display, &event);
2556 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002557 }
2558 while (XPending(demo->display) > 0) {
2559 XNextEvent(demo->display, &event);
2560 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002561 }
2562
Tony Barbour2593b182016-04-19 10:57:58 -06002563 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002564 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002565 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002566 }
2567}
Tony Barbour153cb062016-12-07 13:43:36 -07002568#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002569static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002570 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002571 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002572 case XCB_EXPOSE:
2573 // TODO: Resize window
2574 break;
2575 case XCB_CLIENT_MESSAGE:
2576 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2577 demo->quit = true;
2578 }
2579 break;
2580 case XCB_KEY_RELEASE: {
2581 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002582
Dave Houlton5fa47912018-02-16 11:02:26 -07002583 switch (key->detail) {
2584 case 0x9: // Escape
2585 demo->quit = true;
2586 break;
2587 case 0x71: // left arrow key
2588 demo->spin_angle -= demo->spin_increment;
2589 break;
2590 case 0x72: // right arrow key
2591 demo->spin_angle += demo->spin_increment;
2592 break;
2593 case 0x41: // space bar
2594 demo->pause = !demo->pause;
2595 break;
2596 }
2597 } break;
2598 case XCB_CONFIGURE_NOTIFY: {
2599 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2600 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2601 demo->width = cfg->width;
2602 demo->height = cfg->height;
2603 demo_resize(demo);
2604 }
2605 } break;
2606 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002607 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002608 }
2609}
2610
Tony Barbour2593b182016-04-19 10:57:58 -06002611static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002612 xcb_flush(demo->connection);
2613
2614 while (!demo->quit) {
2615 xcb_generic_event_t *event;
2616
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002617 if (demo->pause) {
2618 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002619 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002620 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002621 }
2622 while (event) {
2623 demo_handle_xcb_event(demo, event);
2624 free(event);
2625 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002626 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002627
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002628 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002629 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002630 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002631 }
2632}
2633
Tony Barbour2593b182016-04-19 10:57:58 -06002634static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002635 uint32_t value_mask, value_list[32];
2636
Tony Barbour2593b182016-04-19 10:57:58 -06002637 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002638
2639 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2640 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002641 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002642
Dave Houlton5fa47912018-02-16 11:02:26 -07002643 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2644 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002645
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002646 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002647 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2648 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002649
Dave Houlton5fa47912018-02-16 11:02:26 -07002650 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2651 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002652
Dave Houlton5fa47912018-02-16 11:02:26 -07002653 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002654 &(*demo->atom_wm_delete_window).atom);
2655 free(reply);
2656
Tony Barbour2593b182016-04-19 10:57:58 -06002657 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002658
Karl Schultze4dc75c2016-02-02 15:37:51 -07002659 // Force the x/y coordinates to 100,100 results are identical in consecutive
2660 // runs
2661 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002662 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002663}
Tony Barbour6b131662016-08-25 13:14:45 -06002664// VK_USE_PLATFORM_XCB_KHR
2665#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002666static void demo_run(struct demo *demo) {
2667 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002668 if (demo->pause) {
2669 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002670 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002671 wl_display_dispatch_pending(demo->display); // don't block
2672 demo_draw(demo);
2673 demo->curFrame++;
2674 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2675 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002676 }
2677}
2678
Dave Houlton5fa47912018-02-16 11:02:26 -07002679static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002680 wl_shell_surface_pong(shell_surface, serial);
2681}
2682
Dave Houlton5fa47912018-02-16 11:02:26 -07002683static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED,
2684 int32_t width UNUSED, int32_t height UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002685
Dave Houlton5fa47912018-02-16 11:02:26 -07002686static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002687
Dave Houlton5fa47912018-02-16 11:02:26 -07002688static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002689
2690static void demo_create_window(struct demo *demo) {
2691 demo->window = wl_compositor_create_surface(demo->compositor);
2692 if (!demo->window) {
2693 printf("Can not create wayland_surface from compositor!\n");
2694 fflush(stdout);
2695 exit(1);
2696 }
2697
2698 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2699 if (!demo->shell_surface) {
2700 printf("Can not get shell_surface from wayland_surface!\n");
2701 fflush(stdout);
2702 exit(1);
2703 }
Dave Houlton5fa47912018-02-16 11:02:26 -07002704 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002705 wl_shell_surface_set_toplevel(demo->shell_surface);
2706 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2707}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002708#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2709static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002710 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002711
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002712 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002713 demo->curFrame++;
2714}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002715#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002716#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2717static VkResult demo_create_display_surface(struct demo *demo) {
2718 VkResult U_ASSERT_ONLY err;
2719 uint32_t display_count;
2720 uint32_t mode_count;
2721 uint32_t plane_count;
2722 VkDisplayPropertiesKHR display_props;
2723 VkDisplayKHR display;
2724 VkDisplayModePropertiesKHR mode_props;
2725 VkDisplayPlanePropertiesKHR *plane_props;
2726 VkBool32 found_plane = VK_FALSE;
2727 uint32_t plane_index;
2728 VkExtent2D image_extent;
2729 VkDisplaySurfaceCreateInfoKHR create_info;
2730
2731 // Get the first display
2732 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2733 assert(!err);
2734
2735 if (display_count == 0) {
2736 printf("Cannot find any display!\n");
2737 fflush(stdout);
2738 exit(1);
2739 }
2740
2741 display_count = 1;
2742 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2743 assert(!err || (err == VK_INCOMPLETE));
2744
2745 display = display_props.display;
2746
2747 // Get the first mode of the display
2748 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2749 assert(!err);
2750
2751 if (mode_count == 0) {
2752 printf("Cannot find any mode for the display!\n");
2753 fflush(stdout);
2754 exit(1);
2755 }
2756
2757 mode_count = 1;
2758 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2759 assert(!err || (err == VK_INCOMPLETE));
2760
2761 // Get the list of planes
2762 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2763 assert(!err);
2764
2765 if (plane_count == 0) {
2766 printf("Cannot find any plane!\n");
2767 fflush(stdout);
2768 exit(1);
2769 }
2770
2771 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2772 assert(plane_props);
2773
2774 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2775 assert(!err);
2776
2777 // Find a plane compatible with the display
2778 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2779 uint32_t supported_count;
2780 VkDisplayKHR *supported_displays;
2781
2782 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002783 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002784 continue;
2785 }
2786
2787 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2788 assert(!err);
2789
2790 if (supported_count == 0) {
2791 continue;
2792 }
2793
2794 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2795 assert(supported_displays);
2796
2797 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2798 assert(!err);
2799
2800 for (uint32_t i = 0; i < supported_count; i++) {
2801 if (supported_displays[i] == display) {
2802 found_plane = VK_TRUE;
2803 break;
2804 }
2805 }
2806
2807 free(supported_displays);
2808
2809 if (found_plane) {
2810 break;
2811 }
2812 }
2813
2814 if (!found_plane) {
2815 printf("Cannot find a plane compatible with the display!\n");
2816 fflush(stdout);
2817 exit(1);
2818 }
2819
2820 free(plane_props);
2821
Tony Barbour820b55b2017-03-14 08:55:46 -06002822 VkDisplayPlaneCapabilitiesKHR planeCaps;
2823 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2824 // Find a supported alpha mode
Mark Young66510e52017-11-10 10:05:14 -07002825 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2826 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002827 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2828 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2829 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2830 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2831 };
2832 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2833 if (planeCaps.supportedAlpha & alphaModes[i]) {
2834 alphaMode = alphaModes[i];
2835 break;
2836 }
2837 }
Damien Leone600c3052017-01-31 10:26:07 -07002838 image_extent.width = mode_props.parameters.visibleRegion.width;
2839 image_extent.height = mode_props.parameters.visibleRegion.height;
2840
2841 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2842 create_info.pNext = NULL;
2843 create_info.flags = 0;
2844 create_info.displayMode = mode_props.displayMode;
2845 create_info.planeIndex = plane_index;
2846 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2847 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002848 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002849 create_info.globalAlpha = 1.0f;
2850 create_info.imageExtent = image_extent;
2851
2852 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2853}
2854
Dave Houlton5fa47912018-02-16 11:02:26 -07002855static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07002856 while (!demo->quit) {
2857 demo_draw(demo);
2858 demo->curFrame++;
2859
2860 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2861 demo->quit = true;
2862 }
2863 }
2864}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002865#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002866
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002867/*
2868 * Return 1 (true) if all layer names specified in check_names
2869 * can be found in given layer properties.
2870 */
Dave Houlton5fa47912018-02-16 11:02:26 -07002871static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002872 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002873 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002874 for (uint32_t j = 0; j < layer_count; j++) {
2875 if (!strcmp(check_names[i], layers[j].layerName)) {
2876 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002877 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002878 }
2879 }
2880 if (!found) {
2881 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2882 return 0;
2883 }
2884 }
2885 return 1;
2886}
2887
Karl Schultze4dc75c2016-02-02 15:37:51 -07002888static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002889 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002890 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002891 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002892 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002893 char **instance_validation_layers = NULL;
2894 demo->enabled_extension_count = 0;
2895 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002896 demo->is_minimized = false;
2897 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002898
Dave Houlton5fa47912018-02-16 11:02:26 -07002899 char *instance_validation_layers_alt1[] = {"VK_LAYER_LUNARG_standard_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06002900
Dave Houlton5fa47912018-02-16 11:02:26 -07002901 char *instance_validation_layers_alt2[] = {"VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2902 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2903 "VK_LAYER_GOOGLE_unique_objects"};
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002904
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002905 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002906 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002907 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002908 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002909 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002910
Karl Schultz7c50ad62016-04-01 12:07:03 -06002911 instance_validation_layers = instance_validation_layers_alt1;
2912 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002913 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2914 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002915 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002916
Dave Houlton5fa47912018-02-16 11:02:26 -07002917 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt1), instance_validation_layers,
2918 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002919 if (validation_found) {
2920 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002921 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2922 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002923 } else {
2924 // use alternative set of validation layers
2925 instance_validation_layers = instance_validation_layers_alt2;
2926 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Dave Houlton5fa47912018-02-16 11:02:26 -07002927 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt2), instance_validation_layers,
2928 instance_layer_count, instance_layers);
2929 validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002930 for (uint32_t i = 0; i < validation_layer_count; i++) {
2931 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002932 }
2933 }
2934 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002935 }
Dustin Graves7c287352016-01-27 13:48:06 -07002936
Karl Schultz7c50ad62016-04-01 12:07:03 -06002937 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002938 ERR_EXIT(
2939 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
2940 "Please look at the Getting Started guide for additional information.\n",
2941 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06002942 }
Dustin Graves7c287352016-01-27 13:48:06 -07002943 }
2944
2945 /* Look for instance extensions */
2946 VkBool32 surfaceExtFound = 0;
2947 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002948 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002949
Dave Houlton5fa47912018-02-16 11:02:26 -07002950 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002951 assert(!err);
2952
Dustin Graves7c287352016-01-27 13:48:06 -07002953 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002954 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2955 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07002956 assert(!err);
2957 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002958 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002959 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002960 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002961 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002962#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002963 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002964 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002965 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002966 }
Tony Barbour153cb062016-12-07 13:43:36 -07002967#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002968 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06002969 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002970 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06002971 }
Tony Barbour153cb062016-12-07 13:43:36 -07002972#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002973 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002974 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002975 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002976 }
Tony Barbour153cb062016-12-07 13:43:36 -07002977#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002978 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002979 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002980 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002981 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002982#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002983#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002984 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07002985 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002986 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07002987 }
Tony Barbour153cb062016-12-07 13:43:36 -07002988#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002989 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002990 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002991 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002992 }
Bill Hollingsf4352142017-02-14 22:58:56 -05002993#elif defined(VK_USE_PLATFORM_IOS_MVK)
2994 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2995 platformSurfaceExtFound = 1;
2996 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
2997 }
2998#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2999 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3000 platformSurfaceExtFound = 1;
3001 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3002 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003003#endif
Dave Houlton5fa47912018-02-16 11:02:26 -07003004 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003005 if (demo->validate) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003006 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003007 }
3008 }
Mark Young66510e52017-11-10 10:05:14 -07003009 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3010 if (demo->validate) {
3011 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3012 }
3013 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003014 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003015 }
Dustin Graves7c287352016-01-27 13:48:06 -07003016
3017 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003018 }
Dustin Graves7c287352016-01-27 13:48:06 -07003019
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003020 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003021 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3022 " extension.\n\n"
3023 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3024 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06003025 "vkCreateInstance Failure");
3026 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003027 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003028#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003029 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3030 " extension.\n\n"
3031 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3032 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003033 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003034#elif defined(VK_USE_PLATFORM_IOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003035 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_IOS_SURFACE_EXTENSION_NAME
3036 " extension.\n\n"
3037 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3038 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003039 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003040#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003041 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_MACOS_SURFACE_EXTENSION_NAME
3042 " extension.\n\n"
3043 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3044 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003045 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003046#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003047 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3048 " extension.\n\n"
3049 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3050 "Please look at the Getting Started guide for additional information.\n",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003051 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003052#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003053 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3054 " extension.\n\n"
3055 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3056 "Please look at the Getting Started guide for additional information.\n",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003057 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003058#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003059#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003060 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3061 " extension.\n\n"
3062 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3063 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07003064 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003065#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003066 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3067 " extension.\n\n"
3068 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3069 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003070 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003071#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003072 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3073 " extension.\n\n"
3074 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3075 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06003076 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003077#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003078 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003079 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003080 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003081 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003082 .pApplicationName = APP_SHORT_NAME,
3083 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003084 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003085 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003086 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003087 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003088 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003089 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003090 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003091 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003092 .enabledLayerCount = demo->enabled_layer_count,
3093 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3094 .enabledExtensionCount = demo->enabled_extension_count,
3095 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003096 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003097
3098 /*
3099 * This is info for a temp callback to use during CreateInstance.
3100 * After the instance is created, we use the instance-based
3101 * function to register the final callback.
3102 */
Mark Young66510e52017-11-10 10:05:14 -07003103 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003104 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003105 // VK_EXT_debug_utils style
3106 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3107 dbg_messenger_create_info.pNext = NULL;
3108 dbg_messenger_create_info.flags = 0;
3109 dbg_messenger_create_info.messageSeverity =
3110 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3111 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3112 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3113 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3114 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3115 dbg_messenger_create_info.pUserData = demo;
3116 inst_info.pNext = &dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003117 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003118
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003119 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003120
Chia-I Wu63636062015-10-26 21:10:41 +08003121 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003122 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003123 ERR_EXIT(
3124 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3125 "Please look at the Getting Started guide for additional information.\n",
3126 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003127 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003128 ERR_EXIT(
3129 "Cannot find a specified extension library.\n"
3130 "Make sure your layers path is set appropriately.\n",
3131 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003132 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003133 ERR_EXIT(
3134 "vkCreateInstance failed.\n\n"
3135 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3136 "Please look at the Getting Started guide for additional information.\n",
3137 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003138 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003139
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003140 /* Make initial call to query gpu_count, then second call for gpu info*/
3141 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3142 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003143
3144 if (gpu_count > 0) {
3145 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3146 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3147 assert(!err);
3148 /* For cube demo we just grab the first physical device */
3149 demo->gpu = physical_devices[0];
3150 free(physical_devices);
3151 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003152 ERR_EXIT(
3153 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3154 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3155 "Please look at the Getting Started guide for additional information.\n",
3156 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003157 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003158
Dustin Graves7c287352016-01-27 13:48:06 -07003159 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003160 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003161 VkBool32 swapchainExtFound = 0;
3162 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003163 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003164
Dave Houlton5fa47912018-02-16 11:02:26 -07003165 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003166 assert(!err);
3167
Dustin Graves7c287352016-01-27 13:48:06 -07003168 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003169 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3170 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003171 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003172
Dustin Graves7c287352016-01-27 13:48:06 -07003173 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003174 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003175 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003176 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003177 }
3178 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003179 }
Dustin Graves7c287352016-01-27 13:48:06 -07003180
Ian Elliott53622a72017-03-28 11:06:33 -06003181 if (demo->VK_KHR_incremental_present_enabled) {
3182 // Even though the user "enabled" the extension via the command
3183 // line, we must make sure that it's enumerated for use with the
3184 // device. Therefore, disable it here, and re-enable it again if
3185 // enumerated.
3186 demo->VK_KHR_incremental_present_enabled = false;
3187 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003188 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3189 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003190 demo->VK_KHR_incremental_present_enabled = true;
3191 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3192 }
3193 assert(demo->enabled_extension_count < 64);
3194 }
3195 if (!demo->VK_KHR_incremental_present_enabled) {
3196 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3197 }
3198 }
3199
Ian Elliottd940ca22017-03-22 08:31:27 -06003200 if (demo->VK_GOOGLE_display_timing_enabled) {
3201 // Even though the user "enabled" the extension via the command
3202 // line, we must make sure that it's enumerated for use with the
3203 // device. Therefore, disable it here, and re-enable it again if
3204 // enumerated.
3205 demo->VK_GOOGLE_display_timing_enabled = false;
3206 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003207 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3208 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003209 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003210 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003211 }
3212 assert(demo->enabled_extension_count < 64);
3213 }
3214 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003215 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003216 }
3217 }
3218
Dustin Graves7c287352016-01-27 13:48:06 -07003219 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003220 }
Dustin Graves7c287352016-01-27 13:48:06 -07003221
Tony Barbourcc69f452015-10-08 13:59:42 -06003222 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003223 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3224 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3225 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003226 "vkCreateInstance Failure");
3227 }
3228
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003229 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003230 // Setup VK_EXT_debug_utils function pointers always (we use them for
3231 // debug labels and names).
3232 demo->CreateDebugUtilsMessengerEXT =
3233 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3234 demo->DestroyDebugUtilsMessengerEXT =
3235 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3236 demo->SubmitDebugUtilsMessageEXT =
3237 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3238 demo->CmdBeginDebugUtilsLabelEXT =
3239 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3240 demo->CmdEndDebugUtilsLabelEXT =
3241 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3242 demo->CmdInsertDebugUtilsLabelEXT =
3243 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3244 demo->SetDebugUtilsObjectNameEXT =
3245 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3246 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3247 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3248 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3249 NULL == demo->SetDebugUtilsObjectNameEXT) {
3250 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003251 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003252
Mark Young66510e52017-11-10 10:05:14 -07003253 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
3254 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3255 dbg_messenger_create_info.pNext = NULL;
3256 dbg_messenger_create_info.flags = 0;
3257 dbg_messenger_create_info.messageSeverity =
3258 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3259 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3260 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3261 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3262 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3263 dbg_messenger_create_info.pUserData = demo;
3264 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003265 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003266 case VK_SUCCESS:
3267 break;
3268 case VK_ERROR_OUT_OF_HOST_MEMORY:
Mark Young66510e52017-11-10 10:05:14 -07003269 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003270 break;
3271 default:
Mark Young66510e52017-11-10 10:05:14 -07003272 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003273 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003274 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003275 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003276 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003277
3278 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003279 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003280 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003281
Dave Houlton5fa47912018-02-16 11:02:26 -07003282 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3283 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003284
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003285 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003286 // If app has specific feature requirements it should check supported
3287 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003288 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003289 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003290
Tony Barbourda2bad52016-01-22 14:36:40 -07003291 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3292 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3293 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3294 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3295 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3296}
3297
Karl Schultze4dc75c2016-02-02 15:37:51 -07003298static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003299 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003300 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003301 VkDeviceQueueCreateInfo queues[2];
3302 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3303 queues[0].pNext = NULL;
3304 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3305 queues[0].queueCount = 1;
3306 queues[0].pQueuePriorities = queue_priorities;
3307 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003308
3309 VkDeviceCreateInfo device = {
3310 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3311 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003312 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003313 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003314 .enabledLayerCount = 0,
3315 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003316 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003317 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003318 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003319 };
Tony Barbour22e06272016-09-07 16:07:56 -06003320 if (demo->separate_present_queue) {
3321 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3322 queues[1].pNext = NULL;
3323 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3324 queues[1].queueCount = 1;
3325 queues[1].pQueuePriorities = queue_priorities;
3326 queues[1].flags = 0;
3327 device.queueCreateInfoCount = 2;
3328 }
Chia-I Wu63636062015-10-26 21:10:41 +08003329 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003330 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003331}
3332
Karl Schultze4dc75c2016-02-02 15:37:51 -07003333static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003334 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003335
Karl Schultze4dc75c2016-02-02 15:37:51 -07003336// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003337#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003338 VkWin32SurfaceCreateInfoKHR createInfo;
3339 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3340 createInfo.pNext = NULL;
3341 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003342 createInfo.hinstance = demo->connection;
3343 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003344
Dave Houlton5fa47912018-02-16 11:02:26 -07003345 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003346#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003347 VkWaylandSurfaceCreateInfoKHR createInfo;
3348 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3349 createInfo.pNext = NULL;
3350 createInfo.flags = 0;
3351 createInfo.display = demo->display;
3352 createInfo.surface = demo->window;
3353
Dave Houlton5fa47912018-02-16 11:02:26 -07003354 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003355#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003356#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3357 VkAndroidSurfaceCreateInfoKHR createInfo;
3358 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3359 createInfo.pNext = NULL;
3360 createInfo.flags = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003361 createInfo.window = (ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003362
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003363 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003364#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3365 VkXlibSurfaceCreateInfoKHR createInfo;
3366 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3367 createInfo.pNext = NULL;
3368 createInfo.flags = 0;
3369 createInfo.dpy = demo->display;
3370 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003371
Dave Houlton5fa47912018-02-16 11:02:26 -07003372 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003373#elif defined(VK_USE_PLATFORM_XCB_KHR)
3374 VkXcbSurfaceCreateInfoKHR createInfo;
3375 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3376 createInfo.pNext = NULL;
3377 createInfo.flags = 0;
3378 createInfo.connection = demo->connection;
3379 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003380
Tony Barbour153cb062016-12-07 13:43:36 -07003381 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003382#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3383 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003384#elif defined(VK_USE_PLATFORM_IOS_MVK)
3385 VkIOSSurfaceCreateInfoMVK surface;
3386 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3387 surface.pNext = NULL;
3388 surface.flags = 0;
3389 surface.pView = demo->window;
3390
3391 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3392#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3393 VkMacOSSurfaceCreateInfoMVK surface;
3394 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3395 surface.pNext = NULL;
3396 surface.flags = 0;
3397 surface.pView = demo->window;
3398
3399 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003400#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003401 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003402
Tony Barbourcc69f452015-10-08 13:59:42 -06003403 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003404 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003405 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003406 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003407 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003408
3409 // Search for a graphics and a present queue in the array of queue
3410 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003411 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3412 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003413 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003414 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3415 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3416 graphicsQueueFamilyIndex = i;
3417 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003418
Tony Barbourab2a0362016-09-06 11:40:12 -06003419 if (supportsPresent[i] == VK_TRUE) {
3420 graphicsQueueFamilyIndex = i;
3421 presentQueueFamilyIndex = i;
3422 break;
3423 }
3424 }
3425 }
3426
3427 if (presentQueueFamilyIndex == UINT32_MAX) {
3428 // If didn't find a queue that supports both graphics and present, then
3429 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003430 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003431 if (supportsPresent[i] == VK_TRUE) {
3432 presentQueueFamilyIndex = i;
3433 break;
3434 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003435 }
3436 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003437
3438 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003439 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3440 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003441 }
3442
Tony Barbourab2a0362016-09-06 11:40:12 -06003443 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3444 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003445 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003446 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003447
Tony Barbourda2bad52016-01-22 14:36:40 -07003448 demo_create_device(demo);
3449
3450 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3451 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3452 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3453 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3454 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003455 if (demo->VK_GOOGLE_display_timing_enabled) {
3456 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3457 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3458 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003459
Dave Houlton5fa47912018-02-16 11:02:26 -07003460 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003461
Tony Barbour22e06272016-09-07 16:07:56 -06003462 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003463 demo->present_queue = demo->graphics_queue;
3464 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003465 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003466 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003467
Ian Elliottaaae5352015-07-06 14:27:58 -06003468 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003469 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003470 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003471 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003472 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3473 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003474 assert(!err);
3475 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3476 // the surface has no preferred format. Otherwise, at least one
3477 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003478 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003479 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003480 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003481 assert(formatCount >= 1);
3482 demo->format = surfFormats[0].format;
3483 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003484 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003485
David Pinedo2bb7c932015-06-18 17:03:14 -06003486 demo->quit = false;
3487 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003488
Tony Barbource7524e2016-07-28 12:01:45 -06003489 // Create semaphores to synchronize acquiring presentable buffers before
3490 // rendering and waiting for drawing to be complete before presenting
3491 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3492 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3493 .pNext = NULL,
3494 .flags = 0,
3495 };
Tony Barbource7524e2016-07-28 12:01:45 -06003496
Tony Barbour66a56c32016-09-21 13:10:56 -06003497 // Create fences that we can use to throttle if we get too far
3498 // ahead of the image presents
3499 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003500 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003501 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003502 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3503 assert(!err);
3504
Dave Houlton5fa47912018-02-16 11:02:26 -07003505 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003506 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003507
Dave Houlton5fa47912018-02-16 11:02:26 -07003508 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003509 assert(!err);
3510
3511 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003512 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003513 assert(!err);
3514 }
Tony Barbour22e06272016-09-07 16:07:56 -06003515 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003516 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003517
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003518 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003519 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003520}
3521
Tony Barbour153cb062016-12-07 13:43:36 -07003522#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003523static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3524 wl_fixed_t sy) {}
3525
3526static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3527
3528static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3529
3530static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3531 uint32_t state) {
3532 struct demo *demo = data;
3533 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3534 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
3535 }
3536}
3537
3538static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3539
3540static const struct wl_pointer_listener pointer_listener = {
3541 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3542};
3543
3544static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3545
3546static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3547 struct wl_array *keys) {}
3548
3549static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3550
3551static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3552 uint32_t state) {
3553 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3554 struct demo *demo = data;
3555 switch (key) {
3556 case KEY_ESC: // Escape
3557 demo->quit = true;
3558 break;
3559 case KEY_LEFT: // left arrow key
3560 demo->spin_angle -= demo->spin_increment;
3561 break;
3562 case KEY_RIGHT: // right arrow key
3563 demo->spin_angle += demo->spin_increment;
3564 break;
3565 case KEY_SPACE: // space bar
3566 demo->pause = !demo->pause;
3567 break;
3568 }
3569}
3570
3571static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3572 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3573
3574static const struct wl_keyboard_listener keyboard_listener = {
3575 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3576};
3577
3578static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3579 // Subscribe to pointer events
3580 struct demo *demo = data;
3581 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3582 demo->pointer = wl_seat_get_pointer(seat);
3583 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3584 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3585 wl_pointer_destroy(demo->pointer);
3586 demo->pointer = NULL;
3587 }
3588 // Subscribe to keyboard events
3589 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3590 demo->keyboard = wl_seat_get_keyboard(seat);
3591 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3592 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3593 wl_keyboard_destroy(demo->keyboard);
3594 demo->keyboard = NULL;
3595 }
3596}
3597
3598static const struct wl_seat_listener seat_listener = {
3599 seat_handle_capabilities,
3600};
3601
3602static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003603 uint32_t version UNUSED) {
3604 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003605 // pickup wayland objects when they appear
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003606 if (strcmp(interface, "wl_compositor") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003607 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003608 } else if (strcmp(interface, "wl_shell") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003609 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3610 } else if (strcmp(interface, "wl_seat") == 0) {
3611 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3612 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003613 }
3614}
3615
Dave Houlton5fa47912018-02-16 11:02:26 -07003616static 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 +09003617
Dave Houlton5fa47912018-02-16 11:02:26 -07003618static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003619#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003620#endif
3621
Karl Schultze4dc75c2016-02-02 15:37:51 -07003622static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003623#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003624 const xcb_setup_t *setup;
3625 xcb_screen_iterator_t iter;
3626 int scr;
3627
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003628 const char *display_envar = getenv("DISPLAY");
3629 if (display_envar == NULL || display_envar[0] == '\0') {
3630 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3631 fflush(stdout);
3632 exit(1);
3633 }
3634
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003635 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003636 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003637 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003638 fflush(stdout);
3639 exit(1);
3640 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003641
3642 setup = xcb_get_setup(demo->connection);
3643 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003644 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003645
3646 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003647#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3648 demo->display = wl_display_connect(NULL);
3649
3650 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003651 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003652 fflush(stdout);
3653 exit(1);
3654 }
3655
3656 demo->registry = wl_display_get_registry(demo->display);
3657 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3658 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003659#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003660#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003661}
3662
Karl Schultze4dc75c2016-02-02 15:37:51 -07003663static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003664 vec3 eye = {0.0f, 3.0f, 5.0f};
3665 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003666 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003667
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003668 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003669 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003670 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003671
Piers Daniell735ee532015-02-23 16:23:13 -07003672 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003673 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003674 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003675 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003676 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003677 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3678 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003679 i++;
3680 continue;
3681 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003682 if (strcmp(argv[i], "--break") == 0) {
3683 demo->use_break = true;
3684 continue;
3685 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003686 if (strcmp(argv[i], "--validate") == 0) {
3687 demo->validate = true;
3688 continue;
3689 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003690 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3691 demo->validate = true;
3692 demo->validate_checks_disabled = true;
3693 continue;
3694 }
Tony Barbour2593b182016-04-19 10:57:58 -06003695 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003696 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003697 continue;
3698 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003699 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3700 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003701 i++;
3702 continue;
3703 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003704 if (strcmp(argv[i], "--suppress_popups") == 0) {
3705 demo->suppress_popups = true;
3706 continue;
3707 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003708 if (strcmp(argv[i], "--display_timing") == 0) {
3709 demo->VK_GOOGLE_display_timing_enabled = true;
3710 continue;
3711 }
Ian Elliott53622a72017-03-28 11:06:33 -06003712 if (strcmp(argv[i], "--incremental_present") == 0) {
3713 demo->VK_KHR_incremental_present_enabled = true;
3714 continue;
3715 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003716
Cody Northropaf28a532016-09-27 10:50:57 -06003717#if defined(ANDROID)
3718 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3719#else
Mike Schuchardt9a881512017-11-01 16:16:22 -06003720 fprintf(stderr,
Mark Young66510e52017-11-10 10:05:14 -07003721 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled] [--break]\n"
3722 "\t[--c <framecount>] [--suppress_popups] [--incremental_present] [--display_timing]\n"
3723 "\t[--present_mode <present mode enum>]\n"
3724 "\t <present_mode_enum>\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3725 "\t\t\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3726 "\t\t\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
3727 "\t\t\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
Mike Schuchardt9a881512017-11-01 16:16:22 -06003728 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
3729 VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003730 fflush(stderr);
3731 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003732#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003733 }
3734
Tony Barbour153cb062016-12-07 13:43:36 -07003735 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003736
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003737 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003738
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003739 demo->width = 500;
3740 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003741
Tony Barbour66a56c32016-09-21 13:10:56 -06003742 demo->spin_angle = 4.0f;
3743 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003744 demo->pause = false;
3745
Dave Houlton5fa47912018-02-16 11:02:26 -07003746 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003747 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3748 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003749
Dave Houlton5fa47912018-02-16 11:02:26 -07003750 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003751}
3752
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003753#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003754// Include header required for parsing the command line options.
3755#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003756
Dave Houlton5fa47912018-02-16 11:02:26 -07003757int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3758 MSG msg; // message
3759 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003760 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003761 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003762
Jamie Madillb2ff6502017-03-15 16:17:46 -04003763 // Ensure wParam is initialized.
3764 msg.wParam = 0;
3765
Karl Schultze4dc75c2016-02-02 15:37:51 -07003766 // Use the CommandLine functions to get the command line arguments.
3767 // Unfortunately, Microsoft outputs
3768 // this information as wide characters for Unicode, and we simply want the
3769 // Ascii version to be compatible
3770 // with the non-Windows side. So, we have to convert the information to
3771 // Ascii character strings.
3772 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3773 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003774 argc = 0;
3775 }
3776
Karl Schultze4dc75c2016-02-02 15:37:51 -07003777 if (argc > 0) {
3778 argv = (char **)malloc(sizeof(char *) * argc);
3779 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003780 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003781 } else {
3782 for (int iii = 0; iii < argc; iii++) {
3783 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003784 size_t numConverted = 0;
3785
Karl Schultze4dc75c2016-02-02 15:37:51 -07003786 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3787 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003788 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003789 }
3790 }
3791 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003792 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003793 argv = NULL;
3794 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003795
3796 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003797
3798 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003799 if (argc > 0 && argv != NULL) {
3800 for (int iii = 0; iii < argc; iii++) {
3801 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003802 free(argv[iii]);
3803 }
3804 }
3805 free(argv);
3806 }
3807
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003808 demo.connection = hInstance;
3809 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003810 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003811 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003812
3813 demo_prepare(&demo);
3814
Dave Houlton5fa47912018-02-16 11:02:26 -07003815 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003816
3817 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003818 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003819 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003820 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003821 {
Dave Houlton5fa47912018-02-16 11:02:26 -07003822 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07003823 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003824 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003825 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003826 DispatchMessage(&msg);
3827 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003828 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003829 }
3830
3831 demo_cleanup(&demo);
3832
Karl Schultze4dc75c2016-02-02 15:37:51 -07003833 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003834}
Bill Hollingsf4352142017-02-14 22:58:56 -05003835
3836#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003837static void demo_main(struct demo *demo, void *view) {
3838 const char *argv[] = {"CubeSample"};
3839 int argc = sizeof(argv) / sizeof(char *);
Bill Hollingsf4352142017-02-14 22:58:56 -05003840
Dave Houlton5fa47912018-02-16 11:02:26 -07003841 demo_init(demo, argc, (char **)argv);
Tony Barbourc65cd752017-03-13 15:29:35 -06003842 demo->window = view;
3843 demo_init_vk_swapchain(demo);
3844 demo_prepare(demo);
3845 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003846}
3847
3848static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003849 // Wait for work to finish before updating MVP.
3850 vkDeviceWaitIdle(demo->device);
3851 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003852
Tony Barbourc65cd752017-03-13 15:29:35 -06003853 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003854}
3855
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003856#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3857#include <android/log.h>
3858#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003859#include "android_util.h"
3860
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003861static bool initialized = false;
3862static bool active = false;
3863struct demo demo;
3864
Dave Houlton5fa47912018-02-16 11:02:26 -07003865static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003866
Dave Houlton5fa47912018-02-16 11:02:26 -07003867static void processCommand(struct android_app *app, int32_t cmd) {
3868 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003869 case APP_CMD_INIT_WINDOW: {
3870 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003871 // We're getting a new window. If the app is starting up, we
3872 // need to initialize. If the app has already been
3873 // initialized, that means that we lost our previous window,
3874 // which means that we have a lot of work to do. At a minimum,
3875 // we need to destroy the swapchain and surface associated with
3876 // the old window, and create a new surface and swapchain.
3877 // However, since there are a lot of other objects/state that
3878 // is tied to the swapchain, it's easiest to simply cleanup and
3879 // start over (i.e. use a brute-force approach of re-starting
3880 // the app)
3881 if (demo.prepared) {
3882 demo_cleanup(&demo);
3883 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003884
3885 // Parse Intents into argc, argv
3886 // Use the following key to send arguments, i.e.
3887 // --es args "--validate"
3888 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07003889 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003890 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003891 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003892
3893 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07003894 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 -06003895
3896 demo_init(&demo, argc, argv);
3897
3898 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07003899 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003900
Dave Houlton5fa47912018-02-16 11:02:26 -07003901 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003902 demo_init_vk_swapchain(&demo);
3903 demo_prepare(&demo);
3904 initialized = true;
3905 }
3906 break;
3907 }
3908 case APP_CMD_GAINED_FOCUS: {
3909 active = true;
3910 break;
3911 }
3912 case APP_CMD_LOST_FOCUS: {
3913 active = false;
3914 break;
3915 }
3916 }
3917}
3918
Dave Houlton5fa47912018-02-16 11:02:26 -07003919void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06003920#ifdef ANDROID
3921 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07003922 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06003923#endif
3924
Ian Elliottf05d5d12016-05-17 12:03:35 -06003925 demo.prepared = false;
3926
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003927 app->onAppCmd = processCommand;
3928 app->onInputEvent = processInput;
3929
Dave Houlton5fa47912018-02-16 11:02:26 -07003930 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003931 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07003932 struct android_poll_source *source;
3933 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003934 if (source) {
3935 source->process(app, source);
3936 }
3937
3938 if (app->destroyRequested != 0) {
3939 demo_cleanup(&demo);
3940 return;
3941 }
3942 }
3943 if (initialized && active) {
3944 demo_run(&demo);
3945 }
3946 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003947}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003948#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003949int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003950 struct demo demo;
3951
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003952 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003953#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003954 demo_create_xcb_window(&demo);
3955#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3956 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003957#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3958 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003959#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003960#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003961
Tony Barbourcc69f452015-10-08 13:59:42 -06003962 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003963
3964 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003965
Tony Barbour153cb062016-12-07 13:43:36 -07003966#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003967 demo_run_xcb(&demo);
3968#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3969 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003970#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3971 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003972#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003973#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3974 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003975#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003976
3977 demo_cleanup(&demo);
3978
Karl Schultz0218c002016-03-22 17:06:13 -06003979 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003980}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003981#endif