blob: 2c33e526fd7f4acbf414d6dc318507aa4adda210 [file] [log] [blame]
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09001/*
Ian Elliottd940ca22017-03-22 08:31:27 -06002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Chia-I Wu <olv@lunarg.com>
19 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20 * Author: Ian Elliott <ian@LunarG.com>
21 * Author: Ian Elliott <ianelliott@google.com>
22 * Author: Jon Ashburn <jon@lunarg.com>
23 * Author: Gwan-gyeong Mun <elongbug@gmail.com>
24 * Author: Tony Barbour <tony@LunarG.com>
25 * Author: Bill Hollings <bill.hollings@brenwill.com>
26 */
Karl Schultze4dc75c2016-02-02 15:37:51 -070027
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060028#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060029#include <stdio.h>
Ian Elliottd940ca22017-03-22 08:31:27 -060030#include <stdarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060031#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -070035#include <signal.h>
Mun Gwan-gyeong22533892016-08-20 14:46:22 +090036#if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -060037#include <X11/Xutil.h>
Joey Bzdek15eb0702017-06-07 09:40:36 -060038#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
39#include <linux/input.h>
Tony Barbour2593b182016-04-19 10:57:58 -060040#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060041
Ian Elliott639ca472015-04-16 15:23:05 -060042#ifdef _WIN32
43#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060044#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070045#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060046
Tony Barbourefd0c5a2016-12-07 14:45:12 -070047#if defined(VK_USE_PLATFORM_MIR_KHR)
48#warning "Cube does not have code for Mir at this time"
49#endif
50
Cody Northrop8707a6e2016-04-26 19:59:19 -060051#ifdef ANDROID
52#include "vulkan_wrapper.h"
53#else
David Pinedoc5193c12015-11-06 12:54:48 -070054#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060055#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070056
David Pinedo541526f2015-11-24 09:00:24 -070057#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060058#include "linmath.h"
Mark Young44212682018-02-21 15:29:41 -070059#include "vk_enum_string_helper.h"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060060
Ian Elliottd940ca22017-03-22 08:31:27 -060061#include "gettime.h"
62#include "inttypes.h"
63#define MILLION 1000000L
64#define BILLION 1000000000L
65
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060066#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060067#define APP_SHORT_NAME "cube"
68#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060069
Tony Barbour66a56c32016-09-21 13:10:56 -060070// Allow a maximum of two outstanding presentation operations.
71#define FRAME_LAG 2
72
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060073#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
74
Tony Barbourfdc2d352015-04-22 09:02:32 -060075#if defined(NDEBUG) && defined(__GNUC__)
76#define U_ASSERT_ONLY __attribute__((unused))
77#else
78#define U_ASSERT_ONLY
79#endif
80
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090081#if defined(__GNUC__)
82#define UNUSED __attribute__((unused))
83#else
84#define UNUSED
85#endif
86
Ian Elliott07264132015-04-28 11:35:02 -060087#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060088bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070089#define ERR_EXIT(err_msg, err_class) \
90 do { \
91 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
92 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070093 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -060094void DbgMsg(char *fmt, ...) {
95 va_list va;
96 va_start(va, fmt);
97 printf(fmt, va);
98 fflush(stdout);
99 va_end(va);
100}
Ian Elliott07264132015-04-28 11:35:02 -0600101
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500102#elif defined __ANDROID__
103#include <android/log.h>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700104#define ERR_EXIT(err_msg, err_class) \
105 do { \
106 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
107 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500108 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600109#ifdef VARARGS_WORKS_ON_ANDROID
110void DbgMsg(const char *fmt, ...) {
111 va_list va;
112 va_start(va, fmt);
113 __android_log_print(ANDROID_LOG_INFO, "Cube", fmt, va);
114 va_end(va);
115}
116#else // VARARGS_WORKS_ON_ANDROID
117#define DbgMsg(fmt, ...) \
118 do { \
119 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", fmt, ##__VA_ARGS__)); \
120 } while (0)
121#endif // VARARGS_WORKS_ON_ANDROID
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500122#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700123#define ERR_EXIT(err_msg, err_class) \
124 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -0800125 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700126 fflush(stdout); \
127 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700128 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600129void DbgMsg(char *fmt, ...) {
130 va_list va;
131 va_start(va, fmt);
132 printf(fmt, va);
133 fflush(stdout);
134 va_end(va);
135}
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500136#endif
Ian Elliott07264132015-04-28 11:35:02 -0600137
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700138#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
139 { \
140 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
141 if (demo->fp##entrypoint == NULL) { \
142 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
143 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700144 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600145
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600146static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
147
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700148#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
149 { \
150 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
151 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
152 if (demo->fp##entrypoint == NULL) { \
153 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
154 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700155 }
Ian Elliott673898b2015-06-22 15:07:49 -0600156
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600157/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700158 * structure to track all objects related to a texture.
159 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600160struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600161 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700162
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600163 VkImage image;
164 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600165
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800166 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500167 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600168 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700169 int32_t tex_width, tex_height;
170};
171
Karl Schultze4dc75c2016-02-02 15:37:51 -0700172static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173
Karl Schultz0218c002016-03-22 17:06:13 -0600174static int validation_error = 0;
175
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600176struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600177 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700178 float mvp[4][4];
179 float position[12 * 3][4];
180 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600181};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600182
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600183//--------------------------------------------------------------------------------------
184// Mesh and VertexFormat Data
185//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700186// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600187static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 -1.0f,-1.0f, 1.0f,
190 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800191 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600192 -1.0f, 1.0f,-1.0f,
193 -1.0f,-1.0f,-1.0f,
194
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 1.0f, 1.0f,-1.0f,
197 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600199 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600200 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600201
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800202 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600203 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600204 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800205 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600206 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600207 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800209 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 -1.0f, 1.0f, 1.0f,
211 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800212 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600213 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600214 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600215
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 1.0f, 1.0f, 1.0f,
218 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800219 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220 1.0f,-1.0f,-1.0f,
221 1.0f, 1.0f,-1.0f,
222
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800226 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227 1.0f,-1.0f, 1.0f,
228 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600229};
230
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600231static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700232 0.0f, 1.0f, // -X side
233 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800235 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600236 0.0f, 0.0f,
237 0.0f, 1.0f,
238
Rene Lindsay76867e82016-07-29 10:49:11 -0700239 1.0f, 1.0f, // -Z side
240 0.0f, 0.0f,
241 0.0f, 1.0f,
242 1.0f, 1.0f,
243 1.0f, 0.0f,
244 0.0f, 0.0f,
245
246 1.0f, 0.0f, // -Y side
247 1.0f, 1.0f,
248 0.0f, 1.0f,
249 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600250 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800251 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600252
Rene Lindsay76867e82016-07-29 10:49:11 -0700253 1.0f, 0.0f, // +Y side
254 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800255 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600256 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700257 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600258 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600259
Rene Lindsay76867e82016-07-29 10:49:11 -0700260 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800261 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700262 0.0f, 1.0f,
263 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600264 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800265 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700266
267 0.0f, 0.0f, // +Z side
268 0.0f, 1.0f,
269 1.0f, 0.0f,
270 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600271 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700272 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600273};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700274// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600275
Karl Schultze4dc75c2016-02-02 15:37:51 -0700276void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600277 int i;
278
279 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700280 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600281 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
282 }
283 printf("\n");
284 fflush(stdout);
285}
286
Karl Schultze4dc75c2016-02-02 15:37:51 -0700287void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600288 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700289 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600290 printf("\n");
291 fflush(stdout);
292}
293
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600294typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600295 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800296 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600297 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600298 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700299 VkBuffer uniform_buffer;
300 VkDeviceMemory uniform_memory;
301 VkFramebuffer framebuffer;
302 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700303} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600304
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600305struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500306#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600307#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700308 HINSTANCE connection; // hInstance - Windows Instance
309 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
310 HWND window; // hWnd - window handle
311 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700312#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700313 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600314 Window xlib_window;
315 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700316#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700317 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318 xcb_connection_t *connection;
319 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600320 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800321 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900322#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
323 struct wl_display *display;
324 struct wl_registry *registry;
325 struct wl_compositor *compositor;
326 struct wl_surface *window;
327 struct wl_shell *shell;
328 struct wl_shell_surface *shell_surface;
Joey Bzdek15eb0702017-06-07 09:40:36 -0600329 struct wl_seat *seat;
330 struct wl_pointer *pointer;
331 struct wl_keyboard *keyboard;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700332#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500333#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mike Schuchardt837d5162018-03-08 12:57:02 -0700334 struct ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500335#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
336 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500337#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700338 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600339 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700340 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600341 bool separate_present_queue;
Jeremy Kniager602e4182018-01-19 10:52:34 -0700342 bool is_minimized;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600343
Ian Elliott53622a72017-03-28 11:06:33 -0600344 bool VK_KHR_incremental_present_enabled;
345
Ian Elliottd940ca22017-03-22 08:31:27 -0600346 bool VK_GOOGLE_display_timing_enabled;
347 bool syncd_with_actual_presents;
348 uint64_t refresh_duration;
349 uint64_t refresh_duration_multiplier;
350 uint64_t target_IPD; // image present duration (inverse of frame rate)
351 uint64_t prev_desired_present_time;
352 uint32_t next_present_id;
353 uint32_t last_early_id; // 0 if no early images
354 uint32_t last_late_id; // 0 if no late images
355
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600357 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600358 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600359 VkQueue graphics_queue;
360 VkQueue present_queue;
361 uint32_t graphics_queue_family_index;
362 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600363 VkSemaphore image_acquired_semaphores[FRAME_LAG];
364 VkSemaphore draw_complete_semaphores[FRAME_LAG];
365 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600366 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600367 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600368 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369
Tony Barbourda2bad52016-01-22 14:36:40 -0700370 uint32_t enabled_extension_count;
371 uint32_t enabled_layer_count;
372 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600373 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700374
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600377 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600378
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700379 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
380 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
381 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
382 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600383 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
384 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
385 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
386 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
387 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600388 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
389 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600390 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600391 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700392 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600393 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600394 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600395 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600396
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800397 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600398 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600399
400 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600401 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600402
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600403 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800404 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500405 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600406 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600407 } depth;
408
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600409 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600410 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600411
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700412 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500413 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600414 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600415 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800416 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600417 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600418
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600419 mat4x4 projection_matrix;
420 mat4x4 view_matrix;
421 mat4x4 model_matrix;
422
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600423 float spin_angle;
424 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600425 bool pause;
426
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600427 VkShaderModule vert_shader_module;
428 VkShaderModule frag_shader_module;
429
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600430 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800431
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600432 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600433 int32_t curFrame;
434 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600435 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600436 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600437 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600438 bool suppress_popups;
Mark Young66510e52017-11-10 10:05:14 -0700439
440 PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
441 PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
442 PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
443 PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
444 PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
445 PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
446 PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
447 VkDebugUtilsMessengerEXT dbg_messenger;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600448
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600449 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600450 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600451};
452
Mark Young66510e52017-11-10 10:05:14 -0700453VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
454 VkDebugUtilsMessageTypeFlagsEXT messageType,
455 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
456 void *pUserData) {
457 char prefix[64] = "";
458 char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000);
lenny-lunargfbe22392016-06-06 11:07:53 -0600459 assert(message);
Mark Young66510e52017-11-10 10:05:14 -0700460 struct demo *demo = (struct demo *)pUserData;
lenny-lunargfbe22392016-06-06 11:07:53 -0600461
Mark Young66510e52017-11-10 10:05:14 -0700462 if (demo->use_break) {
463#ifndef WIN32
464 raise(SIGTRAP);
465#else
466 DebugBreak();
467#endif
468 }
469
470 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
471 strcat(prefix, "VERBOSE : ");
472 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
473 strcat(prefix, "INFO : ");
474 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
475 strcat(prefix, "WARNING : ");
476 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
477 strcat(prefix, "ERROR : ");
478 }
479
480 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
481 strcat(prefix, "GENERAL");
lenny-lunargfbe22392016-06-06 11:07:53 -0600482 } else {
Mark Young66510e52017-11-10 10:05:14 -0700483 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
484 strcat(prefix, "VALIDATION");
485 validation_error = 1;
486 }
487 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
488 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
489 strcat(prefix, "|");
490 }
491 strcat(prefix, "PERFORMANCE");
492 }
493 }
494
495 sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber,
496 pCallbackData->pMessageIdName, pCallbackData->pMessage);
497 if (pCallbackData->objectCount > 0) {
498 char tmp_message[500];
499 sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount);
500 strcat(message, tmp_message);
501 for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
502 if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
503 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object,
Mark Young44212682018-02-21 15:29:41 -0700504 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700505 (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName);
506 } else {
507 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object,
Mark Young44212682018-02-21 15:29:41 -0700508 string_VkObjectType(pCallbackData->pObjects[object].objectType),
Mark Young66510e52017-11-10 10:05:14 -0700509 (void *)(pCallbackData->pObjects[object].objectHandle));
510 }
511 strcat(message, tmp_message);
512 }
513 }
514 if (pCallbackData->cmdBufLabelCount > 0) {
515 char tmp_message[500];
516 sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount);
517 strcat(message, tmp_message);
518 for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
519 sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label,
520 pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0],
521 pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2],
522 pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]);
523 strcat(message, tmp_message);
524 }
lenny-lunargfbe22392016-06-06 11:07:53 -0600525 }
526
527#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600528
Tony Barbour602ca4f2016-09-12 14:02:43 -0600529 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600530 if (!demo->suppress_popups)
531 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600532 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600533
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600534#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600535
Mike Schuchardt837d5162018-03-08 12:57:02 -0700536 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600537 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700538 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600539 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700540 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600541 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Mike Schuchardt837d5162018-03-08 12:57:02 -0700542 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
543 __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600544 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600545 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600546 }
Cody Northropaf28a532016-09-27 10:50:57 -0600547
lenny-lunargfbe22392016-06-06 11:07:53 -0600548#else
Cody Northropaf28a532016-09-27 10:50:57 -0600549
lenny-lunargfbe22392016-06-06 11:07:53 -0600550 printf("%s\n", message);
551 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600552
lenny-lunargfbe22392016-06-06 11:07:53 -0600553#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600554
lenny-lunargfbe22392016-06-06 11:07:53 -0600555 free(message);
556
Mark Young66510e52017-11-10 10:05:14 -0700557 // Don't bail out, but keep going.
lenny-lunargfbe22392016-06-06 11:07:53 -0600558 return false;
559}
560
Ian Elliottd940ca22017-03-22 08:31:27 -0600561bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
562 // The desired time was the earliest time that the present should have
563 // occured. In almost every case, the actual time should be later than the
564 // desired time. We should only consider the actual time "late" if it is
565 // after "desired + rdur".
566 if (actual <= desired) {
567 // The actual time was before or equal to the desired time. This will
568 // probably never happen, but in case it does, return false since the
569 // present was obviously NOT late.
570 return false;
571 }
Liam Middlebrook881fe392018-05-03 15:52:32 -0700572 uint64_t deadline = desired + rdur;
Ian Elliottd940ca22017-03-22 08:31:27 -0600573 if (actual > deadline) {
574 return true;
575 } else {
576 return false;
577 }
578}
Dave Houlton5fa47912018-02-16 11:02:26 -0700579bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600580 if (earliest < actual) {
581 // Consider whether this present could have occured earlier. Make sure
582 // that earliest time was at least 2msec earlier than actual time, and
583 // that the margin was at least 2msec:
584 uint64_t diff = actual - earliest;
585 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
586 // This present could have occured earlier because both: 1) the
587 // earliest time was at least 2 msec before actual time, and 2) the
588 // margin was at least 2msec.
589 return true;
590 }
591 }
592 return false;
593}
594
Ian Elliottcf074d32015-10-16 18:02:43 -0600595// Forward declaration:
596static void demo_resize(struct demo *demo);
597
Dave Houlton5fa47912018-02-16 11:02:26 -0700598static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700599 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600600 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700601 if ((typeBits & 1) == 1) {
602 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700603 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700604 *typeIndex = i;
605 return true;
606 }
607 }
608 typeBits >>= 1;
609 }
610 // No memory types matched, return failure
611 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600612}
613
Karl Schultze4dc75c2016-02-02 15:37:51 -0700614static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600615 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600616
Tony Barbource7524e2016-07-28 12:01:45 -0600617 // This function could get called twice if the texture uses a staging buffer
618 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700619 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600620
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600621 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600622 assert(!err);
623
Tony Barbource7524e2016-07-28 12:01:45 -0600624 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700625 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700626 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
627 assert(!err);
628
Karl Schultze4dc75c2016-02-02 15:37:51 -0700629 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700630 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
631 .pNext = NULL,
632 .waitSemaphoreCount = 0,
633 .pWaitSemaphores = NULL,
634 .pWaitDstStageMask = NULL,
635 .commandBufferCount = 1,
636 .pCommandBuffers = cmd_bufs,
637 .signalSemaphoreCount = 0,
638 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600639
Tony Barbource7524e2016-07-28 12:01:45 -0600640 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600641 assert(!err);
642
Tony Barbource7524e2016-07-28 12:01:45 -0600643 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600644 assert(!err);
645
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600646 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600647 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600648 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600649}
650
Dave Houlton5fa47912018-02-16 11:02:26 -0700651static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
652 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600653 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600654 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600655
Dave Houlton5fa47912018-02-16 11:02:26 -0700656 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
657 .pNext = NULL,
658 .srcAccessMask = srcAccessMask,
659 .dstAccessMask = 0,
660 .oldLayout = old_image_layout,
661 .newLayout = new_image_layout,
662 .image = image,
663 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600664
Tony Barboureb5077b2016-10-19 15:23:36 -0600665 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700666 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
667 /* Make sure anything that was copying from this image has completed */
668 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
669 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600670
Dave Houlton5fa47912018-02-16 11:02:26 -0700671 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
672 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
673 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700674
Dave Houlton5fa47912018-02-16 11:02:26 -0700675 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
676 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
677 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700678
Dave Houlton5fa47912018-02-16 11:02:26 -0700679 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
680 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
681 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600682
Dave Houlton5fa47912018-02-16 11:02:26 -0700683 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
684 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
685 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600686
Dave Houlton5fa47912018-02-16 11:02:26 -0700687 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
688 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
689 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600690
Dave Houlton5fa47912018-02-16 11:02:26 -0700691 default:
692 image_memory_barrier.dstAccessMask = 0;
693 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600694 }
695
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600696 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600697
Dave Houlton5fa47912018-02-16 11:02:26 -0700698 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600699}
700
Karl Schultze4dc75c2016-02-02 15:37:51 -0700701static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Lenny Komowe830b592018-02-23 16:18:36 -0700702 VkDebugUtilsLabelEXT label;
703 memset(&label, 0, sizeof(label));
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700704 const VkCommandBufferBeginInfo cmd_buf_info = {
705 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
706 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600707 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700708 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700709 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800710 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700711 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
712 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800713 };
714 const VkRenderPassBeginInfo rp_begin = {
715 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
716 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800717 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700718 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800719 .renderArea.offset.x = 0,
720 .renderArea.offset.y = 0,
721 .renderArea.extent.width = demo->width,
722 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600723 .clearValueCount = 2,
724 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700725 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800726 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600727
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600728 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Mark Young66510e52017-11-10 10:05:14 -0700729
730 if (demo->validate) {
731 // Set a name for the command buffer
732 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
733 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
734 .pNext = NULL,
735 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
736 .objectHandle = (uint64_t)cmd_buf,
737 .pObjectName = "CubeDrawCommandBuf",
738 };
739 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
740
741 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
742 label.pNext = NULL;
743 label.pLabelName = "DrawBegin";
744 label.color[0] = 0.4f;
745 label.color[1] = 0.3f;
746 label.color[2] = 0.2f;
747 label.color[3] = 0.1f;
748 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
749 }
750
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600751 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800752 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Mark Young66510e52017-11-10 10:05:14 -0700753
754 if (demo->validate) {
755 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
756 label.pNext = NULL;
757 label.pLabelName = "InsideRenderPass";
758 label.color[0] = 8.4f;
759 label.color[1] = 7.3f;
760 label.color[2] = 6.2f;
761 label.color[3] = 7.1f;
762 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
763 }
764
Karl Schultze4dc75c2016-02-02 15:37:51 -0700765 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700766 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
767 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600768 VkViewport viewport;
769 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700770 viewport.height = (float)demo->height;
771 viewport.width = (float)demo->width;
772 viewport.minDepth = (float)0.0f;
773 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700774 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600775
776 VkRect2D scissor;
777 memset(&scissor, 0, sizeof(scissor));
778 scissor.extent.width = demo->width;
779 scissor.extent.height = demo->height;
780 scissor.offset.x = 0;
781 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700782 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Mark Young66510e52017-11-10 10:05:14 -0700783
784 if (demo->validate) {
785 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
786 label.pNext = NULL;
787 label.pLabelName = "ActualDraw";
Mike Schuchardt4cf3aa42018-02-23 12:44:05 -0700788 label.color[0] = -0.4f;
789 label.color[1] = -0.3f;
790 label.color[2] = -0.2f;
791 label.color[3] = -0.1f;
Mark Young66510e52017-11-10 10:05:14 -0700792 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
793 }
794
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600795 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Mark Young66510e52017-11-10 10:05:14 -0700796 if (demo->validate) {
797 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
798 }
799
Tony Barbour98390392016-07-28 10:50:13 -0600800 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600801 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800802 vkCmdEndRenderPass(cmd_buf);
Mark Young66510e52017-11-10 10:05:14 -0700803 if (demo->validate) {
804 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
805 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600806
Tony Barbour22e06272016-09-07 16:07:56 -0600807 if (demo->separate_present_queue) {
808 // We have to transfer ownership from the graphics queue family to the
809 // present queue family to be able to present. Note that we don't have
810 // to transfer from present queue family back to graphics queue family at
811 // the start of the next frame because we don't care about the image's
812 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700813 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
814 .pNext = NULL,
815 .srcAccessMask = 0,
816 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
817 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
818 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
819 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
820 .dstQueueFamilyIndex = demo->present_queue_family_index,
821 .image = demo->swapchain_image_resources[demo->current_buffer].image,
822 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600823
Dave Houlton5fa47912018-02-16 11:02:26 -0700824 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0,
825 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600826 }
Mark Young66510e52017-11-10 10:05:14 -0700827 if (demo->validate) {
828 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
829 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600830 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600831 assert(!err);
832}
833
Tony Barbour22e06272016-09-07 16:07:56 -0600834void demo_build_image_ownership_cmd(struct demo *demo, int i) {
835 VkResult U_ASSERT_ONLY err;
836
837 const VkCommandBufferBeginInfo cmd_buf_info = {
838 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
839 .pNext = NULL,
840 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
841 .pInheritanceInfo = NULL,
842 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700843 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600844 assert(!err);
845
Dave Houlton5fa47912018-02-16 11:02:26 -0700846 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
847 .pNext = NULL,
848 .srcAccessMask = 0,
849 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
850 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
851 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
852 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
853 .dstQueueFamilyIndex = demo->present_queue_family_index,
854 .image = demo->swapchain_image_resources[i].image,
855 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600856
Dave Houlton5fa47912018-02-16 11:02:26 -0700857 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
858 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700859 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600860 assert(!err);
861}
862
Karl Schultze4dc75c2016-02-02 15:37:51 -0700863void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600864 mat4x4 MVP, Model, VP;
865 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600866 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600867 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600868
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600869 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600870
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700871 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600872 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700873 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600874 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600875
Dave Houlton5fa47912018-02-16 11:02:26 -0700876 err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0,
877 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600878 assert(!err);
879
Karl Schultze4dc75c2016-02-02 15:37:51 -0700880 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600881
Tony Barboura72d9492017-01-11 13:35:09 -0700882 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600883}
884
Ian Elliottd940ca22017-03-22 08:31:27 -0600885void DemoUpdateTargetIPD(struct demo *demo) {
886 // Look at what happened to previous presents, and make appropriate
887 // adjustments in timing:
888 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700889 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600890 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600891
Dave Houlton5fa47912018-02-16 11:02:26 -0700892 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600893 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600894 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700895 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600896 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700897 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600898 assert(!err);
899
900 bool early = false;
901 bool late = false;
902 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700903 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600904 if (!demo->syncd_with_actual_presents) {
905 // This is the first time that we've received an
906 // actualPresentTime for this swapchain. In order to not
907 // perceive these early frames as "late", we need to sync-up
908 // our future desiredPresentTime's with the
909 // actualPresentTime(s) that we're receiving now.
910 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600911
Ian Elliottd940ca22017-03-22 08:31:27 -0600912 // So that we don't suspect any pending presents as late,
913 // record them all as suspected-late presents:
914 demo->last_late_id = demo->next_present_id - 1;
915 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600916 demo->syncd_with_actual_presents = true;
917 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700918 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600919 demo->refresh_duration)) {
920 // This image could have been presented earlier. We don't want
921 // to decrease the target_IPD until we've seen early presents
922 // for at least two seconds.
923 if (demo->last_early_id == past[i].presentID) {
924 // We've now seen two seconds worth of early presents.
925 // Flag it as such, and reset the counter:
926 early = true;
927 demo->last_early_id = 0;
928 } else if (demo->last_early_id == 0) {
929 // This is the first early present we've seen.
930 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700931 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
932 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600933 demo->last_early_id = past[i].presentID + howManyPresents;
934 } else {
935 // We are in the midst of a set of early images,
936 // and so we won't do anything.
937 }
938 late = false;
939 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700940 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600941 // This image was presented after its desired time. Since
942 // there's a delay between calling vkQueuePresentKHR and when
943 // we get the timing data, several presents may have been late.
944 // Thus, we need to threat all of the outstanding presents as
945 // being likely late, so that we only increase the target_IPD
946 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700947 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600948 late = true;
949 // Record the last suspected-late present:
950 demo->last_late_id = demo->next_present_id - 1;
951 } else {
952 // We are in the midst of a set of likely-late images,
953 // and so we won't do anything.
954 }
955 early = false;
956 demo->last_early_id = 0;
957 } else {
958 // Since this image was not presented early or late, reset
959 // any sets of early or late presentIDs:
960 early = false;
961 late = false;
962 calibrate_next = true;
963 demo->last_early_id = 0;
964 demo->last_late_id = 0;
965 }
966 }
967
968 if (early) {
969 // Since we've seen at least two-seconds worth of presnts that
970 // could have occured earlier than desired, let's decrease the
971 // target_IPD (i.e. increase the frame rate):
972 //
973 // TODO(ianelliott): Try to calculate a better target_IPD based
974 // on the most recently-seen present (this is overly-simplistic).
975 demo->refresh_duration_multiplier--;
976 if (demo->refresh_duration_multiplier == 0) {
977 // This should never happen, but in case it does, don't
978 // try to go faster.
979 demo->refresh_duration_multiplier = 1;
980 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700981 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600982 }
983 if (late) {
984 // Since we found a new instance of a late present, we want to
985 // increase the target_IPD (i.e. decrease the frame rate):
986 //
987 // TODO(ianelliott): Try to calculate a better target_IPD based
988 // on the most recently-seen present (this is overly-simplistic).
989 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -0700990 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600991 }
992
993 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700994 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
995 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600996 }
997 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600998}
999
Karl Schultze4dc75c2016-02-02 15:37:51 -07001000static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001001 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001002
Damien Leonec336d822017-04-14 16:10:14 -07001003 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +03001004 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1005 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001006
Tony Barbour8b7c9112017-06-29 13:34:41 -06001007 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -06001008 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -07001009 err =
1010 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1011 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001012
Tony Barbour6914e6d2017-06-02 12:11:29 -06001013 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1014 // demo->swapchain is out of date (e.g. the window was resized) and
1015 // must be recreated:
1016 demo_resize(demo);
1017 } else if (err == VK_SUBOPTIMAL_KHR) {
1018 // demo->swapchain is not as optimal as it could be, but the platform's
1019 // presentation engine will still present the image correctly.
1020 break;
1021 } else {
1022 assert(!err);
1023 }
Tony Barbour8b7c9112017-06-29 13:34:41 -06001024 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001025
Tony Barbour3eafe1f2017-06-14 11:59:55 -06001026 demo_update_data_buffer(demo);
1027
Ian Elliottd940ca22017-03-22 08:31:27 -06001028 if (demo->VK_GOOGLE_display_timing_enabled) {
1029 // Look at what happened to previous presents, and make appropriate
1030 // adjustments in timing:
1031 DemoUpdateTargetIPD(demo);
1032
1033 // Note: a real application would position its geometry to that it's in
1034 // the correct locatoin for when the next image is presented. It might
1035 // also wait, so that there's less latency between any input and when
1036 // the next image is rendered/presented. This demo program is so
1037 // simple that it doesn't do either of those.
1038 }
1039
Tony Barbource7524e2016-07-28 12:01:45 -06001040 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001041 // that the image won't be rendered to until the presentation
1042 // engine has fully released ownership to the application, and it is
1043 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001044 VkPipelineStageFlags pipe_stage_flags;
1045 VkSubmitInfo submit_info;
1046 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1047 submit_info.pNext = NULL;
1048 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1049 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1050 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001051 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001052 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001053 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001054 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001055 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -07001056 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 assert(!err);
1058
Tony Barbour22e06272016-09-07 16:07:56 -06001059 if (demo->separate_present_queue) {
1060 // If we are using separate queues, change image ownership to the
1061 // present queue before presenting, waiting for the draw complete
1062 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001063 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001064 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1065 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001066 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001067 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001068 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001069 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001070 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001071 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1072 assert(!err);
1073 }
1074
1075 // If we are using separate queues we have to wait for image ownership,
1076 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001077 VkPresentInfoKHR present = {
1078 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001079 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001080 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -07001081 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1082 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001083 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001084 .pSwapchains = &demo->swapchain,
1085 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001086 };
1087
Ian Elliott53622a72017-03-28 11:06:33 -06001088 if (demo->VK_KHR_incremental_present_enabled) {
1089 // If using VK_KHR_incremental_present, we provide a hint of the region
1090 // that contains changed content relative to the previously-presented
1091 // image. The implementation can use this hint in order to save
1092 // work/power (by only copying the region in the hint). The
1093 // implementation is free to ignore the hint though, and so we must
1094 // ensure that the entire image has the correctly-drawn content.
1095 uint32_t eighthOfWidth = demo->width / 8;
1096 uint32_t eighthOfHeight = demo->height / 8;
1097 VkRectLayerKHR rect = {
1098 .offset.x = eighthOfWidth,
1099 .offset.y = eighthOfHeight,
1100 .extent.width = eighthOfWidth * 6,
1101 .extent.height = eighthOfHeight * 6,
1102 .layer = 0,
1103 };
1104 VkPresentRegionKHR region = {
1105 .rectangleCount = 1,
1106 .pRectangles = &rect,
1107 };
1108 VkPresentRegionsKHR regions = {
1109 .sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1110 .pNext = present.pNext,
1111 .swapchainCount = present.swapchainCount,
1112 .pRegions = &region,
1113 };
1114 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001115 }
1116
Ian Elliottd940ca22017-03-22 08:31:27 -06001117 if (demo->VK_GOOGLE_display_timing_enabled) {
1118 VkPresentTimeGOOGLE ptime;
1119 if (demo->prev_desired_present_time == 0) {
1120 // This must be the first present for this swapchain.
1121 //
1122 // We don't know where we are relative to the presentation engine's
1123 // display's refresh cycle. We also don't know how long rendering
1124 // takes. Let's make a grossly-simplified assumption that the
1125 // desiredPresentTime should be half way between now and
1126 // now+target_IPD. We will adjust over time.
1127 uint64_t curtime = getTimeInNanoseconds();
1128 if (curtime == 0) {
1129 // Since we didn't find out the current time, don't give a
1130 // desiredPresentTime:
1131 ptime.desiredPresentTime = 0;
1132 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001133 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1134 }
1135 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001136 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001137 }
1138 ptime.presentID = demo->next_present_id++;
1139 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001140
1141 VkPresentTimesInfoGOOGLE present_time = {
1142 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1143 .pNext = present.pNext,
1144 .swapchainCount = present.swapchainCount,
1145 .pTimes = &ptime,
1146 };
1147 if (demo->VK_GOOGLE_display_timing_enabled) {
1148 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001149 }
1150 }
1151
Tony Barboura2a67812016-07-18 13:21:06 -06001152 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001153 demo->frame_index += 1;
1154 demo->frame_index %= FRAME_LAG;
1155
Ian Elliottcf074d32015-10-16 18:02:43 -06001156 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1157 // demo->swapchain is out of date (e.g. the window was resized) and
1158 // must be recreated:
1159 demo_resize(demo);
1160 } else if (err == VK_SUBOPTIMAL_KHR) {
1161 // demo->swapchain is not as optimal as it could be, but the platform's
1162 // presentation engine will still present the image correctly.
1163 } else {
1164 assert(!err);
1165 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001166}
1167
Karl Schultze4dc75c2016-02-02 15:37:51 -07001168static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001169 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001170 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001171
Ian Elliottb08e0d92015-11-20 11:55:46 -07001172 // Check the surface capabilities and formats
1173 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001174 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001175 assert(!err);
1176
Ian Elliott8528eff2015-08-07 15:56:59 -06001177 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001178 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001179 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001180 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001181 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001182 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001183 assert(!err);
1184
Ian Elliotta0781972015-08-21 15:09:33 -06001185 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001186 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1187 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1188 // If the surface size is undefined, the size is set to the size
1189 // of the images requested, which must fit within the minimum and
1190 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001191 swapchainExtent.width = demo->width;
1192 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001193
1194 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1195 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1196 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1197 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1198 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001199
Ian Elliottcf7f7482016-08-19 03:46:58 -06001200 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1201 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1202 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1203 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1204 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001205 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001206 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001207 swapchainExtent = surfCapabilities.currentExtent;
1208 demo->width = surfCapabilities.currentExtent.width;
1209 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001210 }
1211
Jeremy Kniager602e4182018-01-19 10:52:34 -07001212 if (demo->width == 0 || demo->height == 0) {
1213 demo->is_minimized = true;
1214 return;
1215 } else {
1216 demo->is_minimized = false;
1217 }
1218
Tony Barbour66a56c32016-09-21 13:10:56 -06001219 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001220 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001221 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001222
Ian Elliottb18fdde2016-10-03 12:11:12 -06001223 // There are times when you may wish to use another present mode. The
1224 // following code shows how to select them, and the comments provide some
1225 // reasons you may wish to use them.
1226 //
1227 // It should be noted that Vulkan 1.0 doesn't provide a method for
1228 // synchronizing rendering with the presentation engine's display. There
1229 // is a method provided for throttling rendering with the display, but
1230 // there are some presentation engines for which this method will not work.
1231 // If an application doesn't throttle its rendering, and if it renders much
1232 // faster than the refresh rate of the display, this can waste power on
1233 // mobile devices. That is because power is being spent rendering images
1234 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001235
Ian Elliottb18fdde2016-10-03 12:11:12 -06001236 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1237 // tearing, or have some way of synchronizing their rendering with the
1238 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001239 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1240 // generally render a new presentable image every refresh cycle, but are
1241 // occasionally early. In this case, the application wants the new image
1242 // to be displayed instead of the previously-queued-for-presentation image
1243 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001244 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1245 // render a new presentable image every refresh cycle, but are occasionally
1246 // late. In this case (perhaps because of stuttering/latency concerns),
1247 // the application wants the late image to be immediately displayed, even
1248 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001249
Dave Houlton5fa47912018-02-16 11:02:26 -07001250 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001251 for (size_t i = 0; i < presentModeCount; ++i) {
1252 if (presentModes[i] == demo->presentMode) {
1253 swapchainPresentMode = demo->presentMode;
1254 break;
1255 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001256 }
1257 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001258 if (swapchainPresentMode != demo->presentMode) {
1259 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1260 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001261
Tony Barbour84376fe2017-01-06 15:54:22 -07001262 // Determine the number of VkImages to use in the swap chain.
1263 // Application desires to acquire 3 images at a time for triple
1264 // buffering
1265 uint32_t desiredNumOfSwapchainImages = 3;
1266 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1267 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1268 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001269 // If maxImageCount is 0, we can ask for as many images as we want;
1270 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001271 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001272 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001273 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001274 }
1275
Tony Barbour9fd6d352015-09-16 15:01:32 -06001276 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001277 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001278 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001279 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001280 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001281 }
1282
Tony Barbour820b55b2017-03-14 08:55:46 -06001283 // Find a supported composite alpha mode - one of these is guaranteed to be set
1284 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1285 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1286 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1287 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1288 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1289 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1290 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001291 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001292 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1293 compositeAlpha = compositeAlphaFlags[i];
1294 break;
1295 }
1296 }
1297
Tony Barboura2a67812016-07-18 13:21:06 -06001298 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001299 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001300 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001301 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001302 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001303 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001304 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001305 .imageExtent =
1306 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001307 .width = swapchainExtent.width,
1308 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001309 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001310 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001311 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001312 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001313 .imageArrayLayers = 1,
1314 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1315 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001316 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001317 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001318 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001319 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001320 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001321 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001322 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001323 assert(!err);
1324
Ian Elliottcf074d32015-10-16 18:02:43 -06001325 // If we just re-created an existing swapchain, we should destroy the old
1326 // swapchain at this point.
1327 // Note: destroying the swapchain also cleans up all its associated
1328 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001329 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001330 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001331 }
1332
Dave Houlton5fa47912018-02-16 11:02:26 -07001333 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001334 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001335
Dave Houlton5fa47912018-02-16 11:02:26 -07001336 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001337 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001338 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001339 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001340
Dave Houlton5fa47912018-02-16 11:02:26 -07001341 demo->swapchain_image_resources =
1342 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001343 assert(demo->swapchain_image_resources);
1344
Ian Elliotta0781972015-08-21 15:09:33 -06001345 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001346 VkImageViewCreateInfo color_image_view = {
1347 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001348 .pNext = NULL,
1349 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001350 .components =
1351 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001352 .r = VK_COMPONENT_SWIZZLE_R,
1353 .g = VK_COMPONENT_SWIZZLE_G,
1354 .b = VK_COMPONENT_SWIZZLE_B,
1355 .a = VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001356 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001357 .subresourceRange =
1358 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001359 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1360 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001361 };
1362
Tony Barboura72d9492017-01-11 13:35:09 -07001363 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001364
Tony Barboura72d9492017-01-11 13:35:09 -07001365 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001366
Dave Houlton5fa47912018-02-16 11:02:26 -07001367 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001368 assert(!err);
1369 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001370
Ian Elliottd940ca22017-03-22 08:31:27 -06001371 if (demo->VK_GOOGLE_display_timing_enabled) {
1372 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001373 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001374 assert(!err);
1375 demo->refresh_duration = rc_dur.refreshDuration;
1376
1377 demo->syncd_with_actual_presents = false;
1378 // Initially target 1X the refresh duration:
1379 demo->target_IPD = demo->refresh_duration;
1380 demo->refresh_duration_multiplier = 1;
1381 demo->prev_desired_present_time = 0;
1382 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001383 }
1384
Mark Young04fd3d82016-01-25 14:43:50 -07001385 if (NULL != presentModes) {
1386 free(presentModes);
1387 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388}
1389
Karl Schultze4dc75c2016-02-02 15:37:51 -07001390static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001391 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001392 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001393 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001394 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001395 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001397 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001398 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001399 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001400 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001401 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001402 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001403 .flags = 0,
1404 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001405
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001406 VkImageViewCreateInfo view = {
1407 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001409 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001410 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001411 .subresourceRange =
1412 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001414 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001416
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001417 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001418 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001419 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001420
1421 demo->depth.format = depth_format;
1422
1423 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001424 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425 assert(!err);
1426
Karl Schultze4dc75c2016-02-02 15:37:51 -07001427 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001428 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001429
Chia-I Wuf48700b2015-11-10 16:21:09 +08001430 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001431 demo->depth.mem_alloc.pNext = NULL;
1432 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1433 demo->depth.mem_alloc.memoryTypeIndex = 0;
1434
Dave Houlton5fa47912018-02-16 11:02:26 -07001435 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001436 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001437 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001439 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001440 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001441 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001442
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001443 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001444 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001445 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001446
1447 /* create image view */
1448 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001449 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001450 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001451}
1452
Tony Barbour1a86d032015-09-21 15:17:33 -06001453/* Load a ppm file into memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001454bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001455#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Dave Houlton5fa47912018-02-16 11:02:26 -07001456 filename = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001457#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001458
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001459#ifdef __ANDROID__
1460#include <lunarg.ppm.h>
1461 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
1480 for (int y = 0; y < *height; y++) {
1481 uint8_t *rowPtr = rgba_data;
1482 for (int x = 0; x < *width; x++) {
1483 memcpy(rowPtr, cPtr, 3);
1484 rowPtr[3] = 255; /* Alpha of 1 */
1485 rowPtr += 4;
1486 cPtr += 3;
1487 }
1488 rgba_data += layout->rowPitch;
1489 }
1490
1491 return true;
1492#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001493 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001494 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001495
Dave Houlton5fa47912018-02-16 11:02:26 -07001496 if (!fPtr) return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001497
Dave Houlton5fa47912018-02-16 11:02:26 -07001498 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001499 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1500 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001501 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001502 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001503
Tony Barbour1a86d032015-09-21 15:17:33 -06001504 do {
1505 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001506 if (cPtr == NULL) {
1507 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001508 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001509 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001510 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001511
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001512 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001513 if (rgba_data == NULL) {
1514 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001515 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001516 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001517 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001518 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001519 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1520 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001521 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001522 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001523
Karl Schultze4dc75c2016-02-02 15:37:51 -07001524 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001525 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001526 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001527 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001528 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001529 rowPtr[3] = 255; /* Alpha of 1 */
1530 rowPtr += 4;
1531 }
1532 rgba_data += layout->rowPitch;
1533 }
1534 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001535 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001536#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001537}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001538
Dave Houlton5fa47912018-02-16 11:02:26 -07001539static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1540 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001541 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001542 int32_t tex_width;
1543 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001544 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001545 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001546
Karl Schultze4dc75c2016-02-02 15:37:51 -07001547 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001548 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001549 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001550
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001551 tex_obj->tex_width = tex_width;
1552 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001553
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001554 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001555 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001556 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001557 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001558 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001559 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001560 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001561 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001562 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001563 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001564 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001565 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001566 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001567 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001568
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001569 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001570
Dave Houlton5fa47912018-02-16 11:02:26 -07001571 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001572 assert(!err);
1573
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001574 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001575
Chia-I Wuf48700b2015-11-10 16:21:09 +08001576 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001577 tex_obj->mem_alloc.pNext = NULL;
1578 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1579 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001580
Dave Houlton5fa47912018-02-16 11:02:26 -07001581 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001582 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001583
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001584 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001585 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001586 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001587
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001588 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001589 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001590 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001591
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001592 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001593 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001594 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001595 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001596 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001597 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001598 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001599 void *data;
1600
Dave Houlton5fa47912018-02-16 11:02:26 -07001601 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001602
Dave Houlton5fa47912018-02-16 11:02:26 -07001603 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001604 assert(!err);
1605
1606 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1607 fprintf(stderr, "Error loading texture: %s\n", filename);
1608 }
1609
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001610 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001611 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001612
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001613 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001614}
1615
Dave Houlton5fa47912018-02-16 11:02:26 -07001616static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001617 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001618 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1619 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001620}
1621
Karl Schultze4dc75c2016-02-02 15:37:51 -07001622static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001623 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001624 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001625 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001626
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001627 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001628
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001629 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001630 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001631
Dave Houlton5fa47912018-02-16 11:02:26 -07001632 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001633 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001634 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1635 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001636 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1637 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001638 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1639 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001640 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001641 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001642 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001643 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001644
Tony Barbour16156cb2016-10-19 14:15:49 -06001645 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Dave Houlton5fa47912018-02-16 11:02:26 -07001646 demo_prepare_texture_image(demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
1647 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1648 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001649
Dave Houlton5fa47912018-02-16 11:02:26 -07001650 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1651 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1652 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001653
Dave Houlton5fa47912018-02-16 11:02:26 -07001654 demo_set_image_layout(demo, demo->staging_texture.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1655 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001656 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001657
Dave Houlton5fa47912018-02-16 11:02:26 -07001658 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1659 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001660 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001661
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001662 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001663 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1664 .srcOffset = {0, 0, 0},
1665 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1666 .dstOffset = {0, 0, 0},
Dave Houlton5fa47912018-02-16 11:02:26 -07001667 .extent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001668 };
Dave Houlton5fa47912018-02-16 11:02:26 -07001669 vkCmdCopyImage(demo->cmd, demo->staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1670 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001671
Dave Houlton5fa47912018-02-16 11:02:26 -07001672 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1673 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001674 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001675
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001676 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001677 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1678 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001679 }
1680
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001681 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001682 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001683 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001684 .magFilter = VK_FILTER_NEAREST,
1685 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001686 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001687 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1688 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1689 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001690 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001691 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001692 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001693 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694 .minLod = 0.0f,
1695 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001696 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001697 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001698 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001699
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001700 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001701 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001702 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001703 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001704 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001705 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001706 .components =
1707 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001708 VK_COMPONENT_SWIZZLE_R,
1709 VK_COMPONENT_SWIZZLE_G,
1710 VK_COMPONENT_SWIZZLE_B,
1711 VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001712 },
1713 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001714 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001715 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001716
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001717 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001718 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719 assert(!err);
1720
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001721 /* create image view */
1722 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001723 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001724 assert(!err);
1725 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001726}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727
Tony Barboura72d9492017-01-11 13:35:09 -07001728void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001729 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001730 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001731 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001732 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001733 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001734 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001735 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001736 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001737
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001738 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001739 mat4x4_mul(MVP, VP, demo->model_matrix);
1740 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001741 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001742
Karl Schultza2615462017-01-20 13:19:20 -07001743 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001744 data.position[i][0] = g_vertex_buffer_data[i * 3];
1745 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1746 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001747 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001748 data.attr[i][0] = g_uv_buffer_data[2 * i];
1749 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001750 data.attr[i][2] = 0;
1751 data.attr[i][3] = 0;
1752 }
1753
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001754 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001755 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001756 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001757 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001758
Tony Barboura72d9492017-01-11 13:35:09 -07001759 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001760 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001761 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001762
Dave Houlton5fa47912018-02-16 11:02:26 -07001763 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001764
Tony Barboura72d9492017-01-11 13:35:09 -07001765 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1766 mem_alloc.pNext = NULL;
1767 mem_alloc.allocationSize = mem_reqs.size;
1768 mem_alloc.memoryTypeIndex = 0;
1769
Dave Houlton5fa47912018-02-16 11:02:26 -07001770 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1771 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1772 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001773 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001774
Dave Houlton5fa47912018-02-16 11:02:26 -07001775 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001776 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001777
Dave Houlton5fa47912018-02-16 11:02:26 -07001778 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 -07001779 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001780
Tony Barboura72d9492017-01-11 13:35:09 -07001781 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001782
Tony Barboura72d9492017-01-11 13:35:09 -07001783 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001784
Tony Barboura72d9492017-01-11 13:35:09 -07001785 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001786 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001787 assert(!err);
1788 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001789}
1790
Karl Schultze4dc75c2016-02-02 15:37:51 -07001791static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001792 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001793 [0] =
1794 {
1795 .binding = 0,
1796 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1797 .descriptorCount = 1,
1798 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1799 .pImmutableSamplers = NULL,
1800 },
1801 [1] =
1802 {
1803 .binding = 1,
1804 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1805 .descriptorCount = DEMO_TEXTURE_COUNT,
1806 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1807 .pImmutableSamplers = NULL,
1808 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001809 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001810 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001811 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001812 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001813 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001814 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001815 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001816 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001817
Dave Houlton5fa47912018-02-16 11:02:26 -07001818 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001819 assert(!err);
1820
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001821 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001822 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1823 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001824 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001825 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001826 };
1827
Dave Houlton5fa47912018-02-16 11:02:26 -07001828 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001829 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830}
1831
Karl Schultze4dc75c2016-02-02 15:37:51 -07001832static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001833 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1834 // because at the start of the renderpass, we don't care about their contents.
1835 // At the start of the subpass, the color attachment's layout will be transitioned
1836 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1837 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1838 // the renderpass, the color attachment's layout will be transitioned to
1839 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1840 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001841 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001842 [0] =
1843 {
1844 .format = demo->format,
1845 .flags = 0,
1846 .samples = VK_SAMPLE_COUNT_1_BIT,
1847 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1848 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1849 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1850 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1851 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1852 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1853 },
1854 [1] =
1855 {
1856 .format = demo->depth.format,
1857 .flags = 0,
1858 .samples = VK_SAMPLE_COUNT_1_BIT,
1859 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1860 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1861 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1862 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1863 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1864 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1865 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001866 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001867 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001868 .attachment = 0,
1869 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001870 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001871 const VkAttachmentReference depth_reference = {
1872 .attachment = 1,
1873 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1874 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001875 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001876 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1877 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001878 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001879 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001880 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001881 .pColorAttachments = &color_reference,
1882 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001883 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001884 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001885 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001886 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001887 const VkRenderPassCreateInfo rp_info = {
1888 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1889 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001890 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001891 .attachmentCount = 2,
1892 .pAttachments = attachments,
1893 .subpassCount = 1,
1894 .pSubpasses = &subpass,
1895 .dependencyCount = 0,
1896 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001897 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001898 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001899
Chia-I Wu63636062015-10-26 21:10:41 +08001900 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001901 assert(!err);
1902}
1903
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001904static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001905 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001906 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001907 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001908
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001909 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1910 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001911 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001912 moduleCreateInfo.codeSize = size;
1913 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001914
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001915 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1916 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001917
1918 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001919}
1920
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001921static void demo_prepare_vs(struct demo *demo) {
1922 const uint32_t vs_code[] = {
1923#include "cube.vert.inc"
1924 };
1925 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001926}
1927
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001928static void demo_prepare_fs(struct demo *demo) {
1929 const uint32_t fs_code[] = {
1930#include "cube.frag.inc"
1931 };
1932 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001933}
1934
Karl Schultze4dc75c2016-02-02 15:37:51 -07001935static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001936 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001937 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001938 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001939 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001940 VkPipelineRasterizationStateCreateInfo rs;
1941 VkPipelineColorBlendStateCreateInfo cb;
1942 VkPipelineDepthStencilStateCreateInfo ds;
1943 VkPipelineViewportStateCreateInfo vp;
1944 VkPipelineMultisampleStateCreateInfo ms;
1945 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1946 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001947 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001948
Piers Daniellba839d12015-09-29 13:01:09 -06001949 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1950 memset(&dynamicState, 0, sizeof dynamicState);
1951 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1952 dynamicState.pDynamicStates = dynamicStateEnables;
1953
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001954 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001955 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001956 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001957
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001958 memset(&vi, 0, sizeof(vi));
1959 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1960
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001961 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001962 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001963 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964
1965 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001966 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1967 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001968 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001969 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001970 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001971 rs.rasterizerDiscardEnable = VK_FALSE;
1972 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001973 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001974
1975 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001976 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1977 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001978 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001979 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001980 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001981 cb.attachmentCount = 1;
1982 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001983
Tony Barbour29645d02015-01-16 14:27:35 -07001984 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001985 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001986 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001987 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001988 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001989 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001990
1991 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001992 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001993 ds.depthTestEnable = VK_TRUE;
1994 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001995 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001996 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001997 ds.back.failOp = VK_STENCIL_OP_KEEP;
1998 ds.back.passOp = VK_STENCIL_OP_KEEP;
1999 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002000 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002001 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002002
Tony Barbour29645d02015-01-16 14:27:35 -07002003 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002004 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002005 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002006 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002007
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002008 demo_prepare_vs(demo);
2009 demo_prepare_fs(demo);
2010
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002011 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002012 VkPipelineShaderStageCreateInfo shaderStages[2];
2013 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2014
Karl Schultze4dc75c2016-02-02 15:37:51 -07002015 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2016 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002017 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002018 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002019
Karl Schultze4dc75c2016-02-02 15:37:51 -07002020 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2021 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002022 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002023 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002024
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002025 memset(&pipelineCache, 0, sizeof(pipelineCache));
2026 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002027
Dave Houlton5fa47912018-02-16 11:02:26 -07002028 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002029 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002030
Karl Schultze4dc75c2016-02-02 15:37:51 -07002031 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002032 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002033 pipeline.pRasterizationState = &rs;
2034 pipeline.pColorBlendState = &cb;
2035 pipeline.pMultisampleState = &ms;
2036 pipeline.pViewportState = &vp;
2037 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002038 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002039 pipeline.pStages = shaderStages;
2040 pipeline.renderPass = demo->render_pass;
2041 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002042
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002043 pipeline.renderPass = demo->render_pass;
2044
Dave Houlton5fa47912018-02-16 11:02:26 -07002045 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002046 assert(!err);
2047
Chia-I Wu63636062015-10-26 21:10:41 +08002048 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2049 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050}
2051
Karl Schultze4dc75c2016-02-02 15:37:51 -07002052static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002053 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07002054 [0] =
2055 {
2056 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2057 .descriptorCount = demo->swapchainImageCount,
2058 },
2059 [1] =
2060 {
2061 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2062 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2063 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002064 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002065 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002066 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002067 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002068 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002069 .poolSizeCount = 2,
2070 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002071 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002072 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002073
Dave Houlton5fa47912018-02-16 11:02:26 -07002074 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002075 assert(!err);
2076}
2077
Karl Schultze4dc75c2016-02-02 15:37:51 -07002078static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002079 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002080 VkWriteDescriptorSet writes[2];
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 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2084 .pNext = NULL,
2085 .descriptorPool = demo->desc_pool,
2086 .descriptorSetCount = 1,
2087 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002088
2089 VkDescriptorBufferInfo buffer_info;
2090 buffer_info.offset = 0;
2091 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002092
Chia-I Wuae721ba2015-05-25 16:27:55 +08002093 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002094 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002095 tex_descs[i].sampler = demo->textures[i].sampler;
2096 tex_descs[i].imageView = demo->textures[i].view;
2097 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002098 }
2099
2100 memset(&writes, 0, sizeof(writes));
2101
2102 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002103 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002104 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002105 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002106
2107 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002108 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002109 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002110 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002111 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002112
Tony Barboura72d9492017-01-11 13:35:09 -07002113 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2114 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2115 assert(!err);
2116 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2117 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2118 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2119 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2120 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002121}
2122
Karl Schultze4dc75c2016-02-02 15:37:51 -07002123static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002124 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002125 attachments[1] = demo->depth.view;
2126
Chia-I Wuf973e322015-07-08 13:34:24 +08002127 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002128 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2129 .pNext = NULL,
2130 .renderPass = demo->render_pass,
2131 .attachmentCount = 2,
2132 .pAttachments = attachments,
2133 .width = demo->width,
2134 .height = demo->height,
2135 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002136 };
2137 VkResult U_ASSERT_ONLY err;
2138 uint32_t i;
2139
Tony Barbourd8e504f2015-10-08 14:26:24 -06002140 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002141 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002142 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002143 assert(!err);
2144 }
2145}
2146
Karl Schultze4dc75c2016-02-02 15:37:51 -07002147static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002148 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002149 if (demo->cmd_pool == VK_NULL_HANDLE) {
2150 const VkCommandPoolCreateInfo cmd_pool_info = {
2151 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2152 .pNext = NULL,
2153 .queueFamilyIndex = demo->graphics_queue_family_index,
2154 .flags = 0,
2155 };
2156 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2157 assert(!err);
2158 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002159
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002160 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002161 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002162 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002163 .commandPool = demo->cmd_pool,
2164 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002165 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002166 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002167 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2168 assert(!err);
2169 VkCommandBufferBeginInfo cmd_buf_info = {
2170 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2171 .pNext = NULL,
2172 .flags = 0,
2173 .pInheritanceInfo = NULL,
2174 };
2175 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2176 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002177
2178 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002179
2180 if (demo->is_minimized) {
2181 demo->prepared = false;
2182 return;
2183 }
2184
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002185 demo_prepare_depth(demo);
2186 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002187 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002188
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002189 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002190 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002191 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002192
Ian Elliotta0781972015-08-21 15:09:33 -06002193 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002194 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002195 assert(!err);
2196 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002197
Tony Barbour22e06272016-09-07 16:07:56 -06002198 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002199 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002200 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2201 .pNext = NULL,
2202 .queueFamilyIndex = demo->present_queue_family_index,
2203 .flags = 0,
2204 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002205 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002206 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002207 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002208 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2209 .pNext = NULL,
2210 .commandPool = demo->present_cmd_pool,
2211 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2212 .commandBufferCount = 1,
2213 };
2214 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002215 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2216 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002217 assert(!err);
2218 demo_build_image_ownership_cmd(demo, i);
2219 }
2220 }
2221
Chia-I Wu63ea9262015-03-26 13:14:16 +08002222 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002223 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002224
Chia-I Wuf973e322015-07-08 13:34:24 +08002225 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002226
Ian Elliotta0781972015-08-21 15:09:33 -06002227 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002228 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002229 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002230 }
2231
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002232 /*
2233 * Prepare functions above may generate pipeline commands
2234 * that need to be flushed before beginning the render loop.
2235 */
2236 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002237 if (demo->staging_texture.image) {
2238 demo_destroy_texture_image(demo, &demo->staging_texture);
2239 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002240
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002241 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002242 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002243}
2244
Karl Schultze4dc75c2016-02-02 15:37:51 -07002245static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002246 uint32_t i;
2247
2248 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002249 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002250
Tony Barbour66a56c32016-09-21 13:10:56 -06002251 // Wait for fences from present operations
2252 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002253 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002254 vkDestroyFence(demo->device, demo->fences[i], NULL);
2255 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2256 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2257 if (demo->separate_present_queue) {
2258 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2259 }
2260 }
2261
Jeremy Kniager602e4182018-01-19 10:52:34 -07002262 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2263 if (!demo->is_minimized) {
2264 for (i = 0; i < demo->swapchainImageCount; i++) {
2265 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2266 }
2267 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002268
Jeremy Kniager602e4182018-01-19 10:52:34 -07002269 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2270 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2271 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2272 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2273 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002274
Jeremy Kniager602e4182018-01-19 10:52:34 -07002275 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2276 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2277 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2278 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2279 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2280 }
2281 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002282
Jeremy Kniager602e4182018-01-19 10:52:34 -07002283 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2284 vkDestroyImage(demo->device, demo->depth.image, NULL);
2285 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002286
Jeremy Kniager602e4182018-01-19 10:52:34 -07002287 for (i = 0; i < demo->swapchainImageCount; i++) {
2288 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2289 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2290 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2291 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2292 }
2293 free(demo->swapchain_image_resources);
2294 free(demo->queue_props);
2295 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002296
Jeremy Kniager602e4182018-01-19 10:52:34 -07002297 if (demo->separate_present_queue) {
2298 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2299 }
Tony Barbour22e06272016-09-07 16:07:56 -06002300 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002301 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002302 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002303 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07002304 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002305 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002306 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002307
Tony Barbour153cb062016-12-07 13:43:36 -07002308#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002309 XDestroyWindow(demo->display, demo->xlib_window);
2310 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002311#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002312 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002313 xcb_disconnect(demo->connection);
2314 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002315#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002316 wl_keyboard_destroy(demo->keyboard);
2317 wl_pointer_destroy(demo->pointer);
2318 wl_seat_destroy(demo->seat);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002319 wl_shell_surface_destroy(demo->shell_surface);
2320 wl_surface_destroy(demo->window);
2321 wl_shell_destroy(demo->shell);
2322 wl_compositor_destroy(demo->compositor);
2323 wl_registry_destroy(demo->registry);
2324 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002325#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002326#endif
Karl Schultz86429112017-06-20 11:27:59 -06002327
2328 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002329}
2330
Karl Schultze4dc75c2016-02-02 15:37:51 -07002331static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002332 uint32_t i;
2333
Mike Stroyanbb60b382015-10-28 09:46:57 -06002334 // Don't react to resize until after first initialization.
2335 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002336 if (demo->is_minimized) {
2337 demo_prepare(demo);
2338 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002339 return;
2340 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002341 // In order to properly resize the window, we must re-create the swapchain
2342 // AND redo the command buffers, etc.
2343 //
2344 // First, perform part of the demo_cleanup() function:
2345 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002346 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002347
2348 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002349 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002350 }
Chia-I Wu63636062015-10-26 21:10:41 +08002351 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002352
Chia-I Wu63636062015-10-26 21:10:41 +08002353 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2354 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2355 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2356 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2357 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002358
2359 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002360 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2361 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2362 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2363 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002364 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002365
Chia-I Wu63636062015-10-26 21:10:41 +08002366 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2367 vkDestroyImage(demo->device, demo->depth.image, NULL);
2368 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002369
Ian Elliottcf074d32015-10-16 18:02:43 -06002370 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002371 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002372 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002373 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2374 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002375 }
Chia-I Wu63636062015-10-26 21:10:41 +08002376 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002377 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002378 if (demo->separate_present_queue) {
2379 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2380 }
Tony Barboura72d9492017-01-11 13:35:09 -07002381 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002382
Ian Elliottcf074d32015-10-16 18:02:43 -06002383 // Second, re-perform the demo_prepare() function, which will re-create the
2384 // swapchain:
2385 demo_prepare(demo);
2386}
2387
David Pinedo2bb7c932015-06-18 17:03:14 -06002388// On MS-Windows, make this a global, so it's available to WndProc()
2389struct demo demo;
2390
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002391#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002392static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002393 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002394
Ian Elliott639ca472015-04-16 15:23:05 -06002395 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002396 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002397 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002398 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002399 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002400}
Ian Elliott639ca472015-04-16 15:23:05 -06002401
2402// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002403LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2404 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002405 case WM_CLOSE:
2406 PostQuitMessage(validation_error);
2407 break;
2408 case WM_PAINT:
2409 // The validation callback calls MessageBox which can generate paint
2410 // events - don't make more Vulkan calls if we got here from the
2411 // callback
2412 if (!in_callback) {
2413 demo_run(&demo);
2414 }
2415 break;
2416 case WM_GETMINMAXINFO: // set window's minimum size
2417 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2418 return 0;
2419 case WM_SIZE:
2420 // Resize the application to the new window size, except when
2421 // it was minimized. Vulkan doesn't support images or swapchains
2422 // with width=0 and height=0.
2423 if (wParam != SIZE_MINIMIZED) {
2424 demo.width = lParam & 0xffff;
2425 demo.height = (lParam & 0xffff0000) >> 16;
2426 demo_resize(&demo);
2427 }
2428 break;
2429 default:
2430 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002431 }
2432 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2433}
2434
Karl Schultze4dc75c2016-02-02 15:37:51 -07002435static void demo_create_window(struct demo *demo) {
2436 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002437
2438 // Initialize the window class structure:
2439 win_class.cbSize = sizeof(WNDCLASSEX);
2440 win_class.style = CS_HREDRAW | CS_VREDRAW;
2441 win_class.lpfnWndProc = WndProc;
2442 win_class.cbClsExtra = 0;
2443 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002444 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002445 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2446 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2447 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2448 win_class.lpszMenuName = NULL;
2449 win_class.lpszClassName = demo->name;
2450 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2451 // Register window class:
2452 if (!RegisterClassEx(&win_class)) {
2453 // It didn't work, so try to give a useful error:
2454 printf("Unexpected error trying to start the application!\n");
2455 fflush(stdout);
2456 exit(1);
2457 }
2458 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002459 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002460 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002461 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002462 demo->name, // class name
2463 demo->name, // app name
2464 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002465 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002466 100, 100, // x/y coords
2467 wr.right - wr.left, // width
2468 wr.bottom - wr.top, // height
2469 NULL, // handle to parent
2470 NULL, // handle to menu
2471 demo->connection, // hInstance
2472 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002473 if (!demo->window) {
2474 // It didn't work, so try to give a useful error:
2475 printf("Cannot create a window in which to draw!\n");
2476 fflush(stdout);
2477 exit(1);
2478 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002479 // Window client area size must be at least 1 pixel high, to prevent crash.
2480 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002481 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002482}
Tony Barbour8295ac42016-08-08 11:04:17 -06002483#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002484static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002485 const char *display_envar = getenv("DISPLAY");
2486 if (display_envar == NULL || display_envar[0] == '\0') {
2487 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2488 fflush(stdout);
2489 exit(1);
2490 }
Tony Barbour2593b182016-04-19 10:57:58 -06002491
Tony Barbouree9cd372017-01-31 16:09:58 -07002492 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002493 demo->display = XOpenDisplay(NULL);
2494 long visualMask = VisualScreenMask;
2495 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002496 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002497 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002498 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002499
Dave Houlton5fa47912018-02-16 11:02:26 -07002500 Colormap colormap =
2501 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002502
Dave Houlton5fa47912018-02-16 11:02:26 -07002503 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002504 windowAttributes.colormap = colormap;
2505 windowAttributes.background_pixel = 0xFFFFFFFF;
2506 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002507 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002508
Dave Houlton5fa47912018-02-16 11:02:26 -07002509 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2510 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2511 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002512
2513 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2514 XMapWindow(demo->display, demo->xlib_window);
2515 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002516 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002517}
2518static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002519 switch (event->type) {
2520 case ClientMessage:
2521 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002522 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002523 case KeyPress:
2524 switch (event->xkey.keycode) {
2525 case 0x9: // Escape
2526 demo->quit = true;
2527 break;
2528 case 0x71: // left arrow key
2529 demo->spin_angle -= demo->spin_increment;
2530 break;
2531 case 0x72: // right arrow key
2532 demo->spin_angle += demo->spin_increment;
2533 break;
2534 case 0x41: // space bar
2535 demo->pause = !demo->pause;
2536 break;
2537 }
Tony Barbour2593b182016-04-19 10:57:58 -06002538 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002539 case ConfigureNotify:
2540 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2541 demo->width = event->xconfigure.width;
2542 demo->height = event->xconfigure.height;
2543 demo_resize(demo);
2544 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002545 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002546 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002547 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002548 }
Tony Barbour2593b182016-04-19 10:57:58 -06002549}
2550
2551static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002552 while (!demo->quit) {
2553 XEvent event;
2554
2555 if (demo->pause) {
2556 XNextEvent(demo->display, &event);
2557 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002558 }
2559 while (XPending(demo->display) > 0) {
2560 XNextEvent(demo->display, &event);
2561 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002562 }
2563
Tony Barbour2593b182016-04-19 10:57:58 -06002564 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002565 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002566 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002567 }
2568}
Tony Barbour153cb062016-12-07 13:43:36 -07002569#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002570static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002571 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002572 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002573 case XCB_EXPOSE:
2574 // TODO: Resize window
2575 break;
2576 case XCB_CLIENT_MESSAGE:
2577 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2578 demo->quit = true;
2579 }
2580 break;
2581 case XCB_KEY_RELEASE: {
2582 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002583
Dave Houlton5fa47912018-02-16 11:02:26 -07002584 switch (key->detail) {
2585 case 0x9: // Escape
2586 demo->quit = true;
2587 break;
2588 case 0x71: // left arrow key
2589 demo->spin_angle -= demo->spin_increment;
2590 break;
2591 case 0x72: // right arrow key
2592 demo->spin_angle += demo->spin_increment;
2593 break;
2594 case 0x41: // space bar
2595 demo->pause = !demo->pause;
2596 break;
2597 }
2598 } break;
2599 case XCB_CONFIGURE_NOTIFY: {
2600 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2601 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2602 demo->width = cfg->width;
2603 demo->height = cfg->height;
2604 demo_resize(demo);
2605 }
2606 } break;
2607 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002608 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002609 }
2610}
2611
Tony Barbour2593b182016-04-19 10:57:58 -06002612static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002613 xcb_flush(demo->connection);
2614
2615 while (!demo->quit) {
2616 xcb_generic_event_t *event;
2617
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002618 if (demo->pause) {
2619 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002620 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002621 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002622 }
2623 while (event) {
2624 demo_handle_xcb_event(demo, event);
2625 free(event);
2626 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002627 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002628
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002629 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002630 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002631 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002632 }
2633}
2634
Tony Barbour2593b182016-04-19 10:57:58 -06002635static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002636 uint32_t value_mask, value_list[32];
2637
Tony Barbour2593b182016-04-19 10:57:58 -06002638 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002639
2640 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2641 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002642 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002643
Dave Houlton5fa47912018-02-16 11:02:26 -07002644 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2645 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002646
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002647 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002648 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2649 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002650
Dave Houlton5fa47912018-02-16 11:02:26 -07002651 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2652 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002653
Dave Houlton5fa47912018-02-16 11:02:26 -07002654 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002655 &(*demo->atom_wm_delete_window).atom);
2656 free(reply);
2657
Tony Barbour2593b182016-04-19 10:57:58 -06002658 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002659
Karl Schultze4dc75c2016-02-02 15:37:51 -07002660 // Force the x/y coordinates to 100,100 results are identical in consecutive
2661 // runs
2662 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002663 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002664}
Tony Barbour6b131662016-08-25 13:14:45 -06002665// VK_USE_PLATFORM_XCB_KHR
2666#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002667static void demo_run(struct demo *demo) {
2668 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002669 if (demo->pause) {
2670 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002671 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002672 wl_display_dispatch_pending(demo->display); // don't block
2673 demo_draw(demo);
2674 demo->curFrame++;
2675 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2676 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002677 }
2678}
2679
Dave Houlton5fa47912018-02-16 11:02:26 -07002680static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002681 wl_shell_surface_pong(shell_surface, serial);
2682}
2683
Dave Houlton5fa47912018-02-16 11:02:26 -07002684static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED,
2685 int32_t width UNUSED, int32_t height UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002686
Dave Houlton5fa47912018-02-16 11:02:26 -07002687static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002688
Dave Houlton5fa47912018-02-16 11:02:26 -07002689static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002690
2691static void demo_create_window(struct demo *demo) {
2692 demo->window = wl_compositor_create_surface(demo->compositor);
2693 if (!demo->window) {
2694 printf("Can not create wayland_surface from compositor!\n");
2695 fflush(stdout);
2696 exit(1);
2697 }
2698
2699 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2700 if (!demo->shell_surface) {
2701 printf("Can not get shell_surface from wayland_surface!\n");
2702 fflush(stdout);
2703 exit(1);
2704 }
Dave Houlton5fa47912018-02-16 11:02:26 -07002705 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002706 wl_shell_surface_set_toplevel(demo->shell_surface);
2707 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2708}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002709#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2710static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002711 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002712
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002713 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002714 demo->curFrame++;
2715}
Karl Schultz206b1c52018-04-13 18:02:07 -06002716#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2717static void demo_run(struct demo *demo) {
2718 demo_draw(demo);
2719 demo->curFrame++;
2720 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2721 demo->quit = TRUE;
2722 }
2723}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002724#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002725#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2726static VkResult demo_create_display_surface(struct demo *demo) {
2727 VkResult U_ASSERT_ONLY err;
2728 uint32_t display_count;
2729 uint32_t mode_count;
2730 uint32_t plane_count;
2731 VkDisplayPropertiesKHR display_props;
2732 VkDisplayKHR display;
2733 VkDisplayModePropertiesKHR mode_props;
2734 VkDisplayPlanePropertiesKHR *plane_props;
2735 VkBool32 found_plane = VK_FALSE;
2736 uint32_t plane_index;
2737 VkExtent2D image_extent;
2738 VkDisplaySurfaceCreateInfoKHR create_info;
2739
2740 // Get the first display
2741 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2742 assert(!err);
2743
2744 if (display_count == 0) {
2745 printf("Cannot find any display!\n");
2746 fflush(stdout);
2747 exit(1);
2748 }
2749
2750 display_count = 1;
2751 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2752 assert(!err || (err == VK_INCOMPLETE));
2753
2754 display = display_props.display;
2755
2756 // Get the first mode of the display
2757 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2758 assert(!err);
2759
2760 if (mode_count == 0) {
2761 printf("Cannot find any mode for the display!\n");
2762 fflush(stdout);
2763 exit(1);
2764 }
2765
2766 mode_count = 1;
2767 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2768 assert(!err || (err == VK_INCOMPLETE));
2769
2770 // Get the list of planes
2771 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2772 assert(!err);
2773
2774 if (plane_count == 0) {
2775 printf("Cannot find any plane!\n");
2776 fflush(stdout);
2777 exit(1);
2778 }
2779
2780 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2781 assert(plane_props);
2782
2783 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2784 assert(!err);
2785
2786 // Find a plane compatible with the display
2787 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2788 uint32_t supported_count;
2789 VkDisplayKHR *supported_displays;
2790
2791 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002792 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002793 continue;
2794 }
2795
2796 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2797 assert(!err);
2798
2799 if (supported_count == 0) {
2800 continue;
2801 }
2802
2803 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2804 assert(supported_displays);
2805
2806 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2807 assert(!err);
2808
2809 for (uint32_t i = 0; i < supported_count; i++) {
2810 if (supported_displays[i] == display) {
2811 found_plane = VK_TRUE;
2812 break;
2813 }
2814 }
2815
2816 free(supported_displays);
2817
2818 if (found_plane) {
2819 break;
2820 }
2821 }
2822
2823 if (!found_plane) {
2824 printf("Cannot find a plane compatible with the display!\n");
2825 fflush(stdout);
2826 exit(1);
2827 }
2828
2829 free(plane_props);
2830
Tony Barbour820b55b2017-03-14 08:55:46 -06002831 VkDisplayPlaneCapabilitiesKHR planeCaps;
2832 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2833 // Find a supported alpha mode
Mark Young66510e52017-11-10 10:05:14 -07002834 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2835 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002836 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2837 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2838 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2839 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2840 };
2841 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2842 if (planeCaps.supportedAlpha & alphaModes[i]) {
2843 alphaMode = alphaModes[i];
2844 break;
2845 }
2846 }
Damien Leone600c3052017-01-31 10:26:07 -07002847 image_extent.width = mode_props.parameters.visibleRegion.width;
2848 image_extent.height = mode_props.parameters.visibleRegion.height;
2849
2850 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2851 create_info.pNext = NULL;
2852 create_info.flags = 0;
2853 create_info.displayMode = mode_props.displayMode;
2854 create_info.planeIndex = plane_index;
2855 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2856 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002857 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002858 create_info.globalAlpha = 1.0f;
2859 create_info.imageExtent = image_extent;
2860
2861 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2862}
2863
Dave Houlton5fa47912018-02-16 11:02:26 -07002864static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07002865 while (!demo->quit) {
2866 demo_draw(demo);
2867 demo->curFrame++;
2868
2869 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2870 demo->quit = true;
2871 }
2872 }
2873}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002874#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002875
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002876/*
2877 * Return 1 (true) if all layer names specified in check_names
2878 * can be found in given layer properties.
2879 */
Dave Houlton5fa47912018-02-16 11:02:26 -07002880static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002881 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002882 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002883 for (uint32_t j = 0; j < layer_count; j++) {
2884 if (!strcmp(check_names[i], layers[j].layerName)) {
2885 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002886 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002887 }
2888 }
2889 if (!found) {
2890 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2891 return 0;
2892 }
2893 }
2894 return 1;
2895}
2896
Karl Schultze4dc75c2016-02-02 15:37:51 -07002897static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002898 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002899 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002900 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002901 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002902 char **instance_validation_layers = NULL;
2903 demo->enabled_extension_count = 0;
2904 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002905 demo->is_minimized = false;
2906 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002907
Dave Houlton5fa47912018-02-16 11:02:26 -07002908 char *instance_validation_layers_alt1[] = {"VK_LAYER_LUNARG_standard_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06002909
Dave Houlton5fa47912018-02-16 11:02:26 -07002910 char *instance_validation_layers_alt2[] = {"VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2911 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2912 "VK_LAYER_GOOGLE_unique_objects"};
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002913
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002914 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002915 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002916 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002917 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002918 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002919
Karl Schultz7c50ad62016-04-01 12:07:03 -06002920 instance_validation_layers = instance_validation_layers_alt1;
2921 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002922 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2923 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002924 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002925
Dave Houlton5fa47912018-02-16 11:02:26 -07002926 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt1), instance_validation_layers,
2927 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002928 if (validation_found) {
2929 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002930 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2931 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002932 } else {
2933 // use alternative set of validation layers
2934 instance_validation_layers = instance_validation_layers_alt2;
2935 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Dave Houlton5fa47912018-02-16 11:02:26 -07002936 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt2), instance_validation_layers,
2937 instance_layer_count, instance_layers);
2938 validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002939 for (uint32_t i = 0; i < validation_layer_count; i++) {
2940 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002941 }
2942 }
2943 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002944 }
Dustin Graves7c287352016-01-27 13:48:06 -07002945
Karl Schultz7c50ad62016-04-01 12:07:03 -06002946 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002947 ERR_EXIT(
2948 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
2949 "Please look at the Getting Started guide for additional information.\n",
2950 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06002951 }
Dustin Graves7c287352016-01-27 13:48:06 -07002952 }
2953
2954 /* Look for instance extensions */
2955 VkBool32 surfaceExtFound = 0;
2956 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002957 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002958
Dave Houlton5fa47912018-02-16 11:02:26 -07002959 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002960 assert(!err);
2961
Dustin Graves7c287352016-01-27 13:48:06 -07002962 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002963 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2964 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07002965 assert(!err);
2966 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002967 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002968 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002969 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002970 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002971#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002972 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002973 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002974 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002975 }
Tony Barbour153cb062016-12-07 13:43:36 -07002976#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002977 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06002978 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002979 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06002980 }
Tony Barbour153cb062016-12-07 13:43:36 -07002981#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002982 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002983 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002984 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002985 }
Tony Barbour153cb062016-12-07 13:43:36 -07002986#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002987 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002988 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002989 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002990 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002991#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002992#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002993 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07002994 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002995 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07002996 }
Tony Barbour153cb062016-12-07 13:43:36 -07002997#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002998 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002999 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003000 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003001 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003002#elif defined(VK_USE_PLATFORM_IOS_MVK)
3003 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3004 platformSurfaceExtFound = 1;
3005 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3006 }
3007#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3008 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3009 platformSurfaceExtFound = 1;
3010 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3011 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003012#endif
Dave Houlton5fa47912018-02-16 11:02:26 -07003013 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003014 if (demo->validate) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003015 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003016 }
3017 }
Mark Young66510e52017-11-10 10:05:14 -07003018 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3019 if (demo->validate) {
3020 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3021 }
3022 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003023 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003024 }
Dustin Graves7c287352016-01-27 13:48:06 -07003025
3026 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003027 }
Dustin Graves7c287352016-01-27 13:48:06 -07003028
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003029 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003030 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3031 " extension.\n\n"
3032 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3033 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06003034 "vkCreateInstance Failure");
3035 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003036 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003037#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003038 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3039 " extension.\n\n"
3040 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3041 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003042 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003043#elif defined(VK_USE_PLATFORM_IOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003044 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_IOS_SURFACE_EXTENSION_NAME
3045 " extension.\n\n"
3046 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3047 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003048 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003049#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003050 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_MACOS_SURFACE_EXTENSION_NAME
3051 " extension.\n\n"
3052 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3053 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003054 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003055#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003056 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3057 " extension.\n\n"
3058 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3059 "Please look at the Getting Started guide for additional information.\n",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003060 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003061#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003062 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3063 " extension.\n\n"
3064 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3065 "Please look at the Getting Started guide for additional information.\n",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003066 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003067#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003068#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003069 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3070 " extension.\n\n"
3071 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3072 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07003073 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003074#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003075 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3076 " extension.\n\n"
3077 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3078 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003079 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003080#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003081 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3082 " extension.\n\n"
3083 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3084 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06003085 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003086#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003087 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003088 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003089 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003090 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003091 .pApplicationName = APP_SHORT_NAME,
3092 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003093 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003094 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003095 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003096 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003097 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003098 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003099 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003100 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003101 .enabledLayerCount = demo->enabled_layer_count,
3102 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3103 .enabledExtensionCount = demo->enabled_extension_count,
3104 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003105 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003106
3107 /*
3108 * This is info for a temp callback to use during CreateInstance.
3109 * After the instance is created, we use the instance-based
3110 * function to register the final callback.
3111 */
Mark Young66510e52017-11-10 10:05:14 -07003112 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003113 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003114 // VK_EXT_debug_utils style
3115 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3116 dbg_messenger_create_info.pNext = NULL;
3117 dbg_messenger_create_info.flags = 0;
3118 dbg_messenger_create_info.messageSeverity =
3119 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3120 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3121 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3122 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3123 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3124 dbg_messenger_create_info.pUserData = demo;
3125 inst_info.pNext = &dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003126 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003127
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003128 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003129
Chia-I Wu63636062015-10-26 21:10:41 +08003130 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003131 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003132 ERR_EXIT(
3133 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3134 "Please look at the Getting Started guide for additional information.\n",
3135 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003136 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003137 ERR_EXIT(
3138 "Cannot find a specified extension library.\n"
3139 "Make sure your layers path is set appropriately.\n",
3140 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003141 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003142 ERR_EXIT(
3143 "vkCreateInstance failed.\n\n"
3144 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3145 "Please look at the Getting Started guide for additional information.\n",
3146 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003147 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003148
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003149 /* Make initial call to query gpu_count, then second call for gpu info*/
3150 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
Petr Kraus2f1dbdb2018-04-14 14:45:17 +02003151 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003152
3153 if (gpu_count > 0) {
3154 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3155 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3156 assert(!err);
3157 /* For cube demo we just grab the first physical device */
3158 demo->gpu = physical_devices[0];
3159 free(physical_devices);
3160 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003161 ERR_EXIT(
3162 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3163 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3164 "Please look at the Getting Started guide for additional information.\n",
3165 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003166 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003167
Dustin Graves7c287352016-01-27 13:48:06 -07003168 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003169 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003170 VkBool32 swapchainExtFound = 0;
3171 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003172 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003173
Dave Houlton5fa47912018-02-16 11:02:26 -07003174 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003175 assert(!err);
3176
Dustin Graves7c287352016-01-27 13:48:06 -07003177 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003178 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3179 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003180 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003181
Dustin Graves7c287352016-01-27 13:48:06 -07003182 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003183 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003184 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003185 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003186 }
3187 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003188 }
Dustin Graves7c287352016-01-27 13:48:06 -07003189
Ian Elliott53622a72017-03-28 11:06:33 -06003190 if (demo->VK_KHR_incremental_present_enabled) {
3191 // Even though the user "enabled" the extension via the command
3192 // line, we must make sure that it's enumerated for use with the
3193 // device. Therefore, disable it here, and re-enable it again if
3194 // enumerated.
3195 demo->VK_KHR_incremental_present_enabled = false;
3196 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003197 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3198 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003199 demo->VK_KHR_incremental_present_enabled = true;
3200 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3201 }
3202 assert(demo->enabled_extension_count < 64);
3203 }
3204 if (!demo->VK_KHR_incremental_present_enabled) {
3205 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3206 }
3207 }
3208
Ian Elliottd940ca22017-03-22 08:31:27 -06003209 if (demo->VK_GOOGLE_display_timing_enabled) {
3210 // Even though the user "enabled" the extension via the command
3211 // line, we must make sure that it's enumerated for use with the
3212 // device. Therefore, disable it here, and re-enable it again if
3213 // enumerated.
3214 demo->VK_GOOGLE_display_timing_enabled = false;
3215 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003216 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3217 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003218 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003219 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003220 }
3221 assert(demo->enabled_extension_count < 64);
3222 }
3223 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003224 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003225 }
3226 }
3227
Dustin Graves7c287352016-01-27 13:48:06 -07003228 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003229 }
Dustin Graves7c287352016-01-27 13:48:06 -07003230
Tony Barbourcc69f452015-10-08 13:59:42 -06003231 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003232 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3233 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3234 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003235 "vkCreateInstance Failure");
3236 }
3237
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003238 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003239 // Setup VK_EXT_debug_utils function pointers always (we use them for
3240 // debug labels and names).
3241 demo->CreateDebugUtilsMessengerEXT =
3242 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3243 demo->DestroyDebugUtilsMessengerEXT =
3244 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3245 demo->SubmitDebugUtilsMessageEXT =
3246 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3247 demo->CmdBeginDebugUtilsLabelEXT =
3248 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3249 demo->CmdEndDebugUtilsLabelEXT =
3250 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3251 demo->CmdInsertDebugUtilsLabelEXT =
3252 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3253 demo->SetDebugUtilsObjectNameEXT =
3254 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3255 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3256 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3257 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3258 NULL == demo->SetDebugUtilsObjectNameEXT) {
3259 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003260 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003261
Mark Young66510e52017-11-10 10:05:14 -07003262 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003263 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003264 case VK_SUCCESS:
3265 break;
3266 case VK_ERROR_OUT_OF_HOST_MEMORY:
Mark Young66510e52017-11-10 10:05:14 -07003267 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003268 break;
3269 default:
Mark Young66510e52017-11-10 10:05:14 -07003270 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003271 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003272 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003273 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003274 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003275
3276 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003277 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003278 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003279
Dave Houlton5fa47912018-02-16 11:02:26 -07003280 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3281 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003282
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003283 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003284 // If app has specific feature requirements it should check supported
3285 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003286 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003287 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003288
Tony Barbourda2bad52016-01-22 14:36:40 -07003289 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3290 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3291 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3292 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3293 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3294}
3295
Karl Schultze4dc75c2016-02-02 15:37:51 -07003296static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003297 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003298 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003299 VkDeviceQueueCreateInfo queues[2];
3300 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3301 queues[0].pNext = NULL;
3302 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3303 queues[0].queueCount = 1;
3304 queues[0].pQueuePriorities = queue_priorities;
3305 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003306
3307 VkDeviceCreateInfo device = {
3308 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3309 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003310 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003311 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003312 .enabledLayerCount = 0,
3313 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003314 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003315 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003316 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003317 };
Tony Barbour22e06272016-09-07 16:07:56 -06003318 if (demo->separate_present_queue) {
3319 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3320 queues[1].pNext = NULL;
3321 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3322 queues[1].queueCount = 1;
3323 queues[1].pQueuePriorities = queue_priorities;
3324 queues[1].flags = 0;
3325 device.queueCreateInfoCount = 2;
3326 }
Chia-I Wu63636062015-10-26 21:10:41 +08003327 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003328 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003329}
3330
Karl Schultze4dc75c2016-02-02 15:37:51 -07003331static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003332 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003333
Karl Schultze4dc75c2016-02-02 15:37:51 -07003334// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003335#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003336 VkWin32SurfaceCreateInfoKHR createInfo;
3337 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3338 createInfo.pNext = NULL;
3339 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003340 createInfo.hinstance = demo->connection;
3341 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003342
Dave Houlton5fa47912018-02-16 11:02:26 -07003343 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003344#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003345 VkWaylandSurfaceCreateInfoKHR createInfo;
3346 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3347 createInfo.pNext = NULL;
3348 createInfo.flags = 0;
3349 createInfo.display = demo->display;
3350 createInfo.surface = demo->window;
3351
Dave Houlton5fa47912018-02-16 11:02:26 -07003352 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003353#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003354#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3355 VkAndroidSurfaceCreateInfoKHR createInfo;
3356 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3357 createInfo.pNext = NULL;
3358 createInfo.flags = 0;
Mike Schuchardt837d5162018-03-08 12:57:02 -07003359 createInfo.window = (struct ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003360
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003361 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003362#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3363 VkXlibSurfaceCreateInfoKHR createInfo;
3364 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3365 createInfo.pNext = NULL;
3366 createInfo.flags = 0;
3367 createInfo.dpy = demo->display;
3368 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003369
Dave Houlton5fa47912018-02-16 11:02:26 -07003370 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003371#elif defined(VK_USE_PLATFORM_XCB_KHR)
3372 VkXcbSurfaceCreateInfoKHR createInfo;
3373 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3374 createInfo.pNext = NULL;
3375 createInfo.flags = 0;
3376 createInfo.connection = demo->connection;
3377 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003378
Tony Barbour153cb062016-12-07 13:43:36 -07003379 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003380#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3381 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003382#elif defined(VK_USE_PLATFORM_IOS_MVK)
3383 VkIOSSurfaceCreateInfoMVK surface;
3384 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3385 surface.pNext = NULL;
3386 surface.flags = 0;
3387 surface.pView = demo->window;
3388
3389 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3390#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3391 VkMacOSSurfaceCreateInfoMVK surface;
3392 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3393 surface.pNext = NULL;
3394 surface.flags = 0;
3395 surface.pView = demo->window;
3396
3397 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003398#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003399 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003400
Tony Barbourcc69f452015-10-08 13:59:42 -06003401 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003402 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003403 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003404 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003405 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003406
3407 // Search for a graphics and a present queue in the array of queue
3408 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003409 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3410 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003411 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003412 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3413 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3414 graphicsQueueFamilyIndex = i;
3415 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003416
Tony Barbourab2a0362016-09-06 11:40:12 -06003417 if (supportsPresent[i] == VK_TRUE) {
3418 graphicsQueueFamilyIndex = i;
3419 presentQueueFamilyIndex = i;
3420 break;
3421 }
3422 }
3423 }
3424
3425 if (presentQueueFamilyIndex == UINT32_MAX) {
3426 // If didn't find a queue that supports both graphics and present, then
3427 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003428 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003429 if (supportsPresent[i] == VK_TRUE) {
3430 presentQueueFamilyIndex = i;
3431 break;
3432 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003433 }
3434 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003435
3436 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003437 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3438 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003439 }
3440
Tony Barbourab2a0362016-09-06 11:40:12 -06003441 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3442 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003443 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003444 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003445
Tony Barbourda2bad52016-01-22 14:36:40 -07003446 demo_create_device(demo);
3447
3448 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3449 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3450 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3451 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3452 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003453 if (demo->VK_GOOGLE_display_timing_enabled) {
3454 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3455 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3456 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003457
Dave Houlton5fa47912018-02-16 11:02:26 -07003458 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003459
Tony Barbour22e06272016-09-07 16:07:56 -06003460 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003461 demo->present_queue = demo->graphics_queue;
3462 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003463 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003464 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003465
Ian Elliottaaae5352015-07-06 14:27:58 -06003466 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003467 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003468 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003469 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003470 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3471 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003472 assert(!err);
3473 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3474 // the surface has no preferred format. Otherwise, at least one
3475 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003476 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003477 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003478 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003479 assert(formatCount >= 1);
3480 demo->format = surfFormats[0].format;
3481 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003482 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003483
David Pinedo2bb7c932015-06-18 17:03:14 -06003484 demo->quit = false;
3485 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003486
Tony Barbource7524e2016-07-28 12:01:45 -06003487 // Create semaphores to synchronize acquiring presentable buffers before
3488 // rendering and waiting for drawing to be complete before presenting
3489 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3490 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3491 .pNext = NULL,
3492 .flags = 0,
3493 };
Tony Barbource7524e2016-07-28 12:01:45 -06003494
Tony Barbour66a56c32016-09-21 13:10:56 -06003495 // Create fences that we can use to throttle if we get too far
3496 // ahead of the image presents
3497 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003498 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003499 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003500 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3501 assert(!err);
3502
Dave Houlton5fa47912018-02-16 11:02:26 -07003503 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003504 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003505
Dave Houlton5fa47912018-02-16 11:02:26 -07003506 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003507 assert(!err);
3508
3509 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003510 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003511 assert(!err);
3512 }
Tony Barbour22e06272016-09-07 16:07:56 -06003513 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003514 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003515
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003516 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003517 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003518}
3519
Tony Barbour153cb062016-12-07 13:43:36 -07003520#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003521static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3522 wl_fixed_t sy) {}
3523
3524static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3525
3526static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3527
3528static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3529 uint32_t state) {
3530 struct demo *demo = data;
3531 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3532 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
3533 }
3534}
3535
3536static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3537
3538static const struct wl_pointer_listener pointer_listener = {
3539 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3540};
3541
3542static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3543
3544static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3545 struct wl_array *keys) {}
3546
3547static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3548
3549static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3550 uint32_t state) {
3551 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3552 struct demo *demo = data;
3553 switch (key) {
3554 case KEY_ESC: // Escape
3555 demo->quit = true;
3556 break;
3557 case KEY_LEFT: // left arrow key
3558 demo->spin_angle -= demo->spin_increment;
3559 break;
3560 case KEY_RIGHT: // right arrow key
3561 demo->spin_angle += demo->spin_increment;
3562 break;
3563 case KEY_SPACE: // space bar
3564 demo->pause = !demo->pause;
3565 break;
3566 }
3567}
3568
3569static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3570 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3571
3572static const struct wl_keyboard_listener keyboard_listener = {
3573 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3574};
3575
3576static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3577 // Subscribe to pointer events
3578 struct demo *demo = data;
3579 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3580 demo->pointer = wl_seat_get_pointer(seat);
3581 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3582 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3583 wl_pointer_destroy(demo->pointer);
3584 demo->pointer = NULL;
3585 }
3586 // Subscribe to keyboard events
3587 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3588 demo->keyboard = wl_seat_get_keyboard(seat);
3589 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3590 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3591 wl_keyboard_destroy(demo->keyboard);
3592 demo->keyboard = NULL;
3593 }
3594}
3595
3596static const struct wl_seat_listener seat_listener = {
3597 seat_handle_capabilities,
3598};
3599
3600static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003601 uint32_t version UNUSED) {
3602 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003603 // pickup wayland objects when they appear
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003604 if (strcmp(interface, "wl_compositor") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003605 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003606 } else if (strcmp(interface, "wl_shell") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003607 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3608 } else if (strcmp(interface, "wl_seat") == 0) {
3609 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3610 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003611 }
3612}
3613
Dave Houlton5fa47912018-02-16 11:02:26 -07003614static 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 +09003615
Dave Houlton5fa47912018-02-16 11:02:26 -07003616static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003617#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003618#endif
3619
Karl Schultze4dc75c2016-02-02 15:37:51 -07003620static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003621#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003622 const xcb_setup_t *setup;
3623 xcb_screen_iterator_t iter;
3624 int scr;
3625
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003626 const char *display_envar = getenv("DISPLAY");
3627 if (display_envar == NULL || display_envar[0] == '\0') {
3628 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3629 fflush(stdout);
3630 exit(1);
3631 }
3632
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003633 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003634 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003635 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003636 fflush(stdout);
3637 exit(1);
3638 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003639
3640 setup = xcb_get_setup(demo->connection);
3641 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003642 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003643
3644 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003645#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3646 demo->display = wl_display_connect(NULL);
3647
3648 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003649 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003650 fflush(stdout);
3651 exit(1);
3652 }
3653
3654 demo->registry = wl_display_get_registry(demo->display);
3655 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3656 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003657#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003658#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003659}
3660
Karl Schultze4dc75c2016-02-02 15:37:51 -07003661static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003662 vec3 eye = {0.0f, 3.0f, 5.0f};
3663 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003664 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003665
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003666 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003667 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003668 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003669
Piers Daniell735ee532015-02-23 16:23:13 -07003670 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003671 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003672 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003673 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003674 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003675 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3676 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003677 i++;
3678 continue;
3679 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003680 if (strcmp(argv[i], "--break") == 0) {
3681 demo->use_break = true;
3682 continue;
3683 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003684 if (strcmp(argv[i], "--validate") == 0) {
3685 demo->validate = true;
3686 continue;
3687 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003688 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3689 demo->validate = true;
3690 demo->validate_checks_disabled = true;
3691 continue;
3692 }
Tony Barbour2593b182016-04-19 10:57:58 -06003693 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003694 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003695 continue;
3696 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003697 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3698 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003699 i++;
3700 continue;
3701 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003702 if (strcmp(argv[i], "--suppress_popups") == 0) {
3703 demo->suppress_popups = true;
3704 continue;
3705 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003706 if (strcmp(argv[i], "--display_timing") == 0) {
3707 demo->VK_GOOGLE_display_timing_enabled = true;
3708 continue;
3709 }
Ian Elliott53622a72017-03-28 11:06:33 -06003710 if (strcmp(argv[i], "--incremental_present") == 0) {
3711 demo->VK_KHR_incremental_present_enabled = true;
3712 continue;
3713 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003714
Cody Northropaf28a532016-09-27 10:50:57 -06003715#if defined(ANDROID)
3716 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3717#else
Mike Schuchardt9a881512017-11-01 16:16:22 -06003718 fprintf(stderr,
Mark Young66510e52017-11-10 10:05:14 -07003719 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled] [--break]\n"
3720 "\t[--c <framecount>] [--suppress_popups] [--incremental_present] [--display_timing]\n"
3721 "\t[--present_mode <present mode enum>]\n"
3722 "\t <present_mode_enum>\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3723 "\t\t\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3724 "\t\t\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
3725 "\t\t\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
Mike Schuchardt9a881512017-11-01 16:16:22 -06003726 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
3727 VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003728 fflush(stderr);
3729 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003730#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003731 }
3732
Tony Barbour153cb062016-12-07 13:43:36 -07003733 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003734
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003735 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003736
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003737 demo->width = 500;
3738 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003739
Tony Barbour66a56c32016-09-21 13:10:56 -06003740 demo->spin_angle = 4.0f;
3741 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003742 demo->pause = false;
3743
Dave Houlton5fa47912018-02-16 11:02:26 -07003744 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003745 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3746 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003747
Dave Houlton5fa47912018-02-16 11:02:26 -07003748 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003749}
3750
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003751#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003752// Include header required for parsing the command line options.
3753#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003754
Dave Houlton5fa47912018-02-16 11:02:26 -07003755int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3756 MSG msg; // message
3757 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003758 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003759 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003760
Jamie Madillb2ff6502017-03-15 16:17:46 -04003761 // Ensure wParam is initialized.
3762 msg.wParam = 0;
3763
Karl Schultze4dc75c2016-02-02 15:37:51 -07003764 // Use the CommandLine functions to get the command line arguments.
3765 // Unfortunately, Microsoft outputs
3766 // this information as wide characters for Unicode, and we simply want the
3767 // Ascii version to be compatible
3768 // with the non-Windows side. So, we have to convert the information to
3769 // Ascii character strings.
3770 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3771 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003772 argc = 0;
3773 }
3774
Karl Schultze4dc75c2016-02-02 15:37:51 -07003775 if (argc > 0) {
3776 argv = (char **)malloc(sizeof(char *) * argc);
3777 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003778 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003779 } else {
3780 for (int iii = 0; iii < argc; iii++) {
3781 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003782 size_t numConverted = 0;
3783
Karl Schultze4dc75c2016-02-02 15:37:51 -07003784 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3785 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003786 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003787 }
3788 }
3789 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003790 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003791 argv = NULL;
3792 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003793
3794 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003795
3796 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003797 if (argc > 0 && argv != NULL) {
3798 for (int iii = 0; iii < argc; iii++) {
3799 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003800 free(argv[iii]);
3801 }
3802 }
3803 free(argv);
3804 }
3805
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003806 demo.connection = hInstance;
3807 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003808 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003809 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003810
3811 demo_prepare(&demo);
3812
Dave Houlton5fa47912018-02-16 11:02:26 -07003813 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003814
3815 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003816 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003817 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003818 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003819 {
Dave Houlton5fa47912018-02-16 11:02:26 -07003820 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07003821 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003822 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003823 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003824 DispatchMessage(&msg);
3825 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003826 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003827 }
3828
3829 demo_cleanup(&demo);
3830
Karl Schultze4dc75c2016-02-02 15:37:51 -07003831 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003832}
Bill Hollingsf4352142017-02-14 22:58:56 -05003833
3834#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
Karl Schultz206b1c52018-04-13 18:02:07 -06003835static void demo_main(struct demo *demo, void *view, int argc, const char *argv[]) {
Bill Hollingsf4352142017-02-14 22:58:56 -05003836
Dave Houlton5fa47912018-02-16 11:02:26 -07003837 demo_init(demo, argc, (char **)argv);
Tony Barbourc65cd752017-03-13 15:29:35 -06003838 demo->window = view;
3839 demo_init_vk_swapchain(demo);
3840 demo_prepare(demo);
3841 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003842}
3843
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003844#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3845#include <android/log.h>
3846#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003847#include "android_util.h"
3848
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003849static bool initialized = false;
3850static bool active = false;
3851struct demo demo;
3852
Dave Houlton5fa47912018-02-16 11:02:26 -07003853static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003854
Dave Houlton5fa47912018-02-16 11:02:26 -07003855static void processCommand(struct android_app *app, int32_t cmd) {
3856 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003857 case APP_CMD_INIT_WINDOW: {
3858 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003859 // We're getting a new window. If the app is starting up, we
3860 // need to initialize. If the app has already been
3861 // initialized, that means that we lost our previous window,
3862 // which means that we have a lot of work to do. At a minimum,
3863 // we need to destroy the swapchain and surface associated with
3864 // the old window, and create a new surface and swapchain.
3865 // However, since there are a lot of other objects/state that
3866 // is tied to the swapchain, it's easiest to simply cleanup and
3867 // start over (i.e. use a brute-force approach of re-starting
3868 // the app)
3869 if (demo.prepared) {
3870 demo_cleanup(&demo);
3871 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003872
3873 // Parse Intents into argc, argv
3874 // Use the following key to send arguments, i.e.
3875 // --es args "--validate"
3876 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07003877 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003878 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003879 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003880
3881 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07003882 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 -06003883
3884 demo_init(&demo, argc, argv);
3885
3886 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07003887 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003888
Dave Houlton5fa47912018-02-16 11:02:26 -07003889 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003890 demo_init_vk_swapchain(&demo);
3891 demo_prepare(&demo);
3892 initialized = true;
3893 }
3894 break;
3895 }
3896 case APP_CMD_GAINED_FOCUS: {
3897 active = true;
3898 break;
3899 }
3900 case APP_CMD_LOST_FOCUS: {
3901 active = false;
3902 break;
3903 }
3904 }
3905}
3906
Dave Houlton5fa47912018-02-16 11:02:26 -07003907void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06003908#ifdef ANDROID
3909 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07003910 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06003911#endif
3912
Ian Elliottf05d5d12016-05-17 12:03:35 -06003913 demo.prepared = false;
3914
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003915 app->onAppCmd = processCommand;
3916 app->onInputEvent = processInput;
3917
Dave Houlton5fa47912018-02-16 11:02:26 -07003918 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003919 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07003920 struct android_poll_source *source;
3921 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003922 if (source) {
3923 source->process(app, source);
3924 }
3925
3926 if (app->destroyRequested != 0) {
3927 demo_cleanup(&demo);
3928 return;
3929 }
3930 }
3931 if (initialized && active) {
3932 demo_run(&demo);
3933 }
3934 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003935}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003936#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003937int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003938 struct demo demo;
3939
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003940 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003941#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003942 demo_create_xcb_window(&demo);
3943#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3944 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003945#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3946 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003947#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003948#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003949
Tony Barbourcc69f452015-10-08 13:59:42 -06003950 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003951
3952 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003953
Tony Barbour153cb062016-12-07 13:43:36 -07003954#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003955 demo_run_xcb(&demo);
3956#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3957 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003958#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3959 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003960#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003961#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3962 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003963#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003964
3965 demo_cleanup(&demo);
3966
Karl Schultz0218c002016-03-22 17:06:13 -06003967 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003968}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003969#endif