blob: fb239361eb4eb2e3512131d6b482ebf7f2828b9e [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 Lobodzinski0edf1382018-04-10 15:40:04 -060059#include "object_type_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;
Tony-LunarGbc9fc052018-09-21 13:47:06 -0600164 VkBuffer buffer;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600165 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600166
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800167 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500168 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600169 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700170 int32_t tex_width, tex_height;
171};
172
Karl Schultze4dc75c2016-02-02 15:37:51 -0700173static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174
Karl Schultz0218c002016-03-22 17:06:13 -0600175static int validation_error = 0;
176
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600177struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600178 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700179 float mvp[4][4];
180 float position[12 * 3][4];
181 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600182};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600183
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600184//--------------------------------------------------------------------------------------
185// Mesh and VertexFormat Data
186//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700187// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600188static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800189 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600190 -1.0f,-1.0f, 1.0f,
191 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800192 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600193 -1.0f, 1.0f,-1.0f,
194 -1.0f,-1.0f,-1.0f,
195
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800196 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600197 1.0f, 1.0f,-1.0f,
198 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800199 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600200 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600201 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600202
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800203 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600204 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600205 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600207 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600208 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600209
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800210 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600211 -1.0f, 1.0f, 1.0f,
212 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800213 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600215 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600216
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800217 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600218 1.0f, 1.0f, 1.0f,
219 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221 1.0f,-1.0f,-1.0f,
222 1.0f, 1.0f,-1.0f,
223
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800224 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600226 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800227 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228 1.0f,-1.0f, 1.0f,
229 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600230};
231
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600232static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700233 0.0f, 1.0f, // -X side
234 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800236 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600237 0.0f, 0.0f,
238 0.0f, 1.0f,
239
Rene Lindsay76867e82016-07-29 10:49:11 -0700240 1.0f, 1.0f, // -Z side
241 0.0f, 0.0f,
242 0.0f, 1.0f,
243 1.0f, 1.0f,
244 1.0f, 0.0f,
245 0.0f, 0.0f,
246
247 1.0f, 0.0f, // -Y side
248 1.0f, 1.0f,
249 0.0f, 1.0f,
250 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600251 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800252 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600253
Rene Lindsay76867e82016-07-29 10:49:11 -0700254 1.0f, 0.0f, // +Y side
255 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800256 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600257 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700258 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600259 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600260
Rene Lindsay76867e82016-07-29 10:49:11 -0700261 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800262 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700263 0.0f, 1.0f,
264 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600265 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800266 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700267
268 0.0f, 0.0f, // +Z side
269 0.0f, 1.0f,
270 1.0f, 0.0f,
271 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600272 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700273 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600274};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700275// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600276
Karl Schultze4dc75c2016-02-02 15:37:51 -0700277void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600278 int i;
279
280 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700281 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600282 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
283 }
284 printf("\n");
285 fflush(stdout);
286}
287
Karl Schultze4dc75c2016-02-02 15:37:51 -0700288void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600289 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700290 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600291 printf("\n");
292 fflush(stdout);
293}
294
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600295typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600296 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800297 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600298 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600299 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700300 VkBuffer uniform_buffer;
301 VkDeviceMemory uniform_memory;
302 VkFramebuffer framebuffer;
303 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700304} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600305
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600306struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500307#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600308#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700309 HINSTANCE connection; // hInstance - Windows Instance
310 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
311 HWND window; // hWnd - window handle
312 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700313#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700314 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600315 Window xlib_window;
316 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700317#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700318 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319 xcb_connection_t *connection;
320 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600321 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800322 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900323#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
324 struct wl_display *display;
325 struct wl_registry *registry;
326 struct wl_compositor *compositor;
327 struct wl_surface *window;
328 struct wl_shell *shell;
329 struct wl_shell_surface *shell_surface;
Joey Bzdek15eb0702017-06-07 09:40:36 -0600330 struct wl_seat *seat;
331 struct wl_pointer *pointer;
332 struct wl_keyboard *keyboard;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700333#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500334#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mike Schuchardt837d5162018-03-08 12:57:02 -0700335 struct ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500336#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
337 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500338#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700339 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600340 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700341 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600342 bool separate_present_queue;
Jeremy Kniager602e4182018-01-19 10:52:34 -0700343 bool is_minimized;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344
Ian Elliott53622a72017-03-28 11:06:33 -0600345 bool VK_KHR_incremental_present_enabled;
346
Ian Elliottd940ca22017-03-22 08:31:27 -0600347 bool VK_GOOGLE_display_timing_enabled;
348 bool syncd_with_actual_presents;
349 uint64_t refresh_duration;
350 uint64_t refresh_duration_multiplier;
351 uint64_t target_IPD; // image present duration (inverse of frame rate)
352 uint64_t prev_desired_present_time;
353 uint32_t next_present_id;
354 uint32_t last_early_id; // 0 if no early images
355 uint32_t last_late_id; // 0 if no late images
356
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600357 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600358 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600359 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600360 VkQueue graphics_queue;
361 VkQueue present_queue;
362 uint32_t graphics_queue_family_index;
363 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600364 VkSemaphore image_acquired_semaphores[FRAME_LAG];
365 VkSemaphore draw_complete_semaphores[FRAME_LAG];
366 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600367 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600368 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600369 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370
Tony Barbourda2bad52016-01-22 14:36:40 -0700371 uint32_t enabled_extension_count;
372 uint32_t enabled_layer_count;
373 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600374 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700375
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600376 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600377 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600378 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700380 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
381 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
382 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
383 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600384 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
385 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
386 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
387 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
388 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600389 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
390 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600391 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600392 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700393 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600394 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600395 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600396 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600397
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800398 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600399 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400
401 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600402 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600404 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800405 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500406 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600407 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600408 } depth;
409
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600410 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600411 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600412
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700413 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500414 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600415 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600416 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800417 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600418 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600419
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600420 mat4x4 projection_matrix;
421 mat4x4 view_matrix;
422 mat4x4 model_matrix;
423
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600424 float spin_angle;
425 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600426 bool pause;
427
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600428 VkShaderModule vert_shader_module;
429 VkShaderModule frag_shader_module;
430
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600431 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800432
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600433 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600434 int32_t curFrame;
435 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600436 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600437 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600438 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600439 bool suppress_popups;
Mark Young66510e52017-11-10 10:05:14 -0700440
441 PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
442 PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
443 PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
444 PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
445 PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
446 PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
447 PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
448 VkDebugUtilsMessengerEXT dbg_messenger;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600449
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600450 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600451 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600452};
453
Mark Young66510e52017-11-10 10:05:14 -0700454VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
455 VkDebugUtilsMessageTypeFlagsEXT messageType,
456 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
457 void *pUserData) {
458 char prefix[64] = "";
459 char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000);
lenny-lunargfbe22392016-06-06 11:07:53 -0600460 assert(message);
Mark Young66510e52017-11-10 10:05:14 -0700461 struct demo *demo = (struct demo *)pUserData;
lenny-lunargfbe22392016-06-06 11:07:53 -0600462
Mark Young66510e52017-11-10 10:05:14 -0700463 if (demo->use_break) {
464#ifndef WIN32
465 raise(SIGTRAP);
466#else
467 DebugBreak();
468#endif
469 }
470
471 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
472 strcat(prefix, "VERBOSE : ");
473 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
474 strcat(prefix, "INFO : ");
475 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
476 strcat(prefix, "WARNING : ");
477 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
478 strcat(prefix, "ERROR : ");
479 }
480
481 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
482 strcat(prefix, "GENERAL");
lenny-lunargfbe22392016-06-06 11:07:53 -0600483 } else {
Mark Young66510e52017-11-10 10:05:14 -0700484 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
485 strcat(prefix, "VALIDATION");
486 validation_error = 1;
487 }
488 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
489 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
490 strcat(prefix, "|");
491 }
492 strcat(prefix, "PERFORMANCE");
493 }
494 }
495
496 sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber,
497 pCallbackData->pMessageIdName, pCallbackData->pMessage);
498 if (pCallbackData->objectCount > 0) {
499 char tmp_message[500];
500 sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount);
501 strcat(message, tmp_message);
502 for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
503 if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
504 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object,
Mark Young44212682018-02-21 15:29:41 -0700505 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700506 (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName);
507 } else {
508 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object,
Mark Young44212682018-02-21 15:29:41 -0700509 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700510 (void *)(pCallbackData->pObjects[object].objectHandle));
511 }
512 strcat(message, tmp_message);
513 }
514 }
515 if (pCallbackData->cmdBufLabelCount > 0) {
516 char tmp_message[500];
517 sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount);
518 strcat(message, tmp_message);
519 for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
520 sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label,
521 pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0],
522 pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2],
523 pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]);
524 strcat(message, tmp_message);
525 }
lenny-lunargfbe22392016-06-06 11:07:53 -0600526 }
527
528#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600529
Tony Barbour602ca4f2016-09-12 14:02:43 -0600530 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600531 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
Mike Schuchardt837d5162018-03-08 12:57:02 -0700537 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600538 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700539 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600540 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700541 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600542 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700543 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
544 __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600545 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600546 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600547 }
Cody Northropaf28a532016-09-27 10:50:57 -0600548
lenny-lunargfbe22392016-06-06 11:07:53 -0600549#else
Cody Northropaf28a532016-09-27 10:50:57 -0600550
lenny-lunargfbe22392016-06-06 11:07:53 -0600551 printf("%s\n", message);
552 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600553
lenny-lunargfbe22392016-06-06 11:07:53 -0600554#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600555
lenny-lunargfbe22392016-06-06 11:07:53 -0600556 free(message);
557
Mark Young66510e52017-11-10 10:05:14 -0700558 // Don't bail out, but keep going.
lenny-lunargfbe22392016-06-06 11:07:53 -0600559 return false;
560}
561
Ian Elliottd940ca22017-03-22 08:31:27 -0600562bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
563 // The desired time was the earliest time that the present should have
564 // occured. In almost every case, the actual time should be later than the
565 // desired time. We should only consider the actual time "late" if it is
566 // after "desired + rdur".
567 if (actual <= desired) {
568 // The actual time was before or equal to the desired time. This will
569 // probably never happen, but in case it does, return false since the
570 // present was obviously NOT late.
571 return false;
572 }
Liam Middlebrook881fe392018-05-03 15:52:32 -0700573 uint64_t deadline = desired + rdur;
Ian Elliottd940ca22017-03-22 08:31:27 -0600574 if (actual > deadline) {
575 return true;
576 } else {
577 return false;
578 }
579}
Dave Houlton5fa47912018-02-16 11:02:26 -0700580bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600581 if (earliest < actual) {
582 // Consider whether this present could have occured earlier. Make sure
583 // that earliest time was at least 2msec earlier than actual time, and
584 // that the margin was at least 2msec:
585 uint64_t diff = actual - earliest;
586 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
587 // This present could have occured earlier because both: 1) the
588 // earliest time was at least 2 msec before actual time, and 2) the
589 // margin was at least 2msec.
590 return true;
591 }
592 }
593 return false;
594}
595
Ian Elliottcf074d32015-10-16 18:02:43 -0600596// Forward declaration:
597static void demo_resize(struct demo *demo);
598
Dave Houlton5fa47912018-02-16 11:02:26 -0700599static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700600 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600601 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700602 if ((typeBits & 1) == 1) {
603 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700604 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700605 *typeIndex = i;
606 return true;
607 }
608 }
609 typeBits >>= 1;
610 }
611 // No memory types matched, return failure
612 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600613}
614
Karl Schultze4dc75c2016-02-02 15:37:51 -0700615static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600616 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600617
Tony Barbource7524e2016-07-28 12:01:45 -0600618 // This function could get called twice if the texture uses a staging buffer
619 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700620 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600621
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600622 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600623 assert(!err);
624
Tony Barbource7524e2016-07-28 12:01:45 -0600625 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700626 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700627 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
628 assert(!err);
629
Karl Schultze4dc75c2016-02-02 15:37:51 -0700630 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700631 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
632 .pNext = NULL,
633 .waitSemaphoreCount = 0,
634 .pWaitSemaphores = NULL,
635 .pWaitDstStageMask = NULL,
636 .commandBufferCount = 1,
637 .pCommandBuffers = cmd_bufs,
638 .signalSemaphoreCount = 0,
639 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600640
Tony Barbource7524e2016-07-28 12:01:45 -0600641 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600642 assert(!err);
643
Tony Barbource7524e2016-07-28 12:01:45 -0600644 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600645 assert(!err);
646
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600647 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600648 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600649 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600650}
651
Dave Houlton5fa47912018-02-16 11:02:26 -0700652static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
653 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600654 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600655 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600656
Dave Houlton5fa47912018-02-16 11:02:26 -0700657 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
658 .pNext = NULL,
659 .srcAccessMask = srcAccessMask,
660 .dstAccessMask = 0,
Tony-LunarGa141b962018-05-30 11:33:19 -0600661 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
662 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
Dave Houlton5fa47912018-02-16 11:02:26 -0700663 .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) {
Lenny Komowe830b592018-02-23 16:18:36 -0700705 VkDebugUtilsLabelEXT label;
706 memset(&label, 0, sizeof(label));
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700707 const VkCommandBufferBeginInfo cmd_buf_info = {
708 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
709 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600710 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700711 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700712 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800713 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700714 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
715 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800716 };
717 const VkRenderPassBeginInfo rp_begin = {
718 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
719 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800720 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700721 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800722 .renderArea.offset.x = 0,
723 .renderArea.offset.y = 0,
724 .renderArea.extent.width = demo->width,
725 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600726 .clearValueCount = 2,
727 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700728 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800729 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600730
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600731 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Mark Young66510e52017-11-10 10:05:14 -0700732
733 if (demo->validate) {
734 // Set a name for the command buffer
735 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
736 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
737 .pNext = NULL,
738 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
739 .objectHandle = (uint64_t)cmd_buf,
740 .pObjectName = "CubeDrawCommandBuf",
741 };
742 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
743
744 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
745 label.pNext = NULL;
746 label.pLabelName = "DrawBegin";
747 label.color[0] = 0.4f;
748 label.color[1] = 0.3f;
749 label.color[2] = 0.2f;
750 label.color[3] = 0.1f;
751 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
752 }
753
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600754 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800755 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Mark Young66510e52017-11-10 10:05:14 -0700756
757 if (demo->validate) {
758 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
759 label.pNext = NULL;
760 label.pLabelName = "InsideRenderPass";
761 label.color[0] = 8.4f;
762 label.color[1] = 7.3f;
763 label.color[2] = 6.2f;
764 label.color[3] = 7.1f;
765 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
766 }
767
Karl Schultze4dc75c2016-02-02 15:37:51 -0700768 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700769 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
770 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600771 VkViewport viewport;
772 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700773 viewport.height = (float)demo->height;
774 viewport.width = (float)demo->width;
775 viewport.minDepth = (float)0.0f;
776 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700777 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600778
779 VkRect2D scissor;
780 memset(&scissor, 0, sizeof(scissor));
781 scissor.extent.width = demo->width;
782 scissor.extent.height = demo->height;
783 scissor.offset.x = 0;
784 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700785 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Mark Young66510e52017-11-10 10:05:14 -0700786
787 if (demo->validate) {
788 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
789 label.pNext = NULL;
790 label.pLabelName = "ActualDraw";
Mike Schuchardt4cf3aa42018-02-23 12:44:05 -0700791 label.color[0] = -0.4f;
792 label.color[1] = -0.3f;
793 label.color[2] = -0.2f;
794 label.color[3] = -0.1f;
Mark Young66510e52017-11-10 10:05:14 -0700795 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
796 }
797
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600798 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Mark Young66510e52017-11-10 10:05:14 -0700799 if (demo->validate) {
800 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
801 }
802
Tony Barbour98390392016-07-28 10:50:13 -0600803 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600804 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800805 vkCmdEndRenderPass(cmd_buf);
Mark Young66510e52017-11-10 10:05:14 -0700806 if (demo->validate) {
807 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
808 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600809
Tony Barbour22e06272016-09-07 16:07:56 -0600810 if (demo->separate_present_queue) {
811 // We have to transfer ownership from the graphics queue family to the
812 // present queue family to be able to present. Note that we don't have
813 // to transfer from present queue family back to graphics queue family at
814 // the start of the next frame because we don't care about the image's
815 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700816 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
817 .pNext = NULL,
818 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600819 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700820 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
821 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
822 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
823 .dstQueueFamilyIndex = demo->present_queue_family_index,
824 .image = demo->swapchain_image_resources[demo->current_buffer].image,
825 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600826
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600827 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
828 NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600829 }
Mark Young66510e52017-11-10 10:05:14 -0700830 if (demo->validate) {
831 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
832 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600833 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600834 assert(!err);
835}
836
Tony Barbour22e06272016-09-07 16:07:56 -0600837void demo_build_image_ownership_cmd(struct demo *demo, int i) {
838 VkResult U_ASSERT_ONLY err;
839
840 const VkCommandBufferBeginInfo cmd_buf_info = {
841 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
842 .pNext = NULL,
843 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
844 .pInheritanceInfo = NULL,
845 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700846 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600847 assert(!err);
848
Dave Houlton5fa47912018-02-16 11:02:26 -0700849 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
850 .pNext = NULL,
851 .srcAccessMask = 0,
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600852 .dstAccessMask = 0,
Dave Houlton5fa47912018-02-16 11:02:26 -0700853 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
854 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
855 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
856 .dstQueueFamilyIndex = demo->present_queue_family_index,
857 .image = demo->swapchain_image_resources[i].image,
858 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600859
Tony-LunarG4ba65cd2018-05-30 14:53:14 -0600860 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
861 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700862 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600863 assert(!err);
864}
865
Karl Schultze4dc75c2016-02-02 15:37:51 -0700866void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600867 mat4x4 MVP, Model, VP;
868 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600869 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600870 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600871
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600872 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600873
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700874 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600875 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700876 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600877 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600878
Dave Houlton5fa47912018-02-16 11:02:26 -0700879 err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0,
880 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600881 assert(!err);
882
Karl Schultze4dc75c2016-02-02 15:37:51 -0700883 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600884
Tony Barboura72d9492017-01-11 13:35:09 -0700885 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600886}
887
Ian Elliottd940ca22017-03-22 08:31:27 -0600888void DemoUpdateTargetIPD(struct demo *demo) {
889 // Look at what happened to previous presents, and make appropriate
890 // adjustments in timing:
891 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700892 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600893 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600894
Dave Houlton5fa47912018-02-16 11:02:26 -0700895 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600896 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600897 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700898 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600899 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700900 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600901 assert(!err);
902
903 bool early = false;
904 bool late = false;
905 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700906 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600907 if (!demo->syncd_with_actual_presents) {
908 // This is the first time that we've received an
909 // actualPresentTime for this swapchain. In order to not
910 // perceive these early frames as "late", we need to sync-up
911 // our future desiredPresentTime's with the
912 // actualPresentTime(s) that we're receiving now.
913 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600914
Ian Elliottd940ca22017-03-22 08:31:27 -0600915 // So that we don't suspect any pending presents as late,
916 // record them all as suspected-late presents:
917 demo->last_late_id = demo->next_present_id - 1;
918 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600919 demo->syncd_with_actual_presents = true;
920 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700921 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600922 demo->refresh_duration)) {
923 // This image could have been presented earlier. We don't want
924 // to decrease the target_IPD until we've seen early presents
925 // for at least two seconds.
926 if (demo->last_early_id == past[i].presentID) {
927 // We've now seen two seconds worth of early presents.
928 // Flag it as such, and reset the counter:
929 early = true;
930 demo->last_early_id = 0;
931 } else if (demo->last_early_id == 0) {
932 // This is the first early present we've seen.
933 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700934 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
935 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600936 demo->last_early_id = past[i].presentID + howManyPresents;
937 } else {
938 // We are in the midst of a set of early images,
939 // and so we won't do anything.
940 }
941 late = false;
942 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700943 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600944 // This image was presented after its desired time. Since
945 // there's a delay between calling vkQueuePresentKHR and when
946 // we get the timing data, several presents may have been late.
947 // Thus, we need to threat all of the outstanding presents as
948 // being likely late, so that we only increase the target_IPD
949 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700950 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600951 late = true;
952 // Record the last suspected-late present:
953 demo->last_late_id = demo->next_present_id - 1;
954 } else {
955 // We are in the midst of a set of likely-late images,
956 // and so we won't do anything.
957 }
958 early = false;
959 demo->last_early_id = 0;
960 } else {
961 // Since this image was not presented early or late, reset
962 // any sets of early or late presentIDs:
963 early = false;
964 late = false;
965 calibrate_next = true;
966 demo->last_early_id = 0;
967 demo->last_late_id = 0;
968 }
969 }
970
971 if (early) {
972 // Since we've seen at least two-seconds worth of presnts that
973 // could have occured earlier than desired, let's decrease the
974 // target_IPD (i.e. increase the frame rate):
975 //
976 // TODO(ianelliott): Try to calculate a better target_IPD based
977 // on the most recently-seen present (this is overly-simplistic).
978 demo->refresh_duration_multiplier--;
979 if (demo->refresh_duration_multiplier == 0) {
980 // This should never happen, but in case it does, don't
981 // try to go faster.
982 demo->refresh_duration_multiplier = 1;
983 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700984 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600985 }
986 if (late) {
987 // Since we found a new instance of a late present, we want to
988 // increase the target_IPD (i.e. decrease the frame rate):
989 //
990 // TODO(ianelliott): Try to calculate a better target_IPD based
991 // on the most recently-seen present (this is overly-simplistic).
992 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -0700993 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600994 }
995
996 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700997 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
998 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600999 }
Karl Schultza33771f2018-05-28 16:16:47 -06001000 free(past);
Ian Elliottd940ca22017-03-22 08:31:27 -06001001 }
Ian Elliottd940ca22017-03-22 08:31:27 -06001002}
1003
Karl Schultze4dc75c2016-02-02 15:37:51 -07001004static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001005 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001006
Damien Leonec336d822017-04-14 16:10:14 -07001007 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +03001008 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1009 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001010
Tony Barbour8b7c9112017-06-29 13:34:41 -06001011 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -06001012 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -07001013 err =
1014 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1015 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001016
Tony Barbour6914e6d2017-06-02 12:11:29 -06001017 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1018 // demo->swapchain is out of date (e.g. the window was resized) and
1019 // must be recreated:
1020 demo_resize(demo);
1021 } else if (err == VK_SUBOPTIMAL_KHR) {
1022 // demo->swapchain is not as optimal as it could be, but the platform's
1023 // presentation engine will still present the image correctly.
1024 break;
1025 } else {
1026 assert(!err);
1027 }
Tony Barbour8b7c9112017-06-29 13:34:41 -06001028 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001029
Tony Barbour3eafe1f2017-06-14 11:59:55 -06001030 demo_update_data_buffer(demo);
1031
Ian Elliottd940ca22017-03-22 08:31:27 -06001032 if (demo->VK_GOOGLE_display_timing_enabled) {
1033 // Look at what happened to previous presents, and make appropriate
1034 // adjustments in timing:
1035 DemoUpdateTargetIPD(demo);
1036
1037 // Note: a real application would position its geometry to that it's in
1038 // the correct locatoin for when the next image is presented. It might
1039 // also wait, so that there's less latency between any input and when
1040 // the next image is rendered/presented. This demo program is so
1041 // simple that it doesn't do either of those.
1042 }
1043
Tony Barbource7524e2016-07-28 12:01:45 -06001044 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001045 // that the image won't be rendered to until the presentation
1046 // engine has fully released ownership to the application, and it is
1047 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001048 VkPipelineStageFlags pipe_stage_flags;
1049 VkSubmitInfo submit_info;
1050 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1051 submit_info.pNext = NULL;
1052 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1053 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1054 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001055 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001056 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001057 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001058 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001059 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -07001060 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001061 assert(!err);
1062
Tony Barbour22e06272016-09-07 16:07:56 -06001063 if (demo->separate_present_queue) {
1064 // If we are using separate queues, change image ownership to the
1065 // present queue before presenting, waiting for the draw complete
1066 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001067 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001068 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1069 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001070 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001071 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001072 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001073 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001074 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001075 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1076 assert(!err);
1077 }
1078
1079 // If we are using separate queues we have to wait for image ownership,
1080 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001081 VkPresentInfoKHR present = {
1082 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001083 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001084 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -07001085 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1086 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001087 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001088 .pSwapchains = &demo->swapchain,
1089 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001090 };
1091
Ian Elliott53622a72017-03-28 11:06:33 -06001092 if (demo->VK_KHR_incremental_present_enabled) {
1093 // If using VK_KHR_incremental_present, we provide a hint of the region
1094 // that contains changed content relative to the previously-presented
1095 // image. The implementation can use this hint in order to save
1096 // work/power (by only copying the region in the hint). The
1097 // implementation is free to ignore the hint though, and so we must
1098 // ensure that the entire image has the correctly-drawn content.
1099 uint32_t eighthOfWidth = demo->width / 8;
1100 uint32_t eighthOfHeight = demo->height / 8;
1101 VkRectLayerKHR rect = {
1102 .offset.x = eighthOfWidth,
1103 .offset.y = eighthOfHeight,
1104 .extent.width = eighthOfWidth * 6,
1105 .extent.height = eighthOfHeight * 6,
1106 .layer = 0,
1107 };
1108 VkPresentRegionKHR region = {
1109 .rectangleCount = 1,
1110 .pRectangles = &rect,
1111 };
1112 VkPresentRegionsKHR regions = {
1113 .sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1114 .pNext = present.pNext,
1115 .swapchainCount = present.swapchainCount,
1116 .pRegions = &region,
1117 };
1118 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001119 }
1120
Ian Elliottd940ca22017-03-22 08:31:27 -06001121 if (demo->VK_GOOGLE_display_timing_enabled) {
1122 VkPresentTimeGOOGLE ptime;
1123 if (demo->prev_desired_present_time == 0) {
1124 // This must be the first present for this swapchain.
1125 //
1126 // We don't know where we are relative to the presentation engine's
1127 // display's refresh cycle. We also don't know how long rendering
1128 // takes. Let's make a grossly-simplified assumption that the
1129 // desiredPresentTime should be half way between now and
1130 // now+target_IPD. We will adjust over time.
1131 uint64_t curtime = getTimeInNanoseconds();
1132 if (curtime == 0) {
1133 // Since we didn't find out the current time, don't give a
1134 // desiredPresentTime:
1135 ptime.desiredPresentTime = 0;
1136 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001137 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1138 }
1139 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001140 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001141 }
1142 ptime.presentID = demo->next_present_id++;
1143 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001144
1145 VkPresentTimesInfoGOOGLE present_time = {
1146 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1147 .pNext = present.pNext,
1148 .swapchainCount = present.swapchainCount,
1149 .pTimes = &ptime,
1150 };
1151 if (demo->VK_GOOGLE_display_timing_enabled) {
1152 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001153 }
1154 }
1155
Tony Barboura2a67812016-07-18 13:21:06 -06001156 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001157 demo->frame_index += 1;
1158 demo->frame_index %= FRAME_LAG;
1159
Ian Elliottcf074d32015-10-16 18:02:43 -06001160 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1161 // demo->swapchain is out of date (e.g. the window was resized) and
1162 // must be recreated:
1163 demo_resize(demo);
1164 } else if (err == VK_SUBOPTIMAL_KHR) {
1165 // demo->swapchain is not as optimal as it could be, but the platform's
1166 // presentation engine will still present the image correctly.
1167 } else {
1168 assert(!err);
1169 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001170}
1171
Karl Schultze4dc75c2016-02-02 15:37:51 -07001172static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001173 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001174 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001175
Ian Elliottb08e0d92015-11-20 11:55:46 -07001176 // Check the surface capabilities and formats
1177 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001178 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001179 assert(!err);
1180
Ian Elliott8528eff2015-08-07 15:56:59 -06001181 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001182 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001183 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001184 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001185 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001186 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001187 assert(!err);
1188
Ian Elliotta0781972015-08-21 15:09:33 -06001189 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001190 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1191 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1192 // If the surface size is undefined, the size is set to the size
1193 // of the images requested, which must fit within the minimum and
1194 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001195 swapchainExtent.width = demo->width;
1196 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001197
1198 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1199 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1200 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1201 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1202 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001203
Ian Elliottcf7f7482016-08-19 03:46:58 -06001204 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1205 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1206 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1207 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1208 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001209 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001210 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001211 swapchainExtent = surfCapabilities.currentExtent;
1212 demo->width = surfCapabilities.currentExtent.width;
1213 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001214 }
1215
Jeremy Kniager602e4182018-01-19 10:52:34 -07001216 if (demo->width == 0 || demo->height == 0) {
1217 demo->is_minimized = true;
1218 return;
1219 } else {
1220 demo->is_minimized = false;
1221 }
1222
Tony Barbour66a56c32016-09-21 13:10:56 -06001223 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001224 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001225 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001226
Ian Elliottb18fdde2016-10-03 12:11:12 -06001227 // There are times when you may wish to use another present mode. The
1228 // following code shows how to select them, and the comments provide some
1229 // reasons you may wish to use them.
1230 //
1231 // It should be noted that Vulkan 1.0 doesn't provide a method for
1232 // synchronizing rendering with the presentation engine's display. There
1233 // is a method provided for throttling rendering with the display, but
1234 // there are some presentation engines for which this method will not work.
1235 // If an application doesn't throttle its rendering, and if it renders much
1236 // faster than the refresh rate of the display, this can waste power on
1237 // mobile devices. That is because power is being spent rendering images
1238 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001239
Ian Elliottb18fdde2016-10-03 12:11:12 -06001240 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1241 // tearing, or have some way of synchronizing their rendering with the
1242 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001243 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1244 // generally render a new presentable image every refresh cycle, but are
1245 // occasionally early. In this case, the application wants the new image
1246 // to be displayed instead of the previously-queued-for-presentation image
1247 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001248 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1249 // render a new presentable image every refresh cycle, but are occasionally
1250 // late. In this case (perhaps because of stuttering/latency concerns),
1251 // the application wants the late image to be immediately displayed, even
1252 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001253
Dave Houlton5fa47912018-02-16 11:02:26 -07001254 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001255 for (size_t i = 0; i < presentModeCount; ++i) {
1256 if (presentModes[i] == demo->presentMode) {
1257 swapchainPresentMode = demo->presentMode;
1258 break;
1259 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001260 }
1261 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001262 if (swapchainPresentMode != demo->presentMode) {
1263 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1264 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001265
Tony Barbour84376fe2017-01-06 15:54:22 -07001266 // Determine the number of VkImages to use in the swap chain.
1267 // Application desires to acquire 3 images at a time for triple
1268 // buffering
1269 uint32_t desiredNumOfSwapchainImages = 3;
1270 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1271 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1272 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001273 // If maxImageCount is 0, we can ask for as many images as we want;
1274 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001275 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001276 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001277 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001278 }
1279
Tony Barbour9fd6d352015-09-16 15:01:32 -06001280 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001281 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001282 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001283 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001284 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001285 }
1286
Tony Barbour820b55b2017-03-14 08:55:46 -06001287 // Find a supported composite alpha mode - one of these is guaranteed to be set
1288 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1289 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1290 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1291 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1292 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1293 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1294 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001295 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001296 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1297 compositeAlpha = compositeAlphaFlags[i];
1298 break;
1299 }
1300 }
1301
Tony Barboura2a67812016-07-18 13:21:06 -06001302 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001303 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001304 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001305 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001306 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001307 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001308 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001309 .imageExtent =
1310 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001311 .width = swapchainExtent.width,
1312 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001313 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001314 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001315 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001316 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001317 .imageArrayLayers = 1,
1318 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1319 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001320 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001321 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001322 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001323 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001324 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001325 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001326 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001327 assert(!err);
1328
Ian Elliottcf074d32015-10-16 18:02:43 -06001329 // If we just re-created an existing swapchain, we should destroy the old
1330 // swapchain at this point.
1331 // Note: destroying the swapchain also cleans up all its associated
1332 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001333 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001334 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001335 }
1336
Dave Houlton5fa47912018-02-16 11:02:26 -07001337 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001338 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001339
Dave Houlton5fa47912018-02-16 11:02:26 -07001340 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001341 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001342 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001343 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001344
Dave Houlton5fa47912018-02-16 11:02:26 -07001345 demo->swapchain_image_resources =
1346 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001347 assert(demo->swapchain_image_resources);
1348
Ian Elliotta0781972015-08-21 15:09:33 -06001349 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001350 VkImageViewCreateInfo color_image_view = {
1351 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001352 .pNext = NULL,
1353 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001354 .components =
1355 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001356 .r = VK_COMPONENT_SWIZZLE_R,
1357 .g = VK_COMPONENT_SWIZZLE_G,
1358 .b = VK_COMPONENT_SWIZZLE_B,
1359 .a = VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001360 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001361 .subresourceRange =
1362 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001363 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1364 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365 };
1366
Tony Barboura72d9492017-01-11 13:35:09 -07001367 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001368
Tony Barboura72d9492017-01-11 13:35:09 -07001369 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001370
Dave Houlton5fa47912018-02-16 11:02:26 -07001371 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001372 assert(!err);
1373 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001374
Ian Elliottd940ca22017-03-22 08:31:27 -06001375 if (demo->VK_GOOGLE_display_timing_enabled) {
1376 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001377 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001378 assert(!err);
1379 demo->refresh_duration = rc_dur.refreshDuration;
1380
1381 demo->syncd_with_actual_presents = false;
1382 // Initially target 1X the refresh duration:
1383 demo->target_IPD = demo->refresh_duration;
1384 demo->refresh_duration_multiplier = 1;
1385 demo->prev_desired_present_time = 0;
1386 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001387 }
1388
Mark Young04fd3d82016-01-25 14:43:50 -07001389 if (NULL != presentModes) {
1390 free(presentModes);
1391 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392}
1393
Karl Schultze4dc75c2016-02-02 15:37:51 -07001394static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001395 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001396 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001397 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001398 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001399 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001401 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001403 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001404 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001405 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001406 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407 .flags = 0,
1408 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001409
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001410 VkImageViewCreateInfo view = {
1411 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001413 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001414 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001415 .subresourceRange =
1416 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001417 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001418 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001420
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001421 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001422 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001423 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424
1425 demo->depth.format = depth_format;
1426
1427 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001428 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429 assert(!err);
1430
Karl Schultze4dc75c2016-02-02 15:37:51 -07001431 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001432 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001433
Chia-I Wuf48700b2015-11-10 16:21:09 +08001434 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001435 demo->depth.mem_alloc.pNext = NULL;
1436 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1437 demo->depth.mem_alloc.memoryTypeIndex = 0;
1438
Dave Houlton5fa47912018-02-16 11:02:26 -07001439 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001440 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001441 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001442
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001443 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001444 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001445 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001446
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001447 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001448 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001449 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001450
1451 /* create image view */
1452 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001453 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001454 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001455}
1456
Karl Schultzb7940402018-05-29 13:09:22 -06001457/* Convert ppm image data from header file into RGBA texture image */
1458#include "lunarg.ppm.h"
Dave Houlton5fa47912018-02-16 11:02:26 -07001459bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Karl Schultzb7940402018-05-29 13:09:22 -06001460 (void)filename;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001461 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001462 cPtr = (char *)lunarg_ppm;
1463 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001464 return false;
1465 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001466 while (strncmp(cPtr++, "\n", 1))
1467 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001468 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001469 if (rgba_data == NULL) {
1470 return true;
1471 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001472 while (strncmp(cPtr++, "\n", 1))
1473 ;
1474 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001475 return false;
1476 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001477 while (strncmp(cPtr++, "\n", 1))
1478 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001479 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 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001489 return true;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001490}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001491
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001492static void demo_prepare_texture_buffer(struct demo *demo, const char *filename, struct texture_object *tex_obj) {
1493 int32_t tex_width;
1494 int32_t tex_height;
1495 VkResult U_ASSERT_ONLY err;
1496 bool U_ASSERT_ONLY pass;
1497
1498 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
1499 ERR_EXIT("Failed to load textures", "Load Texture Failure");
1500 }
1501
1502 tex_obj->tex_width = tex_width;
1503 tex_obj->tex_height = tex_height;
1504
1505 const VkBufferCreateInfo buffer_create_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
1506 .pNext = NULL,
1507 .flags = 0,
1508 .size = tex_width * tex_height * 4,
1509 .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1510 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1511 .queueFamilyIndexCount = 0,
1512 .pQueueFamilyIndices = NULL};
1513
1514 err = vkCreateBuffer(demo->device, &buffer_create_info, NULL, &tex_obj->buffer);
1515 assert(!err);
1516
1517 VkMemoryRequirements mem_reqs;
1518 vkGetBufferMemoryRequirements(demo->device, tex_obj->buffer, &mem_reqs);
1519
1520 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1521 tex_obj->mem_alloc.pNext = NULL;
1522 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1523 tex_obj->mem_alloc.memoryTypeIndex = 0;
1524
1525 VkFlags requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1526 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, requirements, &tex_obj->mem_alloc.memoryTypeIndex);
1527 assert(pass);
1528
1529 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
1530 assert(!err);
1531
1532 /* bind memory */
1533 err = vkBindBufferMemory(demo->device, tex_obj->buffer, tex_obj->mem, 0);
1534 assert(!err);
1535
1536 VkSubresourceLayout layout;
1537 memset(&layout, 0, sizeof(layout));
1538 layout.rowPitch = tex_width * 4;
1539
1540 void *data;
1541 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
1542 assert(!err);
1543
1544 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1545 fprintf(stderr, "Error loading texture: %s\n", filename);
1546 }
1547
1548 vkUnmapMemory(demo->device, tex_obj->mem);
1549}
1550
Dave Houlton5fa47912018-02-16 11:02:26 -07001551static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1552 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001553 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001554 int32_t tex_width;
1555 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001556 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001557 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001558
Karl Schultze4dc75c2016-02-02 15:37:51 -07001559 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001560 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001561 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001562
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001563 tex_obj->tex_width = tex_width;
1564 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001565
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001566 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001567 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001568 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001569 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001570 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001571 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001572 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001573 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001574 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001575 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001576 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001577 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001578 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001579 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001580
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001581 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001582
Dave Houlton5fa47912018-02-16 11:02:26 -07001583 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001584 assert(!err);
1585
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001586 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001587
Chia-I Wuf48700b2015-11-10 16:21:09 +08001588 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001589 tex_obj->mem_alloc.pNext = NULL;
1590 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1591 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001592
Dave Houlton5fa47912018-02-16 11:02:26 -07001593 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001594 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001595
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001596 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001597 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001598 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001599
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001600 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001601 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001602 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001603
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001604 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001605 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001606 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001607 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001608 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001609 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001610 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001611 void *data;
1612
Dave Houlton5fa47912018-02-16 11:02:26 -07001613 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001614
Dave Houlton5fa47912018-02-16 11:02:26 -07001615 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001616 assert(!err);
1617
1618 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1619 fprintf(stderr, "Error loading texture: %s\n", filename);
1620 }
1621
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001622 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001623 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001624
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001625 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001626}
1627
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001628static void demo_destroy_texture(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001629 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001630 vkFreeMemory(demo->device, tex_objs->mem, NULL);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001631 if (tex_objs->image) vkDestroyImage(demo->device, tex_objs->image, NULL);
1632 if (tex_objs->buffer) vkDestroyBuffer(demo->device, tex_objs->buffer, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001633}
1634
Karl Schultze4dc75c2016-02-02 15:37:51 -07001635static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001636 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001637 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001638 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001639
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001640 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001641
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001642 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001643 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001644
Dave Houlton5fa47912018-02-16 11:02:26 -07001645 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001646 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001647 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1648 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001649 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1650 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001651 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1652 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001653 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001654 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001655 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001656 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001657
Tony Barbour16156cb2016-10-19 14:15:49 -06001658 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001659 demo_prepare_texture_buffer(demo, tex_files[i], &demo->staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001660
Dave Houlton5fa47912018-02-16 11:02:26 -07001661 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1662 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1663 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001664
Dave Houlton5fa47912018-02-16 11:02:26 -07001665 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1666 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001667 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001668
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001669 VkBufferImageCopy copy_region = {
1670 .bufferOffset = 0,
1671 .bufferRowLength = demo->staging_texture.tex_width,
1672 .bufferImageHeight = demo->staging_texture.tex_height,
1673 .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1674 .imageOffset = {0, 0, 0},
1675 .imageExtent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001676 };
Tony-LunarGbc9fc052018-09-21 13:47:06 -06001677
1678 vkCmdCopyBufferToImage(demo->cmd, demo->staging_texture.buffer, demo->textures[i].image,
1679 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001680
Dave Houlton5fa47912018-02-16 11:02:26 -07001681 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1682 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001683 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001684
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001685 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001686 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1687 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001688 }
1689
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001690 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001691 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001692 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001693 .magFilter = VK_FILTER_NEAREST,
1694 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001695 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001696 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1697 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1698 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001699 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001700 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001701 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001702 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001703 .minLod = 0.0f,
1704 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001705 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001706 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001708
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001709 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001710 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001711 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001712 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001713 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001714 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001715 .components =
1716 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001717 VK_COMPONENT_SWIZZLE_R,
1718 VK_COMPONENT_SWIZZLE_G,
1719 VK_COMPONENT_SWIZZLE_B,
1720 VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001721 },
1722 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001723 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001724 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001725
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001726 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001727 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001728 assert(!err);
1729
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001730 /* create image view */
1731 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001732 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733 assert(!err);
1734 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001735}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001736
Tony Barboura72d9492017-01-11 13:35:09 -07001737void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001738 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001739 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001740 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001741 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001742 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001743 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001744 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001745 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001746
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001747 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001748 mat4x4_mul(MVP, VP, demo->model_matrix);
1749 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001750 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001751
Karl Schultza2615462017-01-20 13:19:20 -07001752 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001753 data.position[i][0] = g_vertex_buffer_data[i * 3];
1754 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1755 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001756 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001757 data.attr[i][0] = g_uv_buffer_data[2 * i];
1758 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001759 data.attr[i][2] = 0;
1760 data.attr[i][3] = 0;
1761 }
1762
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001763 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001764 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001765 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001766 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001767
Tony Barboura72d9492017-01-11 13:35:09 -07001768 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001769 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001770 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001771
Dave Houlton5fa47912018-02-16 11:02:26 -07001772 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001773
Tony Barboura72d9492017-01-11 13:35:09 -07001774 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1775 mem_alloc.pNext = NULL;
1776 mem_alloc.allocationSize = mem_reqs.size;
1777 mem_alloc.memoryTypeIndex = 0;
1778
Dave Houlton5fa47912018-02-16 11:02:26 -07001779 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1780 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1781 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001782 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001783
Dave Houlton5fa47912018-02-16 11:02:26 -07001784 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001785 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001786
Dave Houlton5fa47912018-02-16 11:02:26 -07001787 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 -07001788 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001789
Tony Barboura72d9492017-01-11 13:35:09 -07001790 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001791
Tony Barboura72d9492017-01-11 13:35:09 -07001792 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001793
Tony Barboura72d9492017-01-11 13:35:09 -07001794 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001795 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001796 assert(!err);
1797 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001798}
1799
Karl Schultze4dc75c2016-02-02 15:37:51 -07001800static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001801 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001802 [0] =
1803 {
1804 .binding = 0,
1805 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1806 .descriptorCount = 1,
1807 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1808 .pImmutableSamplers = NULL,
1809 },
1810 [1] =
1811 {
1812 .binding = 1,
1813 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1814 .descriptorCount = DEMO_TEXTURE_COUNT,
1815 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1816 .pImmutableSamplers = NULL,
1817 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001818 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001819 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001820 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001821 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001822 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001823 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001824 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001825 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001826
Dave Houlton5fa47912018-02-16 11:02:26 -07001827 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001828 assert(!err);
1829
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001830 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001831 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1832 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001833 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001834 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001835 };
1836
Dave Houlton5fa47912018-02-16 11:02:26 -07001837 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001839}
1840
Karl Schultze4dc75c2016-02-02 15:37:51 -07001841static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001842 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1843 // because at the start of the renderpass, we don't care about their contents.
1844 // At the start of the subpass, the color attachment's layout will be transitioned
1845 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1846 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1847 // the renderpass, the color attachment's layout will be transitioned to
1848 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1849 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001850 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001851 [0] =
1852 {
1853 .format = demo->format,
1854 .flags = 0,
1855 .samples = VK_SAMPLE_COUNT_1_BIT,
1856 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1857 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1858 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1859 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1860 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1861 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1862 },
1863 [1] =
1864 {
1865 .format = demo->depth.format,
1866 .flags = 0,
1867 .samples = VK_SAMPLE_COUNT_1_BIT,
1868 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1869 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1870 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1871 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1872 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1873 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1874 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001875 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001876 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001877 .attachment = 0,
1878 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001879 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001880 const VkAttachmentReference depth_reference = {
1881 .attachment = 1,
1882 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1883 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001884 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001885 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1886 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001887 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001888 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001889 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001890 .pColorAttachments = &color_reference,
1891 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001892 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001893 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001894 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001895 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001896 const VkRenderPassCreateInfo rp_info = {
1897 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1898 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001899 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001900 .attachmentCount = 2,
1901 .pAttachments = attachments,
1902 .subpassCount = 1,
1903 .pSubpasses = &subpass,
1904 .dependencyCount = 0,
1905 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001906 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001907 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001908
Chia-I Wu63636062015-10-26 21:10:41 +08001909 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001910 assert(!err);
1911}
1912
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001913static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001914 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001915 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001916 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001917
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001918 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1919 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001920 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001921 moduleCreateInfo.codeSize = size;
1922 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001923
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001924 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1925 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001926
1927 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001928}
1929
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001930static void demo_prepare_vs(struct demo *demo) {
1931 const uint32_t vs_code[] = {
1932#include "cube.vert.inc"
1933 };
1934 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001935}
1936
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001937static void demo_prepare_fs(struct demo *demo) {
1938 const uint32_t fs_code[] = {
1939#include "cube.frag.inc"
1940 };
1941 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001942}
1943
Karl Schultze4dc75c2016-02-02 15:37:51 -07001944static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001945 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001946 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001947 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001948 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001949 VkPipelineRasterizationStateCreateInfo rs;
1950 VkPipelineColorBlendStateCreateInfo cb;
1951 VkPipelineDepthStencilStateCreateInfo ds;
1952 VkPipelineViewportStateCreateInfo vp;
1953 VkPipelineMultisampleStateCreateInfo ms;
1954 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1955 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001956 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001957
Piers Daniellba839d12015-09-29 13:01:09 -06001958 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1959 memset(&dynamicState, 0, sizeof dynamicState);
1960 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1961 dynamicState.pDynamicStates = dynamicStateEnables;
1962
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001963 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001964 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001965 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001966
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001967 memset(&vi, 0, sizeof(vi));
1968 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1969
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001970 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001971 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001972 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001973
1974 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001975 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1976 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001977 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001978 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001979 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001980 rs.rasterizerDiscardEnable = VK_FALSE;
1981 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001982 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001983
1984 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001985 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1986 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001987 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001988 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001989 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001990 cb.attachmentCount = 1;
1991 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001992
Tony Barbour29645d02015-01-16 14:27:35 -07001993 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001994 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001995 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001996 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001997 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001998 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001999
2000 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002001 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002002 ds.depthTestEnable = VK_TRUE;
2003 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002004 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002005 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002006 ds.back.failOp = VK_STENCIL_OP_KEEP;
2007 ds.back.passOp = VK_STENCIL_OP_KEEP;
2008 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002009 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002010 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002011
Tony Barbour29645d02015-01-16 14:27:35 -07002012 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002013 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002014 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002015 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002016
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002017 demo_prepare_vs(demo);
2018 demo_prepare_fs(demo);
2019
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002020 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002021 VkPipelineShaderStageCreateInfo shaderStages[2];
2022 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2023
Karl Schultze4dc75c2016-02-02 15:37:51 -07002024 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2025 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002026 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002027 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002028
Karl Schultze4dc75c2016-02-02 15:37:51 -07002029 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2030 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002031 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002032 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002033
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002034 memset(&pipelineCache, 0, sizeof(pipelineCache));
2035 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002036
Dave Houlton5fa47912018-02-16 11:02:26 -07002037 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002038 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002039
Karl Schultze4dc75c2016-02-02 15:37:51 -07002040 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002041 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002042 pipeline.pRasterizationState = &rs;
2043 pipeline.pColorBlendState = &cb;
2044 pipeline.pMultisampleState = &ms;
2045 pipeline.pViewportState = &vp;
2046 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002047 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002048 pipeline.pStages = shaderStages;
2049 pipeline.renderPass = demo->render_pass;
2050 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002051
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002052 pipeline.renderPass = demo->render_pass;
2053
Dave Houlton5fa47912018-02-16 11:02:26 -07002054 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002055 assert(!err);
2056
Chia-I Wu63636062015-10-26 21:10:41 +08002057 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2058 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002059}
2060
Karl Schultze4dc75c2016-02-02 15:37:51 -07002061static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002062 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07002063 [0] =
2064 {
2065 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2066 .descriptorCount = demo->swapchainImageCount,
2067 },
2068 [1] =
2069 {
2070 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2071 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2072 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002073 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002074 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002075 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002076 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002077 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002078 .poolSizeCount = 2,
2079 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002080 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002081 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002082
Dave Houlton5fa47912018-02-16 11:02:26 -07002083 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002084 assert(!err);
2085}
2086
Karl Schultze4dc75c2016-02-02 15:37:51 -07002087static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002088 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002089 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002090 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002091
Dave Houlton5fa47912018-02-16 11:02:26 -07002092 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2093 .pNext = NULL,
2094 .descriptorPool = demo->desc_pool,
2095 .descriptorSetCount = 1,
2096 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002097
2098 VkDescriptorBufferInfo buffer_info;
2099 buffer_info.offset = 0;
2100 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002101
Chia-I Wuae721ba2015-05-25 16:27:55 +08002102 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002103 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002104 tex_descs[i].sampler = demo->textures[i].sampler;
2105 tex_descs[i].imageView = demo->textures[i].view;
Karl Schultze88bc842018-09-11 16:23:14 -06002106 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002107 }
2108
2109 memset(&writes, 0, sizeof(writes));
2110
2111 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002112 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002113 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002114 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002115
2116 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002117 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002118 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002119 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002120 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002121
Tony Barboura72d9492017-01-11 13:35:09 -07002122 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2123 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2124 assert(!err);
2125 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2126 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2127 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2128 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2129 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002130}
2131
Karl Schultze4dc75c2016-02-02 15:37:51 -07002132static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002133 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002134 attachments[1] = demo->depth.view;
2135
Chia-I Wuf973e322015-07-08 13:34:24 +08002136 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002137 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2138 .pNext = NULL,
2139 .renderPass = demo->render_pass,
2140 .attachmentCount = 2,
2141 .pAttachments = attachments,
2142 .width = demo->width,
2143 .height = demo->height,
2144 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002145 };
2146 VkResult U_ASSERT_ONLY err;
2147 uint32_t i;
2148
Tony Barbourd8e504f2015-10-08 14:26:24 -06002149 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002150 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002151 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002152 assert(!err);
2153 }
2154}
2155
Karl Schultze4dc75c2016-02-02 15:37:51 -07002156static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002157 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002158 if (demo->cmd_pool == VK_NULL_HANDLE) {
2159 const VkCommandPoolCreateInfo cmd_pool_info = {
2160 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2161 .pNext = NULL,
2162 .queueFamilyIndex = demo->graphics_queue_family_index,
2163 .flags = 0,
2164 };
2165 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2166 assert(!err);
2167 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002168
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002169 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002170 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002171 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002172 .commandPool = demo->cmd_pool,
2173 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002174 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002175 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002176 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2177 assert(!err);
2178 VkCommandBufferBeginInfo cmd_buf_info = {
2179 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2180 .pNext = NULL,
2181 .flags = 0,
2182 .pInheritanceInfo = NULL,
2183 };
2184 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2185 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002186
2187 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002188
2189 if (demo->is_minimized) {
2190 demo->prepared = false;
2191 return;
2192 }
2193
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002194 demo_prepare_depth(demo);
2195 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002196 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002197
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002198 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002199 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002200 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002201
Ian Elliotta0781972015-08-21 15:09:33 -06002202 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002203 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002204 assert(!err);
2205 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002206
Tony Barbour22e06272016-09-07 16:07:56 -06002207 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002208 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002209 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2210 .pNext = NULL,
2211 .queueFamilyIndex = demo->present_queue_family_index,
2212 .flags = 0,
2213 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002214 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002215 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002216 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002217 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2218 .pNext = NULL,
2219 .commandPool = demo->present_cmd_pool,
2220 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2221 .commandBufferCount = 1,
2222 };
2223 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002224 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2225 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002226 assert(!err);
2227 demo_build_image_ownership_cmd(demo, i);
2228 }
2229 }
2230
Chia-I Wu63ea9262015-03-26 13:14:16 +08002231 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002232 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002233
Chia-I Wuf973e322015-07-08 13:34:24 +08002234 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002235
Ian Elliotta0781972015-08-21 15:09:33 -06002236 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002237 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002238 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002239 }
2240
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002241 /*
2242 * Prepare functions above may generate pipeline commands
2243 * that need to be flushed before beginning the render loop.
2244 */
2245 demo_flush_init_cmd(demo);
Tony-LunarGbc9fc052018-09-21 13:47:06 -06002246 if (demo->staging_texture.buffer) {
2247 demo_destroy_texture(demo, &demo->staging_texture);
Tony Barbour16156cb2016-10-19 14:15:49 -06002248 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002249
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002250 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002251 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002252}
2253
Karl Schultze4dc75c2016-02-02 15:37:51 -07002254static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002255 uint32_t i;
2256
2257 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002258 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002259
Tony Barbour66a56c32016-09-21 13:10:56 -06002260 // Wait for fences from present operations
2261 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002262 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002263 vkDestroyFence(demo->device, demo->fences[i], NULL);
2264 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2265 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2266 if (demo->separate_present_queue) {
2267 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2268 }
2269 }
2270
Jeremy Kniager602e4182018-01-19 10:52:34 -07002271 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2272 if (!demo->is_minimized) {
2273 for (i = 0; i < demo->swapchainImageCount; i++) {
2274 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2275 }
2276 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002277
Jeremy Kniager602e4182018-01-19 10:52:34 -07002278 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2279 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2280 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2281 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2282 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002283
Jeremy Kniager602e4182018-01-19 10:52:34 -07002284 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2285 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2286 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2287 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2288 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2289 }
2290 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002291
Jeremy Kniager602e4182018-01-19 10:52:34 -07002292 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2293 vkDestroyImage(demo->device, demo->depth.image, NULL);
2294 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002295
Jeremy Kniager602e4182018-01-19 10:52:34 -07002296 for (i = 0; i < demo->swapchainImageCount; i++) {
2297 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2298 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2299 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2300 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2301 }
2302 free(demo->swapchain_image_resources);
2303 free(demo->queue_props);
2304 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002305
Jeremy Kniager602e4182018-01-19 10:52:34 -07002306 if (demo->separate_present_queue) {
2307 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2308 }
Tony Barbour22e06272016-09-07 16:07:56 -06002309 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002310 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002311 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002312 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07002313 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002314 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002315 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002316
Tony Barbour153cb062016-12-07 13:43:36 -07002317#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002318 XDestroyWindow(demo->display, demo->xlib_window);
2319 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002320#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002321 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002322 xcb_disconnect(demo->connection);
2323 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002324#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002325 wl_keyboard_destroy(demo->keyboard);
2326 wl_pointer_destroy(demo->pointer);
2327 wl_seat_destroy(demo->seat);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002328 wl_shell_surface_destroy(demo->shell_surface);
2329 wl_surface_destroy(demo->window);
2330 wl_shell_destroy(demo->shell);
2331 wl_compositor_destroy(demo->compositor);
2332 wl_registry_destroy(demo->registry);
2333 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002334#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002335#endif
Karl Schultz86429112017-06-20 11:27:59 -06002336
2337 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002338}
2339
Karl Schultze4dc75c2016-02-02 15:37:51 -07002340static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002341 uint32_t i;
2342
Mike Stroyanbb60b382015-10-28 09:46:57 -06002343 // Don't react to resize until after first initialization.
2344 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002345 if (demo->is_minimized) {
2346 demo_prepare(demo);
2347 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002348 return;
2349 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002350 // In order to properly resize the window, we must re-create the swapchain
2351 // AND redo the command buffers, etc.
2352 //
2353 // First, perform part of the demo_cleanup() function:
2354 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002355 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002356
2357 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002358 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002359 }
Chia-I Wu63636062015-10-26 21:10:41 +08002360 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002361
Chia-I Wu63636062015-10-26 21:10:41 +08002362 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2363 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2364 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2365 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2366 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002367
2368 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002369 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2370 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2371 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2372 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002373 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002374
Chia-I Wu63636062015-10-26 21:10:41 +08002375 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2376 vkDestroyImage(demo->device, demo->depth.image, NULL);
2377 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002378
Ian Elliottcf074d32015-10-16 18:02:43 -06002379 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002380 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002381 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002382 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2383 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002384 }
Chia-I Wu63636062015-10-26 21:10:41 +08002385 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002386 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002387 if (demo->separate_present_queue) {
2388 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2389 }
Tony Barboura72d9492017-01-11 13:35:09 -07002390 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002391
Ian Elliottcf074d32015-10-16 18:02:43 -06002392 // Second, re-perform the demo_prepare() function, which will re-create the
2393 // swapchain:
2394 demo_prepare(demo);
2395}
2396
David Pinedo2bb7c932015-06-18 17:03:14 -06002397// On MS-Windows, make this a global, so it's available to WndProc()
2398struct demo demo;
2399
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002400#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002401static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002402 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002403
Ian Elliott639ca472015-04-16 15:23:05 -06002404 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002405 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002406 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002407 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002408 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002409}
Ian Elliott639ca472015-04-16 15:23:05 -06002410
2411// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002412LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2413 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002414 case WM_CLOSE:
2415 PostQuitMessage(validation_error);
2416 break;
2417 case WM_PAINT:
2418 // The validation callback calls MessageBox which can generate paint
2419 // events - don't make more Vulkan calls if we got here from the
2420 // callback
2421 if (!in_callback) {
2422 demo_run(&demo);
2423 }
2424 break;
2425 case WM_GETMINMAXINFO: // set window's minimum size
2426 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2427 return 0;
2428 case WM_SIZE:
2429 // Resize the application to the new window size, except when
2430 // it was minimized. Vulkan doesn't support images or swapchains
2431 // with width=0 and height=0.
2432 if (wParam != SIZE_MINIMIZED) {
2433 demo.width = lParam & 0xffff;
2434 demo.height = (lParam & 0xffff0000) >> 16;
2435 demo_resize(&demo);
2436 }
2437 break;
2438 default:
2439 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002440 }
2441 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2442}
2443
Karl Schultze4dc75c2016-02-02 15:37:51 -07002444static void demo_create_window(struct demo *demo) {
2445 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002446
2447 // Initialize the window class structure:
2448 win_class.cbSize = sizeof(WNDCLASSEX);
2449 win_class.style = CS_HREDRAW | CS_VREDRAW;
2450 win_class.lpfnWndProc = WndProc;
2451 win_class.cbClsExtra = 0;
2452 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002453 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002454 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2455 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2456 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2457 win_class.lpszMenuName = NULL;
2458 win_class.lpszClassName = demo->name;
2459 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2460 // Register window class:
2461 if (!RegisterClassEx(&win_class)) {
2462 // It didn't work, so try to give a useful error:
2463 printf("Unexpected error trying to start the application!\n");
2464 fflush(stdout);
2465 exit(1);
2466 }
2467 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002468 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002469 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002470 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002471 demo->name, // class name
2472 demo->name, // app name
2473 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002474 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002475 100, 100, // x/y coords
2476 wr.right - wr.left, // width
2477 wr.bottom - wr.top, // height
2478 NULL, // handle to parent
2479 NULL, // handle to menu
2480 demo->connection, // hInstance
2481 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002482 if (!demo->window) {
2483 // It didn't work, so try to give a useful error:
2484 printf("Cannot create a window in which to draw!\n");
2485 fflush(stdout);
2486 exit(1);
2487 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002488 // Window client area size must be at least 1 pixel high, to prevent crash.
2489 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002490 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002491}
Tony Barbour8295ac42016-08-08 11:04:17 -06002492#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002493static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002494 const char *display_envar = getenv("DISPLAY");
2495 if (display_envar == NULL || display_envar[0] == '\0') {
2496 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2497 fflush(stdout);
2498 exit(1);
2499 }
Tony Barbour2593b182016-04-19 10:57:58 -06002500
Tony Barbouree9cd372017-01-31 16:09:58 -07002501 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002502 demo->display = XOpenDisplay(NULL);
2503 long visualMask = VisualScreenMask;
2504 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002505 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002506 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002507 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002508
Dave Houlton5fa47912018-02-16 11:02:26 -07002509 Colormap colormap =
2510 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002511
Dave Houlton5fa47912018-02-16 11:02:26 -07002512 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002513 windowAttributes.colormap = colormap;
2514 windowAttributes.background_pixel = 0xFFFFFFFF;
2515 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002516 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002517
Dave Houlton5fa47912018-02-16 11:02:26 -07002518 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2519 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2520 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002521
2522 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2523 XMapWindow(demo->display, demo->xlib_window);
2524 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002525 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002526}
2527static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002528 switch (event->type) {
2529 case ClientMessage:
2530 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002531 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002532 case KeyPress:
2533 switch (event->xkey.keycode) {
2534 case 0x9: // Escape
2535 demo->quit = true;
2536 break;
2537 case 0x71: // left arrow key
2538 demo->spin_angle -= demo->spin_increment;
2539 break;
2540 case 0x72: // right arrow key
2541 demo->spin_angle += demo->spin_increment;
2542 break;
2543 case 0x41: // space bar
2544 demo->pause = !demo->pause;
2545 break;
2546 }
Tony Barbour2593b182016-04-19 10:57:58 -06002547 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002548 case ConfigureNotify:
2549 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2550 demo->width = event->xconfigure.width;
2551 demo->height = event->xconfigure.height;
2552 demo_resize(demo);
2553 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002554 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002555 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002556 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002557 }
Tony Barbour2593b182016-04-19 10:57:58 -06002558}
2559
2560static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002561 while (!demo->quit) {
2562 XEvent event;
2563
2564 if (demo->pause) {
2565 XNextEvent(demo->display, &event);
2566 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002567 }
2568 while (XPending(demo->display) > 0) {
2569 XNextEvent(demo->display, &event);
2570 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002571 }
2572
Tony Barbour2593b182016-04-19 10:57:58 -06002573 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002574 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002575 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002576 }
2577}
Tony Barbour153cb062016-12-07 13:43:36 -07002578#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002579static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002580 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002581 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002582 case XCB_EXPOSE:
2583 // TODO: Resize window
2584 break;
2585 case XCB_CLIENT_MESSAGE:
2586 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2587 demo->quit = true;
2588 }
2589 break;
2590 case XCB_KEY_RELEASE: {
2591 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002592
Dave Houlton5fa47912018-02-16 11:02:26 -07002593 switch (key->detail) {
2594 case 0x9: // Escape
2595 demo->quit = true;
2596 break;
2597 case 0x71: // left arrow key
2598 demo->spin_angle -= demo->spin_increment;
2599 break;
2600 case 0x72: // right arrow key
2601 demo->spin_angle += demo->spin_increment;
2602 break;
2603 case 0x41: // space bar
2604 demo->pause = !demo->pause;
2605 break;
2606 }
2607 } break;
2608 case XCB_CONFIGURE_NOTIFY: {
2609 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2610 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2611 demo->width = cfg->width;
2612 demo->height = cfg->height;
2613 demo_resize(demo);
2614 }
2615 } break;
2616 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002617 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002618 }
2619}
2620
Tony Barbour2593b182016-04-19 10:57:58 -06002621static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002622 xcb_flush(demo->connection);
2623
2624 while (!demo->quit) {
2625 xcb_generic_event_t *event;
2626
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002627 if (demo->pause) {
2628 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002629 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002630 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002631 }
2632 while (event) {
2633 demo_handle_xcb_event(demo, event);
2634 free(event);
2635 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002636 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002637
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002638 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002639 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002640 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002641 }
2642}
2643
Tony Barbour2593b182016-04-19 10:57:58 -06002644static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002645 uint32_t value_mask, value_list[32];
2646
Tony Barbour2593b182016-04-19 10:57:58 -06002647 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002648
2649 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2650 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002651 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002652
Dave Houlton5fa47912018-02-16 11:02:26 -07002653 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2654 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002655
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002656 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002657 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2658 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002659
Dave Houlton5fa47912018-02-16 11:02:26 -07002660 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2661 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002662
Dave Houlton5fa47912018-02-16 11:02:26 -07002663 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002664 &(*demo->atom_wm_delete_window).atom);
2665 free(reply);
2666
Tony Barbour2593b182016-04-19 10:57:58 -06002667 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002668
Karl Schultze4dc75c2016-02-02 15:37:51 -07002669 // Force the x/y coordinates to 100,100 results are identical in consecutive
2670 // runs
2671 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002672 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002673}
Tony Barbour6b131662016-08-25 13:14:45 -06002674// VK_USE_PLATFORM_XCB_KHR
2675#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002676static void demo_run(struct demo *demo) {
2677 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002678 if (demo->pause) {
2679 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002680 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002681 wl_display_dispatch_pending(demo->display); // don't block
2682 demo_draw(demo);
2683 demo->curFrame++;
2684 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2685 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002686 }
2687}
2688
Dave Houlton5fa47912018-02-16 11:02:26 -07002689static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002690 wl_shell_surface_pong(shell_surface, serial);
2691}
2692
Dave Houlton5fa47912018-02-16 11:02:26 -07002693static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED,
2694 int32_t width UNUSED, int32_t height UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002695
Dave Houlton5fa47912018-02-16 11:02:26 -07002696static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002697
Dave Houlton5fa47912018-02-16 11:02:26 -07002698static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002699
2700static void demo_create_window(struct demo *demo) {
2701 demo->window = wl_compositor_create_surface(demo->compositor);
2702 if (!demo->window) {
2703 printf("Can not create wayland_surface from compositor!\n");
2704 fflush(stdout);
2705 exit(1);
2706 }
2707
2708 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2709 if (!demo->shell_surface) {
2710 printf("Can not get shell_surface from wayland_surface!\n");
2711 fflush(stdout);
2712 exit(1);
2713 }
Dave Houlton5fa47912018-02-16 11:02:26 -07002714 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002715 wl_shell_surface_set_toplevel(demo->shell_surface);
2716 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2717}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002718#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2719static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002720 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002721
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002722 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002723 demo->curFrame++;
2724}
Karl Schultz206b1c52018-04-13 18:02:07 -06002725#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2726static void demo_run(struct demo *demo) {
2727 demo_draw(demo);
2728 demo->curFrame++;
2729 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2730 demo->quit = TRUE;
2731 }
2732}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002733#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002734#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2735static VkResult demo_create_display_surface(struct demo *demo) {
2736 VkResult U_ASSERT_ONLY err;
2737 uint32_t display_count;
2738 uint32_t mode_count;
2739 uint32_t plane_count;
2740 VkDisplayPropertiesKHR display_props;
2741 VkDisplayKHR display;
2742 VkDisplayModePropertiesKHR mode_props;
2743 VkDisplayPlanePropertiesKHR *plane_props;
2744 VkBool32 found_plane = VK_FALSE;
2745 uint32_t plane_index;
2746 VkExtent2D image_extent;
2747 VkDisplaySurfaceCreateInfoKHR create_info;
2748
2749 // Get the first display
2750 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2751 assert(!err);
2752
2753 if (display_count == 0) {
2754 printf("Cannot find any display!\n");
2755 fflush(stdout);
2756 exit(1);
2757 }
2758
2759 display_count = 1;
2760 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2761 assert(!err || (err == VK_INCOMPLETE));
2762
2763 display = display_props.display;
2764
2765 // Get the first mode of the display
2766 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2767 assert(!err);
2768
2769 if (mode_count == 0) {
2770 printf("Cannot find any mode for the display!\n");
2771 fflush(stdout);
2772 exit(1);
2773 }
2774
2775 mode_count = 1;
2776 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2777 assert(!err || (err == VK_INCOMPLETE));
2778
2779 // Get the list of planes
2780 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2781 assert(!err);
2782
2783 if (plane_count == 0) {
2784 printf("Cannot find any plane!\n");
2785 fflush(stdout);
2786 exit(1);
2787 }
2788
2789 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2790 assert(plane_props);
2791
2792 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2793 assert(!err);
2794
2795 // Find a plane compatible with the display
2796 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2797 uint32_t supported_count;
2798 VkDisplayKHR *supported_displays;
2799
2800 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002801 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002802 continue;
2803 }
2804
2805 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2806 assert(!err);
2807
2808 if (supported_count == 0) {
2809 continue;
2810 }
2811
2812 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2813 assert(supported_displays);
2814
2815 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2816 assert(!err);
2817
2818 for (uint32_t i = 0; i < supported_count; i++) {
2819 if (supported_displays[i] == display) {
2820 found_plane = VK_TRUE;
2821 break;
2822 }
2823 }
2824
2825 free(supported_displays);
2826
2827 if (found_plane) {
2828 break;
2829 }
2830 }
2831
2832 if (!found_plane) {
2833 printf("Cannot find a plane compatible with the display!\n");
2834 fflush(stdout);
2835 exit(1);
2836 }
2837
2838 free(plane_props);
2839
Tony Barbour820b55b2017-03-14 08:55:46 -06002840 VkDisplayPlaneCapabilitiesKHR planeCaps;
2841 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2842 // Find a supported alpha mode
Mark Young66510e52017-11-10 10:05:14 -07002843 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2844 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002845 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2846 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2847 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2848 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2849 };
2850 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2851 if (planeCaps.supportedAlpha & alphaModes[i]) {
2852 alphaMode = alphaModes[i];
2853 break;
2854 }
2855 }
Damien Leone600c3052017-01-31 10:26:07 -07002856 image_extent.width = mode_props.parameters.visibleRegion.width;
2857 image_extent.height = mode_props.parameters.visibleRegion.height;
2858
2859 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2860 create_info.pNext = NULL;
2861 create_info.flags = 0;
2862 create_info.displayMode = mode_props.displayMode;
2863 create_info.planeIndex = plane_index;
2864 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2865 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002866 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002867 create_info.globalAlpha = 1.0f;
2868 create_info.imageExtent = image_extent;
2869
2870 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2871}
2872
Dave Houlton5fa47912018-02-16 11:02:26 -07002873static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07002874 while (!demo->quit) {
2875 demo_draw(demo);
2876 demo->curFrame++;
2877
2878 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2879 demo->quit = true;
2880 }
2881 }
2882}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002883#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002884
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002885/*
2886 * Return 1 (true) if all layer names specified in check_names
2887 * can be found in given layer properties.
2888 */
Dave Houlton5fa47912018-02-16 11:02:26 -07002889static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002890 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002891 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002892 for (uint32_t j = 0; j < layer_count; j++) {
2893 if (!strcmp(check_names[i], layers[j].layerName)) {
2894 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002895 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002896 }
2897 }
2898 if (!found) {
2899 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2900 return 0;
2901 }
2902 }
2903 return 1;
2904}
2905
Karl Schultze4dc75c2016-02-02 15:37:51 -07002906static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002907 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002908 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002909 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002910 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002911 char **instance_validation_layers = NULL;
2912 demo->enabled_extension_count = 0;
2913 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002914 demo->is_minimized = false;
2915 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002916
Dave Houlton5fa47912018-02-16 11:02:26 -07002917 char *instance_validation_layers_alt1[] = {"VK_LAYER_LUNARG_standard_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06002918
Dave Houlton5fa47912018-02-16 11:02:26 -07002919 char *instance_validation_layers_alt2[] = {"VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2920 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2921 "VK_LAYER_GOOGLE_unique_objects"};
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002922
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002923 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002924 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002925 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002926 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002927 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002928
Karl Schultz7c50ad62016-04-01 12:07:03 -06002929 instance_validation_layers = instance_validation_layers_alt1;
2930 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002931 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2932 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002933 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002934
Dave Houlton5fa47912018-02-16 11:02:26 -07002935 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt1), instance_validation_layers,
2936 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002937 if (validation_found) {
2938 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002939 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2940 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002941 } else {
2942 // use alternative set of validation layers
2943 instance_validation_layers = instance_validation_layers_alt2;
2944 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Dave Houlton5fa47912018-02-16 11:02:26 -07002945 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt2), instance_validation_layers,
2946 instance_layer_count, instance_layers);
2947 validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002948 for (uint32_t i = 0; i < validation_layer_count; i++) {
2949 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002950 }
2951 }
2952 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002953 }
Dustin Graves7c287352016-01-27 13:48:06 -07002954
Karl Schultz7c50ad62016-04-01 12:07:03 -06002955 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002956 ERR_EXIT(
2957 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
2958 "Please look at the Getting Started guide for additional information.\n",
2959 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06002960 }
Dustin Graves7c287352016-01-27 13:48:06 -07002961 }
2962
2963 /* Look for instance extensions */
2964 VkBool32 surfaceExtFound = 0;
2965 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002966 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002967
Dave Houlton5fa47912018-02-16 11:02:26 -07002968 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002969 assert(!err);
2970
Dustin Graves7c287352016-01-27 13:48:06 -07002971 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002972 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2973 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07002974 assert(!err);
2975 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002976 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002977 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002978 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002979 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002980#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002981 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002982 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002983 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002984 }
Tony Barbour153cb062016-12-07 13:43:36 -07002985#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002986 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06002987 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002988 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06002989 }
Tony Barbour153cb062016-12-07 13:43:36 -07002990#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002991 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002992 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002993 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002994 }
Tony Barbour153cb062016-12-07 13:43:36 -07002995#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002996 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002997 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002998 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002999 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003000#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003001#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003002 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07003003 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003004 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07003005 }
Tony Barbour153cb062016-12-07 13:43:36 -07003006#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003007 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003008 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003009 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003010 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003011#elif defined(VK_USE_PLATFORM_IOS_MVK)
3012 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3013 platformSurfaceExtFound = 1;
3014 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3015 }
3016#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3017 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3018 platformSurfaceExtFound = 1;
3019 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3020 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003021#endif
Mark Young66510e52017-11-10 10:05:14 -07003022 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3023 if (demo->validate) {
3024 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3025 }
3026 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003027 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003028 }
Dustin Graves7c287352016-01-27 13:48:06 -07003029
3030 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003031 }
Dustin Graves7c287352016-01-27 13:48:06 -07003032
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003033 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003034 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3035 " extension.\n\n"
3036 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3037 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06003038 "vkCreateInstance Failure");
3039 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003040 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003041#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003042 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3043 " extension.\n\n"
3044 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3045 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003046 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003047#elif defined(VK_USE_PLATFORM_IOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003048 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_IOS_SURFACE_EXTENSION_NAME
3049 " extension.\n\n"
3050 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3051 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003052 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003053#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003054 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_MACOS_SURFACE_EXTENSION_NAME
3055 " extension.\n\n"
3056 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3057 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003058 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003059#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003060 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_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",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003064 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003065#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003066 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_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",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003070 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003071#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003072#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003073 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3074 " extension.\n\n"
3075 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3076 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07003077 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003078#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003079 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3080 " extension.\n\n"
3081 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3082 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003083 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003084#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003085 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3086 " extension.\n\n"
3087 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3088 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06003089 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003090#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003091 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003092 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003093 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003094 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003095 .pApplicationName = APP_SHORT_NAME,
3096 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003097 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003098 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003099 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003100 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003101 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003102 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003103 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003104 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003105 .enabledLayerCount = demo->enabled_layer_count,
3106 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3107 .enabledExtensionCount = demo->enabled_extension_count,
3108 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003109 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003110
3111 /*
3112 * This is info for a temp callback to use during CreateInstance.
3113 * After the instance is created, we use the instance-based
3114 * function to register the final callback.
3115 */
Mark Young66510e52017-11-10 10:05:14 -07003116 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003117 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003118 // VK_EXT_debug_utils style
3119 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3120 dbg_messenger_create_info.pNext = NULL;
3121 dbg_messenger_create_info.flags = 0;
3122 dbg_messenger_create_info.messageSeverity =
3123 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3124 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3125 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3126 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3127 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3128 dbg_messenger_create_info.pUserData = demo;
3129 inst_info.pNext = &dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003130 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003131
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003132 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003133
Chia-I Wu63636062015-10-26 21:10:41 +08003134 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003135 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003136 ERR_EXIT(
3137 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3138 "Please look at the Getting Started guide for additional information.\n",
3139 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003140 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003141 ERR_EXIT(
3142 "Cannot find a specified extension library.\n"
3143 "Make sure your layers path is set appropriately.\n",
3144 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003145 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003146 ERR_EXIT(
3147 "vkCreateInstance failed.\n\n"
3148 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3149 "Please look at the Getting Started guide for additional information.\n",
3150 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003151 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003152
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003153 /* Make initial call to query gpu_count, then second call for gpu info*/
3154 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
Petr Kraus2f1dbdb2018-04-14 14:45:17 +02003155 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003156
3157 if (gpu_count > 0) {
3158 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3159 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3160 assert(!err);
3161 /* For cube demo we just grab the first physical device */
3162 demo->gpu = physical_devices[0];
3163 free(physical_devices);
3164 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003165 ERR_EXIT(
3166 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3167 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3168 "Please look at the Getting Started guide for additional information.\n",
3169 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003170 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003171
Dustin Graves7c287352016-01-27 13:48:06 -07003172 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003173 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003174 VkBool32 swapchainExtFound = 0;
3175 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003176 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003177
Dave Houlton5fa47912018-02-16 11:02:26 -07003178 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003179 assert(!err);
3180
Dustin Graves7c287352016-01-27 13:48:06 -07003181 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003182 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3183 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003184 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003185
Dustin Graves7c287352016-01-27 13:48:06 -07003186 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003187 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003188 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003189 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003190 }
3191 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003192 }
Dustin Graves7c287352016-01-27 13:48:06 -07003193
Ian Elliott53622a72017-03-28 11:06:33 -06003194 if (demo->VK_KHR_incremental_present_enabled) {
3195 // Even though the user "enabled" the extension via the command
3196 // line, we must make sure that it's enumerated for use with the
3197 // device. Therefore, disable it here, and re-enable it again if
3198 // enumerated.
3199 demo->VK_KHR_incremental_present_enabled = false;
3200 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003201 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3202 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003203 demo->VK_KHR_incremental_present_enabled = true;
3204 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3205 }
3206 assert(demo->enabled_extension_count < 64);
3207 }
3208 if (!demo->VK_KHR_incremental_present_enabled) {
3209 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3210 }
3211 }
3212
Ian Elliottd940ca22017-03-22 08:31:27 -06003213 if (demo->VK_GOOGLE_display_timing_enabled) {
3214 // Even though the user "enabled" the extension via the command
3215 // line, we must make sure that it's enumerated for use with the
3216 // device. Therefore, disable it here, and re-enable it again if
3217 // enumerated.
3218 demo->VK_GOOGLE_display_timing_enabled = false;
3219 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003220 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3221 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003222 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003223 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003224 }
3225 assert(demo->enabled_extension_count < 64);
3226 }
3227 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003228 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003229 }
3230 }
3231
Dustin Graves7c287352016-01-27 13:48:06 -07003232 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003233 }
Dustin Graves7c287352016-01-27 13:48:06 -07003234
Tony Barbourcc69f452015-10-08 13:59:42 -06003235 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003236 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3237 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3238 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003239 "vkCreateInstance Failure");
3240 }
3241
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003242 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003243 // Setup VK_EXT_debug_utils function pointers always (we use them for
3244 // debug labels and names).
3245 demo->CreateDebugUtilsMessengerEXT =
3246 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3247 demo->DestroyDebugUtilsMessengerEXT =
3248 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3249 demo->SubmitDebugUtilsMessageEXT =
3250 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3251 demo->CmdBeginDebugUtilsLabelEXT =
3252 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3253 demo->CmdEndDebugUtilsLabelEXT =
3254 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3255 demo->CmdInsertDebugUtilsLabelEXT =
3256 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3257 demo->SetDebugUtilsObjectNameEXT =
3258 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3259 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3260 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3261 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3262 NULL == demo->SetDebugUtilsObjectNameEXT) {
3263 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003264 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003265
Mark Young66510e52017-11-10 10:05:14 -07003266 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003267 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003268 case VK_SUCCESS:
3269 break;
3270 case VK_ERROR_OUT_OF_HOST_MEMORY:
Mark Young66510e52017-11-10 10:05:14 -07003271 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003272 break;
3273 default:
Mark Young66510e52017-11-10 10:05:14 -07003274 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003275 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003276 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003277 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003278 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003279
3280 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003281 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003282 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003283
Dave Houlton5fa47912018-02-16 11:02:26 -07003284 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3285 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003286
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003287 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003288 // If app has specific feature requirements it should check supported
3289 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003290 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003291 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003292
Tony Barbourda2bad52016-01-22 14:36:40 -07003293 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3294 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3295 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3296 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3297 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3298}
3299
Karl Schultze4dc75c2016-02-02 15:37:51 -07003300static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003301 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003302 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003303 VkDeviceQueueCreateInfo queues[2];
3304 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3305 queues[0].pNext = NULL;
3306 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3307 queues[0].queueCount = 1;
3308 queues[0].pQueuePriorities = queue_priorities;
3309 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003310
3311 VkDeviceCreateInfo device = {
3312 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3313 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003314 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003315 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003316 .enabledLayerCount = 0,
3317 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003318 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003319 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003320 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003321 };
Tony Barbour22e06272016-09-07 16:07:56 -06003322 if (demo->separate_present_queue) {
3323 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3324 queues[1].pNext = NULL;
3325 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3326 queues[1].queueCount = 1;
3327 queues[1].pQueuePriorities = queue_priorities;
3328 queues[1].flags = 0;
3329 device.queueCreateInfoCount = 2;
3330 }
Chia-I Wu63636062015-10-26 21:10:41 +08003331 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003332 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003333}
3334
Karl Schultze4dc75c2016-02-02 15:37:51 -07003335static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003336 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003337
Karl Schultze4dc75c2016-02-02 15:37:51 -07003338// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003339#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003340 VkWin32SurfaceCreateInfoKHR createInfo;
3341 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3342 createInfo.pNext = NULL;
3343 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003344 createInfo.hinstance = demo->connection;
3345 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003346
Dave Houlton5fa47912018-02-16 11:02:26 -07003347 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003348#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003349 VkWaylandSurfaceCreateInfoKHR createInfo;
3350 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3351 createInfo.pNext = NULL;
3352 createInfo.flags = 0;
3353 createInfo.display = demo->display;
3354 createInfo.surface = demo->window;
3355
Dave Houlton5fa47912018-02-16 11:02:26 -07003356 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003357#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003358#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3359 VkAndroidSurfaceCreateInfoKHR createInfo;
3360 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3361 createInfo.pNext = NULL;
3362 createInfo.flags = 0;
Mike Schuchardt837d5162018-03-08 12:57:02 -07003363 createInfo.window = (struct ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003364
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003365 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003366#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3367 VkXlibSurfaceCreateInfoKHR createInfo;
3368 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3369 createInfo.pNext = NULL;
3370 createInfo.flags = 0;
3371 createInfo.dpy = demo->display;
3372 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003373
Dave Houlton5fa47912018-02-16 11:02:26 -07003374 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003375#elif defined(VK_USE_PLATFORM_XCB_KHR)
3376 VkXcbSurfaceCreateInfoKHR createInfo;
3377 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3378 createInfo.pNext = NULL;
3379 createInfo.flags = 0;
3380 createInfo.connection = demo->connection;
3381 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003382
Tony Barbour153cb062016-12-07 13:43:36 -07003383 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003384#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3385 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003386#elif defined(VK_USE_PLATFORM_IOS_MVK)
3387 VkIOSSurfaceCreateInfoMVK surface;
3388 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3389 surface.pNext = NULL;
3390 surface.flags = 0;
3391 surface.pView = demo->window;
3392
3393 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3394#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3395 VkMacOSSurfaceCreateInfoMVK surface;
3396 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3397 surface.pNext = NULL;
3398 surface.flags = 0;
3399 surface.pView = demo->window;
3400
3401 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003402#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003403 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003404
Tony Barbourcc69f452015-10-08 13:59:42 -06003405 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003406 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003407 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003408 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003409 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003410
3411 // Search for a graphics and a present queue in the array of queue
3412 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003413 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3414 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003415 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003416 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3417 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3418 graphicsQueueFamilyIndex = i;
3419 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003420
Tony Barbourab2a0362016-09-06 11:40:12 -06003421 if (supportsPresent[i] == VK_TRUE) {
3422 graphicsQueueFamilyIndex = i;
3423 presentQueueFamilyIndex = i;
3424 break;
3425 }
3426 }
3427 }
3428
3429 if (presentQueueFamilyIndex == UINT32_MAX) {
3430 // If didn't find a queue that supports both graphics and present, then
3431 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003432 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003433 if (supportsPresent[i] == VK_TRUE) {
3434 presentQueueFamilyIndex = i;
3435 break;
3436 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003437 }
3438 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003439
3440 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003441 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3442 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003443 }
3444
Tony Barbourab2a0362016-09-06 11:40:12 -06003445 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3446 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003447 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003448 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003449
Tony Barbourda2bad52016-01-22 14:36:40 -07003450 demo_create_device(demo);
3451
3452 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3453 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3454 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3455 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3456 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003457 if (demo->VK_GOOGLE_display_timing_enabled) {
3458 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3459 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3460 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003461
Dave Houlton5fa47912018-02-16 11:02:26 -07003462 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003463
Tony Barbour22e06272016-09-07 16:07:56 -06003464 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003465 demo->present_queue = demo->graphics_queue;
3466 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003467 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003468 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003469
Ian Elliottaaae5352015-07-06 14:27:58 -06003470 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003471 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003472 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003473 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003474 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3475 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003476 assert(!err);
3477 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3478 // the surface has no preferred format. Otherwise, at least one
3479 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003480 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003481 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003482 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003483 assert(formatCount >= 1);
3484 demo->format = surfFormats[0].format;
3485 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003486 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003487
David Pinedo2bb7c932015-06-18 17:03:14 -06003488 demo->quit = false;
3489 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003490
Tony Barbource7524e2016-07-28 12:01:45 -06003491 // Create semaphores to synchronize acquiring presentable buffers before
3492 // rendering and waiting for drawing to be complete before presenting
3493 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3494 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3495 .pNext = NULL,
3496 .flags = 0,
3497 };
Tony Barbource7524e2016-07-28 12:01:45 -06003498
Tony Barbour66a56c32016-09-21 13:10:56 -06003499 // Create fences that we can use to throttle if we get too far
3500 // ahead of the image presents
3501 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003502 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003503 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003504 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3505 assert(!err);
3506
Dave Houlton5fa47912018-02-16 11:02:26 -07003507 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003508 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003509
Dave Houlton5fa47912018-02-16 11:02:26 -07003510 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003511 assert(!err);
3512
3513 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003514 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003515 assert(!err);
3516 }
Tony Barbour22e06272016-09-07 16:07:56 -06003517 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003518 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003519
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003520 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003521 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003522}
3523
Tony Barbour153cb062016-12-07 13:43:36 -07003524#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003525static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3526 wl_fixed_t sy) {}
3527
3528static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3529
3530static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3531
3532static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3533 uint32_t state) {
3534 struct demo *demo = data;
3535 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3536 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
3537 }
3538}
3539
3540static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3541
3542static const struct wl_pointer_listener pointer_listener = {
3543 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3544};
3545
3546static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3547
3548static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3549 struct wl_array *keys) {}
3550
3551static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3552
3553static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3554 uint32_t state) {
3555 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3556 struct demo *demo = data;
3557 switch (key) {
3558 case KEY_ESC: // Escape
3559 demo->quit = true;
3560 break;
3561 case KEY_LEFT: // left arrow key
3562 demo->spin_angle -= demo->spin_increment;
3563 break;
3564 case KEY_RIGHT: // right arrow key
3565 demo->spin_angle += demo->spin_increment;
3566 break;
3567 case KEY_SPACE: // space bar
3568 demo->pause = !demo->pause;
3569 break;
3570 }
3571}
3572
3573static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3574 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3575
3576static const struct wl_keyboard_listener keyboard_listener = {
3577 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3578};
3579
3580static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3581 // Subscribe to pointer events
3582 struct demo *demo = data;
3583 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3584 demo->pointer = wl_seat_get_pointer(seat);
3585 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3586 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3587 wl_pointer_destroy(demo->pointer);
3588 demo->pointer = NULL;
3589 }
3590 // Subscribe to keyboard events
3591 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3592 demo->keyboard = wl_seat_get_keyboard(seat);
3593 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3594 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3595 wl_keyboard_destroy(demo->keyboard);
3596 demo->keyboard = NULL;
3597 }
3598}
3599
3600static const struct wl_seat_listener seat_listener = {
3601 seat_handle_capabilities,
3602};
3603
3604static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003605 uint32_t version UNUSED) {
3606 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003607 // pickup wayland objects when they appear
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003608 if (strcmp(interface, "wl_compositor") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003609 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003610 } else if (strcmp(interface, "wl_shell") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003611 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3612 } else if (strcmp(interface, "wl_seat") == 0) {
3613 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3614 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003615 }
3616}
3617
Dave Houlton5fa47912018-02-16 11:02:26 -07003618static 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 +09003619
Dave Houlton5fa47912018-02-16 11:02:26 -07003620static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003621#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003622#endif
3623
Karl Schultze4dc75c2016-02-02 15:37:51 -07003624static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003625#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003626 const xcb_setup_t *setup;
3627 xcb_screen_iterator_t iter;
3628 int scr;
3629
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003630 const char *display_envar = getenv("DISPLAY");
3631 if (display_envar == NULL || display_envar[0] == '\0') {
3632 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3633 fflush(stdout);
3634 exit(1);
3635 }
3636
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003637 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003638 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003639 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003640 fflush(stdout);
3641 exit(1);
3642 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003643
3644 setup = xcb_get_setup(demo->connection);
3645 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003646 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003647
3648 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003649#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3650 demo->display = wl_display_connect(NULL);
3651
3652 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003653 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003654 fflush(stdout);
3655 exit(1);
3656 }
3657
3658 demo->registry = wl_display_get_registry(demo->display);
3659 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3660 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003661#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003662#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003663}
3664
Karl Schultze4dc75c2016-02-02 15:37:51 -07003665static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003666 vec3 eye = {0.0f, 3.0f, 5.0f};
3667 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003668 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003669
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003670 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003671 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003672 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003673
Piers Daniell735ee532015-02-23 16:23:13 -07003674 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003675 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003676 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003677 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003678 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003679 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3680 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003681 i++;
3682 continue;
3683 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003684 if (strcmp(argv[i], "--break") == 0) {
3685 demo->use_break = true;
3686 continue;
3687 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003688 if (strcmp(argv[i], "--validate") == 0) {
3689 demo->validate = true;
3690 continue;
3691 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003692 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3693 demo->validate = true;
3694 demo->validate_checks_disabled = true;
3695 continue;
3696 }
Tony Barbour2593b182016-04-19 10:57:58 -06003697 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003698 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003699 continue;
3700 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003701 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3702 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003703 i++;
3704 continue;
3705 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003706 if (strcmp(argv[i], "--suppress_popups") == 0) {
3707 demo->suppress_popups = true;
3708 continue;
3709 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003710 if (strcmp(argv[i], "--display_timing") == 0) {
3711 demo->VK_GOOGLE_display_timing_enabled = true;
3712 continue;
3713 }
Ian Elliott53622a72017-03-28 11:06:33 -06003714 if (strcmp(argv[i], "--incremental_present") == 0) {
3715 demo->VK_KHR_incremental_present_enabled = true;
3716 continue;
3717 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003718
Cody Northropaf28a532016-09-27 10:50:57 -06003719#if defined(ANDROID)
3720 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3721#else
Mike Schuchardt9a881512017-11-01 16:16:22 -06003722 fprintf(stderr,
Mark Young66510e52017-11-10 10:05:14 -07003723 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled] [--break]\n"
3724 "\t[--c <framecount>] [--suppress_popups] [--incremental_present] [--display_timing]\n"
3725 "\t[--present_mode <present mode enum>]\n"
3726 "\t <present_mode_enum>\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3727 "\t\t\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3728 "\t\t\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
3729 "\t\t\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
Mike Schuchardt9a881512017-11-01 16:16:22 -06003730 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
3731 VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003732 fflush(stderr);
3733 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003734#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003735 }
3736
Tony Barbour153cb062016-12-07 13:43:36 -07003737 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003738
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003739 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003740
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003741 demo->width = 500;
3742 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003743
Tony Barbour66a56c32016-09-21 13:10:56 -06003744 demo->spin_angle = 4.0f;
3745 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003746 demo->pause = false;
3747
Dave Houlton5fa47912018-02-16 11:02:26 -07003748 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003749 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3750 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003751
Dave Houlton5fa47912018-02-16 11:02:26 -07003752 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003753}
3754
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003755#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003756// Include header required for parsing the command line options.
3757#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003758
Dave Houlton5fa47912018-02-16 11:02:26 -07003759int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3760 MSG msg; // message
3761 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003762 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003763 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003764
Jamie Madillb2ff6502017-03-15 16:17:46 -04003765 // Ensure wParam is initialized.
3766 msg.wParam = 0;
3767
Karl Schultze4dc75c2016-02-02 15:37:51 -07003768 // Use the CommandLine functions to get the command line arguments.
3769 // Unfortunately, Microsoft outputs
3770 // this information as wide characters for Unicode, and we simply want the
3771 // Ascii version to be compatible
3772 // with the non-Windows side. So, we have to convert the information to
3773 // Ascii character strings.
3774 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3775 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003776 argc = 0;
3777 }
3778
Karl Schultze4dc75c2016-02-02 15:37:51 -07003779 if (argc > 0) {
3780 argv = (char **)malloc(sizeof(char *) * argc);
3781 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003782 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003783 } else {
3784 for (int iii = 0; iii < argc; iii++) {
3785 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003786 size_t numConverted = 0;
3787
Karl Schultze4dc75c2016-02-02 15:37:51 -07003788 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3789 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003790 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003791 }
3792 }
3793 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003794 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003795 argv = NULL;
3796 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003797
3798 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003799
3800 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003801 if (argc > 0 && argv != NULL) {
3802 for (int iii = 0; iii < argc; iii++) {
3803 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003804 free(argv[iii]);
3805 }
3806 }
3807 free(argv);
3808 }
3809
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003810 demo.connection = hInstance;
3811 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003812 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003813 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003814
3815 demo_prepare(&demo);
3816
Dave Houlton5fa47912018-02-16 11:02:26 -07003817 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003818
3819 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003820 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003821 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003822 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003823 {
Dave Houlton5fa47912018-02-16 11:02:26 -07003824 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07003825 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003826 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003827 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003828 DispatchMessage(&msg);
3829 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003830 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003831 }
3832
3833 demo_cleanup(&demo);
3834
Karl Schultze4dc75c2016-02-02 15:37:51 -07003835 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003836}
Bill Hollingsf4352142017-02-14 22:58:56 -05003837
3838#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
Karl Schultz206b1c52018-04-13 18:02:07 -06003839static void demo_main(struct demo *demo, void *view, int argc, const char *argv[]) {
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
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003848#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3849#include <android/log.h>
3850#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003851#include "android_util.h"
3852
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003853static bool initialized = false;
3854static bool active = false;
3855struct demo demo;
3856
Dave Houlton5fa47912018-02-16 11:02:26 -07003857static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003858
Dave Houlton5fa47912018-02-16 11:02:26 -07003859static void processCommand(struct android_app *app, int32_t cmd) {
3860 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003861 case APP_CMD_INIT_WINDOW: {
3862 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003863 // We're getting a new window. If the app is starting up, we
3864 // need to initialize. If the app has already been
3865 // initialized, that means that we lost our previous window,
3866 // which means that we have a lot of work to do. At a minimum,
3867 // we need to destroy the swapchain and surface associated with
3868 // the old window, and create a new surface and swapchain.
3869 // However, since there are a lot of other objects/state that
3870 // is tied to the swapchain, it's easiest to simply cleanup and
3871 // start over (i.e. use a brute-force approach of re-starting
3872 // the app)
3873 if (demo.prepared) {
3874 demo_cleanup(&demo);
3875 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003876
3877 // Parse Intents into argc, argv
3878 // Use the following key to send arguments, i.e.
3879 // --es args "--validate"
3880 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07003881 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003882 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003883 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003884
3885 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07003886 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 -06003887
3888 demo_init(&demo, argc, argv);
3889
3890 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07003891 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003892
Dave Houlton5fa47912018-02-16 11:02:26 -07003893 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003894 demo_init_vk_swapchain(&demo);
3895 demo_prepare(&demo);
3896 initialized = true;
3897 }
3898 break;
3899 }
3900 case APP_CMD_GAINED_FOCUS: {
3901 active = true;
3902 break;
3903 }
3904 case APP_CMD_LOST_FOCUS: {
3905 active = false;
3906 break;
3907 }
3908 }
3909}
3910
Dave Houlton5fa47912018-02-16 11:02:26 -07003911void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06003912#ifdef ANDROID
3913 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07003914 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06003915#endif
3916
Ian Elliottf05d5d12016-05-17 12:03:35 -06003917 demo.prepared = false;
3918
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003919 app->onAppCmd = processCommand;
3920 app->onInputEvent = processInput;
3921
Dave Houlton5fa47912018-02-16 11:02:26 -07003922 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003923 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07003924 struct android_poll_source *source;
3925 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003926 if (source) {
3927 source->process(app, source);
3928 }
3929
3930 if (app->destroyRequested != 0) {
3931 demo_cleanup(&demo);
3932 return;
3933 }
3934 }
3935 if (initialized && active) {
3936 demo_run(&demo);
3937 }
3938 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003939}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003940#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003941int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003942 struct demo demo;
3943
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003944 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003945#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003946 demo_create_xcb_window(&demo);
3947#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3948 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003949#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3950 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003951#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003952#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003953
Tony Barbourcc69f452015-10-08 13:59:42 -06003954 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003955
3956 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003957
Tony Barbour153cb062016-12-07 13:43:36 -07003958#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003959 demo_run_xcb(&demo);
3960#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3961 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003962#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3963 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003964#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003965#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3966 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003967#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003968
3969 demo_cleanup(&demo);
3970
Karl Schultz0218c002016-03-22 17:06:13 -06003971 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003972}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003973#endif