blob: 68b8ded6e284aeed57345c75ef244107063fb9cd [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 Lobodzinski2dbc2662017-01-26 12:16:30 -0700293VKAPI_ATTR VkBool32 VKAPI_CALL BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
294 size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg,
295 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700296#ifndef WIN32
297 raise(SIGTRAP);
298#else
299 DebugBreak();
300#endif
301
302 return false;
303}
304
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600305typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600306 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800307 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600308 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600309 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700310 VkBuffer uniform_buffer;
311 VkDeviceMemory uniform_memory;
312 VkFramebuffer framebuffer;
313 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700314} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600315
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600316struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500317#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600318#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700319 HINSTANCE connection; // hInstance - Windows Instance
320 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
321 HWND window; // hWnd - window handle
322 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700323#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700324 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600325 Window xlib_window;
326 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700327#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700328 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329 xcb_connection_t *connection;
330 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600331 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800332 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900333#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
334 struct wl_display *display;
335 struct wl_registry *registry;
336 struct wl_compositor *compositor;
337 struct wl_surface *window;
338 struct wl_shell *shell;
339 struct wl_shell_surface *shell_surface;
Joey Bzdek15eb0702017-06-07 09:40:36 -0600340 struct wl_seat *seat;
341 struct wl_pointer *pointer;
342 struct wl_keyboard *keyboard;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700343#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500344#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700345 ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500346#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
347 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500348#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700349 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600350 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700351 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600352 bool separate_present_queue;
Jeremy Kniager602e4182018-01-19 10:52:34 -0700353 bool is_minimized;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
Ian Elliott53622a72017-03-28 11:06:33 -0600355 bool VK_KHR_incremental_present_enabled;
356
Ian Elliottd940ca22017-03-22 08:31:27 -0600357 bool VK_GOOGLE_display_timing_enabled;
358 bool syncd_with_actual_presents;
359 uint64_t refresh_duration;
360 uint64_t refresh_duration_multiplier;
361 uint64_t target_IPD; // image present duration (inverse of frame rate)
362 uint64_t prev_desired_present_time;
363 uint32_t next_present_id;
364 uint32_t last_early_id; // 0 if no early images
365 uint32_t last_late_id; // 0 if no late images
366
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600368 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600369 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600370 VkQueue graphics_queue;
371 VkQueue present_queue;
372 uint32_t graphics_queue_family_index;
373 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600374 VkSemaphore image_acquired_semaphores[FRAME_LAG];
375 VkSemaphore draw_complete_semaphores[FRAME_LAG];
376 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600377 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600378 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600379 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380
Tony Barbourda2bad52016-01-22 14:36:40 -0700381 uint32_t enabled_extension_count;
382 uint32_t enabled_layer_count;
383 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600384 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700385
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600386 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600387 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600388 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600389
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700390 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
391 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
392 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
393 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600394 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
395 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
396 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
397 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
398 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600399 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
400 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600401 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600402 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700403 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600404 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600405 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600406 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600407
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800408 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600409 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600410
411 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600412 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600413
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600414 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800415 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500416 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600417 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600418 } depth;
419
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600420 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600421 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600422
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700423 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500424 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600425 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600426 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800427 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600428 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600429
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600430 mat4x4 projection_matrix;
431 mat4x4 view_matrix;
432 mat4x4 model_matrix;
433
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600434 float spin_angle;
435 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600436 bool pause;
437
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600438 VkShaderModule vert_shader_module;
439 VkShaderModule frag_shader_module;
440
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600441 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800442
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600443 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600444 int32_t curFrame;
445 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600446 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600447 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600448 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600449 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700450 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
451 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
452 VkDebugReportCallbackEXT msg_callback;
453 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600454
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600455 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600456 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600457};
458
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700459VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
460 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600461 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600462 char *message = (char *)malloc(strlen(pMsg) + 100);
463
464 assert(message);
465
Cody Northropaf28a532016-09-27 10:50:57 -0600466 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
467 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600468 validation_error = 1;
469 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600470 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
471 validation_error = 1;
472 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600473 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600474 validation_error = 1;
475 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
476 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
477 validation_error = 1;
478 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
479 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600480 validation_error = 1;
481 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600482 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600483 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600484 }
485
486#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600487
Tony Barbour602ca4f2016-09-12 14:02:43 -0600488 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600489 struct demo *demo = (struct demo*) pUserData;
490 if (!demo->suppress_popups)
491 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600492 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600493
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600494#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600495
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600496 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600497 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600498 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600499 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600500 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600501 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600502 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600503 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600504 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600505 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600506 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600507 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600508 }
Cody Northropaf28a532016-09-27 10:50:57 -0600509
lenny-lunargfbe22392016-06-06 11:07:53 -0600510#else
Cody Northropaf28a532016-09-27 10:50:57 -0600511
lenny-lunargfbe22392016-06-06 11:07:53 -0600512 printf("%s\n", message);
513 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600514
lenny-lunargfbe22392016-06-06 11:07:53 -0600515#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600516
lenny-lunargfbe22392016-06-06 11:07:53 -0600517 free(message);
518
Joey Bzdek15eb0702017-06-07 09:40:36 -0600519 // clang-format on
Cody Northropaf28a532016-09-27 10:50:57 -0600520
lenny-lunargfbe22392016-06-06 11:07:53 -0600521 /*
Dave Houlton5fa47912018-02-16 11:02:26 -0700522 * false indicates that layer should not bail-out of an
523 * API call that had validation failures. This may mean that the
524 * app dies inside the driver due to invalid parameter(s).
525 * That's what would happen without validation layers, so we'll
526 * keep that behavior here.
527 */
lenny-lunargfbe22392016-06-06 11:07:53 -0600528 return false;
529}
530
Ian Elliottd940ca22017-03-22 08:31:27 -0600531bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
532 // The desired time was the earliest time that the present should have
533 // occured. In almost every case, the actual time should be later than the
534 // desired time. We should only consider the actual time "late" if it is
535 // after "desired + rdur".
536 if (actual <= desired) {
537 // The actual time was before or equal to the desired time. This will
538 // probably never happen, but in case it does, return false since the
539 // present was obviously NOT late.
540 return false;
541 }
542 uint64_t deadline = actual + rdur;
543 if (actual > deadline) {
544 return true;
545 } else {
546 return false;
547 }
548}
Dave Houlton5fa47912018-02-16 11:02:26 -0700549bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600550 if (earliest < actual) {
551 // Consider whether this present could have occured earlier. Make sure
552 // that earliest time was at least 2msec earlier than actual time, and
553 // that the margin was at least 2msec:
554 uint64_t diff = actual - earliest;
555 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
556 // This present could have occured earlier because both: 1) the
557 // earliest time was at least 2 msec before actual time, and 2) the
558 // margin was at least 2msec.
559 return true;
560 }
561 }
562 return false;
563}
564
Ian Elliottcf074d32015-10-16 18:02:43 -0600565// Forward declaration:
566static void demo_resize(struct demo *demo);
567
Dave Houlton5fa47912018-02-16 11:02:26 -0700568static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700569 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600570 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700571 if ((typeBits & 1) == 1) {
572 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700573 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700574 *typeIndex = i;
575 return true;
576 }
577 }
578 typeBits >>= 1;
579 }
580 // No memory types matched, return failure
581 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600582}
583
Karl Schultze4dc75c2016-02-02 15:37:51 -0700584static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600585 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600586
Tony Barbource7524e2016-07-28 12:01:45 -0600587 // This function could get called twice if the texture uses a staging buffer
588 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700589 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600590
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600591 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600592 assert(!err);
593
Tony Barbource7524e2016-07-28 12:01:45 -0600594 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700595 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700596 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
597 assert(!err);
598
Karl Schultze4dc75c2016-02-02 15:37:51 -0700599 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700600 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
601 .pNext = NULL,
602 .waitSemaphoreCount = 0,
603 .pWaitSemaphores = NULL,
604 .pWaitDstStageMask = NULL,
605 .commandBufferCount = 1,
606 .pCommandBuffers = cmd_bufs,
607 .signalSemaphoreCount = 0,
608 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600609
Tony Barbource7524e2016-07-28 12:01:45 -0600610 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600611 assert(!err);
612
Tony Barbource7524e2016-07-28 12:01:45 -0600613 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600614 assert(!err);
615
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600616 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600617 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600618 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600619}
620
Dave Houlton5fa47912018-02-16 11:02:26 -0700621static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
622 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600623 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600624 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600625
Dave Houlton5fa47912018-02-16 11:02:26 -0700626 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
627 .pNext = NULL,
628 .srcAccessMask = srcAccessMask,
629 .dstAccessMask = 0,
630 .oldLayout = old_image_layout,
631 .newLayout = new_image_layout,
632 .image = image,
633 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600634
Tony Barboureb5077b2016-10-19 15:23:36 -0600635 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700636 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
637 /* Make sure anything that was copying from this image has completed */
638 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
639 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600640
Dave Houlton5fa47912018-02-16 11:02:26 -0700641 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
642 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
643 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700644
Dave Houlton5fa47912018-02-16 11:02:26 -0700645 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
646 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
647 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700648
Dave Houlton5fa47912018-02-16 11:02:26 -0700649 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
650 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
651 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600652
Dave Houlton5fa47912018-02-16 11:02:26 -0700653 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
654 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
655 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600656
Dave Houlton5fa47912018-02-16 11:02:26 -0700657 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
658 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
659 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600660
Dave Houlton5fa47912018-02-16 11:02:26 -0700661 default:
662 image_memory_barrier.dstAccessMask = 0;
663 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600664 }
665
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600666 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600667
Dave Houlton5fa47912018-02-16 11:02:26 -0700668 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600669}
670
Karl Schultze4dc75c2016-02-02 15:37:51 -0700671static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700672 const VkCommandBufferBeginInfo cmd_buf_info = {
673 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
674 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600675 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700676 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700677 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800678 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700679 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
680 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800681 };
682 const VkRenderPassBeginInfo rp_begin = {
683 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
684 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800685 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700686 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800687 .renderArea.offset.x = 0,
688 .renderArea.offset.y = 0,
689 .renderArea.extent.width = demo->width,
690 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600691 .clearValueCount = 2,
692 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700693 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800694 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600695
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600696 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600697 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800698 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700699 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700700 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
701 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600702 VkViewport viewport;
703 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700704 viewport.height = (float)demo->height;
705 viewport.width = (float)demo->width;
706 viewport.minDepth = (float)0.0f;
707 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700708 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600709
710 VkRect2D scissor;
711 memset(&scissor, 0, sizeof(scissor));
712 scissor.extent.width = demo->width;
713 scissor.extent.height = demo->height;
714 scissor.offset.x = 0;
715 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700716 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600717 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600718 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600719 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800720 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600721
Tony Barbour22e06272016-09-07 16:07:56 -0600722 if (demo->separate_present_queue) {
723 // We have to transfer ownership from the graphics queue family to the
724 // present queue family to be able to present. Note that we don't have
725 // to transfer from present queue family back to graphics queue family at
726 // the start of the next frame because we don't care about the image's
727 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700728 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
729 .pNext = NULL,
730 .srcAccessMask = 0,
731 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
732 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
733 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
734 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
735 .dstQueueFamilyIndex = demo->present_queue_family_index,
736 .image = demo->swapchain_image_resources[demo->current_buffer].image,
737 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600738
Dave Houlton5fa47912018-02-16 11:02:26 -0700739 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0,
740 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600741 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600742 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743 assert(!err);
744}
745
Tony Barbour22e06272016-09-07 16:07:56 -0600746void demo_build_image_ownership_cmd(struct demo *demo, int i) {
747 VkResult U_ASSERT_ONLY err;
748
749 const VkCommandBufferBeginInfo cmd_buf_info = {
750 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
751 .pNext = NULL,
752 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
753 .pInheritanceInfo = NULL,
754 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700755 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600756 assert(!err);
757
Dave Houlton5fa47912018-02-16 11:02:26 -0700758 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
759 .pNext = NULL,
760 .srcAccessMask = 0,
761 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
762 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
763 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
764 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
765 .dstQueueFamilyIndex = demo->present_queue_family_index,
766 .image = demo->swapchain_image_resources[i].image,
767 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600768
Dave Houlton5fa47912018-02-16 11:02:26 -0700769 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
770 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700771 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600772 assert(!err);
773}
774
Karl Schultze4dc75c2016-02-02 15:37:51 -0700775void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600776 mat4x4 MVP, Model, VP;
777 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600778 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600779 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600780
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600781 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600782
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700783 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600784 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700785 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600786 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600787
Dave Houlton5fa47912018-02-16 11:02:26 -0700788 err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0,
789 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600790 assert(!err);
791
Karl Schultze4dc75c2016-02-02 15:37:51 -0700792 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600793
Tony Barboura72d9492017-01-11 13:35:09 -0700794 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600795}
796
Ian Elliottd940ca22017-03-22 08:31:27 -0600797void DemoUpdateTargetIPD(struct demo *demo) {
798 // Look at what happened to previous presents, and make appropriate
799 // adjustments in timing:
800 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700801 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600802 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600803
Dave Houlton5fa47912018-02-16 11:02:26 -0700804 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600805 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600806 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700807 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600808 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700809 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600810 assert(!err);
811
812 bool early = false;
813 bool late = false;
814 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700815 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600816 if (!demo->syncd_with_actual_presents) {
817 // This is the first time that we've received an
818 // actualPresentTime for this swapchain. In order to not
819 // perceive these early frames as "late", we need to sync-up
820 // our future desiredPresentTime's with the
821 // actualPresentTime(s) that we're receiving now.
822 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600823
Ian Elliottd940ca22017-03-22 08:31:27 -0600824 // So that we don't suspect any pending presents as late,
825 // record them all as suspected-late presents:
826 demo->last_late_id = demo->next_present_id - 1;
827 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600828 demo->syncd_with_actual_presents = true;
829 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700830 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600831 demo->refresh_duration)) {
832 // This image could have been presented earlier. We don't want
833 // to decrease the target_IPD until we've seen early presents
834 // for at least two seconds.
835 if (demo->last_early_id == past[i].presentID) {
836 // We've now seen two seconds worth of early presents.
837 // Flag it as such, and reset the counter:
838 early = true;
839 demo->last_early_id = 0;
840 } else if (demo->last_early_id == 0) {
841 // This is the first early present we've seen.
842 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700843 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
844 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600845 demo->last_early_id = past[i].presentID + howManyPresents;
846 } else {
847 // We are in the midst of a set of early images,
848 // and so we won't do anything.
849 }
850 late = false;
851 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700852 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600853 // This image was presented after its desired time. Since
854 // there's a delay between calling vkQueuePresentKHR and when
855 // we get the timing data, several presents may have been late.
856 // Thus, we need to threat all of the outstanding presents as
857 // being likely late, so that we only increase the target_IPD
858 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700859 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600860 late = true;
861 // Record the last suspected-late present:
862 demo->last_late_id = demo->next_present_id - 1;
863 } else {
864 // We are in the midst of a set of likely-late images,
865 // and so we won't do anything.
866 }
867 early = false;
868 demo->last_early_id = 0;
869 } else {
870 // Since this image was not presented early or late, reset
871 // any sets of early or late presentIDs:
872 early = false;
873 late = false;
874 calibrate_next = true;
875 demo->last_early_id = 0;
876 demo->last_late_id = 0;
877 }
878 }
879
880 if (early) {
881 // Since we've seen at least two-seconds worth of presnts that
882 // could have occured earlier than desired, let's decrease the
883 // target_IPD (i.e. increase the frame rate):
884 //
885 // TODO(ianelliott): Try to calculate a better target_IPD based
886 // on the most recently-seen present (this is overly-simplistic).
887 demo->refresh_duration_multiplier--;
888 if (demo->refresh_duration_multiplier == 0) {
889 // This should never happen, but in case it does, don't
890 // try to go faster.
891 demo->refresh_duration_multiplier = 1;
892 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700893 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600894 }
895 if (late) {
896 // Since we found a new instance of a late present, we want to
897 // increase the target_IPD (i.e. decrease the frame rate):
898 //
899 // TODO(ianelliott): Try to calculate a better target_IPD based
900 // on the most recently-seen present (this is overly-simplistic).
901 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -0700902 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600903 }
904
905 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700906 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
907 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600908 }
909 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600910}
911
Karl Schultze4dc75c2016-02-02 15:37:51 -0700912static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600913 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600914
Damien Leonec336d822017-04-14 16:10:14 -0700915 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +0300916 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
917 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600918
Tony Barbour8b7c9112017-06-29 13:34:41 -0600919 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -0600920 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -0700921 err =
922 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
923 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600924
Tony Barbour6914e6d2017-06-02 12:11:29 -0600925 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
926 // demo->swapchain is out of date (e.g. the window was resized) and
927 // must be recreated:
928 demo_resize(demo);
929 } else if (err == VK_SUBOPTIMAL_KHR) {
930 // demo->swapchain is not as optimal as it could be, but the platform's
931 // presentation engine will still present the image correctly.
932 break;
933 } else {
934 assert(!err);
935 }
Tony Barbour8b7c9112017-06-29 13:34:41 -0600936 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -0600937
Tony Barbour3eafe1f2017-06-14 11:59:55 -0600938 demo_update_data_buffer(demo);
939
Ian Elliottd940ca22017-03-22 08:31:27 -0600940 if (demo->VK_GOOGLE_display_timing_enabled) {
941 // Look at what happened to previous presents, and make appropriate
942 // adjustments in timing:
943 DemoUpdateTargetIPD(demo);
944
945 // Note: a real application would position its geometry to that it's in
946 // the correct locatoin for when the next image is presented. It might
947 // also wait, so that there's less latency between any input and when
948 // the next image is rendered/presented. This demo program is so
949 // simple that it doesn't do either of those.
950 }
951
Tony Barbource7524e2016-07-28 12:01:45 -0600952 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600953 // that the image won't be rendered to until the presentation
954 // engine has fully released ownership to the application, and it is
955 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -0600956 VkPipelineStageFlags pipe_stage_flags;
957 VkSubmitInfo submit_info;
958 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
959 submit_info.pNext = NULL;
960 submit_info.pWaitDstStageMask = &pipe_stage_flags;
961 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
962 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600963 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600964 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -0700965 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600966 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600967 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -0700968 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600969 assert(!err);
970
Tony Barbour22e06272016-09-07 16:07:56 -0600971 if (demo->separate_present_queue) {
972 // If we are using separate queues, change image ownership to the
973 // present queue before presenting, waiting for the draw complete
974 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -0700975 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600976 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
977 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600978 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600979 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -0700980 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600981 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600982 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600983 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
984 assert(!err);
985 }
986
987 // If we are using separate queues we have to wait for image ownership,
988 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600989 VkPresentInfoKHR present = {
990 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600991 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600992 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -0700993 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
994 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600995 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700996 .pSwapchains = &demo->swapchain,
997 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600998 };
999
Ian Elliott53622a72017-03-28 11:06:33 -06001000 if (demo->VK_KHR_incremental_present_enabled) {
1001 // If using VK_KHR_incremental_present, we provide a hint of the region
1002 // that contains changed content relative to the previously-presented
1003 // image. The implementation can use this hint in order to save
1004 // work/power (by only copying the region in the hint). The
1005 // implementation is free to ignore the hint though, and so we must
1006 // ensure that the entire image has the correctly-drawn content.
1007 uint32_t eighthOfWidth = demo->width / 8;
1008 uint32_t eighthOfHeight = demo->height / 8;
1009 VkRectLayerKHR rect = {
1010 .offset.x = eighthOfWidth,
1011 .offset.y = eighthOfHeight,
1012 .extent.width = eighthOfWidth * 6,
1013 .extent.height = eighthOfHeight * 6,
1014 .layer = 0,
1015 };
1016 VkPresentRegionKHR region = {
1017 .rectangleCount = 1,
1018 .pRectangles = &rect,
1019 };
1020 VkPresentRegionsKHR regions = {
1021 .sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1022 .pNext = present.pNext,
1023 .swapchainCount = present.swapchainCount,
1024 .pRegions = &region,
1025 };
1026 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001027 }
1028
Ian Elliottd940ca22017-03-22 08:31:27 -06001029 if (demo->VK_GOOGLE_display_timing_enabled) {
1030 VkPresentTimeGOOGLE ptime;
1031 if (demo->prev_desired_present_time == 0) {
1032 // This must be the first present for this swapchain.
1033 //
1034 // We don't know where we are relative to the presentation engine's
1035 // display's refresh cycle. We also don't know how long rendering
1036 // takes. Let's make a grossly-simplified assumption that the
1037 // desiredPresentTime should be half way between now and
1038 // now+target_IPD. We will adjust over time.
1039 uint64_t curtime = getTimeInNanoseconds();
1040 if (curtime == 0) {
1041 // Since we didn't find out the current time, don't give a
1042 // desiredPresentTime:
1043 ptime.desiredPresentTime = 0;
1044 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001045 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1046 }
1047 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001048 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001049 }
1050 ptime.presentID = demo->next_present_id++;
1051 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001052
1053 VkPresentTimesInfoGOOGLE present_time = {
1054 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1055 .pNext = present.pNext,
1056 .swapchainCount = present.swapchainCount,
1057 .pTimes = &ptime,
1058 };
1059 if (demo->VK_GOOGLE_display_timing_enabled) {
1060 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001061 }
1062 }
1063
Tony Barboura2a67812016-07-18 13:21:06 -06001064 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001065 demo->frame_index += 1;
1066 demo->frame_index %= FRAME_LAG;
1067
Ian Elliottcf074d32015-10-16 18:02:43 -06001068 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1069 // demo->swapchain is out of date (e.g. the window was resized) and
1070 // must be recreated:
1071 demo_resize(demo);
1072 } else if (err == VK_SUBOPTIMAL_KHR) {
1073 // demo->swapchain is not as optimal as it could be, but the platform's
1074 // presentation engine will still present the image correctly.
1075 } else {
1076 assert(!err);
1077 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001078}
1079
Karl Schultze4dc75c2016-02-02 15:37:51 -07001080static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001081 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001082 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001083
Ian Elliottb08e0d92015-11-20 11:55:46 -07001084 // Check the surface capabilities and formats
1085 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001086 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001087 assert(!err);
1088
Ian Elliott8528eff2015-08-07 15:56:59 -06001089 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001090 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001091 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001092 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001093 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001094 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001095 assert(!err);
1096
Ian Elliotta0781972015-08-21 15:09:33 -06001097 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001098 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1099 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1100 // If the surface size is undefined, the size is set to the size
1101 // of the images requested, which must fit within the minimum and
1102 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001103 swapchainExtent.width = demo->width;
1104 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001105
1106 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1107 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1108 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1109 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1110 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001111
Ian Elliottcf7f7482016-08-19 03:46:58 -06001112 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1113 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1114 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1115 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1116 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001117 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001118 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001119 swapchainExtent = surfCapabilities.currentExtent;
1120 demo->width = surfCapabilities.currentExtent.width;
1121 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001122 }
1123
Jeremy Kniager602e4182018-01-19 10:52:34 -07001124 if (demo->width == 0 || demo->height == 0) {
1125 demo->is_minimized = true;
1126 return;
1127 } else {
1128 demo->is_minimized = false;
1129 }
1130
Tony Barbour66a56c32016-09-21 13:10:56 -06001131 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001132 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001133 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001134
Ian Elliottb18fdde2016-10-03 12:11:12 -06001135 // There are times when you may wish to use another present mode. The
1136 // following code shows how to select them, and the comments provide some
1137 // reasons you may wish to use them.
1138 //
1139 // It should be noted that Vulkan 1.0 doesn't provide a method for
1140 // synchronizing rendering with the presentation engine's display. There
1141 // is a method provided for throttling rendering with the display, but
1142 // there are some presentation engines for which this method will not work.
1143 // If an application doesn't throttle its rendering, and if it renders much
1144 // faster than the refresh rate of the display, this can waste power on
1145 // mobile devices. That is because power is being spent rendering images
1146 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001147
Ian Elliottb18fdde2016-10-03 12:11:12 -06001148 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1149 // tearing, or have some way of synchronizing their rendering with the
1150 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001151 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1152 // generally render a new presentable image every refresh cycle, but are
1153 // occasionally early. In this case, the application wants the new image
1154 // to be displayed instead of the previously-queued-for-presentation image
1155 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001156 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1157 // render a new presentable image every refresh cycle, but are occasionally
1158 // late. In this case (perhaps because of stuttering/latency concerns),
1159 // the application wants the late image to be immediately displayed, even
1160 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001161
Dave Houlton5fa47912018-02-16 11:02:26 -07001162 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001163 for (size_t i = 0; i < presentModeCount; ++i) {
1164 if (presentModes[i] == demo->presentMode) {
1165 swapchainPresentMode = demo->presentMode;
1166 break;
1167 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001168 }
1169 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001170 if (swapchainPresentMode != demo->presentMode) {
1171 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1172 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001173
Tony Barbour84376fe2017-01-06 15:54:22 -07001174 // Determine the number of VkImages to use in the swap chain.
1175 // Application desires to acquire 3 images at a time for triple
1176 // buffering
1177 uint32_t desiredNumOfSwapchainImages = 3;
1178 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1179 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1180 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001181 // If maxImageCount is 0, we can ask for as many images as we want;
1182 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001183 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001184 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001185 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001186 }
1187
Tony Barbour9fd6d352015-09-16 15:01:32 -06001188 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001189 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001190 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001191 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001192 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001193 }
1194
Tony Barbour820b55b2017-03-14 08:55:46 -06001195 // Find a supported composite alpha mode - one of these is guaranteed to be set
1196 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1197 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1198 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1199 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1200 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1201 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1202 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001203 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001204 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1205 compositeAlpha = compositeAlphaFlags[i];
1206 break;
1207 }
1208 }
1209
Tony Barboura2a67812016-07-18 13:21:06 -06001210 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001211 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001212 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001213 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001214 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001215 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001216 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001217 .imageExtent =
1218 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001219 .width = swapchainExtent.width,
1220 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001221 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001222 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001223 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001224 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001225 .imageArrayLayers = 1,
1226 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1227 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001228 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001229 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001230 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001231 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001233 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001234 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001235 assert(!err);
1236
Ian Elliottcf074d32015-10-16 18:02:43 -06001237 // If we just re-created an existing swapchain, we should destroy the old
1238 // swapchain at this point.
1239 // Note: destroying the swapchain also cleans up all its associated
1240 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001241 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001242 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001243 }
1244
Dave Houlton5fa47912018-02-16 11:02:26 -07001245 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001246 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001247
Dave Houlton5fa47912018-02-16 11:02:26 -07001248 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001249 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001250 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001251 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001252
Dave Houlton5fa47912018-02-16 11:02:26 -07001253 demo->swapchain_image_resources =
1254 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001255 assert(demo->swapchain_image_resources);
1256
Ian Elliotta0781972015-08-21 15:09:33 -06001257 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001258 VkImageViewCreateInfo color_image_view = {
1259 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001260 .pNext = NULL,
1261 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001262 .components =
1263 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001264 .r = VK_COMPONENT_SWIZZLE_R,
1265 .g = VK_COMPONENT_SWIZZLE_G,
1266 .b = VK_COMPONENT_SWIZZLE_B,
1267 .a = VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001268 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001269 .subresourceRange =
1270 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001271 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1272 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001273 };
1274
Tony Barboura72d9492017-01-11 13:35:09 -07001275 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001276
Tony Barboura72d9492017-01-11 13:35:09 -07001277 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001278
Dave Houlton5fa47912018-02-16 11:02:26 -07001279 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001280 assert(!err);
1281 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001282
Ian Elliottd940ca22017-03-22 08:31:27 -06001283 if (demo->VK_GOOGLE_display_timing_enabled) {
1284 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001285 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001286 assert(!err);
1287 demo->refresh_duration = rc_dur.refreshDuration;
1288
1289 demo->syncd_with_actual_presents = false;
1290 // Initially target 1X the refresh duration:
1291 demo->target_IPD = demo->refresh_duration;
1292 demo->refresh_duration_multiplier = 1;
1293 demo->prev_desired_present_time = 0;
1294 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001295 }
1296
Mark Young04fd3d82016-01-25 14:43:50 -07001297 if (NULL != presentModes) {
1298 free(presentModes);
1299 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001300}
1301
Karl Schultze4dc75c2016-02-02 15:37:51 -07001302static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001303 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001304 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001305 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001307 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001308 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001309 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001311 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001312 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001313 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001314 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315 .flags = 0,
1316 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001317
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001318 VkImageViewCreateInfo view = {
1319 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001320 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001321 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001322 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001323 .subresourceRange =
1324 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001325 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001326 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001327 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001328
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001329 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001330 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001331 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001332
1333 demo->depth.format = depth_format;
1334
1335 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001336 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001337 assert(!err);
1338
Karl Schultze4dc75c2016-02-02 15:37:51 -07001339 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001340 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001341
Chia-I Wuf48700b2015-11-10 16:21:09 +08001342 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001343 demo->depth.mem_alloc.pNext = NULL;
1344 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1345 demo->depth.mem_alloc.memoryTypeIndex = 0;
1346
Dave Houlton5fa47912018-02-16 11:02:26 -07001347 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001348 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001349 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001350
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001351 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001352 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001353 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001355 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001356 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001357 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001358
1359 /* create image view */
1360 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001361 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001363}
1364
Tony Barbour1a86d032015-09-21 15:17:33 -06001365/* Load a ppm file into memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001366bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001367#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Dave Houlton5fa47912018-02-16 11:02:26 -07001368 filename = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001369#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001370
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001371#ifdef __ANDROID__
1372#include <lunarg.ppm.h>
1373 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001374 cPtr = (char *)lunarg_ppm;
1375 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001376 return false;
1377 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001378 while (strncmp(cPtr++, "\n", 1))
1379 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001380 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001381 if (rgba_data == NULL) {
1382 return true;
1383 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001384 while (strncmp(cPtr++, "\n", 1))
1385 ;
1386 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001387 return false;
1388 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001389 while (strncmp(cPtr++, "\n", 1))
1390 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001391
1392 for (int y = 0; y < *height; y++) {
1393 uint8_t *rowPtr = rgba_data;
1394 for (int x = 0; x < *width; x++) {
1395 memcpy(rowPtr, cPtr, 3);
1396 rowPtr[3] = 255; /* Alpha of 1 */
1397 rowPtr += 4;
1398 cPtr += 3;
1399 }
1400 rgba_data += layout->rowPitch;
1401 }
1402
1403 return true;
1404#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001405 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001406 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001407
Dave Houlton5fa47912018-02-16 11:02:26 -07001408 if (!fPtr) return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001409
Dave Houlton5fa47912018-02-16 11:02:26 -07001410 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001411 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1412 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001413 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001414 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001415
Tony Barbour1a86d032015-09-21 15:17:33 -06001416 do {
1417 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001418 if (cPtr == NULL) {
1419 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001420 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001421 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001422 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001423
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001424 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001425 if (rgba_data == NULL) {
1426 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001427 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001428 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001429 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001430 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001431 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1432 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001433 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001434 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001435
Karl Schultze4dc75c2016-02-02 15:37:51 -07001436 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001437 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001438 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001439 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001440 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001441 rowPtr[3] = 255; /* Alpha of 1 */
1442 rowPtr += 4;
1443 }
1444 rgba_data += layout->rowPitch;
1445 }
1446 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001447 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001448#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001449}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001450
Dave Houlton5fa47912018-02-16 11:02:26 -07001451static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1452 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001453 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001454 int32_t tex_width;
1455 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001456 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001457 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001458
Karl Schultze4dc75c2016-02-02 15:37:51 -07001459 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001460 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001461 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001462
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001463 tex_obj->tex_width = tex_width;
1464 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001465
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001466 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001467 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001468 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001469 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001470 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001471 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001472 .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,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001475 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001476 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001477 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001478 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001479 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001480
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001481 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001482
Dave Houlton5fa47912018-02-16 11:02:26 -07001483 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001484 assert(!err);
1485
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001486 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001487
Chia-I Wuf48700b2015-11-10 16:21:09 +08001488 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001489 tex_obj->mem_alloc.pNext = NULL;
1490 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1491 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001492
Dave Houlton5fa47912018-02-16 11:02:26 -07001493 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001494 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001495
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001496 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001497 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001498 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001499
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001500 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001501 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001502 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001503
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001504 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001505 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001506 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001507 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001508 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001509 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001510 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001511 void *data;
1512
Dave Houlton5fa47912018-02-16 11:02:26 -07001513 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001514
Dave Houlton5fa47912018-02-16 11:02:26 -07001515 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001516 assert(!err);
1517
1518 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1519 fprintf(stderr, "Error loading texture: %s\n", filename);
1520 }
1521
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001522 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001523 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001524
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001525 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001526}
1527
Dave Houlton5fa47912018-02-16 11:02:26 -07001528static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001529 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001530 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1531 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001532}
1533
Karl Schultze4dc75c2016-02-02 15:37:51 -07001534static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001535 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001536 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001537 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001538
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001539 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001540
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001541 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001542 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001543
Dave Houlton5fa47912018-02-16 11:02:26 -07001544 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001545 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001546 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1547 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001548 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1549 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001550 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1551 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001552 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001553 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001554 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001555 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001556
Tony Barbour16156cb2016-10-19 14:15:49 -06001557 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Dave Houlton5fa47912018-02-16 11:02:26 -07001558 demo_prepare_texture_image(demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
1559 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1560 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001561
Dave Houlton5fa47912018-02-16 11:02:26 -07001562 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1563 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1564 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001565
Dave Houlton5fa47912018-02-16 11:02:26 -07001566 demo_set_image_layout(demo, demo->staging_texture.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1567 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001568 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001569
Dave Houlton5fa47912018-02-16 11:02:26 -07001570 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1571 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001572 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001573
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001574 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001575 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1576 .srcOffset = {0, 0, 0},
1577 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1578 .dstOffset = {0, 0, 0},
Dave Houlton5fa47912018-02-16 11:02:26 -07001579 .extent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001580 };
Dave Houlton5fa47912018-02-16 11:02:26 -07001581 vkCmdCopyImage(demo->cmd, demo->staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1582 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001583
Dave Houlton5fa47912018-02-16 11:02:26 -07001584 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1585 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001586 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001587
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001588 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001589 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1590 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001591 }
1592
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001593 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001594 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001595 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001596 .magFilter = VK_FILTER_NEAREST,
1597 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001598 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001599 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1600 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1601 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001602 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001603 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001604 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001605 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001606 .minLod = 0.0f,
1607 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001608 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001609 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001610 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001611
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001612 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001613 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001614 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001615 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001616 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001617 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001618 .components =
1619 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001620 VK_COMPONENT_SWIZZLE_R,
1621 VK_COMPONENT_SWIZZLE_G,
1622 VK_COMPONENT_SWIZZLE_B,
1623 VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001624 },
1625 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001626 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001628
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001629 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001630 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631 assert(!err);
1632
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001633 /* create image view */
1634 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001635 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001636 assert(!err);
1637 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001638}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001639
Tony Barboura72d9492017-01-11 13:35:09 -07001640void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001641 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001642 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001643 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001644 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001645 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001646 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001647 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001648 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001649
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001650 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001651 mat4x4_mul(MVP, VP, demo->model_matrix);
1652 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001653 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001654
Karl Schultza2615462017-01-20 13:19:20 -07001655 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001656 data.position[i][0] = g_vertex_buffer_data[i * 3];
1657 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1658 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001659 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001660 data.attr[i][0] = g_uv_buffer_data[2 * i];
1661 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001662 data.attr[i][2] = 0;
1663 data.attr[i][3] = 0;
1664 }
1665
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001666 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001667 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001668 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001669 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001670
Tony Barboura72d9492017-01-11 13:35:09 -07001671 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001672 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001673 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001674
Dave Houlton5fa47912018-02-16 11:02:26 -07001675 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001676
Tony Barboura72d9492017-01-11 13:35:09 -07001677 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1678 mem_alloc.pNext = NULL;
1679 mem_alloc.allocationSize = mem_reqs.size;
1680 mem_alloc.memoryTypeIndex = 0;
1681
Dave Houlton5fa47912018-02-16 11:02:26 -07001682 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1683 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1684 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001685 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001686
Dave Houlton5fa47912018-02-16 11:02:26 -07001687 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001688 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001689
Dave Houlton5fa47912018-02-16 11:02:26 -07001690 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 -07001691 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001692
Tony Barboura72d9492017-01-11 13:35:09 -07001693 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001694
Tony Barboura72d9492017-01-11 13:35:09 -07001695 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001696
Tony Barboura72d9492017-01-11 13:35:09 -07001697 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001698 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001699 assert(!err);
1700 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001701}
1702
Karl Schultze4dc75c2016-02-02 15:37:51 -07001703static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001704 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001705 [0] =
1706 {
1707 .binding = 0,
1708 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1709 .descriptorCount = 1,
1710 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1711 .pImmutableSamplers = NULL,
1712 },
1713 [1] =
1714 {
1715 .binding = 1,
1716 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1717 .descriptorCount = DEMO_TEXTURE_COUNT,
1718 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1719 .pImmutableSamplers = NULL,
1720 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001721 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001722 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001723 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001724 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001725 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001726 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001727 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001728 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001729
Dave Houlton5fa47912018-02-16 11:02:26 -07001730 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001731 assert(!err);
1732
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001733 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001734 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1735 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001736 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001737 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001738 };
1739
Dave Houlton5fa47912018-02-16 11:02:26 -07001740 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001741 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742}
1743
Karl Schultze4dc75c2016-02-02 15:37:51 -07001744static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001745 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1746 // because at the start of the renderpass, we don't care about their contents.
1747 // At the start of the subpass, the color attachment's layout will be transitioned
1748 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1749 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1750 // the renderpass, the color attachment's layout will be transitioned to
1751 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1752 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001753 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001754 [0] =
1755 {
1756 .format = demo->format,
1757 .flags = 0,
1758 .samples = VK_SAMPLE_COUNT_1_BIT,
1759 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1760 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1761 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1762 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1763 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1764 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1765 },
1766 [1] =
1767 {
1768 .format = demo->depth.format,
1769 .flags = 0,
1770 .samples = VK_SAMPLE_COUNT_1_BIT,
1771 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1772 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1773 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1774 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1775 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1776 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1777 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001778 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001779 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001780 .attachment = 0,
1781 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001782 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001783 const VkAttachmentReference depth_reference = {
1784 .attachment = 1,
1785 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1786 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001787 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001788 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1789 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001790 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001791 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001792 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001793 .pColorAttachments = &color_reference,
1794 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001795 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001796 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001797 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001798 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001799 const VkRenderPassCreateInfo rp_info = {
1800 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1801 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001802 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001803 .attachmentCount = 2,
1804 .pAttachments = attachments,
1805 .subpassCount = 1,
1806 .pSubpasses = &subpass,
1807 .dependencyCount = 0,
1808 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001809 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001810 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001811
Chia-I Wu63636062015-10-26 21:10:41 +08001812 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001813 assert(!err);
1814}
1815
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001816static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001817 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001818 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001819 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001821 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1822 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001823 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001824 moduleCreateInfo.codeSize = size;
1825 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001826
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001827 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1828 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001829
1830 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001831}
1832
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001833static void demo_prepare_vs(struct demo *demo) {
1834 const uint32_t vs_code[] = {
1835#include "cube.vert.inc"
1836 };
1837 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838}
1839
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001840static void demo_prepare_fs(struct demo *demo) {
1841 const uint32_t fs_code[] = {
1842#include "cube.frag.inc"
1843 };
1844 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001845}
1846
Karl Schultze4dc75c2016-02-02 15:37:51 -07001847static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001848 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001849 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001850 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001851 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001852 VkPipelineRasterizationStateCreateInfo rs;
1853 VkPipelineColorBlendStateCreateInfo cb;
1854 VkPipelineDepthStencilStateCreateInfo ds;
1855 VkPipelineViewportStateCreateInfo vp;
1856 VkPipelineMultisampleStateCreateInfo ms;
1857 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1858 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001859 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001860
Piers Daniellba839d12015-09-29 13:01:09 -06001861 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1862 memset(&dynamicState, 0, sizeof dynamicState);
1863 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1864 dynamicState.pDynamicStates = dynamicStateEnables;
1865
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001866 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001867 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001868 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001869
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001870 memset(&vi, 0, sizeof(vi));
1871 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1872
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001873 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001874 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001875 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001876
1877 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001878 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1879 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001880 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001881 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001882 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001883 rs.rasterizerDiscardEnable = VK_FALSE;
1884 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001885 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001886
1887 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001888 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1889 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001890 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001891 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001892 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001893 cb.attachmentCount = 1;
1894 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001895
Tony Barbour29645d02015-01-16 14:27:35 -07001896 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001897 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001898 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001899 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001900 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001901 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001902
1903 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001904 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001905 ds.depthTestEnable = VK_TRUE;
1906 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001907 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001908 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001909 ds.back.failOp = VK_STENCIL_OP_KEEP;
1910 ds.back.passOp = VK_STENCIL_OP_KEEP;
1911 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001912 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001913 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001914
Tony Barbour29645d02015-01-16 14:27:35 -07001915 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001916 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001917 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001918 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001919
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001920 demo_prepare_vs(demo);
1921 demo_prepare_fs(demo);
1922
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001923 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001924 VkPipelineShaderStageCreateInfo shaderStages[2];
1925 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1926
Karl Schultze4dc75c2016-02-02 15:37:51 -07001927 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1928 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001929 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001930 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001931
Karl Schultze4dc75c2016-02-02 15:37:51 -07001932 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1933 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001934 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001935 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001936
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001937 memset(&pipelineCache, 0, sizeof(pipelineCache));
1938 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001939
Dave Houlton5fa47912018-02-16 11:02:26 -07001940 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001941 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001942
Karl Schultze4dc75c2016-02-02 15:37:51 -07001943 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001944 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001945 pipeline.pRasterizationState = &rs;
1946 pipeline.pColorBlendState = &cb;
1947 pipeline.pMultisampleState = &ms;
1948 pipeline.pViewportState = &vp;
1949 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001950 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001951 pipeline.pStages = shaderStages;
1952 pipeline.renderPass = demo->render_pass;
1953 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001954
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001955 pipeline.renderPass = demo->render_pass;
1956
Dave Houlton5fa47912018-02-16 11:02:26 -07001957 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001958 assert(!err);
1959
Chia-I Wu63636062015-10-26 21:10:41 +08001960 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1961 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001962}
1963
Karl Schultze4dc75c2016-02-02 15:37:51 -07001964static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001965 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001966 [0] =
1967 {
1968 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1969 .descriptorCount = demo->swapchainImageCount,
1970 },
1971 [1] =
1972 {
1973 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1974 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
1975 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001976 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001977 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001978 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001979 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001980 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08001981 .poolSizeCount = 2,
1982 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001983 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001984 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001985
Dave Houlton5fa47912018-02-16 11:02:26 -07001986 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001987 assert(!err);
1988}
1989
Karl Schultze4dc75c2016-02-02 15:37:51 -07001990static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001991 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001992 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001993 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001994
Dave Houlton5fa47912018-02-16 11:02:26 -07001995 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
1996 .pNext = NULL,
1997 .descriptorPool = demo->desc_pool,
1998 .descriptorSetCount = 1,
1999 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002000
2001 VkDescriptorBufferInfo buffer_info;
2002 buffer_info.offset = 0;
2003 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002004
Chia-I Wuae721ba2015-05-25 16:27:55 +08002005 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002006 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002007 tex_descs[i].sampler = demo->textures[i].sampler;
2008 tex_descs[i].imageView = demo->textures[i].view;
2009 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002010 }
2011
2012 memset(&writes, 0, sizeof(writes));
2013
2014 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002015 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002016 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002017 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002018
2019 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002020 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002021 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002022 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002023 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002024
Tony Barboura72d9492017-01-11 13:35:09 -07002025 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2026 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2027 assert(!err);
2028 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2029 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2030 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2031 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2032 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002033}
2034
Karl Schultze4dc75c2016-02-02 15:37:51 -07002035static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002036 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002037 attachments[1] = demo->depth.view;
2038
Chia-I Wuf973e322015-07-08 13:34:24 +08002039 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002040 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2041 .pNext = NULL,
2042 .renderPass = demo->render_pass,
2043 .attachmentCount = 2,
2044 .pAttachments = attachments,
2045 .width = demo->width,
2046 .height = demo->height,
2047 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002048 };
2049 VkResult U_ASSERT_ONLY err;
2050 uint32_t i;
2051
Tony Barbourd8e504f2015-10-08 14:26:24 -06002052 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002053 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002054 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002055 assert(!err);
2056 }
2057}
2058
Karl Schultze4dc75c2016-02-02 15:37:51 -07002059static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002060 VkResult U_ASSERT_ONLY err;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002061 if (demo->cmd_pool == VK_NULL_HANDLE) {
2062 const VkCommandPoolCreateInfo cmd_pool_info = {
2063 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2064 .pNext = NULL,
2065 .queueFamilyIndex = demo->graphics_queue_family_index,
2066 .flags = 0,
2067 };
2068 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2069 assert(!err);
2070 }
Cody Northrop91e221b2015-07-09 18:08:32 -06002071
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002072 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002073 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002074 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002075 .commandPool = demo->cmd_pool,
2076 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002077 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002078 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002079 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2080 assert(!err);
2081 VkCommandBufferBeginInfo cmd_buf_info = {
2082 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2083 .pNext = NULL,
2084 .flags = 0,
2085 .pInheritanceInfo = NULL,
2086 };
2087 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2088 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002089
2090 demo_prepare_buffers(demo);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002091
2092 if (demo->is_minimized) {
2093 demo->prepared = false;
2094 return;
2095 }
2096
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002097 demo_prepare_depth(demo);
2098 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002099 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002100
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002101 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002102 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002103 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002104
Ian Elliotta0781972015-08-21 15:09:33 -06002105 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002106 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002107 assert(!err);
2108 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002109
Tony Barbour22e06272016-09-07 16:07:56 -06002110 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002111 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002112 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2113 .pNext = NULL,
2114 .queueFamilyIndex = demo->present_queue_family_index,
2115 .flags = 0,
2116 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002117 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002118 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002119 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002120 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2121 .pNext = NULL,
2122 .commandPool = demo->present_cmd_pool,
2123 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2124 .commandBufferCount = 1,
2125 };
2126 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002127 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2128 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002129 assert(!err);
2130 demo_build_image_ownership_cmd(demo, i);
2131 }
2132 }
2133
Chia-I Wu63ea9262015-03-26 13:14:16 +08002134 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002135 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002136
Chia-I Wuf973e322015-07-08 13:34:24 +08002137 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002138
Ian Elliotta0781972015-08-21 15:09:33 -06002139 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002140 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002141 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002142 }
2143
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002144 /*
2145 * Prepare functions above may generate pipeline commands
2146 * that need to be flushed before beginning the render loop.
2147 */
2148 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002149 if (demo->staging_texture.image) {
2150 demo_destroy_texture_image(demo, &demo->staging_texture);
2151 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002152
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002153 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002154 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002155}
2156
Karl Schultze4dc75c2016-02-02 15:37:51 -07002157static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002158 uint32_t i;
2159
2160 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002161 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002162
Tony Barbour66a56c32016-09-21 13:10:56 -06002163 // Wait for fences from present operations
2164 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002165 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002166 vkDestroyFence(demo->device, demo->fences[i], NULL);
2167 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2168 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2169 if (demo->separate_present_queue) {
2170 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2171 }
2172 }
2173
Jeremy Kniager602e4182018-01-19 10:52:34 -07002174 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2175 if (!demo->is_minimized) {
2176 for (i = 0; i < demo->swapchainImageCount; i++) {
2177 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2178 }
2179 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002180
Jeremy Kniager602e4182018-01-19 10:52:34 -07002181 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2182 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2183 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2184 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2185 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002186
Jeremy Kniager602e4182018-01-19 10:52:34 -07002187 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2188 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2189 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2190 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2191 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2192 }
2193 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002194
Jeremy Kniager602e4182018-01-19 10:52:34 -07002195 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2196 vkDestroyImage(demo->device, demo->depth.image, NULL);
2197 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002198
Jeremy Kniager602e4182018-01-19 10:52:34 -07002199 for (i = 0; i < demo->swapchainImageCount; i++) {
2200 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2201 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2202 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2203 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2204 }
2205 free(demo->swapchain_image_resources);
2206 free(demo->queue_props);
2207 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002208
Jeremy Kniager602e4182018-01-19 10:52:34 -07002209 if (demo->separate_present_queue) {
2210 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2211 }
Tony Barbour22e06272016-09-07 16:07:56 -06002212 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002213 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002214 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002215 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002216 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002217 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002218 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002219
Tony Barbour153cb062016-12-07 13:43:36 -07002220#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002221 XDestroyWindow(demo->display, demo->xlib_window);
2222 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002223#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002224 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002225 xcb_disconnect(demo->connection);
2226 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002227#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002228 wl_keyboard_destroy(demo->keyboard);
2229 wl_pointer_destroy(demo->pointer);
2230 wl_seat_destroy(demo->seat);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002231 wl_shell_surface_destroy(demo->shell_surface);
2232 wl_surface_destroy(demo->window);
2233 wl_shell_destroy(demo->shell);
2234 wl_compositor_destroy(demo->compositor);
2235 wl_registry_destroy(demo->registry);
2236 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002237#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002238#endif
Karl Schultz86429112017-06-20 11:27:59 -06002239
2240 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002241}
2242
Karl Schultze4dc75c2016-02-02 15:37:51 -07002243static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002244 uint32_t i;
2245
Mike Stroyanbb60b382015-10-28 09:46:57 -06002246 // Don't react to resize until after first initialization.
2247 if (!demo->prepared) {
Jeremy Kniager602e4182018-01-19 10:52:34 -07002248 if (demo->is_minimized) {
2249 demo_prepare(demo);
2250 }
Mike Stroyanbb60b382015-10-28 09:46:57 -06002251 return;
2252 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002253 // In order to properly resize the window, we must re-create the swapchain
2254 // AND redo the command buffers, etc.
2255 //
2256 // First, perform part of the demo_cleanup() function:
2257 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002258 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002259
2260 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002261 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002262 }
Chia-I Wu63636062015-10-26 21:10:41 +08002263 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002264
Chia-I Wu63636062015-10-26 21:10:41 +08002265 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2266 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2267 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2268 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2269 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002270
2271 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002272 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2273 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2274 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2275 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002276 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002277
Chia-I Wu63636062015-10-26 21:10:41 +08002278 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2279 vkDestroyImage(demo->device, demo->depth.image, NULL);
2280 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002281
Ian Elliottcf074d32015-10-16 18:02:43 -06002282 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002283 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002284 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002285 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2286 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002287 }
Chia-I Wu63636062015-10-26 21:10:41 +08002288 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Jeremy Kniager602e4182018-01-19 10:52:34 -07002289 demo->cmd_pool = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06002290 if (demo->separate_present_queue) {
2291 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2292 }
Tony Barboura72d9492017-01-11 13:35:09 -07002293 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002294
Ian Elliottcf074d32015-10-16 18:02:43 -06002295 // Second, re-perform the demo_prepare() function, which will re-create the
2296 // swapchain:
2297 demo_prepare(demo);
2298}
2299
David Pinedo2bb7c932015-06-18 17:03:14 -06002300// On MS-Windows, make this a global, so it's available to WndProc()
2301struct demo demo;
2302
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002303#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002304static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002305 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002306
Ian Elliott639ca472015-04-16 15:23:05 -06002307 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002308 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002309 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002310 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002311 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002312}
Ian Elliott639ca472015-04-16 15:23:05 -06002313
2314// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002315LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2316 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002317 case WM_CLOSE:
2318 PostQuitMessage(validation_error);
2319 break;
2320 case WM_PAINT:
2321 // The validation callback calls MessageBox which can generate paint
2322 // events - don't make more Vulkan calls if we got here from the
2323 // callback
2324 if (!in_callback) {
2325 demo_run(&demo);
2326 }
2327 break;
2328 case WM_GETMINMAXINFO: // set window's minimum size
2329 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2330 return 0;
2331 case WM_SIZE:
2332 // Resize the application to the new window size, except when
2333 // it was minimized. Vulkan doesn't support images or swapchains
2334 // with width=0 and height=0.
2335 if (wParam != SIZE_MINIMIZED) {
2336 demo.width = lParam & 0xffff;
2337 demo.height = (lParam & 0xffff0000) >> 16;
2338 demo_resize(&demo);
2339 }
2340 break;
2341 default:
2342 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002343 }
2344 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2345}
2346
Karl Schultze4dc75c2016-02-02 15:37:51 -07002347static void demo_create_window(struct demo *demo) {
2348 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002349
2350 // Initialize the window class structure:
2351 win_class.cbSize = sizeof(WNDCLASSEX);
2352 win_class.style = CS_HREDRAW | CS_VREDRAW;
2353 win_class.lpfnWndProc = WndProc;
2354 win_class.cbClsExtra = 0;
2355 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002356 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002357 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2358 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2359 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2360 win_class.lpszMenuName = NULL;
2361 win_class.lpszClassName = demo->name;
2362 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2363 // Register window class:
2364 if (!RegisterClassEx(&win_class)) {
2365 // It didn't work, so try to give a useful error:
2366 printf("Unexpected error trying to start the application!\n");
2367 fflush(stdout);
2368 exit(1);
2369 }
2370 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002371 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002372 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002373 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002374 demo->name, // class name
2375 demo->name, // app name
2376 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002377 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002378 100, 100, // x/y coords
2379 wr.right - wr.left, // width
2380 wr.bottom - wr.top, // height
2381 NULL, // handle to parent
2382 NULL, // handle to menu
2383 demo->connection, // hInstance
2384 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002385 if (!demo->window) {
2386 // It didn't work, so try to give a useful error:
2387 printf("Cannot create a window in which to draw!\n");
2388 fflush(stdout);
2389 exit(1);
2390 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002391 // Window client area size must be at least 1 pixel high, to prevent crash.
2392 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002393 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002394}
Tony Barbour8295ac42016-08-08 11:04:17 -06002395#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002396static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002397 const char *display_envar = getenv("DISPLAY");
2398 if (display_envar == NULL || display_envar[0] == '\0') {
2399 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2400 fflush(stdout);
2401 exit(1);
2402 }
Tony Barbour2593b182016-04-19 10:57:58 -06002403
Tony Barbouree9cd372017-01-31 16:09:58 -07002404 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002405 demo->display = XOpenDisplay(NULL);
2406 long visualMask = VisualScreenMask;
2407 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002408 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002409 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002410 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002411
Dave Houlton5fa47912018-02-16 11:02:26 -07002412 Colormap colormap =
2413 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002414
Dave Houlton5fa47912018-02-16 11:02:26 -07002415 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002416 windowAttributes.colormap = colormap;
2417 windowAttributes.background_pixel = 0xFFFFFFFF;
2418 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002419 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002420
Dave Houlton5fa47912018-02-16 11:02:26 -07002421 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2422 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2423 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002424
2425 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2426 XMapWindow(demo->display, demo->xlib_window);
2427 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002428 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002429}
2430static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002431 switch (event->type) {
2432 case ClientMessage:
2433 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002434 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002435 case KeyPress:
2436 switch (event->xkey.keycode) {
2437 case 0x9: // Escape
2438 demo->quit = true;
2439 break;
2440 case 0x71: // left arrow key
2441 demo->spin_angle -= demo->spin_increment;
2442 break;
2443 case 0x72: // right arrow key
2444 demo->spin_angle += demo->spin_increment;
2445 break;
2446 case 0x41: // space bar
2447 demo->pause = !demo->pause;
2448 break;
2449 }
Tony Barbour2593b182016-04-19 10:57:58 -06002450 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002451 case ConfigureNotify:
2452 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2453 demo->width = event->xconfigure.width;
2454 demo->height = event->xconfigure.height;
2455 demo_resize(demo);
2456 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002457 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002458 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002459 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002460 }
Tony Barbour2593b182016-04-19 10:57:58 -06002461}
2462
2463static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002464 while (!demo->quit) {
2465 XEvent event;
2466
2467 if (demo->pause) {
2468 XNextEvent(demo->display, &event);
2469 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002470 }
2471 while (XPending(demo->display) > 0) {
2472 XNextEvent(demo->display, &event);
2473 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002474 }
2475
Tony Barbour2593b182016-04-19 10:57:58 -06002476 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002477 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002478 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002479 }
2480}
Tony Barbour153cb062016-12-07 13:43:36 -07002481#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002482static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002483 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002484 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002485 case XCB_EXPOSE:
2486 // TODO: Resize window
2487 break;
2488 case XCB_CLIENT_MESSAGE:
2489 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2490 demo->quit = true;
2491 }
2492 break;
2493 case XCB_KEY_RELEASE: {
2494 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002495
Dave Houlton5fa47912018-02-16 11:02:26 -07002496 switch (key->detail) {
2497 case 0x9: // Escape
2498 demo->quit = true;
2499 break;
2500 case 0x71: // left arrow key
2501 demo->spin_angle -= demo->spin_increment;
2502 break;
2503 case 0x72: // right arrow key
2504 demo->spin_angle += demo->spin_increment;
2505 break;
2506 case 0x41: // space bar
2507 demo->pause = !demo->pause;
2508 break;
2509 }
2510 } break;
2511 case XCB_CONFIGURE_NOTIFY: {
2512 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2513 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2514 demo->width = cfg->width;
2515 demo->height = cfg->height;
2516 demo_resize(demo);
2517 }
2518 } break;
2519 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002520 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002521 }
2522}
2523
Tony Barbour2593b182016-04-19 10:57:58 -06002524static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002525 xcb_flush(demo->connection);
2526
2527 while (!demo->quit) {
2528 xcb_generic_event_t *event;
2529
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002530 if (demo->pause) {
2531 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002532 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002533 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002534 }
2535 while (event) {
2536 demo_handle_xcb_event(demo, event);
2537 free(event);
2538 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002539 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002540
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002541 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002542 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002543 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002544 }
2545}
2546
Tony Barbour2593b182016-04-19 10:57:58 -06002547static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002548 uint32_t value_mask, value_list[32];
2549
Tony Barbour2593b182016-04-19 10:57:58 -06002550 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002551
2552 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2553 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002554 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002555
Dave Houlton5fa47912018-02-16 11:02:26 -07002556 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2557 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002558
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002559 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002560 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2561 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002562
Dave Houlton5fa47912018-02-16 11:02:26 -07002563 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2564 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002565
Dave Houlton5fa47912018-02-16 11:02:26 -07002566 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002567 &(*demo->atom_wm_delete_window).atom);
2568 free(reply);
2569
Tony Barbour2593b182016-04-19 10:57:58 -06002570 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002571
Karl Schultze4dc75c2016-02-02 15:37:51 -07002572 // Force the x/y coordinates to 100,100 results are identical in consecutive
2573 // runs
2574 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002575 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002576}
Tony Barbour6b131662016-08-25 13:14:45 -06002577// VK_USE_PLATFORM_XCB_KHR
2578#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002579static void demo_run(struct demo *demo) {
2580 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002581 if (demo->pause) {
2582 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002583 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002584 wl_display_dispatch_pending(demo->display); // don't block
2585 demo_draw(demo);
2586 demo->curFrame++;
2587 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2588 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002589 }
2590}
2591
Dave Houlton5fa47912018-02-16 11:02:26 -07002592static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002593 wl_shell_surface_pong(shell_surface, serial);
2594}
2595
Dave Houlton5fa47912018-02-16 11:02:26 -07002596static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED,
2597 int32_t width UNUSED, int32_t height UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002598
Dave Houlton5fa47912018-02-16 11:02:26 -07002599static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002600
Dave Houlton5fa47912018-02-16 11:02:26 -07002601static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002602
2603static void demo_create_window(struct demo *demo) {
2604 demo->window = wl_compositor_create_surface(demo->compositor);
2605 if (!demo->window) {
2606 printf("Can not create wayland_surface from compositor!\n");
2607 fflush(stdout);
2608 exit(1);
2609 }
2610
2611 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2612 if (!demo->shell_surface) {
2613 printf("Can not get shell_surface from wayland_surface!\n");
2614 fflush(stdout);
2615 exit(1);
2616 }
Dave Houlton5fa47912018-02-16 11:02:26 -07002617 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002618 wl_shell_surface_set_toplevel(demo->shell_surface);
2619 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2620}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002621#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2622static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002623 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002624
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002625 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002626 demo->curFrame++;
2627}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002628#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002629#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2630static VkResult demo_create_display_surface(struct demo *demo) {
2631 VkResult U_ASSERT_ONLY err;
2632 uint32_t display_count;
2633 uint32_t mode_count;
2634 uint32_t plane_count;
2635 VkDisplayPropertiesKHR display_props;
2636 VkDisplayKHR display;
2637 VkDisplayModePropertiesKHR mode_props;
2638 VkDisplayPlanePropertiesKHR *plane_props;
2639 VkBool32 found_plane = VK_FALSE;
2640 uint32_t plane_index;
2641 VkExtent2D image_extent;
2642 VkDisplaySurfaceCreateInfoKHR create_info;
2643
2644 // Get the first display
2645 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2646 assert(!err);
2647
2648 if (display_count == 0) {
2649 printf("Cannot find any display!\n");
2650 fflush(stdout);
2651 exit(1);
2652 }
2653
2654 display_count = 1;
2655 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2656 assert(!err || (err == VK_INCOMPLETE));
2657
2658 display = display_props.display;
2659
2660 // Get the first mode of the display
2661 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2662 assert(!err);
2663
2664 if (mode_count == 0) {
2665 printf("Cannot find any mode for the display!\n");
2666 fflush(stdout);
2667 exit(1);
2668 }
2669
2670 mode_count = 1;
2671 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2672 assert(!err || (err == VK_INCOMPLETE));
2673
2674 // Get the list of planes
2675 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2676 assert(!err);
2677
2678 if (plane_count == 0) {
2679 printf("Cannot find any plane!\n");
2680 fflush(stdout);
2681 exit(1);
2682 }
2683
2684 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2685 assert(plane_props);
2686
2687 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2688 assert(!err);
2689
2690 // Find a plane compatible with the display
2691 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2692 uint32_t supported_count;
2693 VkDisplayKHR *supported_displays;
2694
2695 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002696 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002697 continue;
2698 }
2699
2700 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2701 assert(!err);
2702
2703 if (supported_count == 0) {
2704 continue;
2705 }
2706
2707 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2708 assert(supported_displays);
2709
2710 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2711 assert(!err);
2712
2713 for (uint32_t i = 0; i < supported_count; i++) {
2714 if (supported_displays[i] == display) {
2715 found_plane = VK_TRUE;
2716 break;
2717 }
2718 }
2719
2720 free(supported_displays);
2721
2722 if (found_plane) {
2723 break;
2724 }
2725 }
2726
2727 if (!found_plane) {
2728 printf("Cannot find a plane compatible with the display!\n");
2729 fflush(stdout);
2730 exit(1);
2731 }
2732
2733 free(plane_props);
2734
Tony Barbour820b55b2017-03-14 08:55:46 -06002735 VkDisplayPlaneCapabilitiesKHR planeCaps;
2736 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2737 // Find a supported alpha mode
Wladimir J. van der Laan8ec4fce2017-10-07 14:17:41 +02002738 VkDisplayPlaneAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2739 VkDisplayPlaneAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002740 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2741 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2742 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2743 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2744 };
2745 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2746 if (planeCaps.supportedAlpha & alphaModes[i]) {
2747 alphaMode = alphaModes[i];
2748 break;
2749 }
2750 }
Damien Leone600c3052017-01-31 10:26:07 -07002751 image_extent.width = mode_props.parameters.visibleRegion.width;
2752 image_extent.height = mode_props.parameters.visibleRegion.height;
2753
2754 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2755 create_info.pNext = NULL;
2756 create_info.flags = 0;
2757 create_info.displayMode = mode_props.displayMode;
2758 create_info.planeIndex = plane_index;
2759 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2760 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002761 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002762 create_info.globalAlpha = 1.0f;
2763 create_info.imageExtent = image_extent;
2764
2765 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2766}
2767
Dave Houlton5fa47912018-02-16 11:02:26 -07002768static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07002769 while (!demo->quit) {
2770 demo_draw(demo);
2771 demo->curFrame++;
2772
2773 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2774 demo->quit = true;
2775 }
2776 }
2777}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002778#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002779
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002780/*
2781 * Return 1 (true) if all layer names specified in check_names
2782 * can be found in given layer properties.
2783 */
Dave Houlton5fa47912018-02-16 11:02:26 -07002784static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002785 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002786 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002787 for (uint32_t j = 0; j < layer_count; j++) {
2788 if (!strcmp(check_names[i], layers[j].layerName)) {
2789 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002790 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002791 }
2792 }
2793 if (!found) {
2794 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2795 return 0;
2796 }
2797 }
2798 return 1;
2799}
2800
Karl Schultze4dc75c2016-02-02 15:37:51 -07002801static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002802 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002803 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002804 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002805 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002806 char **instance_validation_layers = NULL;
2807 demo->enabled_extension_count = 0;
2808 demo->enabled_layer_count = 0;
Jeremy Kniager602e4182018-01-19 10:52:34 -07002809 demo->is_minimized = false;
2810 demo->cmd_pool = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002811
Dave Houlton5fa47912018-02-16 11:02:26 -07002812 char *instance_validation_layers_alt1[] = {"VK_LAYER_LUNARG_standard_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06002813
Dave Houlton5fa47912018-02-16 11:02:26 -07002814 char *instance_validation_layers_alt2[] = {"VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2815 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2816 "VK_LAYER_GOOGLE_unique_objects"};
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002817
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002818 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002819 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002820 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002821 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002822 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002823
Karl Schultz7c50ad62016-04-01 12:07:03 -06002824 instance_validation_layers = instance_validation_layers_alt1;
2825 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002826 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2827 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002828 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002829
Dave Houlton5fa47912018-02-16 11:02:26 -07002830 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt1), instance_validation_layers,
2831 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002832 if (validation_found) {
2833 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002834 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2835 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002836 } else {
2837 // use alternative set of validation layers
2838 instance_validation_layers = instance_validation_layers_alt2;
2839 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Dave Houlton5fa47912018-02-16 11:02:26 -07002840 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt2), instance_validation_layers,
2841 instance_layer_count, instance_layers);
2842 validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002843 for (uint32_t i = 0; i < validation_layer_count; i++) {
2844 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002845 }
2846 }
2847 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002848 }
Dustin Graves7c287352016-01-27 13:48:06 -07002849
Karl Schultz7c50ad62016-04-01 12:07:03 -06002850 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002851 ERR_EXIT(
2852 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
2853 "Please look at the Getting Started guide for additional information.\n",
2854 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06002855 }
Dustin Graves7c287352016-01-27 13:48:06 -07002856 }
2857
2858 /* Look for instance extensions */
2859 VkBool32 surfaceExtFound = 0;
2860 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002861 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002862
Dave Houlton5fa47912018-02-16 11:02:26 -07002863 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002864 assert(!err);
2865
Dustin Graves7c287352016-01-27 13:48:06 -07002866 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002867 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2868 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07002869 assert(!err);
2870 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002871 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002872 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002873 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002874 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002875#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002876 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002877 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002878 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002879 }
Tony Barbour153cb062016-12-07 13:43:36 -07002880#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002881 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06002882 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002883 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06002884 }
Tony Barbour153cb062016-12-07 13:43:36 -07002885#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002886 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002887 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002888 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002889 }
Tony Barbour153cb062016-12-07 13:43:36 -07002890#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002891 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002892 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002893 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002894 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002895#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002896#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002897 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07002898 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002899 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07002900 }
Tony Barbour153cb062016-12-07 13:43:36 -07002901#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002902 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002903 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002904 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002905 }
Bill Hollingsf4352142017-02-14 22:58:56 -05002906#elif defined(VK_USE_PLATFORM_IOS_MVK)
2907 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2908 platformSurfaceExtFound = 1;
2909 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
2910 }
2911#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2912 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2913 platformSurfaceExtFound = 1;
2914 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
2915 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002916#endif
Dave Houlton5fa47912018-02-16 11:02:26 -07002917 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002918 if (demo->validate) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002919 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002920 }
2921 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002922 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002923 }
Dustin Graves7c287352016-01-27 13:48:06 -07002924
2925 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002926 }
Dustin Graves7c287352016-01-27 13:48:06 -07002927
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002928 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002929 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
2930 " extension.\n\n"
2931 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2932 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06002933 "vkCreateInstance Failure");
2934 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002935 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002936#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002937 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2938 " extension.\n\n"
2939 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2940 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002941 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002942#elif defined(VK_USE_PLATFORM_IOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07002943 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_IOS_SURFACE_EXTENSION_NAME
2944 " extension.\n\n"
2945 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2946 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06002947 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002948#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07002949 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_MACOS_SURFACE_EXTENSION_NAME
2950 " extension.\n\n"
2951 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2952 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06002953 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002954#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002955 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2956 " extension.\n\n"
2957 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2958 "Please look at the Getting Started guide for additional information.\n",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002959 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002960#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002961 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
2962 " extension.\n\n"
2963 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2964 "Please look at the Getting Started guide for additional information.\n",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002965 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002966#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002967#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002968 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
2969 " extension.\n\n"
2970 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2971 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07002972 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002973#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002974 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2975 " extension.\n\n"
2976 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2977 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002978 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07002979#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002980 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2981 " extension.\n\n"
2982 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2983 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06002984 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002985#endif
Tony Barbour153cb062016-12-07 13:43:36 -07002986 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002987 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002988 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002989 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002990 .pApplicationName = APP_SHORT_NAME,
2991 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002992 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002993 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002994 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002995 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002996 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002997 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002998 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002999 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003000 .enabledLayerCount = demo->enabled_layer_count,
3001 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3002 .enabledExtensionCount = demo->enabled_extension_count,
3003 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003004 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003005
3006 /*
3007 * This is info for a temp callback to use during CreateInstance.
3008 * After the instance is created, we use the instance-based
3009 * function to register the final callback.
3010 */
Karl Schultza2615462017-01-20 13:19:20 -07003011 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003012 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003013 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07003014 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
3015 dbgCreateInfoTemp.pNext = NULL;
3016 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
3017 dbgCreateInfoTemp.pUserData = demo;
Dave Houlton5fa47912018-02-16 11:02:26 -07003018 dbgCreateInfoTemp.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003019 if (demo->validate_checks_disabled) {
3020 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
3021 val_flags.pNext = NULL;
3022 val_flags.disabledValidationCheckCount = 1;
3023 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
3024 val_flags.pDisabledValidationChecks = &disabled_check;
Dave Houlton5fa47912018-02-16 11:02:26 -07003025 dbgCreateInfoTemp.pNext = (void *)&val_flags;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003026 }
Karl Schultza2615462017-01-20 13:19:20 -07003027 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003028 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003029
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003030 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003031
Chia-I Wu63636062015-10-26 21:10:41 +08003032 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003033 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003034 ERR_EXIT(
3035 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3036 "Please look at the Getting Started guide for additional information.\n",
3037 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003038 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003039 ERR_EXIT(
3040 "Cannot find a specified extension library.\n"
3041 "Make sure your layers path is set appropriately.\n",
3042 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003043 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003044 ERR_EXIT(
3045 "vkCreateInstance failed.\n\n"
3046 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3047 "Please look at the Getting Started guide for additional information.\n",
3048 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003049 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003050
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003051 /* Make initial call to query gpu_count, then second call for gpu info*/
3052 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3053 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003054
3055 if (gpu_count > 0) {
3056 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3057 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3058 assert(!err);
3059 /* For cube demo we just grab the first physical device */
3060 demo->gpu = physical_devices[0];
3061 free(physical_devices);
3062 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003063 ERR_EXIT(
3064 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3065 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3066 "Please look at the Getting Started guide for additional information.\n",
3067 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003068 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003069
Dustin Graves7c287352016-01-27 13:48:06 -07003070 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003071 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003072 VkBool32 swapchainExtFound = 0;
3073 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003074 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003075
Dave Houlton5fa47912018-02-16 11:02:26 -07003076 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003077 assert(!err);
3078
Dustin Graves7c287352016-01-27 13:48:06 -07003079 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003080 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3081 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003082 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003083
Dustin Graves7c287352016-01-27 13:48:06 -07003084 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003085 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003086 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003087 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003088 }
3089 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003090 }
Dustin Graves7c287352016-01-27 13:48:06 -07003091
Ian Elliott53622a72017-03-28 11:06:33 -06003092 if (demo->VK_KHR_incremental_present_enabled) {
3093 // Even though the user "enabled" the extension via the command
3094 // line, we must make sure that it's enumerated for use with the
3095 // device. Therefore, disable it here, and re-enable it again if
3096 // enumerated.
3097 demo->VK_KHR_incremental_present_enabled = false;
3098 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003099 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3100 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003101 demo->VK_KHR_incremental_present_enabled = true;
3102 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3103 }
3104 assert(demo->enabled_extension_count < 64);
3105 }
3106 if (!demo->VK_KHR_incremental_present_enabled) {
3107 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3108 }
3109 }
3110
Ian Elliottd940ca22017-03-22 08:31:27 -06003111 if (demo->VK_GOOGLE_display_timing_enabled) {
3112 // Even though the user "enabled" the extension via the command
3113 // line, we must make sure that it's enumerated for use with the
3114 // device. Therefore, disable it here, and re-enable it again if
3115 // enumerated.
3116 demo->VK_GOOGLE_display_timing_enabled = false;
3117 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003118 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3119 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003120 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003121 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003122 }
3123 assert(demo->enabled_extension_count < 64);
3124 }
3125 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003126 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003127 }
3128 }
3129
Dustin Graves7c287352016-01-27 13:48:06 -07003130 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003131 }
Dustin Graves7c287352016-01-27 13:48:06 -07003132
Tony Barbourcc69f452015-10-08 13:59:42 -06003133 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003134 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3135 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3136 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003137 "vkCreateInstance Failure");
3138 }
3139
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003140 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003141 demo->CreateDebugReportCallback =
Dave Houlton5fa47912018-02-16 11:02:26 -07003142 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugReportCallbackEXT");
Karl Schultze4dc75c2016-02-02 15:37:51 -07003143 demo->DestroyDebugReportCallback =
Dave Houlton5fa47912018-02-16 11:02:26 -07003144 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003145 if (!demo->CreateDebugReportCallback) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003146 ERR_EXIT("GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n", "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003147 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003148 if (!demo->DestroyDebugReportCallback) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003149 ERR_EXIT("GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n", "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003150 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003151 demo->DebugReportMessage = (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003152 if (!demo->DebugReportMessage) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003153 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n", "vkGetProcAddr Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003154 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003155
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003156 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003157 PFN_vkDebugReportCallbackEXT callback;
3158 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003159 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003160 dbgCreateInfo.pNext = NULL;
3161 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003162 dbgCreateInfo.pUserData = demo;
Dave Houlton5fa47912018-02-16 11:02:26 -07003163 dbgCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
3164 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL, &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003165 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003166 case VK_SUCCESS:
3167 break;
3168 case VK_ERROR_OUT_OF_HOST_MEMORY:
3169 ERR_EXIT("CreateDebugReportCallback: out of host memory\n", "CreateDebugReportCallback Failure");
3170 break;
3171 default:
3172 ERR_EXIT("CreateDebugReportCallback: unknown failure\n", "CreateDebugReportCallback Failure");
3173 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003174 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003175 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003176 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003177
3178 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003179 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003180 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003181
Dave Houlton5fa47912018-02-16 11:02:26 -07003182 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3183 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003184
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003185 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003186 // If app has specific feature requirements it should check supported
3187 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003188 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003189 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003190
Tony Barbourda2bad52016-01-22 14:36:40 -07003191 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3192 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3193 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3194 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3195 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3196}
3197
Karl Schultze4dc75c2016-02-02 15:37:51 -07003198static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003199 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003200 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003201 VkDeviceQueueCreateInfo queues[2];
3202 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3203 queues[0].pNext = NULL;
3204 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3205 queues[0].queueCount = 1;
3206 queues[0].pQueuePriorities = queue_priorities;
3207 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003208
3209 VkDeviceCreateInfo device = {
3210 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3211 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003212 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003213 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003214 .enabledLayerCount = 0,
3215 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003216 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003217 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003218 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003219 };
Tony Barbour22e06272016-09-07 16:07:56 -06003220 if (demo->separate_present_queue) {
3221 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3222 queues[1].pNext = NULL;
3223 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3224 queues[1].queueCount = 1;
3225 queues[1].pQueuePriorities = queue_priorities;
3226 queues[1].flags = 0;
3227 device.queueCreateInfoCount = 2;
3228 }
Chia-I Wu63636062015-10-26 21:10:41 +08003229 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003230 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003231}
3232
Karl Schultze4dc75c2016-02-02 15:37:51 -07003233static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003234 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003235
Karl Schultze4dc75c2016-02-02 15:37:51 -07003236// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003237#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003238 VkWin32SurfaceCreateInfoKHR createInfo;
3239 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3240 createInfo.pNext = NULL;
3241 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003242 createInfo.hinstance = demo->connection;
3243 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003244
Dave Houlton5fa47912018-02-16 11:02:26 -07003245 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003246#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003247 VkWaylandSurfaceCreateInfoKHR createInfo;
3248 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3249 createInfo.pNext = NULL;
3250 createInfo.flags = 0;
3251 createInfo.display = demo->display;
3252 createInfo.surface = demo->window;
3253
Dave Houlton5fa47912018-02-16 11:02:26 -07003254 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003255#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003256#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3257 VkAndroidSurfaceCreateInfoKHR createInfo;
3258 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3259 createInfo.pNext = NULL;
3260 createInfo.flags = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003261 createInfo.window = (ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003262
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003263 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003264#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3265 VkXlibSurfaceCreateInfoKHR createInfo;
3266 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3267 createInfo.pNext = NULL;
3268 createInfo.flags = 0;
3269 createInfo.dpy = demo->display;
3270 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003271
Dave Houlton5fa47912018-02-16 11:02:26 -07003272 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003273#elif defined(VK_USE_PLATFORM_XCB_KHR)
3274 VkXcbSurfaceCreateInfoKHR createInfo;
3275 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3276 createInfo.pNext = NULL;
3277 createInfo.flags = 0;
3278 createInfo.connection = demo->connection;
3279 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003280
Tony Barbour153cb062016-12-07 13:43:36 -07003281 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003282#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3283 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003284#elif defined(VK_USE_PLATFORM_IOS_MVK)
3285 VkIOSSurfaceCreateInfoMVK surface;
3286 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3287 surface.pNext = NULL;
3288 surface.flags = 0;
3289 surface.pView = demo->window;
3290
3291 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3292#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3293 VkMacOSSurfaceCreateInfoMVK surface;
3294 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3295 surface.pNext = NULL;
3296 surface.flags = 0;
3297 surface.pView = demo->window;
3298
3299 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003300#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003301 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003302
Tony Barbourcc69f452015-10-08 13:59:42 -06003303 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003304 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003305 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003306 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003307 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003308
3309 // Search for a graphics and a present queue in the array of queue
3310 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003311 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3312 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003313 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003314 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3315 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3316 graphicsQueueFamilyIndex = i;
3317 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003318
Tony Barbourab2a0362016-09-06 11:40:12 -06003319 if (supportsPresent[i] == VK_TRUE) {
3320 graphicsQueueFamilyIndex = i;
3321 presentQueueFamilyIndex = i;
3322 break;
3323 }
3324 }
3325 }
3326
3327 if (presentQueueFamilyIndex == UINT32_MAX) {
3328 // If didn't find a queue that supports both graphics and present, then
3329 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003330 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003331 if (supportsPresent[i] == VK_TRUE) {
3332 presentQueueFamilyIndex = i;
3333 break;
3334 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003335 }
3336 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003337
3338 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003339 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3340 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003341 }
3342
Tony Barbourab2a0362016-09-06 11:40:12 -06003343 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3344 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003345 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003346 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003347
Tony Barbourda2bad52016-01-22 14:36:40 -07003348 demo_create_device(demo);
3349
3350 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3351 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3352 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3353 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3354 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003355 if (demo->VK_GOOGLE_display_timing_enabled) {
3356 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3357 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3358 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003359
Dave Houlton5fa47912018-02-16 11:02:26 -07003360 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003361
Tony Barbour22e06272016-09-07 16:07:56 -06003362 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003363 demo->present_queue = demo->graphics_queue;
3364 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003365 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003366 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003367
Ian Elliottaaae5352015-07-06 14:27:58 -06003368 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003369 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003370 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003371 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003372 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3373 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003374 assert(!err);
3375 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3376 // the surface has no preferred format. Otherwise, at least one
3377 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003378 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003379 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003380 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003381 assert(formatCount >= 1);
3382 demo->format = surfFormats[0].format;
3383 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003384 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003385
David Pinedo2bb7c932015-06-18 17:03:14 -06003386 demo->quit = false;
3387 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003388
Tony Barbource7524e2016-07-28 12:01:45 -06003389 // Create semaphores to synchronize acquiring presentable buffers before
3390 // rendering and waiting for drawing to be complete before presenting
3391 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3392 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3393 .pNext = NULL,
3394 .flags = 0,
3395 };
Tony Barbource7524e2016-07-28 12:01:45 -06003396
Tony Barbour66a56c32016-09-21 13:10:56 -06003397 // Create fences that we can use to throttle if we get too far
3398 // ahead of the image presents
3399 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003400 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003401 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003402 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3403 assert(!err);
3404
Dave Houlton5fa47912018-02-16 11:02:26 -07003405 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003406 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003407
Dave Houlton5fa47912018-02-16 11:02:26 -07003408 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003409 assert(!err);
3410
3411 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003412 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003413 assert(!err);
3414 }
Tony Barbour22e06272016-09-07 16:07:56 -06003415 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003416 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003417
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003418 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003419 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003420}
3421
Tony Barbour153cb062016-12-07 13:43:36 -07003422#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003423static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3424 wl_fixed_t sy) {}
3425
3426static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3427
3428static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3429
3430static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3431 uint32_t state) {
3432 struct demo *demo = data;
3433 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3434 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
3435 }
3436}
3437
3438static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3439
3440static const struct wl_pointer_listener pointer_listener = {
3441 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3442};
3443
3444static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3445
3446static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3447 struct wl_array *keys) {}
3448
3449static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3450
3451static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3452 uint32_t state) {
3453 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3454 struct demo *demo = data;
3455 switch (key) {
3456 case KEY_ESC: // Escape
3457 demo->quit = true;
3458 break;
3459 case KEY_LEFT: // left arrow key
3460 demo->spin_angle -= demo->spin_increment;
3461 break;
3462 case KEY_RIGHT: // right arrow key
3463 demo->spin_angle += demo->spin_increment;
3464 break;
3465 case KEY_SPACE: // space bar
3466 demo->pause = !demo->pause;
3467 break;
3468 }
3469}
3470
3471static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3472 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3473
3474static const struct wl_keyboard_listener keyboard_listener = {
3475 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3476};
3477
3478static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3479 // Subscribe to pointer events
3480 struct demo *demo = data;
3481 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3482 demo->pointer = wl_seat_get_pointer(seat);
3483 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3484 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3485 wl_pointer_destroy(demo->pointer);
3486 demo->pointer = NULL;
3487 }
3488 // Subscribe to keyboard events
3489 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3490 demo->keyboard = wl_seat_get_keyboard(seat);
3491 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3492 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3493 wl_keyboard_destroy(demo->keyboard);
3494 demo->keyboard = NULL;
3495 }
3496}
3497
3498static const struct wl_seat_listener seat_listener = {
3499 seat_handle_capabilities,
3500};
3501
3502static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003503 uint32_t version UNUSED) {
3504 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003505 // pickup wayland objects when they appear
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003506 if (strcmp(interface, "wl_compositor") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003507 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003508 } else if (strcmp(interface, "wl_shell") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003509 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3510 } else if (strcmp(interface, "wl_seat") == 0) {
3511 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3512 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003513 }
3514}
3515
Dave Houlton5fa47912018-02-16 11:02:26 -07003516static 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 +09003517
Dave Houlton5fa47912018-02-16 11:02:26 -07003518static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003519#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003520#endif
3521
Karl Schultze4dc75c2016-02-02 15:37:51 -07003522static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003523#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003524 const xcb_setup_t *setup;
3525 xcb_screen_iterator_t iter;
3526 int scr;
3527
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003528 const char *display_envar = getenv("DISPLAY");
3529 if (display_envar == NULL || display_envar[0] == '\0') {
3530 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3531 fflush(stdout);
3532 exit(1);
3533 }
3534
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003535 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003536 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003537 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003538 fflush(stdout);
3539 exit(1);
3540 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003541
3542 setup = xcb_get_setup(demo->connection);
3543 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003544 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003545
3546 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003547#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3548 demo->display = wl_display_connect(NULL);
3549
3550 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003551 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003552 fflush(stdout);
3553 exit(1);
3554 }
3555
3556 demo->registry = wl_display_get_registry(demo->display);
3557 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3558 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003559#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003560#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003561}
3562
Karl Schultze4dc75c2016-02-02 15:37:51 -07003563static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003564 vec3 eye = {0.0f, 3.0f, 5.0f};
3565 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003566 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003567
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003568 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003569 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003570 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003571
Piers Daniell735ee532015-02-23 16:23:13 -07003572 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003573 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003574 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003575 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003576 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003577 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3578 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003579 i++;
3580 continue;
3581 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003582 if (strcmp(argv[i], "--break") == 0) {
3583 demo->use_break = true;
3584 continue;
3585 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003586 if (strcmp(argv[i], "--validate") == 0) {
3587 demo->validate = true;
3588 continue;
3589 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003590 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3591 demo->validate = true;
3592 demo->validate_checks_disabled = true;
3593 continue;
3594 }
Tony Barbour2593b182016-04-19 10:57:58 -06003595 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003596 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003597 continue;
3598 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003599 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3600 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003601 i++;
3602 continue;
3603 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003604 if (strcmp(argv[i], "--suppress_popups") == 0) {
3605 demo->suppress_popups = true;
3606 continue;
3607 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003608 if (strcmp(argv[i], "--display_timing") == 0) {
3609 demo->VK_GOOGLE_display_timing_enabled = true;
3610 continue;
3611 }
Ian Elliott53622a72017-03-28 11:06:33 -06003612 if (strcmp(argv[i], "--incremental_present") == 0) {
3613 demo->VK_KHR_incremental_present_enabled = true;
3614 continue;
3615 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003616
Cody Northropaf28a532016-09-27 10:50:57 -06003617#if defined(ANDROID)
3618 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3619#else
Mike Schuchardt9a881512017-11-01 16:16:22 -06003620 fprintf(stderr,
3621 "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled]\n"
3622 " [--break] [--c <framecount>] [--suppress_popups]\n"
3623 " [--incremental_present] [--display_timing]\n"
3624 " [--present_mode {0,1,2,3}]\n"
3625 "\n"
3626 "Options for --present_mode:\n"
3627 " %d: VK_PRESENT_MODE_IMMEDIATE_KHR\n"
3628 " %d: VK_PRESENT_MODE_MAILBOX_KHR\n"
3629 " %d: VK_PRESENT_MODE_FIFO_KHR (default)\n"
3630 " %d: VK_PRESENT_MODE_FIFO_RELAXED_KHR\n",
3631 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
3632 VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003633 fflush(stderr);
3634 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003635#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003636 }
3637
Tony Barbour153cb062016-12-07 13:43:36 -07003638 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003639
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003640 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003641
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003642 demo->width = 500;
3643 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003644
Tony Barbour66a56c32016-09-21 13:10:56 -06003645 demo->spin_angle = 4.0f;
3646 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003647 demo->pause = false;
3648
Dave Houlton5fa47912018-02-16 11:02:26 -07003649 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003650 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3651 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003652
Dave Houlton5fa47912018-02-16 11:02:26 -07003653 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003654}
3655
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003656#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003657// Include header required for parsing the command line options.
3658#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003659
Dave Houlton5fa47912018-02-16 11:02:26 -07003660int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3661 MSG msg; // message
3662 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003663 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003664 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003665
Jamie Madillb2ff6502017-03-15 16:17:46 -04003666 // Ensure wParam is initialized.
3667 msg.wParam = 0;
3668
Karl Schultze4dc75c2016-02-02 15:37:51 -07003669 // Use the CommandLine functions to get the command line arguments.
3670 // Unfortunately, Microsoft outputs
3671 // this information as wide characters for Unicode, and we simply want the
3672 // Ascii version to be compatible
3673 // with the non-Windows side. So, we have to convert the information to
3674 // Ascii character strings.
3675 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3676 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003677 argc = 0;
3678 }
3679
Karl Schultze4dc75c2016-02-02 15:37:51 -07003680 if (argc > 0) {
3681 argv = (char **)malloc(sizeof(char *) * argc);
3682 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003683 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003684 } else {
3685 for (int iii = 0; iii < argc; iii++) {
3686 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003687 size_t numConverted = 0;
3688
Karl Schultze4dc75c2016-02-02 15:37:51 -07003689 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3690 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003691 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003692 }
3693 }
3694 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003695 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003696 argv = NULL;
3697 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003698
3699 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003700
3701 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003702 if (argc > 0 && argv != NULL) {
3703 for (int iii = 0; iii < argc; iii++) {
3704 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003705 free(argv[iii]);
3706 }
3707 }
3708 free(argv);
3709 }
3710
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003711 demo.connection = hInstance;
3712 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003713 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003714 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003715
3716 demo_prepare(&demo);
3717
Dave Houlton5fa47912018-02-16 11:02:26 -07003718 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003719
3720 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003721 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003722 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003723 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003724 {
Dave Houlton5fa47912018-02-16 11:02:26 -07003725 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07003726 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003727 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003728 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003729 DispatchMessage(&msg);
3730 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003731 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003732 }
3733
3734 demo_cleanup(&demo);
3735
Karl Schultze4dc75c2016-02-02 15:37:51 -07003736 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003737}
Bill Hollingsf4352142017-02-14 22:58:56 -05003738
3739#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003740static void demo_main(struct demo *demo, void *view) {
3741 const char *argv[] = {"CubeSample"};
3742 int argc = sizeof(argv) / sizeof(char *);
Bill Hollingsf4352142017-02-14 22:58:56 -05003743
Dave Houlton5fa47912018-02-16 11:02:26 -07003744 demo_init(demo, argc, (char **)argv);
Tony Barbourc65cd752017-03-13 15:29:35 -06003745 demo->window = view;
3746 demo_init_vk_swapchain(demo);
3747 demo_prepare(demo);
3748 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003749}
3750
3751static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003752 // Wait for work to finish before updating MVP.
3753 vkDeviceWaitIdle(demo->device);
3754 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003755
Tony Barbourc65cd752017-03-13 15:29:35 -06003756 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003757}
3758
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003759#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3760#include <android/log.h>
3761#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003762#include "android_util.h"
3763
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003764static bool initialized = false;
3765static bool active = false;
3766struct demo demo;
3767
Dave Houlton5fa47912018-02-16 11:02:26 -07003768static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003769
Dave Houlton5fa47912018-02-16 11:02:26 -07003770static void processCommand(struct android_app *app, int32_t cmd) {
3771 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003772 case APP_CMD_INIT_WINDOW: {
3773 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003774 // We're getting a new window. If the app is starting up, we
3775 // need to initialize. If the app has already been
3776 // initialized, that means that we lost our previous window,
3777 // which means that we have a lot of work to do. At a minimum,
3778 // we need to destroy the swapchain and surface associated with
3779 // the old window, and create a new surface and swapchain.
3780 // However, since there are a lot of other objects/state that
3781 // is tied to the swapchain, it's easiest to simply cleanup and
3782 // start over (i.e. use a brute-force approach of re-starting
3783 // the app)
3784 if (demo.prepared) {
3785 demo_cleanup(&demo);
3786 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003787
3788 // Parse Intents into argc, argv
3789 // Use the following key to send arguments, i.e.
3790 // --es args "--validate"
3791 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07003792 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003793 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003794 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003795
3796 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07003797 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 -06003798
3799 demo_init(&demo, argc, argv);
3800
3801 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07003802 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003803
Dave Houlton5fa47912018-02-16 11:02:26 -07003804 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003805 demo_init_vk_swapchain(&demo);
3806 demo_prepare(&demo);
3807 initialized = true;
3808 }
3809 break;
3810 }
3811 case APP_CMD_GAINED_FOCUS: {
3812 active = true;
3813 break;
3814 }
3815 case APP_CMD_LOST_FOCUS: {
3816 active = false;
3817 break;
3818 }
3819 }
3820}
3821
Dave Houlton5fa47912018-02-16 11:02:26 -07003822void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06003823#ifdef ANDROID
3824 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07003825 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06003826#endif
3827
Ian Elliottf05d5d12016-05-17 12:03:35 -06003828 demo.prepared = false;
3829
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003830 app->onAppCmd = processCommand;
3831 app->onInputEvent = processInput;
3832
Dave Houlton5fa47912018-02-16 11:02:26 -07003833 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003834 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07003835 struct android_poll_source *source;
3836 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003837 if (source) {
3838 source->process(app, source);
3839 }
3840
3841 if (app->destroyRequested != 0) {
3842 demo_cleanup(&demo);
3843 return;
3844 }
3845 }
3846 if (initialized && active) {
3847 demo_run(&demo);
3848 }
3849 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003850}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003851#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003852int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003853 struct demo demo;
3854
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003855 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003856#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003857 demo_create_xcb_window(&demo);
3858#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3859 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003860#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3861 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003862#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003863#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003864
Tony Barbourcc69f452015-10-08 13:59:42 -06003865 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003866
3867 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003868
Tony Barbour153cb062016-12-07 13:43:36 -07003869#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003870 demo_run_xcb(&demo);
3871#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3872 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003873#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3874 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003875#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003876#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3877 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003878#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003879
3880 demo_cleanup(&demo);
3881
Karl Schultz0218c002016-03-22 17:06:13 -06003882 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003883}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003884#endif