blob: 54a131adac35da4f53d0f3b2c587f034f1169ce0 [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"
59
Ian Elliottd940ca22017-03-22 08:31:27 -060060#include "gettime.h"
61#include "inttypes.h"
62#define MILLION 1000000L
63#define BILLION 1000000000L
64
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060065#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060066#define APP_SHORT_NAME "cube"
67#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060068
Tony Barbour66a56c32016-09-21 13:10:56 -060069// Allow a maximum of two outstanding presentation operations.
70#define FRAME_LAG 2
71
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060072#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
73
Tony Barbourfdc2d352015-04-22 09:02:32 -060074#if defined(NDEBUG) && defined(__GNUC__)
75#define U_ASSERT_ONLY __attribute__((unused))
76#else
77#define U_ASSERT_ONLY
78#endif
79
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090080#if defined(__GNUC__)
81#define UNUSED __attribute__((unused))
82#else
83#define UNUSED
84#endif
85
Ian Elliott07264132015-04-28 11:35:02 -060086#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060087bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070088#define ERR_EXIT(err_msg, err_class) \
89 do { \
90 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
91 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070092 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -060093void DbgMsg(char *fmt, ...) {
94 va_list va;
95 va_start(va, fmt);
96 printf(fmt, va);
97 fflush(stdout);
98 va_end(va);
99}
Ian Elliott07264132015-04-28 11:35:02 -0600100
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500101#elif defined __ANDROID__
102#include <android/log.h>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700103#define ERR_EXIT(err_msg, err_class) \
104 do { \
105 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
106 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500107 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600108#ifdef VARARGS_WORKS_ON_ANDROID
109void DbgMsg(const char *fmt, ...) {
110 va_list va;
111 va_start(va, fmt);
112 __android_log_print(ANDROID_LOG_INFO, "Cube", fmt, va);
113 va_end(va);
114}
115#else // VARARGS_WORKS_ON_ANDROID
116#define DbgMsg(fmt, ...) \
117 do { \
118 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", fmt, ##__VA_ARGS__)); \
119 } while (0)
120#endif // VARARGS_WORKS_ON_ANDROID
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500121#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700122#define ERR_EXIT(err_msg, err_class) \
123 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -0800124 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700125 fflush(stdout); \
126 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700127 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600128void DbgMsg(char *fmt, ...) {
129 va_list va;
130 va_start(va, fmt);
131 printf(fmt, va);
132 fflush(stdout);
133 va_end(va);
134}
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500135#endif
Ian Elliott07264132015-04-28 11:35:02 -0600136
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700137#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
138 { \
139 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
140 if (demo->fp##entrypoint == NULL) { \
141 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
142 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700143 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600144
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600145static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
146
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700147#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
148 { \
149 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
150 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
151 if (demo->fp##entrypoint == NULL) { \
152 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
153 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700154 }
Ian Elliott673898b2015-06-22 15:07:49 -0600155
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600156/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700157 * structure to track all objects related to a texture.
158 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600159struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600160 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700161
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600162 VkImage image;
163 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600164
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800165 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500166 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600167 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700168 int32_t tex_width, tex_height;
169};
170
Karl Schultze4dc75c2016-02-02 15:37:51 -0700171static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172
Karl Schultz0218c002016-03-22 17:06:13 -0600173static int validation_error = 0;
174
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600175struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600176 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700177 float mvp[4][4];
178 float position[12 * 3][4];
179 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600180};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600181
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600182//--------------------------------------------------------------------------------------
183// Mesh and VertexFormat Data
184//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700185// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600186static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 -1.0f,-1.0f, 1.0f,
189 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800190 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600191 -1.0f, 1.0f,-1.0f,
192 -1.0f,-1.0f,-1.0f,
193
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800194 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600195 1.0f, 1.0f,-1.0f,
196 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600198 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600199 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600200
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800201 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600202 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600203 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800204 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600205 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600206 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600207
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800208 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600209 -1.0f, 1.0f, 1.0f,
210 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800211 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600213 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800215 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600216 1.0f, 1.0f, 1.0f,
217 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800218 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219 1.0f,-1.0f,-1.0f,
220 1.0f, 1.0f,-1.0f,
221
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800222 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600224 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800225 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600226 1.0f,-1.0f, 1.0f,
227 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600228};
229
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600230static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700231 0.0f, 1.0f, // -X side
232 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800233 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800234 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600235 0.0f, 0.0f,
236 0.0f, 1.0f,
237
Rene Lindsay76867e82016-07-29 10:49:11 -0700238 1.0f, 1.0f, // -Z side
239 0.0f, 0.0f,
240 0.0f, 1.0f,
241 1.0f, 1.0f,
242 1.0f, 0.0f,
243 0.0f, 0.0f,
244
245 1.0f, 0.0f, // -Y side
246 1.0f, 1.0f,
247 0.0f, 1.0f,
248 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600249 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800250 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600251
Rene Lindsay76867e82016-07-29 10:49:11 -0700252 1.0f, 0.0f, // +Y side
253 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800254 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600255 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700256 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600257 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600258
Rene Lindsay76867e82016-07-29 10:49:11 -0700259 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800260 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700261 0.0f, 1.0f,
262 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600263 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800264 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700265
266 0.0f, 0.0f, // +Z side
267 0.0f, 1.0f,
268 1.0f, 0.0f,
269 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600270 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700271 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600272};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700273// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600274
Karl Schultze4dc75c2016-02-02 15:37:51 -0700275void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600276 int i;
277
278 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700279 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600280 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
281 }
282 printf("\n");
283 fflush(stdout);
284}
285
Karl Schultze4dc75c2016-02-02 15:37:51 -0700286void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600287 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700288 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600289 printf("\n");
290 fflush(stdout);
291}
292
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600293typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600294 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800295 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600296 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600297 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700298 VkBuffer uniform_buffer;
299 VkDeviceMemory uniform_memory;
300 VkFramebuffer framebuffer;
301 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700302} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600303
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600304struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500305#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600306#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700307 HINSTANCE connection; // hInstance - Windows Instance
308 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
309 HWND window; // hWnd - window handle
310 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700311#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700312 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600313 Window xlib_window;
314 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700315#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700316 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600317 xcb_connection_t *connection;
318 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600319 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800320 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900321#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
322 struct wl_display *display;
323 struct wl_registry *registry;
324 struct wl_compositor *compositor;
325 struct wl_surface *window;
326 struct wl_shell *shell;
327 struct wl_shell_surface *shell_surface;
Joey Bzdek15eb0702017-06-07 09:40:36 -0600328 struct wl_seat *seat;
329 struct wl_pointer *pointer;
330 struct wl_keyboard *keyboard;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700331#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500332#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700333 ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500334#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
335 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500336#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700337 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600338 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700339 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600340 bool separate_present_queue;
Jeremy Kniager602e4182018-01-19 10:52:34 -0700341 bool is_minimized;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600342
Ian Elliott53622a72017-03-28 11:06:33 -0600343 bool VK_KHR_incremental_present_enabled;
344
Ian Elliottd940ca22017-03-22 08:31:27 -0600345 bool VK_GOOGLE_display_timing_enabled;
346 bool syncd_with_actual_presents;
347 uint64_t refresh_duration;
348 uint64_t refresh_duration_multiplier;
349 uint64_t target_IPD; // image present duration (inverse of frame rate)
350 uint64_t prev_desired_present_time;
351 uint32_t next_present_id;
352 uint32_t last_early_id; // 0 if no early images
353 uint32_t last_late_id; // 0 if no late images
354
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600355 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600356 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600357 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600358 VkQueue graphics_queue;
359 VkQueue present_queue;
360 uint32_t graphics_queue_family_index;
361 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600362 VkSemaphore image_acquired_semaphores[FRAME_LAG];
363 VkSemaphore draw_complete_semaphores[FRAME_LAG];
364 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600365 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600366 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600367 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600368
Tony Barbourda2bad52016-01-22 14:36:40 -0700369 uint32_t enabled_extension_count;
370 uint32_t enabled_layer_count;
371 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600372 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700373
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600375 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600376 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600377
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700378 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
379 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
380 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
381 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600382 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
383 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
384 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
385 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
386 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600387 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
388 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600389 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600390 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700391 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600392 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600393 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600394 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600395
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800396 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600397 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600398
399 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600400 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600401
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600402 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800403 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500404 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600405 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600406 } depth;
407
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600408 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600409 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600410
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700411 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500412 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600413 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600414 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800415 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600416 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600417
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600418 mat4x4 projection_matrix;
419 mat4x4 view_matrix;
420 mat4x4 model_matrix;
421
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600422 float spin_angle;
423 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600424 bool pause;
425
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600426 VkShaderModule vert_shader_module;
427 VkShaderModule frag_shader_module;
428
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600429 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800430
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600431 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600432 int32_t curFrame;
433 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600434 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600435 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600436 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600437 bool suppress_popups;
Mark Young66510e52017-11-10 10:05:14 -0700438
439 PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
440 PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
441 PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
442 PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
443 PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
444 PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
445 PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
446 VkDebugUtilsMessengerEXT dbg_messenger;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600447
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600448 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600449 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600450};
451
Mark Young66510e52017-11-10 10:05:14 -0700452static const char *DebugAnnotObjectToString(const VkObjectType object_type) {
453 switch (object_type) {
454 case VK_OBJECT_TYPE_INSTANCE:
455 return "VkInstance";
456 case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
457 return "VkPhysicalDevice";
458 case VK_OBJECT_TYPE_DEVICE:
459 return "VkDevice";
460 case VK_OBJECT_TYPE_QUEUE:
461 return "VkQueue";
462 case VK_OBJECT_TYPE_SEMAPHORE:
463 return "VkSemaphore";
464 case VK_OBJECT_TYPE_COMMAND_BUFFER:
465 return "VkCommandBuffer";
466 case VK_OBJECT_TYPE_FENCE:
467 return "VkFence";
468 case VK_OBJECT_TYPE_DEVICE_MEMORY:
469 return "VkDeviceMemory";
470 case VK_OBJECT_TYPE_BUFFER:
471 return "VkBuffer";
472 case VK_OBJECT_TYPE_IMAGE:
473 return "VkImage";
474 case VK_OBJECT_TYPE_EVENT:
475 return "VkEvent";
476 case VK_OBJECT_TYPE_QUERY_POOL:
477 return "VkQueryPool";
478 case VK_OBJECT_TYPE_BUFFER_VIEW:
479 return "VkBufferView";
480 case VK_OBJECT_TYPE_IMAGE_VIEW:
481 return "VkImageView";
482 case VK_OBJECT_TYPE_SHADER_MODULE:
483 return "VkShaderModule";
484 case VK_OBJECT_TYPE_PIPELINE_CACHE:
485 return "VkPipelineCache";
486 case VK_OBJECT_TYPE_PIPELINE_LAYOUT:
487 return "VkPipelineLayout";
488 case VK_OBJECT_TYPE_RENDER_PASS:
489 return "VkRenderPass";
490 case VK_OBJECT_TYPE_PIPELINE:
491 return "VkPipeline";
492 case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT:
493 return "VkDescriptorSetLayout";
494 case VK_OBJECT_TYPE_SAMPLER:
495 return "VkSampler";
496 case VK_OBJECT_TYPE_DESCRIPTOR_POOL:
497 return "VkDescriptorPool";
498 case VK_OBJECT_TYPE_DESCRIPTOR_SET:
499 return "VkDescriptorSet";
500 case VK_OBJECT_TYPE_FRAMEBUFFER:
501 return "VkFramebuffer";
502 case VK_OBJECT_TYPE_COMMAND_POOL:
503 return "VkCommandPool";
504 case VK_OBJECT_TYPE_SURFACE_KHR:
505 return "VkSurfaceKHR";
506 case VK_OBJECT_TYPE_SWAPCHAIN_KHR:
507 return "VkSwapchainKHR";
508 case VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT:
509 return "VkDebugReportCallbackEXT";
510 case VK_OBJECT_TYPE_DISPLAY_KHR:
511 return "VkDisplayKHR";
512 case VK_OBJECT_TYPE_DISPLAY_MODE_KHR:
513 return "VkDisplayModeKHR";
514 case VK_OBJECT_TYPE_OBJECT_TABLE_NVX:
515 return "VkObjectTableNVX";
516 case VK_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_NVX:
517 return "VkIndirectCommandsLayoutNVX";
518 case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE_KHR:
519 return "VkDescriptorUpdateTemplateKHR";
520 default:
521 return "Unknown Type";
522 }
523}
lenny-lunargfbe22392016-06-06 11:07:53 -0600524
Mark Young66510e52017-11-10 10:05:14 -0700525VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
526 VkDebugUtilsMessageTypeFlagsEXT messageType,
527 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
528 void *pUserData) {
529 char prefix[64] = "";
530 char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000);
lenny-lunargfbe22392016-06-06 11:07:53 -0600531 assert(message);
Mark Young66510e52017-11-10 10:05:14 -0700532 struct demo *demo = (struct demo *)pUserData;
lenny-lunargfbe22392016-06-06 11:07:53 -0600533
Mark Young66510e52017-11-10 10:05:14 -0700534 if (demo->use_break) {
535#ifndef WIN32
536 raise(SIGTRAP);
537#else
538 DebugBreak();
539#endif
540 }
541
542 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
543 strcat(prefix, "VERBOSE : ");
544 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
545 strcat(prefix, "INFO : ");
546 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
547 strcat(prefix, "WARNING : ");
548 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
549 strcat(prefix, "ERROR : ");
550 }
551
552 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
553 strcat(prefix, "GENERAL");
lenny-lunargfbe22392016-06-06 11:07:53 -0600554 } else {
Mark Young66510e52017-11-10 10:05:14 -0700555 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
556 strcat(prefix, "VALIDATION");
557 validation_error = 1;
558 }
559 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
560 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
561 strcat(prefix, "|");
562 }
563 strcat(prefix, "PERFORMANCE");
564 }
565 }
566
567 sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber,
568 pCallbackData->pMessageIdName, pCallbackData->pMessage);
569 if (pCallbackData->objectCount > 0) {
570 char tmp_message[500];
571 sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount);
572 strcat(message, tmp_message);
573 for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
574 if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
575 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object,
576 DebugAnnotObjectToString(pCallbackData->pObjects[object].objectType),
577 (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName);
578 } else {
579 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object,
580 DebugAnnotObjectToString(pCallbackData->pObjects[object].objectType),
581 (void *)(pCallbackData->pObjects[object].objectHandle));
582 }
583 strcat(message, tmp_message);
584 }
585 }
586 if (pCallbackData->cmdBufLabelCount > 0) {
587 char tmp_message[500];
588 sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount);
589 strcat(message, tmp_message);
590 for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
591 sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label,
592 pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0],
593 pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2],
594 pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]);
595 strcat(message, tmp_message);
596 }
lenny-lunargfbe22392016-06-06 11:07:53 -0600597 }
598
599#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600600
Tony Barbour602ca4f2016-09-12 14:02:43 -0600601 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600602 struct demo *demo = (struct demo*) pUserData;
603 if (!demo->suppress_popups)
604 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600605 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600606
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600607#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600608
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600609 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600610 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600611 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600612 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600613 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600614 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600615 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600616 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600617 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600618 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600619 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600620 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600621 }
Cody Northropaf28a532016-09-27 10:50:57 -0600622
lenny-lunargfbe22392016-06-06 11:07:53 -0600623#else
Cody Northropaf28a532016-09-27 10:50:57 -0600624
lenny-lunargfbe22392016-06-06 11:07:53 -0600625 printf("%s\n", message);
626 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600627
lenny-lunargfbe22392016-06-06 11:07:53 -0600628#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600629
lenny-lunargfbe22392016-06-06 11:07:53 -0600630 free(message);
631
Mark Young66510e52017-11-10 10:05:14 -0700632 // Don't bail out, but keep going.
lenny-lunargfbe22392016-06-06 11:07:53 -0600633 return false;
634}
635
Ian Elliottd940ca22017-03-22 08:31:27 -0600636bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
637 // The desired time was the earliest time that the present should have
638 // occured. In almost every case, the actual time should be later than the
639 // desired time. We should only consider the actual time "late" if it is
640 // after "desired + rdur".
641 if (actual <= desired) {
642 // The actual time was before or equal to the desired time. This will
643 // probably never happen, but in case it does, return false since the
644 // present was obviously NOT late.
645 return false;
646 }
647 uint64_t deadline = actual + rdur;
648 if (actual > deadline) {
649 return true;
650 } else {
651 return false;
652 }
653}
Dave Houlton5fa47912018-02-16 11:02:26 -0700654bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600655 if (earliest < actual) {
656 // Consider whether this present could have occured earlier. Make sure
657 // that earliest time was at least 2msec earlier than actual time, and
658 // that the margin was at least 2msec:
659 uint64_t diff = actual - earliest;
660 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
661 // This present could have occured earlier because both: 1) the
662 // earliest time was at least 2 msec before actual time, and 2) the
663 // margin was at least 2msec.
664 return true;
665 }
666 }
667 return false;
668}
669
Ian Elliottcf074d32015-10-16 18:02:43 -0600670// Forward declaration:
671static void demo_resize(struct demo *demo);
672
Dave Houlton5fa47912018-02-16 11:02:26 -0700673static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700674 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600675 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700676 if ((typeBits & 1) == 1) {
677 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700678 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700679 *typeIndex = i;
680 return true;
681 }
682 }
683 typeBits >>= 1;
684 }
685 // No memory types matched, return failure
686 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600687}
688
Karl Schultze4dc75c2016-02-02 15:37:51 -0700689static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600690 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600691
Tony Barbource7524e2016-07-28 12:01:45 -0600692 // This function could get called twice if the texture uses a staging buffer
693 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700694 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600695
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600696 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600697 assert(!err);
698
Tony Barbource7524e2016-07-28 12:01:45 -0600699 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700700 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700701 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
702 assert(!err);
703
Karl Schultze4dc75c2016-02-02 15:37:51 -0700704 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700705 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
706 .pNext = NULL,
707 .waitSemaphoreCount = 0,
708 .pWaitSemaphores = NULL,
709 .pWaitDstStageMask = NULL,
710 .commandBufferCount = 1,
711 .pCommandBuffers = cmd_bufs,
712 .signalSemaphoreCount = 0,
713 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600714
Tony Barbource7524e2016-07-28 12:01:45 -0600715 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600716 assert(!err);
717
Tony Barbource7524e2016-07-28 12:01:45 -0600718 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600719 assert(!err);
720
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600721 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600722 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600723 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600724}
725
Dave Houlton5fa47912018-02-16 11:02:26 -0700726static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
727 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600728 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600729 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600730
Dave Houlton5fa47912018-02-16 11:02:26 -0700731 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
732 .pNext = NULL,
733 .srcAccessMask = srcAccessMask,
734 .dstAccessMask = 0,
735 .oldLayout = old_image_layout,
736 .newLayout = new_image_layout,
737 .image = image,
738 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600739
Tony Barboureb5077b2016-10-19 15:23:36 -0600740 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700741 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
742 /* Make sure anything that was copying from this image has completed */
743 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
744 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600745
Dave Houlton5fa47912018-02-16 11:02:26 -0700746 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
747 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
748 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700749
Dave Houlton5fa47912018-02-16 11:02:26 -0700750 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
751 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
752 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700753
Dave Houlton5fa47912018-02-16 11:02:26 -0700754 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
755 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
756 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600757
Dave Houlton5fa47912018-02-16 11:02:26 -0700758 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
759 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
760 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600761
Dave Houlton5fa47912018-02-16 11:02:26 -0700762 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
763 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
764 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600765
Dave Houlton5fa47912018-02-16 11:02:26 -0700766 default:
767 image_memory_barrier.dstAccessMask = 0;
768 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600769 }
770
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600771 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600772
Dave Houlton5fa47912018-02-16 11:02:26 -0700773 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600774}
775
Karl Schultze4dc75c2016-02-02 15:37:51 -0700776static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Mark Young66510e52017-11-10 10:05:14 -0700777 VkDebugUtilsLabelEXT label = {};
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700778 const VkCommandBufferBeginInfo cmd_buf_info = {
779 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
780 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600781 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700782 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700783 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800784 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700785 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
786 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800787 };
788 const VkRenderPassBeginInfo rp_begin = {
789 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
790 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800791 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700792 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800793 .renderArea.offset.x = 0,
794 .renderArea.offset.y = 0,
795 .renderArea.extent.width = demo->width,
796 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600797 .clearValueCount = 2,
798 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700799 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800800 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600802 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Mark Young66510e52017-11-10 10:05:14 -0700803
804 if (demo->validate) {
805 // Set a name for the command buffer
806 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
807 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
808 .pNext = NULL,
809 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
810 .objectHandle = (uint64_t)cmd_buf,
811 .pObjectName = "CubeDrawCommandBuf",
812 };
813 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
814
815 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
816 label.pNext = NULL;
817 label.pLabelName = "DrawBegin";
818 label.color[0] = 0.4f;
819 label.color[1] = 0.3f;
820 label.color[2] = 0.2f;
821 label.color[3] = 0.1f;
822 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
823 }
824
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600825 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800826 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Mark Young66510e52017-11-10 10:05:14 -0700827
828 if (demo->validate) {
829 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
830 label.pNext = NULL;
831 label.pLabelName = "InsideRenderPass";
832 label.color[0] = 8.4f;
833 label.color[1] = 7.3f;
834 label.color[2] = 6.2f;
835 label.color[3] = 7.1f;
836 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
837 }
838
Karl Schultze4dc75c2016-02-02 15:37:51 -0700839 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700840 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
841 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600842 VkViewport viewport;
843 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700844 viewport.height = (float)demo->height;
845 viewport.width = (float)demo->width;
846 viewport.minDepth = (float)0.0f;
847 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700848 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600849
850 VkRect2D scissor;
851 memset(&scissor, 0, sizeof(scissor));
852 scissor.extent.width = demo->width;
853 scissor.extent.height = demo->height;
854 scissor.offset.x = 0;
855 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700856 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Mark Young66510e52017-11-10 10:05:14 -0700857
858 if (demo->validate) {
859 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
860 label.pNext = NULL;
861 label.pLabelName = "ActualDraw";
862 label.color[0] = -0.4f;
863 label.color[1] = -0.3f;
864 label.color[2] = -0.2f;
865 label.color[3] = -0.1f;
866 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
867 }
868
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600869 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Mark Young66510e52017-11-10 10:05:14 -0700870 if (demo->validate) {
871 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
872 }
873
Tony Barbour98390392016-07-28 10:50:13 -0600874 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600875 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800876 vkCmdEndRenderPass(cmd_buf);
Mark Young66510e52017-11-10 10:05:14 -0700877 if (demo->validate) {
878 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
879 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600880
Tony Barbour22e06272016-09-07 16:07:56 -0600881 if (demo->separate_present_queue) {
882 // We have to transfer ownership from the graphics queue family to the
883 // present queue family to be able to present. Note that we don't have
884 // to transfer from present queue family back to graphics queue family at
885 // the start of the next frame because we don't care about the image's
886 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700887 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
888 .pNext = NULL,
889 .srcAccessMask = 0,
890 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
891 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
892 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
893 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
894 .dstQueueFamilyIndex = demo->present_queue_family_index,
895 .image = demo->swapchain_image_resources[demo->current_buffer].image,
896 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600897
Dave Houlton5fa47912018-02-16 11:02:26 -0700898 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0,
899 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600900 }
Mark Young66510e52017-11-10 10:05:14 -0700901 if (demo->validate) {
902 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
903 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600904 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600905 assert(!err);
906}
907
Tony Barbour22e06272016-09-07 16:07:56 -0600908void demo_build_image_ownership_cmd(struct demo *demo, int i) {
909 VkResult U_ASSERT_ONLY err;
910
911 const VkCommandBufferBeginInfo cmd_buf_info = {
912 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
913 .pNext = NULL,
914 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
915 .pInheritanceInfo = NULL,
916 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700917 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600918 assert(!err);
919
Dave Houlton5fa47912018-02-16 11:02:26 -0700920 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
921 .pNext = NULL,
922 .srcAccessMask = 0,
923 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
924 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
925 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
926 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
927 .dstQueueFamilyIndex = demo->present_queue_family_index,
928 .image = demo->swapchain_image_resources[i].image,
929 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600930
Dave Houlton5fa47912018-02-16 11:02:26 -0700931 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
932 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700933 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600934 assert(!err);
935}
936
Karl Schultze4dc75c2016-02-02 15:37:51 -0700937void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600938 mat4x4 MVP, Model, VP;
939 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600940 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600941 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600942
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600943 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600944
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700945 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600946 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700947 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600948 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600949
Dave Houlton5fa47912018-02-16 11:02:26 -0700950 err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0,
951 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600952 assert(!err);
953
Karl Schultze4dc75c2016-02-02 15:37:51 -0700954 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600955
Tony Barboura72d9492017-01-11 13:35:09 -0700956 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600957}
958
Ian Elliottd940ca22017-03-22 08:31:27 -0600959void DemoUpdateTargetIPD(struct demo *demo) {
960 // Look at what happened to previous presents, and make appropriate
961 // adjustments in timing:
962 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700963 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600964 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600965
Dave Houlton5fa47912018-02-16 11:02:26 -0700966 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600967 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600968 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700969 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600970 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700971 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600972 assert(!err);
973
974 bool early = false;
975 bool late = false;
976 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700977 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600978 if (!demo->syncd_with_actual_presents) {
979 // This is the first time that we've received an
980 // actualPresentTime for this swapchain. In order to not
981 // perceive these early frames as "late", we need to sync-up
982 // our future desiredPresentTime's with the
983 // actualPresentTime(s) that we're receiving now.
984 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600985
Ian Elliottd940ca22017-03-22 08:31:27 -0600986 // So that we don't suspect any pending presents as late,
987 // record them all as suspected-late presents:
988 demo->last_late_id = demo->next_present_id - 1;
989 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600990 demo->syncd_with_actual_presents = true;
991 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700992 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600993 demo->refresh_duration)) {
994 // This image could have been presented earlier. We don't want
995 // to decrease the target_IPD until we've seen early presents
996 // for at least two seconds.
997 if (demo->last_early_id == past[i].presentID) {
998 // We've now seen two seconds worth of early presents.
999 // Flag it as such, and reset the counter:
1000 early = true;
1001 demo->last_early_id = 0;
1002 } else if (demo->last_early_id == 0) {
1003 // This is the first early present we've seen.
1004 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -07001005 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
1006 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001007 demo->last_early_id = past[i].presentID + howManyPresents;
1008 } else {
1009 // We are in the midst of a set of early images,
1010 // and so we won't do anything.
1011 }
1012 late = false;
1013 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001014 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -06001015 // This image was presented after its desired time. Since
1016 // there's a delay between calling vkQueuePresentKHR and when
1017 // we get the timing data, several presents may have been late.
1018 // Thus, we need to threat all of the outstanding presents as
1019 // being likely late, so that we only increase the target_IPD
1020 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -07001021 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -06001022 late = true;
1023 // Record the last suspected-late present:
1024 demo->last_late_id = demo->next_present_id - 1;
1025 } else {
1026 // We are in the midst of a set of likely-late images,
1027 // and so we won't do anything.
1028 }
1029 early = false;
1030 demo->last_early_id = 0;
1031 } else {
1032 // Since this image was not presented early or late, reset
1033 // any sets of early or late presentIDs:
1034 early = false;
1035 late = false;
1036 calibrate_next = true;
1037 demo->last_early_id = 0;
1038 demo->last_late_id = 0;
1039 }
1040 }
1041
1042 if (early) {
1043 // Since we've seen at least two-seconds worth of presnts that
1044 // could have occured earlier than desired, let's decrease the
1045 // target_IPD (i.e. increase the frame rate):
1046 //
1047 // TODO(ianelliott): Try to calculate a better target_IPD based
1048 // on the most recently-seen present (this is overly-simplistic).
1049 demo->refresh_duration_multiplier--;
1050 if (demo->refresh_duration_multiplier == 0) {
1051 // This should never happen, but in case it does, don't
1052 // try to go faster.
1053 demo->refresh_duration_multiplier = 1;
1054 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001055 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -06001056 }
1057 if (late) {
1058 // Since we found a new instance of a late present, we want to
1059 // increase the target_IPD (i.e. decrease the frame rate):
1060 //
1061 // TODO(ianelliott): Try to calculate a better target_IPD based
1062 // on the most recently-seen present (this is overly-simplistic).
1063 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -07001064 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -06001065 }
1066
1067 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001068 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
1069 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -06001070 }
1071 }
Ian Elliottd940ca22017-03-22 08:31:27 -06001072}
1073
Karl Schultze4dc75c2016-02-02 15:37:51 -07001074static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -06001075 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -06001076
Damien Leonec336d822017-04-14 16:10:14 -07001077 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +03001078 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1079 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -06001080
Tony Barbour8b7c9112017-06-29 13:34:41 -06001081 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -06001082 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -07001083 err =
1084 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1085 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -06001086
Tony Barbour6914e6d2017-06-02 12:11:29 -06001087 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1088 // demo->swapchain is out of date (e.g. the window was resized) and
1089 // must be recreated:
1090 demo_resize(demo);
1091 } else if (err == VK_SUBOPTIMAL_KHR) {
1092 // demo->swapchain is not as optimal as it could be, but the platform's
1093 // presentation engine will still present the image correctly.
1094 break;
1095 } else {
1096 assert(!err);
1097 }
Tony Barbour8b7c9112017-06-29 13:34:41 -06001098 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -06001099
Tony Barbour3eafe1f2017-06-14 11:59:55 -06001100 demo_update_data_buffer(demo);
1101
Ian Elliottd940ca22017-03-22 08:31:27 -06001102 if (demo->VK_GOOGLE_display_timing_enabled) {
1103 // Look at what happened to previous presents, and make appropriate
1104 // adjustments in timing:
1105 DemoUpdateTargetIPD(demo);
1106
1107 // Note: a real application would position its geometry to that it's in
1108 // the correct locatoin for when the next image is presented. It might
1109 // also wait, so that there's less latency between any input and when
1110 // the next image is rendered/presented. This demo program is so
1111 // simple that it doesn't do either of those.
1112 }
1113
Tony Barbource7524e2016-07-28 12:01:45 -06001114 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -06001115 // that the image won't be rendered to until the presentation
1116 // engine has fully released ownership to the application, and it is
1117 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001118 VkPipelineStageFlags pipe_stage_flags;
1119 VkSubmitInfo submit_info;
1120 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1121 submit_info.pNext = NULL;
1122 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1123 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1124 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001125 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001126 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001127 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001128 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001129 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -07001130 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001131 assert(!err);
1132
Tony Barbour22e06272016-09-07 16:07:56 -06001133 if (demo->separate_present_queue) {
1134 // If we are using separate queues, change image ownership to the
1135 // present queue before presenting, waiting for the draw complete
1136 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001137 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001138 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1139 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001140 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001141 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001142 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001143 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001144 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001145 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1146 assert(!err);
1147 }
1148
1149 // If we are using separate queues we have to wait for image ownership,
1150 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001151 VkPresentInfoKHR present = {
1152 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001153 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001154 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -07001155 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1156 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001157 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001158 .pSwapchains = &demo->swapchain,
1159 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001160 };
1161
Ian Elliott53622a72017-03-28 11:06:33 -06001162 if (demo->VK_KHR_incremental_present_enabled) {
1163 // If using VK_KHR_incremental_present, we provide a hint of the region
1164 // that contains changed content relative to the previously-presented
1165 // image. The implementation can use this hint in order to save
1166 // work/power (by only copying the region in the hint). The
1167 // implementation is free to ignore the hint though, and so we must
1168 // ensure that the entire image has the correctly-drawn content.
1169 uint32_t eighthOfWidth = demo->width / 8;
1170 uint32_t eighthOfHeight = demo->height / 8;
1171 VkRectLayerKHR rect = {
1172 .offset.x = eighthOfWidth,
1173 .offset.y = eighthOfHeight,
1174 .extent.width = eighthOfWidth * 6,
1175 .extent.height = eighthOfHeight * 6,
1176 .layer = 0,
1177 };
1178 VkPresentRegionKHR region = {
1179 .rectangleCount = 1,
1180 .pRectangles = &rect,
1181 };
1182 VkPresentRegionsKHR regions = {
1183 .sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1184 .pNext = present.pNext,
1185 .swapchainCount = present.swapchainCount,
1186 .pRegions = &region,
1187 };
1188 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001189 }
1190
Ian Elliottd940ca22017-03-22 08:31:27 -06001191 if (demo->VK_GOOGLE_display_timing_enabled) {
1192 VkPresentTimeGOOGLE ptime;
1193 if (demo->prev_desired_present_time == 0) {
1194 // This must be the first present for this swapchain.
1195 //
1196 // We don't know where we are relative to the presentation engine's
1197 // display's refresh cycle. We also don't know how long rendering
1198 // takes. Let's make a grossly-simplified assumption that the
1199 // desiredPresentTime should be half way between now and
1200 // now+target_IPD. We will adjust over time.
1201 uint64_t curtime = getTimeInNanoseconds();
1202 if (curtime == 0) {
1203 // Since we didn't find out the current time, don't give a
1204 // desiredPresentTime:
1205 ptime.desiredPresentTime = 0;
1206 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001207 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1208 }
1209 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001210 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001211 }
1212 ptime.presentID = demo->next_present_id++;
1213 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001214
1215 VkPresentTimesInfoGOOGLE present_time = {
1216 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1217 .pNext = present.pNext,
1218 .swapchainCount = present.swapchainCount,
1219 .pTimes = &ptime,
1220 };
1221 if (demo->VK_GOOGLE_display_timing_enabled) {
1222 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001223 }
1224 }
1225
Tony Barboura2a67812016-07-18 13:21:06 -06001226 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001227 demo->frame_index += 1;
1228 demo->frame_index %= FRAME_LAG;
1229
Ian Elliottcf074d32015-10-16 18:02:43 -06001230 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1231 // demo->swapchain is out of date (e.g. the window was resized) and
1232 // must be recreated:
1233 demo_resize(demo);
1234 } else if (err == VK_SUBOPTIMAL_KHR) {
1235 // demo->swapchain is not as optimal as it could be, but the platform's
1236 // presentation engine will still present the image correctly.
1237 } else {
1238 assert(!err);
1239 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001240}
1241
Karl Schultze4dc75c2016-02-02 15:37:51 -07001242static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001243 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001244 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001245
Ian Elliottb08e0d92015-11-20 11:55:46 -07001246 // Check the surface capabilities and formats
1247 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001248 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001249 assert(!err);
1250
Ian Elliott8528eff2015-08-07 15:56:59 -06001251 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001252 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001253 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001254 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001255 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001256 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001257 assert(!err);
1258
Ian Elliotta0781972015-08-21 15:09:33 -06001259 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001260 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1261 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1262 // If the surface size is undefined, the size is set to the size
1263 // of the images requested, which must fit within the minimum and
1264 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001265 swapchainExtent.width = demo->width;
1266 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001267
1268 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1269 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1270 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1271 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1272 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001273
Ian Elliottcf7f7482016-08-19 03:46:58 -06001274 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1275 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1276 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1277 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1278 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001279 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001280 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001281 swapchainExtent = surfCapabilities.currentExtent;
1282 demo->width = surfCapabilities.currentExtent.width;
1283 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001284 }
1285
Jeremy Kniager602e4182018-01-19 10:52:34 -07001286 if (demo->width == 0 || demo->height == 0) {
1287 demo->is_minimized = true;
1288 return;
1289 } else {
1290 demo->is_minimized = false;
1291 }
1292
Tony Barbour66a56c32016-09-21 13:10:56 -06001293 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001294 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001295 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001296
Ian Elliottb18fdde2016-10-03 12:11:12 -06001297 // There are times when you may wish to use another present mode. The
1298 // following code shows how to select them, and the comments provide some
1299 // reasons you may wish to use them.
1300 //
1301 // It should be noted that Vulkan 1.0 doesn't provide a method for
1302 // synchronizing rendering with the presentation engine's display. There
1303 // is a method provided for throttling rendering with the display, but
1304 // there are some presentation engines for which this method will not work.
1305 // If an application doesn't throttle its rendering, and if it renders much
1306 // faster than the refresh rate of the display, this can waste power on
1307 // mobile devices. That is because power is being spent rendering images
1308 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001309
Ian Elliottb18fdde2016-10-03 12:11:12 -06001310 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1311 // tearing, or have some way of synchronizing their rendering with the
1312 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001313 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1314 // generally render a new presentable image every refresh cycle, but are
1315 // occasionally early. In this case, the application wants the new image
1316 // to be displayed instead of the previously-queued-for-presentation image
1317 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001318 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1319 // render a new presentable image every refresh cycle, but are occasionally
1320 // late. In this case (perhaps because of stuttering/latency concerns),
1321 // the application wants the late image to be immediately displayed, even
1322 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001323
Dave Houlton5fa47912018-02-16 11:02:26 -07001324 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001325 for (size_t i = 0; i < presentModeCount; ++i) {
1326 if (presentModes[i] == demo->presentMode) {
1327 swapchainPresentMode = demo->presentMode;
1328 break;
1329 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001330 }
1331 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001332 if (swapchainPresentMode != demo->presentMode) {
1333 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1334 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001335
Tony Barbour84376fe2017-01-06 15:54:22 -07001336 // Determine the number of VkImages to use in the swap chain.
1337 // Application desires to acquire 3 images at a time for triple
1338 // buffering
1339 uint32_t desiredNumOfSwapchainImages = 3;
1340 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1341 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1342 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001343 // If maxImageCount is 0, we can ask for as many images as we want;
1344 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001345 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001346 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001347 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001348 }
1349
Tony Barbour9fd6d352015-09-16 15:01:32 -06001350 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001351 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001352 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001353 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001354 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001355 }
1356
Tony Barbour820b55b2017-03-14 08:55:46 -06001357 // Find a supported composite alpha mode - one of these is guaranteed to be set
1358 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1359 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1360 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1361 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1362 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1363 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1364 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001365 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001366 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1367 compositeAlpha = compositeAlphaFlags[i];
1368 break;
1369 }
1370 }
1371
Tony Barboura2a67812016-07-18 13:21:06 -06001372 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001373 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001374 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001375 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001376 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001377 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001378 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001379 .imageExtent =
1380 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001381 .width = swapchainExtent.width,
1382 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001384 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001385 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001386 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001387 .imageArrayLayers = 1,
1388 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1389 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001390 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001391 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001392 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001393 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001394 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001395 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001396 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001397 assert(!err);
1398
Ian Elliottcf074d32015-10-16 18:02:43 -06001399 // If we just re-created an existing swapchain, we should destroy the old
1400 // swapchain at this point.
1401 // Note: destroying the swapchain also cleans up all its associated
1402 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001403 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001404 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001405 }
1406
Dave Houlton5fa47912018-02-16 11:02:26 -07001407 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001408 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001409
Dave Houlton5fa47912018-02-16 11:02:26 -07001410 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001411 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001412 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001413 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001414
Dave Houlton5fa47912018-02-16 11:02:26 -07001415 demo->swapchain_image_resources =
1416 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001417 assert(demo->swapchain_image_resources);
1418
Ian Elliotta0781972015-08-21 15:09:33 -06001419 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001420 VkImageViewCreateInfo color_image_view = {
1421 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001422 .pNext = NULL,
1423 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001424 .components =
1425 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001426 .r = VK_COMPONENT_SWIZZLE_R,
1427 .g = VK_COMPONENT_SWIZZLE_G,
1428 .b = VK_COMPONENT_SWIZZLE_B,
1429 .a = VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001430 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001431 .subresourceRange =
1432 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001433 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1434 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435 };
1436
Tony Barboura72d9492017-01-11 13:35:09 -07001437 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438
Tony Barboura72d9492017-01-11 13:35:09 -07001439 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001440
Dave Houlton5fa47912018-02-16 11:02:26 -07001441 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001442 assert(!err);
1443 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001444
Ian Elliottd940ca22017-03-22 08:31:27 -06001445 if (demo->VK_GOOGLE_display_timing_enabled) {
1446 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001447 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001448 assert(!err);
1449 demo->refresh_duration = rc_dur.refreshDuration;
1450
1451 demo->syncd_with_actual_presents = false;
1452 // Initially target 1X the refresh duration:
1453 demo->target_IPD = demo->refresh_duration;
1454 demo->refresh_duration_multiplier = 1;
1455 demo->prev_desired_present_time = 0;
1456 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001457 }
1458
Mark Young04fd3d82016-01-25 14:43:50 -07001459 if (NULL != presentModes) {
1460 free(presentModes);
1461 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001462}
1463
Karl Schultze4dc75c2016-02-02 15:37:51 -07001464static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001465 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001466 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001467 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001469 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001470 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001471 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001473 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001474 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001475 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001476 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001477 .flags = 0,
1478 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001479
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001480 VkImageViewCreateInfo view = {
1481 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001482 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001483 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001484 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001485 .subresourceRange =
1486 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001487 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001488 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001489 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001490
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001491 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001492 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001493 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001494
1495 demo->depth.format = depth_format;
1496
1497 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001498 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001499 assert(!err);
1500
Karl Schultze4dc75c2016-02-02 15:37:51 -07001501 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001502 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001503
Chia-I Wuf48700b2015-11-10 16:21:09 +08001504 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001505 demo->depth.mem_alloc.pNext = NULL;
1506 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1507 demo->depth.mem_alloc.memoryTypeIndex = 0;
1508
Dave Houlton5fa47912018-02-16 11:02:26 -07001509 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001510 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001511 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001512
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001513 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001514 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001515 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001516
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001517 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001518 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001519 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520
1521 /* create image view */
1522 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001523 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001524 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001525}
1526
Tony Barbour1a86d032015-09-21 15:17:33 -06001527/* Load a ppm file into memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001528bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001529#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Dave Houlton5fa47912018-02-16 11:02:26 -07001530 filename = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001531#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001532
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001533#ifdef __ANDROID__
1534#include <lunarg.ppm.h>
1535 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001536 cPtr = (char *)lunarg_ppm;
1537 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001538 return false;
1539 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001540 while (strncmp(cPtr++, "\n", 1))
1541 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001542 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001543 if (rgba_data == NULL) {
1544 return true;
1545 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001546 while (strncmp(cPtr++, "\n", 1))
1547 ;
1548 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001549 return false;
1550 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001551 while (strncmp(cPtr++, "\n", 1))
1552 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001553
1554 for (int y = 0; y < *height; y++) {
1555 uint8_t *rowPtr = rgba_data;
1556 for (int x = 0; x < *width; x++) {
1557 memcpy(rowPtr, cPtr, 3);
1558 rowPtr[3] = 255; /* Alpha of 1 */
1559 rowPtr += 4;
1560 cPtr += 3;
1561 }
1562 rgba_data += layout->rowPitch;
1563 }
1564
1565 return true;
1566#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001567 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001568 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001569
Dave Houlton5fa47912018-02-16 11:02:26 -07001570 if (!fPtr) return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001571
Dave Houlton5fa47912018-02-16 11:02:26 -07001572 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001573 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1574 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001575 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001576 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001577
Tony Barbour1a86d032015-09-21 15:17:33 -06001578 do {
1579 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001580 if (cPtr == NULL) {
1581 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001582 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001583 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001584 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001585
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001586 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001587 if (rgba_data == NULL) {
1588 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001589 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001590 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001591 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001592 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001593 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1594 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001595 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001596 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001597
Karl Schultze4dc75c2016-02-02 15:37:51 -07001598 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001599 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001600 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001601 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001602 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001603 rowPtr[3] = 255; /* Alpha of 1 */
1604 rowPtr += 4;
1605 }
1606 rgba_data += layout->rowPitch;
1607 }
1608 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001609 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001610#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001611}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001612
Dave Houlton5fa47912018-02-16 11:02:26 -07001613static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1614 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001615 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001616 int32_t tex_width;
1617 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001618 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001619 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001620
Karl Schultze4dc75c2016-02-02 15:37:51 -07001621 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001622 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001623 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001624
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001625 tex_obj->tex_width = tex_width;
1626 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001627
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001628 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001629 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001630 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001631 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001632 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001633 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001634 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001635 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001636 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001637 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001638 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001639 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001640 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001641 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001642
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001643 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001644
Dave Houlton5fa47912018-02-16 11:02:26 -07001645 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001646 assert(!err);
1647
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001648 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001649
Chia-I Wuf48700b2015-11-10 16:21:09 +08001650 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001651 tex_obj->mem_alloc.pNext = NULL;
1652 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1653 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001654
Dave Houlton5fa47912018-02-16 11:02:26 -07001655 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001656 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001657
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001658 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001659 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001660 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001661
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001662 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001663 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001664 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001665
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001666 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001667 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001668 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001669 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001670 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001671 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001672 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001673 void *data;
1674
Dave Houlton5fa47912018-02-16 11:02:26 -07001675 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001676
Dave Houlton5fa47912018-02-16 11:02:26 -07001677 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001678 assert(!err);
1679
1680 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1681 fprintf(stderr, "Error loading texture: %s\n", filename);
1682 }
1683
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001684 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001685 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001686
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001687 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001688}
1689
Dave Houlton5fa47912018-02-16 11:02:26 -07001690static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001691 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001692 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1693 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001694}
1695
Karl Schultze4dc75c2016-02-02 15:37:51 -07001696static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001697 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001698 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001699 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001700
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001701 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001702
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001703 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001704 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001705
Dave Houlton5fa47912018-02-16 11:02:26 -07001706 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001707 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001708 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1709 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001710 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1711 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001712 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1713 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001714 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001715 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001716 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001717 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001718
Tony Barbour16156cb2016-10-19 14:15:49 -06001719 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Dave Houlton5fa47912018-02-16 11:02:26 -07001720 demo_prepare_texture_image(demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
1721 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1722 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001723
Dave Houlton5fa47912018-02-16 11:02:26 -07001724 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1725 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1726 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001727
Dave Houlton5fa47912018-02-16 11:02:26 -07001728 demo_set_image_layout(demo, demo->staging_texture.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1729 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001730 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001731
Dave Houlton5fa47912018-02-16 11:02:26 -07001732 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1733 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001734 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001735
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001736 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001737 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1738 .srcOffset = {0, 0, 0},
1739 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1740 .dstOffset = {0, 0, 0},
Dave Houlton5fa47912018-02-16 11:02:26 -07001741 .extent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001742 };
Dave Houlton5fa47912018-02-16 11:02:26 -07001743 vkCmdCopyImage(demo->cmd, demo->staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1744 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001745
Dave Houlton5fa47912018-02-16 11:02:26 -07001746 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1747 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001748 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001749
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001750 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001751 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1752 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001753 }
1754
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001755 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001756 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001757 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001758 .magFilter = VK_FILTER_NEAREST,
1759 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001760 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001761 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1762 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1763 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001764 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001765 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001766 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001767 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001768 .minLod = 0.0f,
1769 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001770 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001771 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001772 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001773
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001774 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001775 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001776 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001777 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001778 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001779 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001780 .components =
1781 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001782 VK_COMPONENT_SWIZZLE_R,
1783 VK_COMPONENT_SWIZZLE_G,
1784 VK_COMPONENT_SWIZZLE_B,
1785 VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001786 },
1787 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001788 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001789 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001790
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001791 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001792 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001793 assert(!err);
1794
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001795 /* create image view */
1796 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001797 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001798 assert(!err);
1799 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001800}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001801
Tony Barboura72d9492017-01-11 13:35:09 -07001802void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001803 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001804 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001805 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001806 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001807 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001808 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001809 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001810 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001811
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001812 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001813 mat4x4_mul(MVP, VP, demo->model_matrix);
1814 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001815 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001816
Karl Schultza2615462017-01-20 13:19:20 -07001817 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001818 data.position[i][0] = g_vertex_buffer_data[i * 3];
1819 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1820 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001821 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001822 data.attr[i][0] = g_uv_buffer_data[2 * i];
1823 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001824 data.attr[i][2] = 0;
1825 data.attr[i][3] = 0;
1826 }
1827
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001828 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001829 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001830 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001831 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001832
Tony Barboura72d9492017-01-11 13:35:09 -07001833 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001834 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001835 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001836
Dave Houlton5fa47912018-02-16 11:02:26 -07001837 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001838
Tony Barboura72d9492017-01-11 13:35:09 -07001839 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1840 mem_alloc.pNext = NULL;
1841 mem_alloc.allocationSize = mem_reqs.size;
1842 mem_alloc.memoryTypeIndex = 0;
1843
Dave Houlton5fa47912018-02-16 11:02:26 -07001844 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1845 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1846 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001847 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001848
Dave Houlton5fa47912018-02-16 11:02:26 -07001849 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001850 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001851
Dave Houlton5fa47912018-02-16 11:02:26 -07001852 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 -07001853 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001854
Tony Barboura72d9492017-01-11 13:35:09 -07001855 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001856
Tony Barboura72d9492017-01-11 13:35:09 -07001857 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001858
Tony Barboura72d9492017-01-11 13:35:09 -07001859 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001860 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001861 assert(!err);
1862 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001863}
1864
Karl Schultze4dc75c2016-02-02 15:37:51 -07001865static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001866 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001867 [0] =
1868 {
1869 .binding = 0,
1870 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1871 .descriptorCount = 1,
1872 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1873 .pImmutableSamplers = NULL,
1874 },
1875 [1] =
1876 {
1877 .binding = 1,
1878 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1879 .descriptorCount = DEMO_TEXTURE_COUNT,
1880 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1881 .pImmutableSamplers = NULL,
1882 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001883 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001884 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001885 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001886 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001887 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001888 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001889 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001890 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001891
Dave Houlton5fa47912018-02-16 11:02:26 -07001892 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001893 assert(!err);
1894
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001895 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001896 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1897 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001898 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001899 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001900 };
1901
Dave Houlton5fa47912018-02-16 11:02:26 -07001902 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001903 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001904}
1905
Karl Schultze4dc75c2016-02-02 15:37:51 -07001906static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001907 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1908 // because at the start of the renderpass, we don't care about their contents.
1909 // At the start of the subpass, the color attachment's layout will be transitioned
1910 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1911 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1912 // the renderpass, the color attachment's layout will be transitioned to
1913 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1914 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001915 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001916 [0] =
1917 {
1918 .format = demo->format,
1919 .flags = 0,
1920 .samples = VK_SAMPLE_COUNT_1_BIT,
1921 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1922 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1923 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1924 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1925 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1926 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1927 },
1928 [1] =
1929 {
1930 .format = demo->depth.format,
1931 .flags = 0,
1932 .samples = VK_SAMPLE_COUNT_1_BIT,
1933 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1934 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1935 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1936 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1937 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1938 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1939 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001940 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001941 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001942 .attachment = 0,
1943 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001944 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001945 const VkAttachmentReference depth_reference = {
1946 .attachment = 1,
1947 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1948 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001949 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001950 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1951 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001952 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001953 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001954 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001955 .pColorAttachments = &color_reference,
1956 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001957 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001958 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001959 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001960 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001961 const VkRenderPassCreateInfo rp_info = {
1962 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1963 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001964 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001965 .attachmentCount = 2,
1966 .pAttachments = attachments,
1967 .subpassCount = 1,
1968 .pSubpasses = &subpass,
1969 .dependencyCount = 0,
1970 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001971 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001972 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001973
Chia-I Wu63636062015-10-26 21:10:41 +08001974 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001975 assert(!err);
1976}
1977
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001978static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001979 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001980 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001981 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001983 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1984 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001985 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001986 moduleCreateInfo.codeSize = size;
1987 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001988
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001989 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1990 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001991
1992 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001993}
1994
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001995static void demo_prepare_vs(struct demo *demo) {
1996 const uint32_t vs_code[] = {
1997#include "cube.vert.inc"
1998 };
1999 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002000}
2001
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002002static void demo_prepare_fs(struct demo *demo) {
2003 const uint32_t fs_code[] = {
2004#include "cube.frag.inc"
2005 };
2006 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002007}
2008
Karl Schultze4dc75c2016-02-02 15:37:51 -07002009static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002010 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002011 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002012 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002013 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002014 VkPipelineRasterizationStateCreateInfo rs;
2015 VkPipelineColorBlendStateCreateInfo cb;
2016 VkPipelineDepthStencilStateCreateInfo ds;
2017 VkPipelineViewportStateCreateInfo vp;
2018 VkPipelineMultisampleStateCreateInfo ms;
2019 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
2020 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06002021 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002022
Piers Daniellba839d12015-09-29 13:01:09 -06002023 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2024 memset(&dynamicState, 0, sizeof dynamicState);
2025 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2026 dynamicState.pDynamicStates = dynamicStateEnables;
2027
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002028 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002029 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002030 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002031
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002032 memset(&vi, 0, sizeof(vi));
2033 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2034
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002035 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002036 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002037 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002038
2039 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002040 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2041 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002042 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002043 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002044 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002045 rs.rasterizerDiscardEnable = VK_FALSE;
2046 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002047 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002048
2049 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002050 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2051 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002052 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002053 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002054 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002055 cb.attachmentCount = 1;
2056 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002057
Tony Barbour29645d02015-01-16 14:27:35 -07002058 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002059 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002060 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002061 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002062 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002063 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002064
2065 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002066 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002067 ds.depthTestEnable = VK_TRUE;
2068 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002069 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002070 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002071 ds.back.failOp = VK_STENCIL_OP_KEEP;
2072 ds.back.passOp = VK_STENCIL_OP_KEEP;
2073 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002074 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002075 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002076
Tony Barbour29645d02015-01-16 14:27:35 -07002077 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002078 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002079 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002080 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002081
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002082 demo_prepare_vs(demo);
2083 demo_prepare_fs(demo);
2084
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002085 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002086 VkPipelineShaderStageCreateInfo shaderStages[2];
2087 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2088
Karl Schultze4dc75c2016-02-02 15:37:51 -07002089 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2090 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002091 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002092 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002093
Karl Schultze4dc75c2016-02-02 15:37:51 -07002094 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2095 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002096 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002097 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002098
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002099 memset(&pipelineCache, 0, sizeof(pipelineCache));
2100 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002101
Dave Houlton5fa47912018-02-16 11:02:26 -07002102 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002103 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002104
Karl Schultze4dc75c2016-02-02 15:37:51 -07002105 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002106 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002107 pipeline.pRasterizationState = &rs;
2108 pipeline.pColorBlendState = &cb;
2109 pipeline.pMultisampleState = &ms;
2110 pipeline.pViewportState = &vp;
2111 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002112 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002113 pipeline.pStages = shaderStages;
2114 pipeline.renderPass = demo->render_pass;
2115 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002116
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002117 pipeline.renderPass = demo->render_pass;
2118
Dave Houlton5fa47912018-02-16 11:02:26 -07002119 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002120 assert(!err);
2121
Chia-I Wu63636062015-10-26 21:10:41 +08002122 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2123 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002124}
2125
Karl Schultze4dc75c2016-02-02 15:37:51 -07002126static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002127 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07002128 [0] =
2129 {
2130 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2131 .descriptorCount = demo->swapchainImageCount,
2132 },
2133 [1] =
2134 {
2135 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2136 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2137 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002138 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002139 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002140 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002141 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002142 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002143 .poolSizeCount = 2,
2144 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002145 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002146 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002147
Dave Houlton5fa47912018-02-16 11:02:26 -07002148 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002149 assert(!err);
2150}
2151
Karl Schultze4dc75c2016-02-02 15:37:51 -07002152static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002153 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002154 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002155 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002156
Dave Houlton5fa47912018-02-16 11:02:26 -07002157 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2158 .pNext = NULL,
2159 .descriptorPool = demo->desc_pool,
2160 .descriptorSetCount = 1,
2161 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002162
2163 VkDescriptorBufferInfo buffer_info;
2164 buffer_info.offset = 0;
2165 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002166
Chia-I Wuae721ba2015-05-25 16:27:55 +08002167 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002168 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002169 tex_descs[i].sampler = demo->textures[i].sampler;
2170 tex_descs[i].imageView = demo->textures[i].view;
2171 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002172 }
2173
2174 memset(&writes, 0, sizeof(writes));
2175
2176 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002177 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002178 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002179 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002180
2181 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002182 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002183 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002184 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002185 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002186
Tony Barboura72d9492017-01-11 13:35:09 -07002187 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2188 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2189 assert(!err);
2190 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2191 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2192 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2193 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2194 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002195}
2196
Karl Schultze4dc75c2016-02-02 15:37:51 -07002197static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002198 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002199 attachments[1] = demo->depth.view;
2200
Chia-I Wuf973e322015-07-08 13:34:24 +08002201 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002202 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2203 .pNext = NULL,
2204 .renderPass = demo->render_pass,
2205 .attachmentCount = 2,
2206 .pAttachments = attachments,
2207 .width = demo->width,
2208 .height = demo->height,
2209 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002210 };
2211 VkResult U_ASSERT_ONLY err;
2212 uint32_t i;
2213
Tony Barbourd8e504f2015-10-08 14:26:24 -06002214 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002215 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002216 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002217 assert(!err);
2218 }
2219}
2220
Karl Schultze4dc75c2016-02-02 15:37:51 -07002221static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002222 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002223 if (demo->cmd_pool == VK_NULL_HANDLE) {
2224 const VkCommandPoolCreateInfo cmd_pool_info = {
2225 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2226 .pNext = NULL,
2227 .queueFamilyIndex = demo->graphics_queue_family_index,
2228 .flags = 0,
2229 };
2230 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2231 assert(!err);
2232 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002233
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002234 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002235 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002236 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002237 .commandPool = demo->cmd_pool,
2238 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002239 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002240 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002241 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2242 assert(!err);
2243 VkCommandBufferBeginInfo cmd_buf_info = {
2244 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2245 .pNext = NULL,
2246 .flags = 0,
2247 .pInheritanceInfo = NULL,
2248 };
2249 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2250 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002251
2252 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002253
2254 if (demo->is_minimized) {
2255 demo->prepared = false;
2256 return;
2257 }
2258
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002259 demo_prepare_depth(demo);
2260 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002261 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002262
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002263 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002264 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002265 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002266
Ian Elliotta0781972015-08-21 15:09:33 -06002267 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002268 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002269 assert(!err);
2270 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002271
Tony Barbour22e06272016-09-07 16:07:56 -06002272 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002273 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002274 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2275 .pNext = NULL,
2276 .queueFamilyIndex = demo->present_queue_family_index,
2277 .flags = 0,
2278 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002279 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002280 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002281 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002282 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2283 .pNext = NULL,
2284 .commandPool = demo->present_cmd_pool,
2285 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2286 .commandBufferCount = 1,
2287 };
2288 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002289 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2290 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002291 assert(!err);
2292 demo_build_image_ownership_cmd(demo, i);
2293 }
2294 }
2295
Chia-I Wu63ea9262015-03-26 13:14:16 +08002296 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002297 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002298
Chia-I Wuf973e322015-07-08 13:34:24 +08002299 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002300
Ian Elliotta0781972015-08-21 15:09:33 -06002301 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002302 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002303 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002304 }
2305
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002306 /*
2307 * Prepare functions above may generate pipeline commands
2308 * that need to be flushed before beginning the render loop.
2309 */
2310 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002311 if (demo->staging_texture.image) {
2312 demo_destroy_texture_image(demo, &demo->staging_texture);
2313 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002314
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002315 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002316 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002317}
2318
Karl Schultze4dc75c2016-02-02 15:37:51 -07002319static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002320 uint32_t i;
2321
2322 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002323 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002324
Tony Barbour66a56c32016-09-21 13:10:56 -06002325 // Wait for fences from present operations
2326 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002327 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002328 vkDestroyFence(demo->device, demo->fences[i], NULL);
2329 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2330 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2331 if (demo->separate_present_queue) {
2332 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2333 }
2334 }
2335
Jeremy Kniager602e4182018-01-19 10:52:34 -07002336 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2337 if (!demo->is_minimized) {
2338 for (i = 0; i < demo->swapchainImageCount; i++) {
2339 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2340 }
2341 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002342
Jeremy Kniager602e4182018-01-19 10:52:34 -07002343 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2344 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2345 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2346 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2347 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002348
Jeremy Kniager602e4182018-01-19 10:52:34 -07002349 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2350 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2351 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2352 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2353 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2354 }
2355 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002356
Jeremy Kniager602e4182018-01-19 10:52:34 -07002357 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2358 vkDestroyImage(demo->device, demo->depth.image, NULL);
2359 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002360
Jeremy Kniager602e4182018-01-19 10:52:34 -07002361 for (i = 0; i < demo->swapchainImageCount; i++) {
2362 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2363 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2364 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2365 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2366 }
2367 free(demo->swapchain_image_resources);
2368 free(demo->queue_props);
2369 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002370
Jeremy Kniager602e4182018-01-19 10:52:34 -07002371 if (demo->separate_present_queue) {
2372 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2373 }
Tony Barbour22e06272016-09-07 16:07:56 -06002374 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002375 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002376 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002377 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07002378 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002379 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002380 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002381
Tony Barbour153cb062016-12-07 13:43:36 -07002382#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002383 XDestroyWindow(demo->display, demo->xlib_window);
2384 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002385#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002386 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002387 xcb_disconnect(demo->connection);
2388 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002389#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002390 wl_keyboard_destroy(demo->keyboard);
2391 wl_pointer_destroy(demo->pointer);
2392 wl_seat_destroy(demo->seat);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002393 wl_shell_surface_destroy(demo->shell_surface);
2394 wl_surface_destroy(demo->window);
2395 wl_shell_destroy(demo->shell);
2396 wl_compositor_destroy(demo->compositor);
2397 wl_registry_destroy(demo->registry);
2398 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002399#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002400#endif
Karl Schultz86429112017-06-20 11:27:59 -06002401
2402 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002403}
2404
Karl Schultze4dc75c2016-02-02 15:37:51 -07002405static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002406 uint32_t i;
2407
Mike Stroyanbb60b382015-10-28 09:46:57 -06002408 // Don't react to resize until after first initialization.
2409 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002410 if (demo->is_minimized) {
2411 demo_prepare(demo);
2412 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002413 return;
2414 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002415 // In order to properly resize the window, we must re-create the swapchain
2416 // AND redo the command buffers, etc.
2417 //
2418 // First, perform part of the demo_cleanup() function:
2419 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002420 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002421
2422 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002423 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002424 }
Chia-I Wu63636062015-10-26 21:10:41 +08002425 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002426
Chia-I Wu63636062015-10-26 21:10:41 +08002427 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2428 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2429 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2430 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2431 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002432
2433 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002434 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2435 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2436 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2437 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002438 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002439
Chia-I Wu63636062015-10-26 21:10:41 +08002440 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2441 vkDestroyImage(demo->device, demo->depth.image, NULL);
2442 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002443
Ian Elliottcf074d32015-10-16 18:02:43 -06002444 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002445 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002446 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002447 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2448 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002449 }
Chia-I Wu63636062015-10-26 21:10:41 +08002450 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002451 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002452 if (demo->separate_present_queue) {
2453 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2454 }
Tony Barboura72d9492017-01-11 13:35:09 -07002455 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002456
Ian Elliottcf074d32015-10-16 18:02:43 -06002457 // Second, re-perform the demo_prepare() function, which will re-create the
2458 // swapchain:
2459 demo_prepare(demo);
2460}
2461
David Pinedo2bb7c932015-06-18 17:03:14 -06002462// On MS-Windows, make this a global, so it's available to WndProc()
2463struct demo demo;
2464
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002465#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002466static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002467 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002468
Ian Elliott639ca472015-04-16 15:23:05 -06002469 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002470 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002471 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002472 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002473 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002474}
Ian Elliott639ca472015-04-16 15:23:05 -06002475
2476// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002477LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2478 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002479 case WM_CLOSE:
2480 PostQuitMessage(validation_error);
2481 break;
2482 case WM_PAINT:
2483 // The validation callback calls MessageBox which can generate paint
2484 // events - don't make more Vulkan calls if we got here from the
2485 // callback
2486 if (!in_callback) {
2487 demo_run(&demo);
2488 }
2489 break;
2490 case WM_GETMINMAXINFO: // set window's minimum size
2491 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2492 return 0;
2493 case WM_SIZE:
2494 // Resize the application to the new window size, except when
2495 // it was minimized. Vulkan doesn't support images or swapchains
2496 // with width=0 and height=0.
2497 if (wParam != SIZE_MINIMIZED) {
2498 demo.width = lParam & 0xffff;
2499 demo.height = (lParam & 0xffff0000) >> 16;
2500 demo_resize(&demo);
2501 }
2502 break;
2503 default:
2504 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002505 }
2506 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2507}
2508
Karl Schultze4dc75c2016-02-02 15:37:51 -07002509static void demo_create_window(struct demo *demo) {
2510 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002511
2512 // Initialize the window class structure:
2513 win_class.cbSize = sizeof(WNDCLASSEX);
2514 win_class.style = CS_HREDRAW | CS_VREDRAW;
2515 win_class.lpfnWndProc = WndProc;
2516 win_class.cbClsExtra = 0;
2517 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002518 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002519 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2520 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2521 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2522 win_class.lpszMenuName = NULL;
2523 win_class.lpszClassName = demo->name;
2524 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2525 // Register window class:
2526 if (!RegisterClassEx(&win_class)) {
2527 // It didn't work, so try to give a useful error:
2528 printf("Unexpected error trying to start the application!\n");
2529 fflush(stdout);
2530 exit(1);
2531 }
2532 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002533 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002534 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002535 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002536 demo->name, // class name
2537 demo->name, // app name
2538 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002539 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002540 100, 100, // x/y coords
2541 wr.right - wr.left, // width
2542 wr.bottom - wr.top, // height
2543 NULL, // handle to parent
2544 NULL, // handle to menu
2545 demo->connection, // hInstance
2546 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002547 if (!demo->window) {
2548 // It didn't work, so try to give a useful error:
2549 printf("Cannot create a window in which to draw!\n");
2550 fflush(stdout);
2551 exit(1);
2552 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002553 // Window client area size must be at least 1 pixel high, to prevent crash.
2554 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002555 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002556}
Tony Barbour8295ac42016-08-08 11:04:17 -06002557#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002558static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002559 const char *display_envar = getenv("DISPLAY");
2560 if (display_envar == NULL || display_envar[0] == '\0') {
2561 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2562 fflush(stdout);
2563 exit(1);
2564 }
Tony Barbour2593b182016-04-19 10:57:58 -06002565
Tony Barbouree9cd372017-01-31 16:09:58 -07002566 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002567 demo->display = XOpenDisplay(NULL);
2568 long visualMask = VisualScreenMask;
2569 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002570 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002571 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002572 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002573
Dave Houlton5fa47912018-02-16 11:02:26 -07002574 Colormap colormap =
2575 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002576
Dave Houlton5fa47912018-02-16 11:02:26 -07002577 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002578 windowAttributes.colormap = colormap;
2579 windowAttributes.background_pixel = 0xFFFFFFFF;
2580 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002581 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002582
Dave Houlton5fa47912018-02-16 11:02:26 -07002583 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2584 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2585 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002586
2587 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2588 XMapWindow(demo->display, demo->xlib_window);
2589 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002590 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002591}
2592static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002593 switch (event->type) {
2594 case ClientMessage:
2595 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002596 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002597 case KeyPress:
2598 switch (event->xkey.keycode) {
2599 case 0x9: // Escape
2600 demo->quit = true;
2601 break;
2602 case 0x71: // left arrow key
2603 demo->spin_angle -= demo->spin_increment;
2604 break;
2605 case 0x72: // right arrow key
2606 demo->spin_angle += demo->spin_increment;
2607 break;
2608 case 0x41: // space bar
2609 demo->pause = !demo->pause;
2610 break;
2611 }
Tony Barbour2593b182016-04-19 10:57:58 -06002612 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002613 case ConfigureNotify:
2614 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2615 demo->width = event->xconfigure.width;
2616 demo->height = event->xconfigure.height;
2617 demo_resize(demo);
2618 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002619 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002620 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002621 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002622 }
Tony Barbour2593b182016-04-19 10:57:58 -06002623}
2624
2625static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002626 while (!demo->quit) {
2627 XEvent event;
2628
2629 if (demo->pause) {
2630 XNextEvent(demo->display, &event);
2631 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002632 }
2633 while (XPending(demo->display) > 0) {
2634 XNextEvent(demo->display, &event);
2635 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002636 }
2637
Tony Barbour2593b182016-04-19 10:57:58 -06002638 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002639 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002640 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002641 }
2642}
Tony Barbour153cb062016-12-07 13:43:36 -07002643#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002644static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002645 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002646 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002647 case XCB_EXPOSE:
2648 // TODO: Resize window
2649 break;
2650 case XCB_CLIENT_MESSAGE:
2651 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2652 demo->quit = true;
2653 }
2654 break;
2655 case XCB_KEY_RELEASE: {
2656 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002657
Dave Houlton5fa47912018-02-16 11:02:26 -07002658 switch (key->detail) {
2659 case 0x9: // Escape
2660 demo->quit = true;
2661 break;
2662 case 0x71: // left arrow key
2663 demo->spin_angle -= demo->spin_increment;
2664 break;
2665 case 0x72: // right arrow key
2666 demo->spin_angle += demo->spin_increment;
2667 break;
2668 case 0x41: // space bar
2669 demo->pause = !demo->pause;
2670 break;
2671 }
2672 } break;
2673 case XCB_CONFIGURE_NOTIFY: {
2674 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2675 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2676 demo->width = cfg->width;
2677 demo->height = cfg->height;
2678 demo_resize(demo);
2679 }
2680 } break;
2681 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002682 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002683 }
2684}
2685
Tony Barbour2593b182016-04-19 10:57:58 -06002686static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002687 xcb_flush(demo->connection);
2688
2689 while (!demo->quit) {
2690 xcb_generic_event_t *event;
2691
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002692 if (demo->pause) {
2693 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002694 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002695 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002696 }
2697 while (event) {
2698 demo_handle_xcb_event(demo, event);
2699 free(event);
2700 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002701 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002702
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002703 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002704 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002705 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002706 }
2707}
2708
Tony Barbour2593b182016-04-19 10:57:58 -06002709static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002710 uint32_t value_mask, value_list[32];
2711
Tony Barbour2593b182016-04-19 10:57:58 -06002712 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002713
2714 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2715 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002716 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002717
Dave Houlton5fa47912018-02-16 11:02:26 -07002718 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2719 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002720
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002721 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002722 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2723 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002724
Dave Houlton5fa47912018-02-16 11:02:26 -07002725 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2726 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002727
Dave Houlton5fa47912018-02-16 11:02:26 -07002728 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002729 &(*demo->atom_wm_delete_window).atom);
2730 free(reply);
2731
Tony Barbour2593b182016-04-19 10:57:58 -06002732 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002733
Karl Schultze4dc75c2016-02-02 15:37:51 -07002734 // Force the x/y coordinates to 100,100 results are identical in consecutive
2735 // runs
2736 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002737 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002738}
Tony Barbour6b131662016-08-25 13:14:45 -06002739// VK_USE_PLATFORM_XCB_KHR
2740#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002741static void demo_run(struct demo *demo) {
2742 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002743 if (demo->pause) {
2744 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002745 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002746 wl_display_dispatch_pending(demo->display); // don't block
2747 demo_draw(demo);
2748 demo->curFrame++;
2749 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2750 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002751 }
2752}
2753
Dave Houlton5fa47912018-02-16 11:02:26 -07002754static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002755 wl_shell_surface_pong(shell_surface, serial);
2756}
2757
Dave Houlton5fa47912018-02-16 11:02:26 -07002758static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED,
2759 int32_t width UNUSED, int32_t height UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002760
Dave Houlton5fa47912018-02-16 11:02:26 -07002761static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002762
Dave Houlton5fa47912018-02-16 11:02:26 -07002763static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002764
2765static void demo_create_window(struct demo *demo) {
2766 demo->window = wl_compositor_create_surface(demo->compositor);
2767 if (!demo->window) {
2768 printf("Can not create wayland_surface from compositor!\n");
2769 fflush(stdout);
2770 exit(1);
2771 }
2772
2773 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2774 if (!demo->shell_surface) {
2775 printf("Can not get shell_surface from wayland_surface!\n");
2776 fflush(stdout);
2777 exit(1);
2778 }
Dave Houlton5fa47912018-02-16 11:02:26 -07002779 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002780 wl_shell_surface_set_toplevel(demo->shell_surface);
2781 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2782}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002783#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2784static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002785 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002786
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002787 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002788 demo->curFrame++;
2789}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002790#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002791#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2792static VkResult demo_create_display_surface(struct demo *demo) {
2793 VkResult U_ASSERT_ONLY err;
2794 uint32_t display_count;
2795 uint32_t mode_count;
2796 uint32_t plane_count;
2797 VkDisplayPropertiesKHR display_props;
2798 VkDisplayKHR display;
2799 VkDisplayModePropertiesKHR mode_props;
2800 VkDisplayPlanePropertiesKHR *plane_props;
2801 VkBool32 found_plane = VK_FALSE;
2802 uint32_t plane_index;
2803 VkExtent2D image_extent;
2804 VkDisplaySurfaceCreateInfoKHR create_info;
2805
2806 // Get the first display
2807 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2808 assert(!err);
2809
2810 if (display_count == 0) {
2811 printf("Cannot find any display!\n");
2812 fflush(stdout);
2813 exit(1);
2814 }
2815
2816 display_count = 1;
2817 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2818 assert(!err || (err == VK_INCOMPLETE));
2819
2820 display = display_props.display;
2821
2822 // Get the first mode of the display
2823 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2824 assert(!err);
2825
2826 if (mode_count == 0) {
2827 printf("Cannot find any mode for the display!\n");
2828 fflush(stdout);
2829 exit(1);
2830 }
2831
2832 mode_count = 1;
2833 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2834 assert(!err || (err == VK_INCOMPLETE));
2835
2836 // Get the list of planes
2837 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2838 assert(!err);
2839
2840 if (plane_count == 0) {
2841 printf("Cannot find any plane!\n");
2842 fflush(stdout);
2843 exit(1);
2844 }
2845
2846 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2847 assert(plane_props);
2848
2849 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2850 assert(!err);
2851
2852 // Find a plane compatible with the display
2853 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2854 uint32_t supported_count;
2855 VkDisplayKHR *supported_displays;
2856
2857 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002858 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002859 continue;
2860 }
2861
2862 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2863 assert(!err);
2864
2865 if (supported_count == 0) {
2866 continue;
2867 }
2868
2869 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2870 assert(supported_displays);
2871
2872 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2873 assert(!err);
2874
2875 for (uint32_t i = 0; i < supported_count; i++) {
2876 if (supported_displays[i] == display) {
2877 found_plane = VK_TRUE;
2878 break;
2879 }
2880 }
2881
2882 free(supported_displays);
2883
2884 if (found_plane) {
2885 break;
2886 }
2887 }
2888
2889 if (!found_plane) {
2890 printf("Cannot find a plane compatible with the display!\n");
2891 fflush(stdout);
2892 exit(1);
2893 }
2894
2895 free(plane_props);
2896
Tony Barbour820b55b2017-03-14 08:55:46 -06002897 VkDisplayPlaneCapabilitiesKHR planeCaps;
2898 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2899 // Find a supported alpha mode
Mark Young66510e52017-11-10 10:05:14 -07002900 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2901 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002902 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2903 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2904 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2905 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2906 };
2907 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2908 if (planeCaps.supportedAlpha & alphaModes[i]) {
2909 alphaMode = alphaModes[i];
2910 break;
2911 }
2912 }
Damien Leone600c3052017-01-31 10:26:07 -07002913 image_extent.width = mode_props.parameters.visibleRegion.width;
2914 image_extent.height = mode_props.parameters.visibleRegion.height;
2915
2916 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2917 create_info.pNext = NULL;
2918 create_info.flags = 0;
2919 create_info.displayMode = mode_props.displayMode;
2920 create_info.planeIndex = plane_index;
2921 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2922 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002923 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002924 create_info.globalAlpha = 1.0f;
2925 create_info.imageExtent = image_extent;
2926
2927 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2928}
2929
Dave Houlton5fa47912018-02-16 11:02:26 -07002930static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07002931 while (!demo->quit) {
2932 demo_draw(demo);
2933 demo->curFrame++;
2934
2935 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2936 demo->quit = true;
2937 }
2938 }
2939}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002940#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002941
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002942/*
2943 * Return 1 (true) if all layer names specified in check_names
2944 * can be found in given layer properties.
2945 */
Dave Houlton5fa47912018-02-16 11:02:26 -07002946static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002947 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002948 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002949 for (uint32_t j = 0; j < layer_count; j++) {
2950 if (!strcmp(check_names[i], layers[j].layerName)) {
2951 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002952 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002953 }
2954 }
2955 if (!found) {
2956 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2957 return 0;
2958 }
2959 }
2960 return 1;
2961}
2962
Karl Schultze4dc75c2016-02-02 15:37:51 -07002963static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002964 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002965 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002966 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002967 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002968 char **instance_validation_layers = NULL;
2969 demo->enabled_extension_count = 0;
2970 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002971 demo->is_minimized = false;
2972 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002973
Dave Houlton5fa47912018-02-16 11:02:26 -07002974 char *instance_validation_layers_alt1[] = {"VK_LAYER_LUNARG_standard_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06002975
Dave Houlton5fa47912018-02-16 11:02:26 -07002976 char *instance_validation_layers_alt2[] = {"VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2977 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2978 "VK_LAYER_GOOGLE_unique_objects"};
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002979
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002980 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002981 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002982 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002983 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002984 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002985
Karl Schultz7c50ad62016-04-01 12:07:03 -06002986 instance_validation_layers = instance_validation_layers_alt1;
2987 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002988 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2989 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002990 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002991
Dave Houlton5fa47912018-02-16 11:02:26 -07002992 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt1), instance_validation_layers,
2993 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002994 if (validation_found) {
2995 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002996 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2997 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002998 } else {
2999 // use alternative set of validation layers
3000 instance_validation_layers = instance_validation_layers_alt2;
3001 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Dave Houlton5fa47912018-02-16 11:02:26 -07003002 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt2), instance_validation_layers,
3003 instance_layer_count, instance_layers);
3004 validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003005 for (uint32_t i = 0; i < validation_layer_count; i++) {
3006 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06003007 }
3008 }
3009 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003010 }
Dustin Graves7c287352016-01-27 13:48:06 -07003011
Karl Schultz7c50ad62016-04-01 12:07:03 -06003012 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003013 ERR_EXIT(
3014 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
3015 "Please look at the Getting Started guide for additional information.\n",
3016 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06003017 }
Dustin Graves7c287352016-01-27 13:48:06 -07003018 }
3019
3020 /* Look for instance extensions */
3021 VkBool32 surfaceExtFound = 0;
3022 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003023 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003024
Dave Houlton5fa47912018-02-16 11:02:26 -07003025 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003026 assert(!err);
3027
Dustin Graves7c287352016-01-27 13:48:06 -07003028 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003029 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3030 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003031 assert(!err);
3032 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003033 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003034 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003035 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003036 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003037#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003038 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003039 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003040 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003041 }
Tony Barbour153cb062016-12-07 13:43:36 -07003042#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003043 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06003044 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003045 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06003046 }
Tony Barbour153cb062016-12-07 13:43:36 -07003047#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003048 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003049 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003050 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003051 }
Tony Barbour153cb062016-12-07 13:43:36 -07003052#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003053 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003054 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003055 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003056 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003057#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003058#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003059 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07003060 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003061 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07003062 }
Tony Barbour153cb062016-12-07 13:43:36 -07003063#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003064 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003065 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003066 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003067 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003068#elif defined(VK_USE_PLATFORM_IOS_MVK)
3069 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3070 platformSurfaceExtFound = 1;
3071 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3072 }
3073#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3074 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3075 platformSurfaceExtFound = 1;
3076 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3077 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003078#endif
Dave Houlton5fa47912018-02-16 11:02:26 -07003079 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003080 if (demo->validate) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003081 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003082 }
3083 }
Mark Young66510e52017-11-10 10:05:14 -07003084 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3085 if (demo->validate) {
3086 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3087 }
3088 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003089 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003090 }
Dustin Graves7c287352016-01-27 13:48:06 -07003091
3092 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003093 }
Dustin Graves7c287352016-01-27 13:48:06 -07003094
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003095 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003096 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3097 " extension.\n\n"
3098 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3099 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06003100 "vkCreateInstance Failure");
3101 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003102 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003103#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003104 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3105 " extension.\n\n"
3106 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3107 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003108 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003109#elif defined(VK_USE_PLATFORM_IOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003110 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_IOS_SURFACE_EXTENSION_NAME
3111 " extension.\n\n"
3112 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3113 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003114 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003115#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003116 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_MACOS_SURFACE_EXTENSION_NAME
3117 " extension.\n\n"
3118 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3119 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06003120 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003121#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003122 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3123 " extension.\n\n"
3124 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3125 "Please look at the Getting Started guide for additional information.\n",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003126 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003127#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003128 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3129 " extension.\n\n"
3130 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3131 "Please look at the Getting Started guide for additional information.\n",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003132 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003133#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003134#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003135 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3136 " extension.\n\n"
3137 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3138 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07003139 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003140#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003141 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3142 " extension.\n\n"
3143 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3144 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003145 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003146#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07003147 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3148 " extension.\n\n"
3149 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3150 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06003151 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003152#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003153 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003154 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003155 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003156 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003157 .pApplicationName = APP_SHORT_NAME,
3158 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003159 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003160 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003161 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003162 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003163 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003164 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003165 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003166 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003167 .enabledLayerCount = demo->enabled_layer_count,
3168 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3169 .enabledExtensionCount = demo->enabled_extension_count,
3170 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003171 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003172
3173 /*
3174 * This is info for a temp callback to use during CreateInstance.
3175 * After the instance is created, we use the instance-based
3176 * function to register the final callback.
3177 */
Mark Young66510e52017-11-10 10:05:14 -07003178 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003179 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003180 // VK_EXT_debug_utils style
3181 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3182 dbg_messenger_create_info.pNext = NULL;
3183 dbg_messenger_create_info.flags = 0;
3184 dbg_messenger_create_info.messageSeverity =
3185 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3186 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3187 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3188 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3189 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3190 dbg_messenger_create_info.pUserData = demo;
3191 inst_info.pNext = &dbg_messenger_create_info;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003192 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003193
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003194 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003195
Chia-I Wu63636062015-10-26 21:10:41 +08003196 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003197 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003198 ERR_EXIT(
3199 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3200 "Please look at the Getting Started guide for additional information.\n",
3201 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003202 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003203 ERR_EXIT(
3204 "Cannot find a specified extension library.\n"
3205 "Make sure your layers path is set appropriately.\n",
3206 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003207 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003208 ERR_EXIT(
3209 "vkCreateInstance failed.\n\n"
3210 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3211 "Please look at the Getting Started guide for additional information.\n",
3212 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003213 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003214
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003215 /* Make initial call to query gpu_count, then second call for gpu info*/
3216 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3217 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003218
3219 if (gpu_count > 0) {
3220 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3221 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3222 assert(!err);
3223 /* For cube demo we just grab the first physical device */
3224 demo->gpu = physical_devices[0];
3225 free(physical_devices);
3226 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003227 ERR_EXIT(
3228 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3229 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3230 "Please look at the Getting Started guide for additional information.\n",
3231 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003232 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003233
Dustin Graves7c287352016-01-27 13:48:06 -07003234 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003235 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003236 VkBool32 swapchainExtFound = 0;
3237 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003238 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003239
Dave Houlton5fa47912018-02-16 11:02:26 -07003240 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003241 assert(!err);
3242
Dustin Graves7c287352016-01-27 13:48:06 -07003243 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003244 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3245 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003246 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003247
Dustin Graves7c287352016-01-27 13:48:06 -07003248 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003249 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003250 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003251 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003252 }
3253 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003254 }
Dustin Graves7c287352016-01-27 13:48:06 -07003255
Ian Elliott53622a72017-03-28 11:06:33 -06003256 if (demo->VK_KHR_incremental_present_enabled) {
3257 // Even though the user "enabled" the extension via the command
3258 // line, we must make sure that it's enumerated for use with the
3259 // device. Therefore, disable it here, and re-enable it again if
3260 // enumerated.
3261 demo->VK_KHR_incremental_present_enabled = false;
3262 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003263 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3264 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003265 demo->VK_KHR_incremental_present_enabled = true;
3266 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3267 }
3268 assert(demo->enabled_extension_count < 64);
3269 }
3270 if (!demo->VK_KHR_incremental_present_enabled) {
3271 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3272 }
3273 }
3274
Ian Elliottd940ca22017-03-22 08:31:27 -06003275 if (demo->VK_GOOGLE_display_timing_enabled) {
3276 // Even though the user "enabled" the extension via the command
3277 // line, we must make sure that it's enumerated for use with the
3278 // device. Therefore, disable it here, and re-enable it again if
3279 // enumerated.
3280 demo->VK_GOOGLE_display_timing_enabled = false;
3281 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003282 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3283 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003284 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003285 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003286 }
3287 assert(demo->enabled_extension_count < 64);
3288 }
3289 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003290 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003291 }
3292 }
3293
Dustin Graves7c287352016-01-27 13:48:06 -07003294 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003295 }
Dustin Graves7c287352016-01-27 13:48:06 -07003296
Tony Barbourcc69f452015-10-08 13:59:42 -06003297 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003298 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3299 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3300 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003301 "vkCreateInstance Failure");
3302 }
3303
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003304 if (demo->validate) {
Mark Young66510e52017-11-10 10:05:14 -07003305 // Setup VK_EXT_debug_utils function pointers always (we use them for
3306 // debug labels and names).
3307 demo->CreateDebugUtilsMessengerEXT =
3308 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3309 demo->DestroyDebugUtilsMessengerEXT =
3310 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3311 demo->SubmitDebugUtilsMessageEXT =
3312 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3313 demo->CmdBeginDebugUtilsLabelEXT =
3314 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3315 demo->CmdEndDebugUtilsLabelEXT =
3316 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3317 demo->CmdInsertDebugUtilsLabelEXT =
3318 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3319 demo->SetDebugUtilsObjectNameEXT =
3320 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3321 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3322 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3323 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3324 NULL == demo->SetDebugUtilsObjectNameEXT) {
3325 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003326 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003327
Mark Young66510e52017-11-10 10:05:14 -07003328 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
3329 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3330 dbg_messenger_create_info.pNext = NULL;
3331 dbg_messenger_create_info.flags = 0;
3332 dbg_messenger_create_info.messageSeverity =
3333 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3334 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3335 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3336 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3337 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3338 dbg_messenger_create_info.pUserData = demo;
3339 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003340 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003341 case VK_SUCCESS:
3342 break;
3343 case VK_ERROR_OUT_OF_HOST_MEMORY:
Mark Young66510e52017-11-10 10:05:14 -07003344 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003345 break;
3346 default:
Mark Young66510e52017-11-10 10:05:14 -07003347 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
Dave Houlton5fa47912018-02-16 11:02:26 -07003348 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003349 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003350 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003351 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003352
3353 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003354 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003355 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003356
Dave Houlton5fa47912018-02-16 11:02:26 -07003357 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3358 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003359
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003360 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003361 // If app has specific feature requirements it should check supported
3362 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003363 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003364 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003365
Tony Barbourda2bad52016-01-22 14:36:40 -07003366 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3367 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3368 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3369 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3370 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3371}
3372
Karl Schultze4dc75c2016-02-02 15:37:51 -07003373static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003374 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003375 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003376 VkDeviceQueueCreateInfo queues[2];
3377 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3378 queues[0].pNext = NULL;
3379 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3380 queues[0].queueCount = 1;
3381 queues[0].pQueuePriorities = queue_priorities;
3382 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003383
3384 VkDeviceCreateInfo device = {
3385 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3386 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003387 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003388 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003389 .enabledLayerCount = 0,
3390 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003391 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003392 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003393 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003394 };
Tony Barbour22e06272016-09-07 16:07:56 -06003395 if (demo->separate_present_queue) {
3396 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3397 queues[1].pNext = NULL;
3398 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3399 queues[1].queueCount = 1;
3400 queues[1].pQueuePriorities = queue_priorities;
3401 queues[1].flags = 0;
3402 device.queueCreateInfoCount = 2;
3403 }
Chia-I Wu63636062015-10-26 21:10:41 +08003404 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003405 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003406}
3407
Karl Schultze4dc75c2016-02-02 15:37:51 -07003408static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003409 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003410
Karl Schultze4dc75c2016-02-02 15:37:51 -07003411// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003412#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003413 VkWin32SurfaceCreateInfoKHR createInfo;
3414 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3415 createInfo.pNext = NULL;
3416 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003417 createInfo.hinstance = demo->connection;
3418 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003419
Dave Houlton5fa47912018-02-16 11:02:26 -07003420 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003421#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003422 VkWaylandSurfaceCreateInfoKHR createInfo;
3423 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3424 createInfo.pNext = NULL;
3425 createInfo.flags = 0;
3426 createInfo.display = demo->display;
3427 createInfo.surface = demo->window;
3428
Dave Houlton5fa47912018-02-16 11:02:26 -07003429 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003430#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003431#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3432 VkAndroidSurfaceCreateInfoKHR createInfo;
3433 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3434 createInfo.pNext = NULL;
3435 createInfo.flags = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003436 createInfo.window = (ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003437
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003438 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003439#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3440 VkXlibSurfaceCreateInfoKHR createInfo;
3441 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3442 createInfo.pNext = NULL;
3443 createInfo.flags = 0;
3444 createInfo.dpy = demo->display;
3445 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003446
Dave Houlton5fa47912018-02-16 11:02:26 -07003447 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003448#elif defined(VK_USE_PLATFORM_XCB_KHR)
3449 VkXcbSurfaceCreateInfoKHR createInfo;
3450 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3451 createInfo.pNext = NULL;
3452 createInfo.flags = 0;
3453 createInfo.connection = demo->connection;
3454 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003455
Tony Barbour153cb062016-12-07 13:43:36 -07003456 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003457#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3458 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003459#elif defined(VK_USE_PLATFORM_IOS_MVK)
3460 VkIOSSurfaceCreateInfoMVK surface;
3461 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3462 surface.pNext = NULL;
3463 surface.flags = 0;
3464 surface.pView = demo->window;
3465
3466 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3467#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3468 VkMacOSSurfaceCreateInfoMVK surface;
3469 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3470 surface.pNext = NULL;
3471 surface.flags = 0;
3472 surface.pView = demo->window;
3473
3474 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003475#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003476 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003477
Tony Barbourcc69f452015-10-08 13:59:42 -06003478 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003479 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003480 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003481 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003482 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003483
3484 // Search for a graphics and a present queue in the array of queue
3485 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003486 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3487 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003488 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003489 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3490 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3491 graphicsQueueFamilyIndex = i;
3492 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003493
Tony Barbourab2a0362016-09-06 11:40:12 -06003494 if (supportsPresent[i] == VK_TRUE) {
3495 graphicsQueueFamilyIndex = i;
3496 presentQueueFamilyIndex = i;
3497 break;
3498 }
3499 }
3500 }
3501
3502 if (presentQueueFamilyIndex == UINT32_MAX) {
3503 // If didn't find a queue that supports both graphics and present, then
3504 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003505 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003506 if (supportsPresent[i] == VK_TRUE) {
3507 presentQueueFamilyIndex = i;
3508 break;
3509 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003510 }
3511 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003512
3513 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003514 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3515 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003516 }
3517
Tony Barbourab2a0362016-09-06 11:40:12 -06003518 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3519 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003520 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003521 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003522
Tony Barbourda2bad52016-01-22 14:36:40 -07003523 demo_create_device(demo);
3524
3525 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3526 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3527 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3528 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3529 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003530 if (demo->VK_GOOGLE_display_timing_enabled) {
3531 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3532 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3533 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003534
Dave Houlton5fa47912018-02-16 11:02:26 -07003535 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003536
Tony Barbour22e06272016-09-07 16:07:56 -06003537 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003538 demo->present_queue = demo->graphics_queue;
3539 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003540 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003541 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003542
Ian Elliottaaae5352015-07-06 14:27:58 -06003543 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003544 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003545 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003546 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003547 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3548 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003549 assert(!err);
3550 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3551 // the surface has no preferred format. Otherwise, at least one
3552 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003553 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003554 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003555 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003556 assert(formatCount >= 1);
3557 demo->format = surfFormats[0].format;
3558 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003559 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003560
David Pinedo2bb7c932015-06-18 17:03:14 -06003561 demo->quit = false;
3562 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003563
Tony Barbource7524e2016-07-28 12:01:45 -06003564 // Create semaphores to synchronize acquiring presentable buffers before
3565 // rendering and waiting for drawing to be complete before presenting
3566 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3567 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3568 .pNext = NULL,
3569 .flags = 0,
3570 };
Tony Barbource7524e2016-07-28 12:01:45 -06003571
Tony Barbour66a56c32016-09-21 13:10:56 -06003572 // Create fences that we can use to throttle if we get too far
3573 // ahead of the image presents
3574 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003575 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003576 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003577 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3578 assert(!err);
3579
Dave Houlton5fa47912018-02-16 11:02:26 -07003580 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003581 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003582
Dave Houlton5fa47912018-02-16 11:02:26 -07003583 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003584 assert(!err);
3585
3586 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003587 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003588 assert(!err);
3589 }
Tony Barbour22e06272016-09-07 16:07:56 -06003590 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003591 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003592
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003593 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003594 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003595}
3596
Tony Barbour153cb062016-12-07 13:43:36 -07003597#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003598static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3599 wl_fixed_t sy) {}
3600
3601static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3602
3603static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3604
3605static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3606 uint32_t state) {
3607 struct demo *demo = data;
3608 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3609 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
3610 }
3611}
3612
3613static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3614
3615static const struct wl_pointer_listener pointer_listener = {
3616 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3617};
3618
3619static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3620
3621static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3622 struct wl_array *keys) {}
3623
3624static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3625
3626static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3627 uint32_t state) {
3628 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3629 struct demo *demo = data;
3630 switch (key) {
3631 case KEY_ESC: // Escape
3632 demo->quit = true;
3633 break;
3634 case KEY_LEFT: // left arrow key
3635 demo->spin_angle -= demo->spin_increment;
3636 break;
3637 case KEY_RIGHT: // right arrow key
3638 demo->spin_angle += demo->spin_increment;
3639 break;
3640 case KEY_SPACE: // space bar
3641 demo->pause = !demo->pause;
3642 break;
3643 }
3644}
3645
3646static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3647 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3648
3649static const struct wl_keyboard_listener keyboard_listener = {
3650 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3651};
3652
3653static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3654 // Subscribe to pointer events
3655 struct demo *demo = data;
3656 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3657 demo->pointer = wl_seat_get_pointer(seat);
3658 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3659 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3660 wl_pointer_destroy(demo->pointer);
3661 demo->pointer = NULL;
3662 }
3663 // Subscribe to keyboard events
3664 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3665 demo->keyboard = wl_seat_get_keyboard(seat);
3666 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3667 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3668 wl_keyboard_destroy(demo->keyboard);
3669 demo->keyboard = NULL;
3670 }
3671}
3672
3673static const struct wl_seat_listener seat_listener = {
3674 seat_handle_capabilities,
3675};
3676
3677static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003678 uint32_t version UNUSED) {
3679 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003680 // pickup wayland objects when they appear
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003681 if (strcmp(interface, "wl_compositor") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003682 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003683 } else if (strcmp(interface, "wl_shell") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003684 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3685 } else if (strcmp(interface, "wl_seat") == 0) {
3686 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3687 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003688 }
3689}
3690
Dave Houlton5fa47912018-02-16 11:02:26 -07003691static 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 +09003692
Dave Houlton5fa47912018-02-16 11:02:26 -07003693static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003694#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003695#endif
3696
Karl Schultze4dc75c2016-02-02 15:37:51 -07003697static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003698#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003699 const xcb_setup_t *setup;
3700 xcb_screen_iterator_t iter;
3701 int scr;
3702
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003703 const char *display_envar = getenv("DISPLAY");
3704 if (display_envar == NULL || display_envar[0] == '\0') {
3705 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3706 fflush(stdout);
3707 exit(1);
3708 }
3709
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003710 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003711 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003712 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003713 fflush(stdout);
3714 exit(1);
3715 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003716
3717 setup = xcb_get_setup(demo->connection);
3718 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003719 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003720
3721 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003722#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3723 demo->display = wl_display_connect(NULL);
3724
3725 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003726 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003727 fflush(stdout);
3728 exit(1);
3729 }
3730
3731 demo->registry = wl_display_get_registry(demo->display);
3732 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3733 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003734#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003735#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003736}
3737
Karl Schultze4dc75c2016-02-02 15:37:51 -07003738static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003739 vec3 eye = {0.0f, 3.0f, 5.0f};
3740 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003741 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003742
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003743 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003744 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003745 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003746
Piers Daniell735ee532015-02-23 16:23:13 -07003747 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003748 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003749 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003750 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003751 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003752 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3753 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003754 i++;
3755 continue;
3756 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003757 if (strcmp(argv[i], "--break") == 0) {
3758 demo->use_break = true;
3759 continue;
3760 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003761 if (strcmp(argv[i], "--validate") == 0) {
3762 demo->validate = true;
3763 continue;
3764 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003765 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3766 demo->validate = true;
3767 demo->validate_checks_disabled = true;
3768 continue;
3769 }
Tony Barbour2593b182016-04-19 10:57:58 -06003770 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003771 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003772 continue;
3773 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003774 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3775 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003776 i++;
3777 continue;
3778 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003779 if (strcmp(argv[i], "--suppress_popups") == 0) {
3780 demo->suppress_popups = true;
3781 continue;
3782 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003783 if (strcmp(argv[i], "--display_timing") == 0) {
3784 demo->VK_GOOGLE_display_timing_enabled = true;
3785 continue;
3786 }
Ian Elliott53622a72017-03-28 11:06:33 -06003787 if (strcmp(argv[i], "--incremental_present") == 0) {
3788 demo->VK_KHR_incremental_present_enabled = true;
3789 continue;
3790 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003791
Cody Northropaf28a532016-09-27 10:50:57 -06003792#if defined(ANDROID)
3793 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3794#else
Mike Schuchardt9a881512017-11-01 16:16:22 -06003795 fprintf(stderr,
Mark Young66510e52017-11-10 10:05:14 -07003796 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled] [--break]\n"
3797 "\t[--c <framecount>] [--suppress_popups] [--incremental_present] [--display_timing]\n"
3798 "\t[--present_mode <present mode enum>]\n"
3799 "\t <present_mode_enum>\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3800 "\t\t\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3801 "\t\t\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
3802 "\t\t\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
Mike Schuchardt9a881512017-11-01 16:16:22 -06003803 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
3804 VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003805 fflush(stderr);
3806 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003807#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003808 }
3809
Tony Barbour153cb062016-12-07 13:43:36 -07003810 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003811
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003812 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003813
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003814 demo->width = 500;
3815 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003816
Tony Barbour66a56c32016-09-21 13:10:56 -06003817 demo->spin_angle = 4.0f;
3818 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003819 demo->pause = false;
3820
Dave Houlton5fa47912018-02-16 11:02:26 -07003821 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003822 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3823 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003824
Dave Houlton5fa47912018-02-16 11:02:26 -07003825 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003826}
3827
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003828#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003829// Include header required for parsing the command line options.
3830#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003831
Dave Houlton5fa47912018-02-16 11:02:26 -07003832int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3833 MSG msg; // message
3834 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003835 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003836 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003837
Jamie Madillb2ff6502017-03-15 16:17:46 -04003838 // Ensure wParam is initialized.
3839 msg.wParam = 0;
3840
Karl Schultze4dc75c2016-02-02 15:37:51 -07003841 // Use the CommandLine functions to get the command line arguments.
3842 // Unfortunately, Microsoft outputs
3843 // this information as wide characters for Unicode, and we simply want the
3844 // Ascii version to be compatible
3845 // with the non-Windows side. So, we have to convert the information to
3846 // Ascii character strings.
3847 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3848 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003849 argc = 0;
3850 }
3851
Karl Schultze4dc75c2016-02-02 15:37:51 -07003852 if (argc > 0) {
3853 argv = (char **)malloc(sizeof(char *) * argc);
3854 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003855 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003856 } else {
3857 for (int iii = 0; iii < argc; iii++) {
3858 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003859 size_t numConverted = 0;
3860
Karl Schultze4dc75c2016-02-02 15:37:51 -07003861 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3862 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003863 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003864 }
3865 }
3866 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003867 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003868 argv = NULL;
3869 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003870
3871 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003872
3873 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003874 if (argc > 0 && argv != NULL) {
3875 for (int iii = 0; iii < argc; iii++) {
3876 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003877 free(argv[iii]);
3878 }
3879 }
3880 free(argv);
3881 }
3882
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003883 demo.connection = hInstance;
3884 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003885 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003886 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003887
3888 demo_prepare(&demo);
3889
Dave Houlton5fa47912018-02-16 11:02:26 -07003890 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003891
3892 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003893 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003894 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003895 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003896 {
Dave Houlton5fa47912018-02-16 11:02:26 -07003897 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07003898 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003899 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003900 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003901 DispatchMessage(&msg);
3902 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003903 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003904 }
3905
3906 demo_cleanup(&demo);
3907
Karl Schultze4dc75c2016-02-02 15:37:51 -07003908 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003909}
Bill Hollingsf4352142017-02-14 22:58:56 -05003910
3911#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003912static void demo_main(struct demo *demo, void *view) {
3913 const char *argv[] = {"CubeSample"};
3914 int argc = sizeof(argv) / sizeof(char *);
Bill Hollingsf4352142017-02-14 22:58:56 -05003915
Dave Houlton5fa47912018-02-16 11:02:26 -07003916 demo_init(demo, argc, (char **)argv);
Tony Barbourc65cd752017-03-13 15:29:35 -06003917 demo->window = view;
3918 demo_init_vk_swapchain(demo);
3919 demo_prepare(demo);
3920 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003921}
3922
3923static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003924 // Wait for work to finish before updating MVP.
3925 vkDeviceWaitIdle(demo->device);
3926 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003927
Tony Barbourc65cd752017-03-13 15:29:35 -06003928 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003929}
3930
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003931#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3932#include <android/log.h>
3933#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003934#include "android_util.h"
3935
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003936static bool initialized = false;
3937static bool active = false;
3938struct demo demo;
3939
Dave Houlton5fa47912018-02-16 11:02:26 -07003940static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003941
Dave Houlton5fa47912018-02-16 11:02:26 -07003942static void processCommand(struct android_app *app, int32_t cmd) {
3943 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003944 case APP_CMD_INIT_WINDOW: {
3945 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003946 // We're getting a new window. If the app is starting up, we
3947 // need to initialize. If the app has already been
3948 // initialized, that means that we lost our previous window,
3949 // which means that we have a lot of work to do. At a minimum,
3950 // we need to destroy the swapchain and surface associated with
3951 // the old window, and create a new surface and swapchain.
3952 // However, since there are a lot of other objects/state that
3953 // is tied to the swapchain, it's easiest to simply cleanup and
3954 // start over (i.e. use a brute-force approach of re-starting
3955 // the app)
3956 if (demo.prepared) {
3957 demo_cleanup(&demo);
3958 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003959
3960 // Parse Intents into argc, argv
3961 // Use the following key to send arguments, i.e.
3962 // --es args "--validate"
3963 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07003964 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003965 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003966 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003967
3968 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07003969 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 -06003970
3971 demo_init(&demo, argc, argv);
3972
3973 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07003974 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003975
Dave Houlton5fa47912018-02-16 11:02:26 -07003976 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003977 demo_init_vk_swapchain(&demo);
3978 demo_prepare(&demo);
3979 initialized = true;
3980 }
3981 break;
3982 }
3983 case APP_CMD_GAINED_FOCUS: {
3984 active = true;
3985 break;
3986 }
3987 case APP_CMD_LOST_FOCUS: {
3988 active = false;
3989 break;
3990 }
3991 }
3992}
3993
Dave Houlton5fa47912018-02-16 11:02:26 -07003994void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06003995#ifdef ANDROID
3996 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07003997 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06003998#endif
3999
Ian Elliottf05d5d12016-05-17 12:03:35 -06004000 demo.prepared = false;
4001
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004002 app->onAppCmd = processCommand;
4003 app->onInputEvent = processInput;
4004
Dave Houlton5fa47912018-02-16 11:02:26 -07004005 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004006 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07004007 struct android_poll_source *source;
4008 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004009 if (source) {
4010 source->process(app, source);
4011 }
4012
4013 if (app->destroyRequested != 0) {
4014 demo_cleanup(&demo);
4015 return;
4016 }
4017 }
4018 if (initialized && active) {
4019 demo_run(&demo);
4020 }
4021 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004022}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004023#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004024int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004025 struct demo demo;
4026
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004027 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004028#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004029 demo_create_xcb_window(&demo);
4030#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4031 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004032#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4033 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004034#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004035#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004036
Tony Barbourcc69f452015-10-08 13:59:42 -06004037 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004038
4039 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004040
Tony Barbour153cb062016-12-07 13:43:36 -07004041#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004042 demo_run_xcb(&demo);
4043#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4044 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004045#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4046 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004047#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07004048#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4049 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004050#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004051
4052 demo_cleanup(&demo);
4053
Karl Schultz0218c002016-03-22 17:06:13 -06004054 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004055}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004056#endif