blob: d322f6ce36de5430decbcfaa269805c47dd825b1 [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;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600353
Ian Elliott53622a72017-03-28 11:06:33 -0600354 bool VK_KHR_incremental_present_enabled;
355
Ian Elliottd940ca22017-03-22 08:31:27 -0600356 bool VK_GOOGLE_display_timing_enabled;
357 bool syncd_with_actual_presents;
358 uint64_t refresh_duration;
359 uint64_t refresh_duration_multiplier;
360 uint64_t target_IPD; // image present duration (inverse of frame rate)
361 uint64_t prev_desired_present_time;
362 uint32_t next_present_id;
363 uint32_t last_early_id; // 0 if no early images
364 uint32_t last_late_id; // 0 if no late images
365
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600367 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600368 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600369 VkQueue graphics_queue;
370 VkQueue present_queue;
371 uint32_t graphics_queue_family_index;
372 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600373 VkSemaphore image_acquired_semaphores[FRAME_LAG];
374 VkSemaphore draw_complete_semaphores[FRAME_LAG];
375 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600376 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600377 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600378 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379
Tony Barbourda2bad52016-01-22 14:36:40 -0700380 uint32_t enabled_extension_count;
381 uint32_t enabled_layer_count;
382 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600383 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700384
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600385 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600386 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600387 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700389 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
390 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
391 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
392 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600393 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
394 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
395 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
396 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
397 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600398 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
399 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600400 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600401 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700402 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600403 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600404 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600405 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600406
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800407 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600408 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600409
410 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600411 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600412
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600413 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800414 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500415 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600416 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600417 } depth;
418
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600420 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600421
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700422 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500423 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600424 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600425 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800426 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600427 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600428
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600429 mat4x4 projection_matrix;
430 mat4x4 view_matrix;
431 mat4x4 model_matrix;
432
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600433 float spin_angle;
434 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600435 bool pause;
436
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600437 VkShaderModule vert_shader_module;
438 VkShaderModule frag_shader_module;
439
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600440 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800441
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600442 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600443 int32_t curFrame;
444 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600445 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600446 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600447 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600448 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700449 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
450 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
451 VkDebugReportCallbackEXT msg_callback;
452 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600453
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600454 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600455 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600456};
457
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700458VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
459 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600460 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600461 char *message = (char *)malloc(strlen(pMsg) + 100);
462
463 assert(message);
464
Cody Northropaf28a532016-09-27 10:50:57 -0600465 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
466 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600467 validation_error = 1;
468 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600469 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
470 validation_error = 1;
471 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600472 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600473 validation_error = 1;
474 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
475 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
476 validation_error = 1;
477 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
478 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600479 validation_error = 1;
480 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600481 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600482 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600483 }
484
485#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600486
Tony Barbour602ca4f2016-09-12 14:02:43 -0600487 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600488 struct demo *demo = (struct demo*) pUserData;
489 if (!demo->suppress_popups)
490 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600491 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600492
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600493#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600494
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600495 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600496 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600497 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600498 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600499 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600500 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600501 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600502 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600503 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600504 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600505 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600506 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600507 }
Cody Northropaf28a532016-09-27 10:50:57 -0600508
lenny-lunargfbe22392016-06-06 11:07:53 -0600509#else
Cody Northropaf28a532016-09-27 10:50:57 -0600510
lenny-lunargfbe22392016-06-06 11:07:53 -0600511 printf("%s\n", message);
512 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600513
lenny-lunargfbe22392016-06-06 11:07:53 -0600514#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600515
lenny-lunargfbe22392016-06-06 11:07:53 -0600516 free(message);
517
Joey Bzdek15eb0702017-06-07 09:40:36 -0600518 // clang-format on
Cody Northropaf28a532016-09-27 10:50:57 -0600519
lenny-lunargfbe22392016-06-06 11:07:53 -0600520 /*
Dave Houlton5fa47912018-02-16 11:02:26 -0700521 * false indicates that layer should not bail-out of an
522 * API call that had validation failures. This may mean that the
523 * app dies inside the driver due to invalid parameter(s).
524 * That's what would happen without validation layers, so we'll
525 * keep that behavior here.
526 */
lenny-lunargfbe22392016-06-06 11:07:53 -0600527 return false;
528}
529
Ian Elliottd940ca22017-03-22 08:31:27 -0600530bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
531 // The desired time was the earliest time that the present should have
532 // occured. In almost every case, the actual time should be later than the
533 // desired time. We should only consider the actual time "late" if it is
534 // after "desired + rdur".
535 if (actual <= desired) {
536 // The actual time was before or equal to the desired time. This will
537 // probably never happen, but in case it does, return false since the
538 // present was obviously NOT late.
539 return false;
540 }
541 uint64_t deadline = actual + rdur;
542 if (actual > deadline) {
543 return true;
544 } else {
545 return false;
546 }
547}
Dave Houlton5fa47912018-02-16 11:02:26 -0700548bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600549 if (earliest < actual) {
550 // Consider whether this present could have occured earlier. Make sure
551 // that earliest time was at least 2msec earlier than actual time, and
552 // that the margin was at least 2msec:
553 uint64_t diff = actual - earliest;
554 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
555 // This present could have occured earlier because both: 1) the
556 // earliest time was at least 2 msec before actual time, and 2) the
557 // margin was at least 2msec.
558 return true;
559 }
560 }
561 return false;
562}
563
Ian Elliottcf074d32015-10-16 18:02:43 -0600564// Forward declaration:
565static void demo_resize(struct demo *demo);
566
Dave Houlton5fa47912018-02-16 11:02:26 -0700567static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700568 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600569 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700570 if ((typeBits & 1) == 1) {
571 // Type is available, does it match user properties?
Dave Houlton5fa47912018-02-16 11:02:26 -0700572 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700573 *typeIndex = i;
574 return true;
575 }
576 }
577 typeBits >>= 1;
578 }
579 // No memory types matched, return failure
580 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600581}
582
Karl Schultze4dc75c2016-02-02 15:37:51 -0700583static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600584 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585
Tony Barbource7524e2016-07-28 12:01:45 -0600586 // This function could get called twice if the texture uses a staging buffer
587 // In that case the second call should be ignored
Dave Houlton5fa47912018-02-16 11:02:26 -0700588 if (demo->cmd == VK_NULL_HANDLE) return;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600589
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600590 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600591 assert(!err);
592
Tony Barbource7524e2016-07-28 12:01:45 -0600593 VkFence fence;
Dave Houlton5fa47912018-02-16 11:02:26 -0700594 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700595 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
596 assert(!err);
597
Karl Schultze4dc75c2016-02-02 15:37:51 -0700598 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700599 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
600 .pNext = NULL,
601 .waitSemaphoreCount = 0,
602 .pWaitSemaphores = NULL,
603 .pWaitDstStageMask = NULL,
604 .commandBufferCount = 1,
605 .pCommandBuffers = cmd_bufs,
606 .signalSemaphoreCount = 0,
607 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600608
Tony Barbource7524e2016-07-28 12:01:45 -0600609 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600610 assert(!err);
611
Tony Barbource7524e2016-07-28 12:01:45 -0600612 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600613 assert(!err);
614
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600615 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600616 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600617 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600618}
619
Dave Houlton5fa47912018-02-16 11:02:26 -0700620static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
621 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
Tony Barbour5ff13182016-10-19 13:58:29 -0600622 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600623 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600624
Dave Houlton5fa47912018-02-16 11:02:26 -0700625 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
626 .pNext = NULL,
627 .srcAccessMask = srcAccessMask,
628 .dstAccessMask = 0,
629 .oldLayout = old_image_layout,
630 .newLayout = new_image_layout,
631 .image = image,
632 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600633
Tony Barboureb5077b2016-10-19 15:23:36 -0600634 switch (new_image_layout) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700635 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
636 /* Make sure anything that was copying from this image has completed */
637 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
638 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600639
Dave Houlton5fa47912018-02-16 11:02:26 -0700640 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
641 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
642 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700643
Dave Houlton5fa47912018-02-16 11:02:26 -0700644 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
645 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
646 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700647
Dave Houlton5fa47912018-02-16 11:02:26 -0700648 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
649 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
650 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600651
Dave Houlton5fa47912018-02-16 11:02:26 -0700652 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
653 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
654 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600655
Dave Houlton5fa47912018-02-16 11:02:26 -0700656 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
657 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
658 break;
Tony Barboureb5077b2016-10-19 15:23:36 -0600659
Dave Houlton5fa47912018-02-16 11:02:26 -0700660 default:
661 image_memory_barrier.dstAccessMask = 0;
662 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600663 }
664
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600665 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600666
Dave Houlton5fa47912018-02-16 11:02:26 -0700667 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600668}
669
Karl Schultze4dc75c2016-02-02 15:37:51 -0700670static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700671 const VkCommandBufferBeginInfo cmd_buf_info = {
672 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
673 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600674 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700675 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700676 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800677 const VkClearValue clear_values[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -0700678 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
679 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800680 };
681 const VkRenderPassBeginInfo rp_begin = {
682 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
683 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800684 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700685 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800686 .renderArea.offset.x = 0,
687 .renderArea.offset.y = 0,
688 .renderArea.extent.width = demo->width,
689 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600690 .clearValueCount = 2,
691 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700692 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800693 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600694
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600695 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600696 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800697 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700698 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
Dave Houlton5fa47912018-02-16 11:02:26 -0700699 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
700 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600701 VkViewport viewport;
702 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700703 viewport.height = (float)demo->height;
704 viewport.width = (float)demo->width;
705 viewport.minDepth = (float)0.0f;
706 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700707 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600708
709 VkRect2D scissor;
710 memset(&scissor, 0, sizeof(scissor));
711 scissor.extent.width = demo->width;
712 scissor.extent.height = demo->height;
713 scissor.offset.x = 0;
714 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700715 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600716 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600717 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600718 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800719 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600720
Tony Barbour22e06272016-09-07 16:07:56 -0600721 if (demo->separate_present_queue) {
722 // We have to transfer ownership from the graphics queue family to the
723 // present queue family to be able to present. Note that we don't have
724 // to transfer from present queue family back to graphics queue family at
725 // the start of the next frame because we don't care about the image's
726 // contents at that point.
Dave Houlton5fa47912018-02-16 11:02:26 -0700727 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
728 .pNext = NULL,
729 .srcAccessMask = 0,
730 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
731 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
732 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
733 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
734 .dstQueueFamilyIndex = demo->present_queue_family_index,
735 .image = demo->swapchain_image_resources[demo->current_buffer].image,
736 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600737
Dave Houlton5fa47912018-02-16 11:02:26 -0700738 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0,
739 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barbour22e06272016-09-07 16:07:56 -0600740 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600741 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600742 assert(!err);
743}
744
Tony Barbour22e06272016-09-07 16:07:56 -0600745void demo_build_image_ownership_cmd(struct demo *demo, int i) {
746 VkResult U_ASSERT_ONLY err;
747
748 const VkCommandBufferBeginInfo cmd_buf_info = {
749 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
750 .pNext = NULL,
751 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
752 .pInheritanceInfo = NULL,
753 };
Dave Houlton5fa47912018-02-16 11:02:26 -0700754 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
Tony Barbour22e06272016-09-07 16:07:56 -0600755 assert(!err);
756
Dave Houlton5fa47912018-02-16 11:02:26 -0700757 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
758 .pNext = NULL,
759 .srcAccessMask = 0,
760 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
761 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
762 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
763 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
764 .dstQueueFamilyIndex = demo->present_queue_family_index,
765 .image = demo->swapchain_image_resources[i].image,
766 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
Tony Barbour22e06272016-09-07 16:07:56 -0600767
Dave Houlton5fa47912018-02-16 11:02:26 -0700768 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
769 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700770 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600771 assert(!err);
772}
773
Karl Schultze4dc75c2016-02-02 15:37:51 -0700774void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600775 mat4x4 MVP, Model, VP;
776 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600777 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600778 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600779
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600780 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600781
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700782 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600783 mat4x4_dup(Model, demo->model_matrix);
Dave Houlton5fa47912018-02-16 11:02:26 -0700784 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600785 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600786
Dave Houlton5fa47912018-02-16 11:02:26 -0700787 err = vkMapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0, VK_WHOLE_SIZE, 0,
788 (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600789 assert(!err);
790
Karl Schultze4dc75c2016-02-02 15:37:51 -0700791 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600792
Tony Barboura72d9492017-01-11 13:35:09 -0700793 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600794}
795
Ian Elliottd940ca22017-03-22 08:31:27 -0600796void DemoUpdateTargetIPD(struct demo *demo) {
797 // Look at what happened to previous presents, and make appropriate
798 // adjustments in timing:
799 VkResult U_ASSERT_ONLY err;
Dave Houlton5fa47912018-02-16 11:02:26 -0700800 VkPastPresentationTimingGOOGLE *past = NULL;
Ian Elliottd940ca22017-03-22 08:31:27 -0600801 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600802
Dave Houlton5fa47912018-02-16 11:02:26 -0700803 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
Ian Elliottd940ca22017-03-22 08:31:27 -0600804 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600805 if (count) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700806 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
Ian Elliottd940ca22017-03-22 08:31:27 -0600807 assert(past);
Dave Houlton5fa47912018-02-16 11:02:26 -0700808 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600809 assert(!err);
810
811 bool early = false;
812 bool late = false;
813 bool calibrate_next = false;
Dave Houlton5fa47912018-02-16 11:02:26 -0700814 for (uint32_t i = 0; i < count; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600815 if (!demo->syncd_with_actual_presents) {
816 // This is the first time that we've received an
817 // actualPresentTime for this swapchain. In order to not
818 // perceive these early frames as "late", we need to sync-up
819 // our future desiredPresentTime's with the
820 // actualPresentTime(s) that we're receiving now.
821 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600822
Ian Elliottd940ca22017-03-22 08:31:27 -0600823 // So that we don't suspect any pending presents as late,
824 // record them all as suspected-late presents:
825 demo->last_late_id = demo->next_present_id - 1;
826 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600827 demo->syncd_with_actual_presents = true;
828 break;
Dave Houlton5fa47912018-02-16 11:02:26 -0700829 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
Ian Elliottd940ca22017-03-22 08:31:27 -0600830 demo->refresh_duration)) {
831 // This image could have been presented earlier. We don't want
832 // to decrease the target_IPD until we've seen early presents
833 // for at least two seconds.
834 if (demo->last_early_id == past[i].presentID) {
835 // We've now seen two seconds worth of early presents.
836 // Flag it as such, and reset the counter:
837 early = true;
838 demo->last_early_id = 0;
839 } else if (demo->last_early_id == 0) {
840 // This is the first early present we've seen.
841 // Calculate the presentID for two seconds from now.
Dave Houlton5fa47912018-02-16 11:02:26 -0700842 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
843 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -0600844 demo->last_early_id = past[i].presentID + howManyPresents;
845 } else {
846 // We are in the midst of a set of early images,
847 // and so we won't do anything.
848 }
849 late = false;
850 demo->last_late_id = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -0700851 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600852 // This image was presented after its desired time. Since
853 // there's a delay between calling vkQueuePresentKHR and when
854 // we get the timing data, several presents may have been late.
855 // Thus, we need to threat all of the outstanding presents as
856 // being likely late, so that we only increase the target_IPD
857 // once for all of those presents.
Dave Houlton5fa47912018-02-16 11:02:26 -0700858 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600859 late = true;
860 // Record the last suspected-late present:
861 demo->last_late_id = demo->next_present_id - 1;
862 } else {
863 // We are in the midst of a set of likely-late images,
864 // and so we won't do anything.
865 }
866 early = false;
867 demo->last_early_id = 0;
868 } else {
869 // Since this image was not presented early or late, reset
870 // any sets of early or late presentIDs:
871 early = false;
872 late = false;
873 calibrate_next = true;
874 demo->last_early_id = 0;
875 demo->last_late_id = 0;
876 }
877 }
878
879 if (early) {
880 // Since we've seen at least two-seconds worth of presnts that
881 // could have occured earlier than desired, let's decrease the
882 // target_IPD (i.e. increase the frame rate):
883 //
884 // TODO(ianelliott): Try to calculate a better target_IPD based
885 // on the most recently-seen present (this is overly-simplistic).
886 demo->refresh_duration_multiplier--;
887 if (demo->refresh_duration_multiplier == 0) {
888 // This should never happen, but in case it does, don't
889 // try to go faster.
890 demo->refresh_duration_multiplier = 1;
891 }
Dave Houlton5fa47912018-02-16 11:02:26 -0700892 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600893 }
894 if (late) {
895 // Since we found a new instance of a late present, we want to
896 // increase the target_IPD (i.e. decrease the frame rate):
897 //
898 // TODO(ianelliott): Try to calculate a better target_IPD based
899 // on the most recently-seen present (this is overly-simplistic).
900 demo->refresh_duration_multiplier++;
Dave Houlton5fa47912018-02-16 11:02:26 -0700901 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600902 }
903
904 if (calibrate_next) {
Dave Houlton5fa47912018-02-16 11:02:26 -0700905 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
906 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600907 }
908 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600909}
910
Karl Schultze4dc75c2016-02-02 15:37:51 -0700911static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600912 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600913
Damien Leonec336d822017-04-14 16:10:14 -0700914 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +0300915 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
916 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600917
Tony Barbour8b7c9112017-06-29 13:34:41 -0600918 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -0600919 // Get the index of the next available swapchain image:
Dave Houlton5fa47912018-02-16 11:02:26 -0700920 err =
921 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
922 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600923
Tony Barbour6914e6d2017-06-02 12:11:29 -0600924 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
925 // demo->swapchain is out of date (e.g. the window was resized) and
926 // must be recreated:
927 demo_resize(demo);
928 } else if (err == VK_SUBOPTIMAL_KHR) {
929 // demo->swapchain is not as optimal as it could be, but the platform's
930 // presentation engine will still present the image correctly.
931 break;
932 } else {
933 assert(!err);
934 }
Tony Barbour8b7c9112017-06-29 13:34:41 -0600935 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -0600936
Tony Barbour3eafe1f2017-06-14 11:59:55 -0600937 demo_update_data_buffer(demo);
938
Ian Elliottd940ca22017-03-22 08:31:27 -0600939 if (demo->VK_GOOGLE_display_timing_enabled) {
940 // Look at what happened to previous presents, and make appropriate
941 // adjustments in timing:
942 DemoUpdateTargetIPD(demo);
943
944 // Note: a real application would position its geometry to that it's in
945 // the correct locatoin for when the next image is presented. It might
946 // also wait, so that there's less latency between any input and when
947 // the next image is rendered/presented. This demo program is so
948 // simple that it doesn't do either of those.
949 }
950
Tony Barbource7524e2016-07-28 12:01:45 -0600951 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600952 // that the image won't be rendered to until the presentation
953 // engine has fully released ownership to the application, and it is
954 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -0600955 VkPipelineStageFlags pipe_stage_flags;
956 VkSubmitInfo submit_info;
957 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
958 submit_info.pNext = NULL;
959 submit_info.pWaitDstStageMask = &pipe_stage_flags;
960 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
961 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600962 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600963 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -0700964 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600965 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600966 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Dave Houlton5fa47912018-02-16 11:02:26 -0700967 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600968 assert(!err);
969
Tony Barbour22e06272016-09-07 16:07:56 -0600970 if (demo->separate_present_queue) {
971 // If we are using separate queues, change image ownership to the
972 // present queue before presenting, waiting for the draw complete
973 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -0700974 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -0600975 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
976 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600977 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600978 submit_info.commandBufferCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -0700979 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600980 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -0600981 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -0600982 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
983 assert(!err);
984 }
985
986 // If we are using separate queues we have to wait for image ownership,
987 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -0600988 VkPresentInfoKHR present = {
989 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -0600990 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -0600991 .waitSemaphoreCount = 1,
Dave Houlton5fa47912018-02-16 11:02:26 -0700992 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
993 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -0600994 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -0700995 .pSwapchains = &demo->swapchain,
996 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -0600997 };
998
Ian Elliott53622a72017-03-28 11:06:33 -0600999 if (demo->VK_KHR_incremental_present_enabled) {
1000 // If using VK_KHR_incremental_present, we provide a hint of the region
1001 // that contains changed content relative to the previously-presented
1002 // image. The implementation can use this hint in order to save
1003 // work/power (by only copying the region in the hint). The
1004 // implementation is free to ignore the hint though, and so we must
1005 // ensure that the entire image has the correctly-drawn content.
1006 uint32_t eighthOfWidth = demo->width / 8;
1007 uint32_t eighthOfHeight = demo->height / 8;
1008 VkRectLayerKHR rect = {
1009 .offset.x = eighthOfWidth,
1010 .offset.y = eighthOfHeight,
1011 .extent.width = eighthOfWidth * 6,
1012 .extent.height = eighthOfHeight * 6,
1013 .layer = 0,
1014 };
1015 VkPresentRegionKHR region = {
1016 .rectangleCount = 1,
1017 .pRectangles = &rect,
1018 };
1019 VkPresentRegionsKHR regions = {
1020 .sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1021 .pNext = present.pNext,
1022 .swapchainCount = present.swapchainCount,
1023 .pRegions = &region,
1024 };
1025 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001026 }
1027
Ian Elliottd940ca22017-03-22 08:31:27 -06001028 if (demo->VK_GOOGLE_display_timing_enabled) {
1029 VkPresentTimeGOOGLE ptime;
1030 if (demo->prev_desired_present_time == 0) {
1031 // This must be the first present for this swapchain.
1032 //
1033 // We don't know where we are relative to the presentation engine's
1034 // display's refresh cycle. We also don't know how long rendering
1035 // takes. Let's make a grossly-simplified assumption that the
1036 // desiredPresentTime should be half way between now and
1037 // now+target_IPD. We will adjust over time.
1038 uint64_t curtime = getTimeInNanoseconds();
1039 if (curtime == 0) {
1040 // Since we didn't find out the current time, don't give a
1041 // desiredPresentTime:
1042 ptime.desiredPresentTime = 0;
1043 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001044 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1045 }
1046 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07001047 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
Ian Elliottd940ca22017-03-22 08:31:27 -06001048 }
1049 ptime.presentID = demo->next_present_id++;
1050 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001051
1052 VkPresentTimesInfoGOOGLE present_time = {
1053 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1054 .pNext = present.pNext,
1055 .swapchainCount = present.swapchainCount,
1056 .pTimes = &ptime,
1057 };
1058 if (demo->VK_GOOGLE_display_timing_enabled) {
1059 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001060 }
1061 }
1062
Tony Barboura2a67812016-07-18 13:21:06 -06001063 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001064 demo->frame_index += 1;
1065 demo->frame_index %= FRAME_LAG;
1066
Ian Elliottcf074d32015-10-16 18:02:43 -06001067 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1068 // demo->swapchain is out of date (e.g. the window was resized) and
1069 // must be recreated:
1070 demo_resize(demo);
1071 } else if (err == VK_SUBOPTIMAL_KHR) {
1072 // demo->swapchain is not as optimal as it could be, but the platform's
1073 // presentation engine will still present the image correctly.
1074 } else {
1075 assert(!err);
1076 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001077}
1078
Karl Schultze4dc75c2016-02-02 15:37:51 -07001079static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001080 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001081 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001082
Ian Elliottb08e0d92015-11-20 11:55:46 -07001083 // Check the surface capabilities and formats
1084 VkSurfaceCapabilitiesKHR surfCapabilities;
Dave Houlton5fa47912018-02-16 11:02:26 -07001085 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001086 assert(!err);
1087
Ian Elliott8528eff2015-08-07 15:56:59 -06001088 uint32_t presentModeCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07001089 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001090 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07001091 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001092 assert(presentModes);
Dave Houlton5fa47912018-02-16 11:02:26 -07001093 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001094 assert(!err);
1095
Ian Elliotta0781972015-08-21 15:09:33 -06001096 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001097 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1098 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1099 // If the surface size is undefined, the size is set to the size
1100 // of the images requested, which must fit within the minimum and
1101 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001102 swapchainExtent.width = demo->width;
1103 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001104
1105 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1106 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1107 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1108 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1109 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001110
Ian Elliottcf7f7482016-08-19 03:46:58 -06001111 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1112 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1113 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1114 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1115 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001116 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001117 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001118 swapchainExtent = surfCapabilities.currentExtent;
1119 demo->width = surfCapabilities.currentExtent.width;
1120 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001121 }
1122
Tony Barbour66a56c32016-09-21 13:10:56 -06001123 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001124 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001125 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001126
Ian Elliottb18fdde2016-10-03 12:11:12 -06001127 // There are times when you may wish to use another present mode. The
1128 // following code shows how to select them, and the comments provide some
1129 // reasons you may wish to use them.
1130 //
1131 // It should be noted that Vulkan 1.0 doesn't provide a method for
1132 // synchronizing rendering with the presentation engine's display. There
1133 // is a method provided for throttling rendering with the display, but
1134 // there are some presentation engines for which this method will not work.
1135 // If an application doesn't throttle its rendering, and if it renders much
1136 // faster than the refresh rate of the display, this can waste power on
1137 // mobile devices. That is because power is being spent rendering images
1138 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001139
Ian Elliottb18fdde2016-10-03 12:11:12 -06001140 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1141 // tearing, or have some way of synchronizing their rendering with the
1142 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001143 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1144 // generally render a new presentable image every refresh cycle, but are
1145 // occasionally early. In this case, the application wants the new image
1146 // to be displayed instead of the previously-queued-for-presentation image
1147 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001148 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1149 // render a new presentable image every refresh cycle, but are occasionally
1150 // late. In this case (perhaps because of stuttering/latency concerns),
1151 // the application wants the late image to be immediately displayed, even
1152 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001153
Dave Houlton5fa47912018-02-16 11:02:26 -07001154 if (demo->presentMode != swapchainPresentMode) {
Tony Barbour291d57b2016-10-21 14:47:07 -06001155 for (size_t i = 0; i < presentModeCount; ++i) {
1156 if (presentModes[i] == demo->presentMode) {
1157 swapchainPresentMode = demo->presentMode;
1158 break;
1159 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001160 }
1161 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001162 if (swapchainPresentMode != demo->presentMode) {
1163 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1164 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001165
Tony Barbour84376fe2017-01-06 15:54:22 -07001166 // Determine the number of VkImages to use in the swap chain.
1167 // Application desires to acquire 3 images at a time for triple
1168 // buffering
1169 uint32_t desiredNumOfSwapchainImages = 3;
1170 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1171 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1172 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001173 // If maxImageCount is 0, we can ask for as many images as we want;
1174 // otherwise we're limited to maxImageCount
Dave Houlton5fa47912018-02-16 11:02:26 -07001175 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001176 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001177 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001178 }
1179
Tony Barbour9fd6d352015-09-16 15:01:32 -06001180 VkSurfaceTransformFlagsKHR preTransform;
Dave Houlton5fa47912018-02-16 11:02:26 -07001181 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001182 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001183 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001184 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001185 }
1186
Tony Barbour820b55b2017-03-14 08:55:46 -06001187 // Find a supported composite alpha mode - one of these is guaranteed to be set
1188 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1189 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1190 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1191 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1192 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1193 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1194 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001195 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001196 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1197 compositeAlpha = compositeAlphaFlags[i];
1198 break;
1199 }
1200 }
1201
Tony Barboura2a67812016-07-18 13:21:06 -06001202 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001203 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001204 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001205 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001206 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001207 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001208 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001209 .imageExtent =
1210 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001211 .width = swapchainExtent.width,
1212 .height = swapchainExtent.height,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001213 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001214 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001215 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001216 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001217 .imageArrayLayers = 1,
1218 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1219 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001220 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001221 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001222 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001223 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001224 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001225 uint32_t i;
Dave Houlton5fa47912018-02-16 11:02:26 -07001226 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001227 assert(!err);
1228
Ian Elliottcf074d32015-10-16 18:02:43 -06001229 // If we just re-created an existing swapchain, we should destroy the old
1230 // swapchain at this point.
1231 // Note: destroying the swapchain also cleans up all its associated
1232 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001233 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001234 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001235 }
1236
Dave Houlton5fa47912018-02-16 11:02:26 -07001237 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001238 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001239
Dave Houlton5fa47912018-02-16 11:02:26 -07001240 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001241 assert(swapchainImages);
Dave Houlton5fa47912018-02-16 11:02:26 -07001242 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001243 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001244
Dave Houlton5fa47912018-02-16 11:02:26 -07001245 demo->swapchain_image_resources =
1246 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001247 assert(demo->swapchain_image_resources);
1248
Ian Elliotta0781972015-08-21 15:09:33 -06001249 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001250 VkImageViewCreateInfo color_image_view = {
1251 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252 .pNext = NULL,
1253 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001254 .components =
1255 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001256 .r = VK_COMPONENT_SWIZZLE_R,
1257 .g = VK_COMPONENT_SWIZZLE_G,
1258 .b = VK_COMPONENT_SWIZZLE_B,
1259 .a = VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001260 },
Dave Houlton5fa47912018-02-16 11:02:26 -07001261 .subresourceRange =
1262 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001263 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1264 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001265 };
1266
Tony Barboura72d9492017-01-11 13:35:09 -07001267 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001268
Tony Barboura72d9492017-01-11 13:35:09 -07001269 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001270
Dave Houlton5fa47912018-02-16 11:02:26 -07001271 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001272 assert(!err);
1273 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001274
Ian Elliottd940ca22017-03-22 08:31:27 -06001275 if (demo->VK_GOOGLE_display_timing_enabled) {
1276 VkRefreshCycleDurationGOOGLE rc_dur;
Dave Houlton5fa47912018-02-16 11:02:26 -07001277 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
Ian Elliottd940ca22017-03-22 08:31:27 -06001278 assert(!err);
1279 demo->refresh_duration = rc_dur.refreshDuration;
1280
1281 demo->syncd_with_actual_presents = false;
1282 // Initially target 1X the refresh duration:
1283 demo->target_IPD = demo->refresh_duration;
1284 demo->refresh_duration_multiplier = 1;
1285 demo->prev_desired_present_time = 0;
1286 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001287 }
1288
Mark Young04fd3d82016-01-25 14:43:50 -07001289 if (NULL != presentModes) {
1290 free(presentModes);
1291 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001292}
1293
Karl Schultze4dc75c2016-02-02 15:37:51 -07001294static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001295 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001296 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001297 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001298 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001299 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001300 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001301 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001302 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001303 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001304 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001305 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001306 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307 .flags = 0,
1308 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001309
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001310 VkImageViewCreateInfo view = {
1311 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001312 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001313 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001314 .format = depth_format,
Dave Houlton5fa47912018-02-16 11:02:26 -07001315 .subresourceRange =
1316 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001318 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001319 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001320
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001321 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001322 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001323 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001324
1325 demo->depth.format = depth_format;
1326
1327 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001328 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001329 assert(!err);
1330
Karl Schultze4dc75c2016-02-02 15:37:51 -07001331 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001332 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001333
Chia-I Wuf48700b2015-11-10 16:21:09 +08001334 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001335 demo->depth.mem_alloc.pNext = NULL;
1336 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1337 demo->depth.mem_alloc.memoryTypeIndex = 0;
1338
Dave Houlton5fa47912018-02-16 11:02:26 -07001339 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001340 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001341 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001342
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001343 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001344 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001345 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001346
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001347 /* bind memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001348 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001349 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001350
1351 /* create image view */
1352 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001353 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001355}
1356
Tony Barbour1a86d032015-09-21 15:17:33 -06001357/* Load a ppm file into memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001358bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001359#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Dave Houlton5fa47912018-02-16 11:02:26 -07001360 filename = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001361#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001362
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001363#ifdef __ANDROID__
1364#include <lunarg.ppm.h>
1365 char *cPtr;
Dave Houlton5fa47912018-02-16 11:02:26 -07001366 cPtr = (char *)lunarg_ppm;
1367 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001368 return false;
1369 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001370 while (strncmp(cPtr++, "\n", 1))
1371 ;
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001372 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001373 if (rgba_data == NULL) {
1374 return true;
1375 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001376 while (strncmp(cPtr++, "\n", 1))
1377 ;
1378 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001379 return false;
1380 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001381 while (strncmp(cPtr++, "\n", 1))
1382 ;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001383
1384 for (int y = 0; y < *height; y++) {
1385 uint8_t *rowPtr = rgba_data;
1386 for (int x = 0; x < *width; x++) {
1387 memcpy(rowPtr, cPtr, 3);
1388 rowPtr[3] = 255; /* Alpha of 1 */
1389 rowPtr += 4;
1390 cPtr += 3;
1391 }
1392 rgba_data += layout->rowPitch;
1393 }
1394
1395 return true;
1396#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001397 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001398 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001399
Dave Houlton5fa47912018-02-16 11:02:26 -07001400 if (!fPtr) return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001401
Dave Houlton5fa47912018-02-16 11:02:26 -07001402 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001403 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1404 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001405 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001406 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001407
Tony Barbour1a86d032015-09-21 15:17:33 -06001408 do {
1409 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001410 if (cPtr == NULL) {
1411 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001412 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001413 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001414 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001415
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001416 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001417 if (rgba_data == NULL) {
1418 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001419 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001420 }
Dave Houlton5fa47912018-02-16 11:02:26 -07001421 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001422 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001423 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1424 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001425 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001426 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001427
Karl Schultze4dc75c2016-02-02 15:37:51 -07001428 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001429 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001430 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001431 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001432 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001433 rowPtr[3] = 255; /* Alpha of 1 */
1434 rowPtr += 4;
1435 }
1436 rgba_data += layout->rowPitch;
1437 }
1438 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001439 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001440#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001441}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001442
Dave Houlton5fa47912018-02-16 11:02:26 -07001443static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1444 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001445 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001446 int32_t tex_width;
1447 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001448 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001449 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001450
Karl Schultze4dc75c2016-02-02 15:37:51 -07001451 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001452 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001453 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001454
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001455 tex_obj->tex_width = tex_width;
1456 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001457
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001458 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001459 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001460 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001461 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001462 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001463 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001464 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001465 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001466 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001467 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001468 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001469 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001470 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001471 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001472
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001473 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001474
Dave Houlton5fa47912018-02-16 11:02:26 -07001475 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001476 assert(!err);
1477
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001478 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001479
Chia-I Wuf48700b2015-11-10 16:21:09 +08001480 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001481 tex_obj->mem_alloc.pNext = NULL;
1482 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1483 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001484
Dave Houlton5fa47912018-02-16 11:02:26 -07001485 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001486 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001487
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001488 /* allocate memory */
Dave Houlton5fa47912018-02-16 11:02:26 -07001489 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001490 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001491
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001492 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001493 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001494 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001495
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001496 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001497 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001498 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001499 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001500 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001501 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001502 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001503 void *data;
1504
Dave Houlton5fa47912018-02-16 11:02:26 -07001505 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001506
Dave Houlton5fa47912018-02-16 11:02:26 -07001507 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001508 assert(!err);
1509
1510 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1511 fprintf(stderr, "Error loading texture: %s\n", filename);
1512 }
1513
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001514 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001515 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001516
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001517 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001518}
1519
Dave Houlton5fa47912018-02-16 11:02:26 -07001520static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001521 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001522 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1523 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001524}
1525
Karl Schultze4dc75c2016-02-02 15:37:51 -07001526static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001527 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001528 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001529 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001530
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001531 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001532
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001533 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001534 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001535
Dave Houlton5fa47912018-02-16 11:02:26 -07001536 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001537 /* Device can texture using linear textures */
Dave Houlton5fa47912018-02-16 11:02:26 -07001538 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1539 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001540 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1541 // shader to run until layout transition completes
Dave Houlton5fa47912018-02-16 11:02:26 -07001542 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1543 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001544 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001545 demo->staging_texture.image = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07001546 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001547 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001548
Tony Barbour16156cb2016-10-19 14:15:49 -06001549 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Dave Houlton5fa47912018-02-16 11:02:26 -07001550 demo_prepare_texture_image(demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
1551 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1552 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001553
Dave Houlton5fa47912018-02-16 11:02:26 -07001554 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1555 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1556 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001557
Dave Houlton5fa47912018-02-16 11:02:26 -07001558 demo_set_image_layout(demo, demo->staging_texture.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1559 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001560 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001561
Dave Houlton5fa47912018-02-16 11:02:26 -07001562 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1563 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001564 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001565
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001566 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001567 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1568 .srcOffset = {0, 0, 0},
1569 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1570 .dstOffset = {0, 0, 0},
Dave Houlton5fa47912018-02-16 11:02:26 -07001571 .extent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001572 };
Dave Houlton5fa47912018-02-16 11:02:26 -07001573 vkCmdCopyImage(demo->cmd, demo->staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1574 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001575
Dave Houlton5fa47912018-02-16 11:02:26 -07001576 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1577 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001578 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001579
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001580 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001581 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1582 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001583 }
1584
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001585 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001586 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001587 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001588 .magFilter = VK_FILTER_NEAREST,
1589 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001590 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001591 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1592 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1593 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001594 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001595 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001596 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001597 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001598 .minLod = 0.0f,
1599 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001600 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001601 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001602 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001603
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001604 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001605 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001606 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001607 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001608 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001609 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001610 .components =
1611 {
Dave Houlton5fa47912018-02-16 11:02:26 -07001612 VK_COMPONENT_SWIZZLE_R,
1613 VK_COMPONENT_SWIZZLE_G,
1614 VK_COMPONENT_SWIZZLE_B,
1615 VK_COMPONENT_SWIZZLE_A,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001616 },
1617 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001618 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001619 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001620
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001621 /* create sampler */
Dave Houlton5fa47912018-02-16 11:02:26 -07001622 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001623 assert(!err);
1624
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001625 /* create image view */
1626 view.image = demo->textures[i].image;
Dave Houlton5fa47912018-02-16 11:02:26 -07001627 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001628 assert(!err);
1629 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001630}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631
Tony Barboura72d9492017-01-11 13:35:09 -07001632void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001633 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001634 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001635 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001636 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001637 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001638 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001639 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001640 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001641
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001642 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001643 mat4x4_mul(MVP, VP, demo->model_matrix);
1644 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001645 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001646
Karl Schultza2615462017-01-20 13:19:20 -07001647 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001648 data.position[i][0] = g_vertex_buffer_data[i * 3];
1649 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1650 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001651 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001652 data.attr[i][0] = g_uv_buffer_data[2 * i];
1653 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001654 data.attr[i][2] = 0;
1655 data.attr[i][3] = 0;
1656 }
1657
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001658 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001659 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001660 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001661 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001662
Tony Barboura72d9492017-01-11 13:35:09 -07001663 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07001664 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
Tony Barboura72d9492017-01-11 13:35:09 -07001665 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001666
Dave Houlton5fa47912018-02-16 11:02:26 -07001667 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001668
Tony Barboura72d9492017-01-11 13:35:09 -07001669 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1670 mem_alloc.pNext = NULL;
1671 mem_alloc.allocationSize = mem_reqs.size;
1672 mem_alloc.memoryTypeIndex = 0;
1673
Dave Houlton5fa47912018-02-16 11:02:26 -07001674 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1675 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1676 &mem_alloc.memoryTypeIndex);
Tony Barboura72d9492017-01-11 13:35:09 -07001677 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001678
Dave Houlton5fa47912018-02-16 11:02:26 -07001679 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
Tony Barboura72d9492017-01-11 13:35:09 -07001680 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001681
Dave Houlton5fa47912018-02-16 11:02:26 -07001682 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 -07001683 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001684
Tony Barboura72d9492017-01-11 13:35:09 -07001685 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001686
Tony Barboura72d9492017-01-11 13:35:09 -07001687 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001688
Tony Barboura72d9492017-01-11 13:35:09 -07001689 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
Dave Houlton5fa47912018-02-16 11:02:26 -07001690 demo->swapchain_image_resources[i].uniform_memory, 0);
Tony Barboura72d9492017-01-11 13:35:09 -07001691 assert(!err);
1692 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001693}
1694
Karl Schultze4dc75c2016-02-02 15:37:51 -07001695static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001696 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001697 [0] =
1698 {
1699 .binding = 0,
1700 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1701 .descriptorCount = 1,
1702 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1703 .pImmutableSamplers = NULL,
1704 },
1705 [1] =
1706 {
1707 .binding = 1,
1708 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1709 .descriptorCount = DEMO_TEXTURE_COUNT,
1710 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1711 .pImmutableSamplers = NULL,
1712 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001713 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001714 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001715 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001716 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001717 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001718 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001719 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001720 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001721
Dave Houlton5fa47912018-02-16 11:02:26 -07001722 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001723 assert(!err);
1724
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001725 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001726 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1727 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001728 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001729 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001730 };
1731
Dave Houlton5fa47912018-02-16 11:02:26 -07001732 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001734}
1735
Karl Schultze4dc75c2016-02-02 15:37:51 -07001736static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001737 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1738 // because at the start of the renderpass, we don't care about their contents.
1739 // At the start of the subpass, the color attachment's layout will be transitioned
1740 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1741 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1742 // the renderpass, the color attachment's layout will be transitioned to
1743 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1744 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001745 const VkAttachmentDescription attachments[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001746 [0] =
1747 {
1748 .format = demo->format,
1749 .flags = 0,
1750 .samples = VK_SAMPLE_COUNT_1_BIT,
1751 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1752 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1753 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1754 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1755 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1756 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1757 },
1758 [1] =
1759 {
1760 .format = demo->depth.format,
1761 .flags = 0,
1762 .samples = VK_SAMPLE_COUNT_1_BIT,
1763 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1764 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1765 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1766 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1767 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1768 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1769 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001770 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001771 const VkAttachmentReference color_reference = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001772 .attachment = 0,
1773 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001774 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001775 const VkAttachmentReference depth_reference = {
1776 .attachment = 1,
1777 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1778 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001779 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001780 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1781 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001782 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001783 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001784 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001785 .pColorAttachments = &color_reference,
1786 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001787 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001788 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001789 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001790 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001791 const VkRenderPassCreateInfo rp_info = {
1792 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1793 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001794 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001795 .attachmentCount = 2,
1796 .pAttachments = attachments,
1797 .subpassCount = 1,
1798 .pSubpasses = &subpass,
1799 .dependencyCount = 0,
1800 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001801 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001802 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001803
Chia-I Wu63636062015-10-26 21:10:41 +08001804 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001805 assert(!err);
1806}
1807
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001808static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001809 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001810 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001811 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001812
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001813 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1814 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001815 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001816 moduleCreateInfo.codeSize = size;
1817 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001818
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001819 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1820 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001821
1822 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001823}
1824
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001825static void demo_prepare_vs(struct demo *demo) {
1826 const uint32_t vs_code[] = {
1827#include "cube.vert.inc"
1828 };
1829 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001830}
1831
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001832static void demo_prepare_fs(struct demo *demo) {
1833 const uint32_t fs_code[] = {
1834#include "cube.frag.inc"
1835 };
1836 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001837}
1838
Karl Schultze4dc75c2016-02-02 15:37:51 -07001839static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001840 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001841 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001842 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001843 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001844 VkPipelineRasterizationStateCreateInfo rs;
1845 VkPipelineColorBlendStateCreateInfo cb;
1846 VkPipelineDepthStencilStateCreateInfo ds;
1847 VkPipelineViewportStateCreateInfo vp;
1848 VkPipelineMultisampleStateCreateInfo ms;
1849 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1850 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001851 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852
Piers Daniellba839d12015-09-29 13:01:09 -06001853 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1854 memset(&dynamicState, 0, sizeof dynamicState);
1855 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1856 dynamicState.pDynamicStates = dynamicStateEnables;
1857
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001859 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001860 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001861
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001862 memset(&vi, 0, sizeof(vi));
1863 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1864
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001865 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001866 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001867 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001868
1869 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001870 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1871 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001872 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001873 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001874 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001875 rs.rasterizerDiscardEnable = VK_FALSE;
1876 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001877 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001878
1879 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001880 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1881 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001882 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08001883 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001884 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001885 cb.attachmentCount = 1;
1886 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001887
Tony Barbour29645d02015-01-16 14:27:35 -07001888 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001889 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001890 vp.viewportCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001891 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06001892 vp.scissorCount = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07001893 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07001894
1895 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001896 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001897 ds.depthTestEnable = VK_TRUE;
1898 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001899 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06001900 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08001901 ds.back.failOp = VK_STENCIL_OP_KEEP;
1902 ds.back.passOp = VK_STENCIL_OP_KEEP;
1903 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001904 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001905 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001906
Tony Barbour29645d02015-01-16 14:27:35 -07001907 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001908 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06001909 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08001910 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001911
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001912 demo_prepare_vs(demo);
1913 demo_prepare_fs(demo);
1914
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001915 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001916 VkPipelineShaderStageCreateInfo shaderStages[2];
1917 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1918
Karl Schultze4dc75c2016-02-02 15:37:51 -07001919 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1920 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001921 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001922 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001923
Karl Schultze4dc75c2016-02-02 15:37:51 -07001924 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1925 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001926 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001927 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001928
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001929 memset(&pipelineCache, 0, sizeof(pipelineCache));
1930 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001931
Dave Houlton5fa47912018-02-16 11:02:26 -07001932 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001933 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001934
Karl Schultze4dc75c2016-02-02 15:37:51 -07001935 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001936 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001937 pipeline.pRasterizationState = &rs;
1938 pipeline.pColorBlendState = &cb;
1939 pipeline.pMultisampleState = &ms;
1940 pipeline.pViewportState = &vp;
1941 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001942 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001943 pipeline.pStages = shaderStages;
1944 pipeline.renderPass = demo->render_pass;
1945 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06001946
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06001947 pipeline.renderPass = demo->render_pass;
1948
Dave Houlton5fa47912018-02-16 11:02:26 -07001949 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001950 assert(!err);
1951
Chia-I Wu63636062015-10-26 21:10:41 +08001952 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
1953 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001954}
1955
Karl Schultze4dc75c2016-02-02 15:37:51 -07001956static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08001957 const VkDescriptorPoolSize type_counts[2] = {
Dave Houlton5fa47912018-02-16 11:02:26 -07001958 [0] =
1959 {
1960 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1961 .descriptorCount = demo->swapchainImageCount,
1962 },
1963 [1] =
1964 {
1965 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1966 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
1967 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001968 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001969 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001970 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001971 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001972 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08001973 .poolSizeCount = 2,
1974 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001975 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001976 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001977
Dave Houlton5fa47912018-02-16 11:02:26 -07001978 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001979 assert(!err);
1980}
1981
Karl Schultze4dc75c2016-02-02 15:37:51 -07001982static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001983 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08001984 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001985 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001986
Dave Houlton5fa47912018-02-16 11:02:26 -07001987 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
1988 .pNext = NULL,
1989 .descriptorPool = demo->desc_pool,
1990 .descriptorSetCount = 1,
1991 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07001992
1993 VkDescriptorBufferInfo buffer_info;
1994 buffer_info.offset = 0;
1995 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001996
Chia-I Wuae721ba2015-05-25 16:27:55 +08001997 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07001998 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06001999 tex_descs[i].sampler = demo->textures[i].sampler;
2000 tex_descs[i].imageView = demo->textures[i].view;
2001 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002002 }
2003
2004 memset(&writes, 0, sizeof(writes));
2005
2006 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002007 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002008 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002009 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002010
2011 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002012 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002013 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002014 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002015 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002016
Tony Barboura72d9492017-01-11 13:35:09 -07002017 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2018 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2019 assert(!err);
2020 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2021 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2022 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2023 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2024 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002025}
2026
Karl Schultze4dc75c2016-02-02 15:37:51 -07002027static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002028 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002029 attachments[1] = demo->depth.view;
2030
Chia-I Wuf973e322015-07-08 13:34:24 +08002031 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002032 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2033 .pNext = NULL,
2034 .renderPass = demo->render_pass,
2035 .attachmentCount = 2,
2036 .pAttachments = attachments,
2037 .width = demo->width,
2038 .height = demo->height,
2039 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002040 };
2041 VkResult U_ASSERT_ONLY err;
2042 uint32_t i;
2043
Tony Barbourd8e504f2015-10-08 14:26:24 -06002044 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002045 attachments[0] = demo->swapchain_image_resources[i].view;
Dave Houlton5fa47912018-02-16 11:02:26 -07002046 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002047 assert(!err);
2048 }
2049}
2050
Karl Schultze4dc75c2016-02-02 15:37:51 -07002051static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002052 VkResult U_ASSERT_ONLY err;
2053
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002054 const VkCommandPoolCreateInfo cmd_pool_info = {
2055 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06002056 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002057 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06002058 .flags = 0,
2059 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002060 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06002061 assert(!err);
2062
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002063 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002064 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002065 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002066 .commandPool = demo->cmd_pool,
2067 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002068 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002069 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002070 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2071 assert(!err);
2072 VkCommandBufferBeginInfo cmd_buf_info = {
2073 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2074 .pNext = NULL,
2075 .flags = 0,
2076 .pInheritanceInfo = NULL,
2077 };
2078 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2079 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002080
2081 demo_prepare_buffers(demo);
2082 demo_prepare_depth(demo);
2083 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002084 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002085
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002086 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002087 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002088 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002089
Ian Elliotta0781972015-08-21 15:09:33 -06002090 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002091 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002092 assert(!err);
2093 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002094
Tony Barbour22e06272016-09-07 16:07:56 -06002095 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002096 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002097 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2098 .pNext = NULL,
2099 .queueFamilyIndex = demo->present_queue_family_index,
2100 .flags = 0,
2101 };
Dave Houlton5fa47912018-02-16 11:02:26 -07002102 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
Tony Barbour22e06272016-09-07 16:07:56 -06002103 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002104 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002105 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2106 .pNext = NULL,
2107 .commandPool = demo->present_cmd_pool,
2108 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2109 .commandBufferCount = 1,
2110 };
2111 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002112 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2113 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002114 assert(!err);
2115 demo_build_image_ownership_cmd(demo, i);
2116 }
2117 }
2118
Chia-I Wu63ea9262015-03-26 13:14:16 +08002119 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002120 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002121
Chia-I Wuf973e322015-07-08 13:34:24 +08002122 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002123
Ian Elliotta0781972015-08-21 15:09:33 -06002124 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002125 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002126 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002127 }
2128
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002129 /*
2130 * Prepare functions above may generate pipeline commands
2131 * that need to be flushed before beginning the render loop.
2132 */
2133 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002134 if (demo->staging_texture.image) {
2135 demo_destroy_texture_image(demo, &demo->staging_texture);
2136 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002137
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002138 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002139 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002140}
2141
Karl Schultze4dc75c2016-02-02 15:37:51 -07002142static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002143 uint32_t i;
2144
2145 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002146 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002147
Tony Barbour66a56c32016-09-21 13:10:56 -06002148 // Wait for fences from present operations
2149 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002150 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002151 vkDestroyFence(demo->device, demo->fences[i], NULL);
2152 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2153 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2154 if (demo->separate_present_queue) {
2155 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2156 }
2157 }
2158
Tony Barbourd8e504f2015-10-08 14:26:24 -06002159 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002160 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002161 }
Chia-I Wu63636062015-10-26 21:10:41 +08002162 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002163
Chia-I Wu63636062015-10-26 21:10:41 +08002164 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2165 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2166 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2167 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2168 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002169
2170 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002171 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2172 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2173 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2174 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002175 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002176 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002177
Chia-I Wu63636062015-10-26 21:10:41 +08002178 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2179 vkDestroyImage(demo->device, demo->depth.image, NULL);
2180 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002181
Ian Elliotta0781972015-08-21 15:09:33 -06002182 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002183 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002184 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002185 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2186 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002187 }
Tony Barboura72d9492017-01-11 13:35:09 -07002188 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002189 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002190 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002191
Tony Barbour22e06272016-09-07 16:07:56 -06002192 if (demo->separate_present_queue) {
2193 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002194 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002195 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002196 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002197 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002198 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002199 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002200 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002201
Tony Barbour153cb062016-12-07 13:43:36 -07002202#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002203 XDestroyWindow(demo->display, demo->xlib_window);
2204 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002205#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002206 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002207 xcb_disconnect(demo->connection);
2208 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002209#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002210 wl_keyboard_destroy(demo->keyboard);
2211 wl_pointer_destroy(demo->pointer);
2212 wl_seat_destroy(demo->seat);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002213 wl_shell_surface_destroy(demo->shell_surface);
2214 wl_surface_destroy(demo->window);
2215 wl_shell_destroy(demo->shell);
2216 wl_compositor_destroy(demo->compositor);
2217 wl_registry_destroy(demo->registry);
2218 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002219#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002220#endif
Karl Schultz86429112017-06-20 11:27:59 -06002221
2222 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002223}
2224
Karl Schultze4dc75c2016-02-02 15:37:51 -07002225static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002226 uint32_t i;
2227
Mike Stroyanbb60b382015-10-28 09:46:57 -06002228 // Don't react to resize until after first initialization.
2229 if (!demo->prepared) {
2230 return;
2231 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002232 // In order to properly resize the window, we must re-create the swapchain
2233 // AND redo the command buffers, etc.
2234 //
2235 // First, perform part of the demo_cleanup() function:
2236 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002237 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002238
2239 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002240 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002241 }
Chia-I Wu63636062015-10-26 21:10:41 +08002242 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002243
Chia-I Wu63636062015-10-26 21:10:41 +08002244 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2245 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2246 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2247 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2248 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002249
2250 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002251 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2252 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2253 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2254 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002255 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002256
Chia-I Wu63636062015-10-26 21:10:41 +08002257 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2258 vkDestroyImage(demo->device, demo->depth.image, NULL);
2259 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002260
Ian Elliottcf074d32015-10-16 18:02:43 -06002261 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002262 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Dave Houlton5fa47912018-02-16 11:02:26 -07002263 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
Tony Barboura72d9492017-01-11 13:35:09 -07002264 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2265 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002266 }
Chia-I Wu63636062015-10-26 21:10:41 +08002267 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002268 if (demo->separate_present_queue) {
2269 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2270 }
Tony Barboura72d9492017-01-11 13:35:09 -07002271 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002272
Ian Elliottcf074d32015-10-16 18:02:43 -06002273 // Second, re-perform the demo_prepare() function, which will re-create the
2274 // swapchain:
2275 demo_prepare(demo);
2276}
2277
David Pinedo2bb7c932015-06-18 17:03:14 -06002278// On MS-Windows, make this a global, so it's available to WndProc()
2279struct demo demo;
2280
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002281#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002282static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002283 if (!demo->prepared) return;
Tony Barbource7524e2016-07-28 12:01:45 -06002284
Ian Elliott639ca472015-04-16 15:23:05 -06002285 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002286 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002287 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002288 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002289 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002290}
Ian Elliott639ca472015-04-16 15:23:05 -06002291
2292// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002293LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2294 switch (uMsg) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002295 case WM_CLOSE:
2296 PostQuitMessage(validation_error);
2297 break;
2298 case WM_PAINT:
2299 // The validation callback calls MessageBox which can generate paint
2300 // events - don't make more Vulkan calls if we got here from the
2301 // callback
2302 if (!in_callback) {
2303 demo_run(&demo);
2304 }
2305 break;
2306 case WM_GETMINMAXINFO: // set window's minimum size
2307 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2308 return 0;
2309 case WM_SIZE:
2310 // Resize the application to the new window size, except when
2311 // it was minimized. Vulkan doesn't support images or swapchains
2312 // with width=0 and height=0.
2313 if (wParam != SIZE_MINIMIZED) {
2314 demo.width = lParam & 0xffff;
2315 demo.height = (lParam & 0xffff0000) >> 16;
2316 demo_resize(&demo);
2317 }
2318 break;
2319 default:
2320 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002321 }
2322 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2323}
2324
Karl Schultze4dc75c2016-02-02 15:37:51 -07002325static void demo_create_window(struct demo *demo) {
2326 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002327
2328 // Initialize the window class structure:
2329 win_class.cbSize = sizeof(WNDCLASSEX);
2330 win_class.style = CS_HREDRAW | CS_VREDRAW;
2331 win_class.lpfnWndProc = WndProc;
2332 win_class.cbClsExtra = 0;
2333 win_class.cbWndExtra = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002334 win_class.hInstance = demo->connection; // hInstance
Ian Elliott639ca472015-04-16 15:23:05 -06002335 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2336 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2337 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2338 win_class.lpszMenuName = NULL;
2339 win_class.lpszClassName = demo->name;
2340 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2341 // Register window class:
2342 if (!RegisterClassEx(&win_class)) {
2343 // It didn't work, so try to give a useful error:
2344 printf("Unexpected error trying to start the application!\n");
2345 fflush(stdout);
2346 exit(1);
2347 }
2348 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002349 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002350 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002351 demo->window = CreateWindowEx(0,
Dave Houlton5fa47912018-02-16 11:02:26 -07002352 demo->name, // class name
2353 demo->name, // app name
2354 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002355 WS_VISIBLE | WS_SYSMENU,
Dave Houlton5fa47912018-02-16 11:02:26 -07002356 100, 100, // x/y coords
2357 wr.right - wr.left, // width
2358 wr.bottom - wr.top, // height
2359 NULL, // handle to parent
2360 NULL, // handle to menu
2361 demo->connection, // hInstance
2362 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002363 if (!demo->window) {
2364 // It didn't work, so try to give a useful error:
2365 printf("Cannot create a window in which to draw!\n");
2366 fflush(stdout);
2367 exit(1);
2368 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002369 // Window client area size must be at least 1 pixel high, to prevent crash.
2370 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
Dave Houlton5fa47912018-02-16 11:02:26 -07002371 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
Ian Elliott639ca472015-04-16 15:23:05 -06002372}
Tony Barbour8295ac42016-08-08 11:04:17 -06002373#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002374static void demo_create_xlib_window(struct demo *demo) {
Mike Weiblen5d2ad542018-02-13 13:56:13 -07002375 const char *display_envar = getenv("DISPLAY");
2376 if (display_envar == NULL || display_envar[0] == '\0') {
2377 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2378 fflush(stdout);
2379 exit(1);
2380 }
Tony Barbour2593b182016-04-19 10:57:58 -06002381
Tony Barbouree9cd372017-01-31 16:09:58 -07002382 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002383 demo->display = XOpenDisplay(NULL);
2384 long visualMask = VisualScreenMask;
2385 int numberOfVisuals;
Dave Houlton5fa47912018-02-16 11:02:26 -07002386 XVisualInfo vInfoTemplate = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002387 vInfoTemplate.screen = DefaultScreen(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002388 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
Tony Barbour2593b182016-04-19 10:57:58 -06002389
Dave Houlton5fa47912018-02-16 11:02:26 -07002390 Colormap colormap =
2391 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
Tony Barbour2593b182016-04-19 10:57:58 -06002392
Dave Houlton5fa47912018-02-16 11:02:26 -07002393 XSetWindowAttributes windowAttributes = {};
Tony Barbour2593b182016-04-19 10:57:58 -06002394 windowAttributes.colormap = colormap;
2395 windowAttributes.background_pixel = 0xFFFFFFFF;
2396 windowAttributes.border_pixel = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07002397 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
Tony Barbour2593b182016-04-19 10:57:58 -06002398
Dave Houlton5fa47912018-02-16 11:02:26 -07002399 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2400 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2401 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
Tony Barbour2593b182016-04-19 10:57:58 -06002402
2403 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2404 XMapWindow(demo->display, demo->xlib_window);
2405 XFlush(demo->display);
Dave Houlton5fa47912018-02-16 11:02:26 -07002406 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
Tony Barbour2593b182016-04-19 10:57:58 -06002407}
2408static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002409 switch (event->type) {
2410 case ClientMessage:
2411 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002412 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002413 case KeyPress:
2414 switch (event->xkey.keycode) {
2415 case 0x9: // Escape
2416 demo->quit = true;
2417 break;
2418 case 0x71: // left arrow key
2419 demo->spin_angle -= demo->spin_increment;
2420 break;
2421 case 0x72: // right arrow key
2422 demo->spin_angle += demo->spin_increment;
2423 break;
2424 case 0x41: // space bar
2425 demo->pause = !demo->pause;
2426 break;
2427 }
Tony Barbour2593b182016-04-19 10:57:58 -06002428 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002429 case ConfigureNotify:
2430 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2431 demo->width = event->xconfigure.width;
2432 demo->height = event->xconfigure.height;
2433 demo_resize(demo);
2434 }
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002435 break;
Dave Houlton5fa47912018-02-16 11:02:26 -07002436 default:
Tony Barbour2593b182016-04-19 10:57:58 -06002437 break;
Tony Barbour2593b182016-04-19 10:57:58 -06002438 }
Tony Barbour2593b182016-04-19 10:57:58 -06002439}
2440
2441static void demo_run_xlib(struct demo *demo) {
Tony Barbour2593b182016-04-19 10:57:58 -06002442 while (!demo->quit) {
2443 XEvent event;
2444
2445 if (demo->pause) {
2446 XNextEvent(demo->display, &event);
2447 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002448 }
2449 while (XPending(demo->display) > 0) {
2450 XNextEvent(demo->display, &event);
2451 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002452 }
2453
Tony Barbour2593b182016-04-19 10:57:58 -06002454 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002455 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002456 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Tony Barbour2593b182016-04-19 10:57:58 -06002457 }
2458}
Tony Barbour153cb062016-12-07 13:43:36 -07002459#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002460static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002461 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002462 switch (event_code) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002463 case XCB_EXPOSE:
2464 // TODO: Resize window
2465 break;
2466 case XCB_CLIENT_MESSAGE:
2467 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2468 demo->quit = true;
2469 }
2470 break;
2471 case XCB_KEY_RELEASE: {
2472 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002473
Dave Houlton5fa47912018-02-16 11:02:26 -07002474 switch (key->detail) {
2475 case 0x9: // Escape
2476 demo->quit = true;
2477 break;
2478 case 0x71: // left arrow key
2479 demo->spin_angle -= demo->spin_increment;
2480 break;
2481 case 0x72: // right arrow key
2482 demo->spin_angle += demo->spin_increment;
2483 break;
2484 case 0x41: // space bar
2485 demo->pause = !demo->pause;
2486 break;
2487 }
2488 } break;
2489 case XCB_CONFIGURE_NOTIFY: {
2490 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2491 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2492 demo->width = cfg->width;
2493 demo->height = cfg->height;
2494 demo_resize(demo);
2495 }
2496 } break;
2497 default:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002498 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002499 }
2500}
2501
Tony Barbour2593b182016-04-19 10:57:58 -06002502static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002503 xcb_flush(demo->connection);
2504
2505 while (!demo->quit) {
2506 xcb_generic_event_t *event;
2507
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002508 if (demo->pause) {
2509 event = xcb_wait_for_event(demo->connection);
Dave Houlton5fa47912018-02-16 11:02:26 -07002510 } else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002511 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002512 }
2513 while (event) {
2514 demo_handle_xcb_event(demo, event);
2515 free(event);
2516 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002517 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002518
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002519 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002520 demo->curFrame++;
Dave Houlton5fa47912018-02-16 11:02:26 -07002521 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002522 }
2523}
2524
Tony Barbour2593b182016-04-19 10:57:58 -06002525static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002526 uint32_t value_mask, value_list[32];
2527
Tony Barbour2593b182016-04-19 10:57:58 -06002528 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002529
2530 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2531 value_list[0] = demo->screen->black_pixel;
Dave Houlton5fa47912018-02-16 11:02:26 -07002532 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002533
Dave Houlton5fa47912018-02-16 11:02:26 -07002534 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2535 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002536
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002537 /* Magic code that will send notification when window is destroyed */
Dave Houlton5fa47912018-02-16 11:02:26 -07002538 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2539 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002540
Dave Houlton5fa47912018-02-16 11:02:26 -07002541 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2542 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002543
Dave Houlton5fa47912018-02-16 11:02:26 -07002544 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002545 &(*demo->atom_wm_delete_window).atom);
2546 free(reply);
2547
Tony Barbour2593b182016-04-19 10:57:58 -06002548 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002549
Karl Schultze4dc75c2016-02-02 15:37:51 -07002550 // Force the x/y coordinates to 100,100 results are identical in consecutive
2551 // runs
2552 const uint32_t coords[] = {100, 100};
Dave Houlton5fa47912018-02-16 11:02:26 -07002553 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002554}
Tony Barbour6b131662016-08-25 13:14:45 -06002555// VK_USE_PLATFORM_XCB_KHR
2556#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002557static void demo_run(struct demo *demo) {
2558 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002559 if (demo->pause) {
2560 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002561 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002562 wl_display_dispatch_pending(demo->display); // don't block
2563 demo_draw(demo);
2564 demo->curFrame++;
2565 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2566 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002567 }
2568}
2569
Dave Houlton5fa47912018-02-16 11:02:26 -07002570static void handle_ping(void *data UNUSED, struct wl_shell_surface *shell_surface, uint32_t serial) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002571 wl_shell_surface_pong(shell_surface, serial);
2572}
2573
Dave Houlton5fa47912018-02-16 11:02:26 -07002574static void handle_configure(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED,
2575 int32_t width UNUSED, int32_t height UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002576
Dave Houlton5fa47912018-02-16 11:02:26 -07002577static void handle_popup_done(void *data UNUSED, struct wl_shell_surface *shell_surface UNUSED) {}
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002578
Dave Houlton5fa47912018-02-16 11:02:26 -07002579static const struct wl_shell_surface_listener shell_surface_listener = {handle_ping, handle_configure, handle_popup_done};
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002580
2581static void demo_create_window(struct demo *demo) {
2582 demo->window = wl_compositor_create_surface(demo->compositor);
2583 if (!demo->window) {
2584 printf("Can not create wayland_surface from compositor!\n");
2585 fflush(stdout);
2586 exit(1);
2587 }
2588
2589 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2590 if (!demo->shell_surface) {
2591 printf("Can not get shell_surface from wayland_surface!\n");
2592 fflush(stdout);
2593 exit(1);
2594 }
Dave Houlton5fa47912018-02-16 11:02:26 -07002595 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002596 wl_shell_surface_set_toplevel(demo->shell_surface);
2597 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2598}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002599#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2600static void demo_run(struct demo *demo) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002601 if (!demo->prepared) return;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002602
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002603 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002604 demo->curFrame++;
2605}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002606#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002607#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2608static VkResult demo_create_display_surface(struct demo *demo) {
2609 VkResult U_ASSERT_ONLY err;
2610 uint32_t display_count;
2611 uint32_t mode_count;
2612 uint32_t plane_count;
2613 VkDisplayPropertiesKHR display_props;
2614 VkDisplayKHR display;
2615 VkDisplayModePropertiesKHR mode_props;
2616 VkDisplayPlanePropertiesKHR *plane_props;
2617 VkBool32 found_plane = VK_FALSE;
2618 uint32_t plane_index;
2619 VkExtent2D image_extent;
2620 VkDisplaySurfaceCreateInfoKHR create_info;
2621
2622 // Get the first display
2623 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2624 assert(!err);
2625
2626 if (display_count == 0) {
2627 printf("Cannot find any display!\n");
2628 fflush(stdout);
2629 exit(1);
2630 }
2631
2632 display_count = 1;
2633 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2634 assert(!err || (err == VK_INCOMPLETE));
2635
2636 display = display_props.display;
2637
2638 // Get the first mode of the display
2639 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2640 assert(!err);
2641
2642 if (mode_count == 0) {
2643 printf("Cannot find any mode for the display!\n");
2644 fflush(stdout);
2645 exit(1);
2646 }
2647
2648 mode_count = 1;
2649 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2650 assert(!err || (err == VK_INCOMPLETE));
2651
2652 // Get the list of planes
2653 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2654 assert(!err);
2655
2656 if (plane_count == 0) {
2657 printf("Cannot find any plane!\n");
2658 fflush(stdout);
2659 exit(1);
2660 }
2661
2662 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2663 assert(plane_props);
2664
2665 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2666 assert(!err);
2667
2668 // Find a plane compatible with the display
2669 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2670 uint32_t supported_count;
2671 VkDisplayKHR *supported_displays;
2672
2673 // Disqualify planes that are bound to a different display
Dave Houlton5fa47912018-02-16 11:02:26 -07002674 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
Damien Leone600c3052017-01-31 10:26:07 -07002675 continue;
2676 }
2677
2678 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2679 assert(!err);
2680
2681 if (supported_count == 0) {
2682 continue;
2683 }
2684
2685 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2686 assert(supported_displays);
2687
2688 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2689 assert(!err);
2690
2691 for (uint32_t i = 0; i < supported_count; i++) {
2692 if (supported_displays[i] == display) {
2693 found_plane = VK_TRUE;
2694 break;
2695 }
2696 }
2697
2698 free(supported_displays);
2699
2700 if (found_plane) {
2701 break;
2702 }
2703 }
2704
2705 if (!found_plane) {
2706 printf("Cannot find a plane compatible with the display!\n");
2707 fflush(stdout);
2708 exit(1);
2709 }
2710
2711 free(plane_props);
2712
Tony Barbour820b55b2017-03-14 08:55:46 -06002713 VkDisplayPlaneCapabilitiesKHR planeCaps;
2714 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2715 // Find a supported alpha mode
Wladimir J. van der Laan8ec4fce2017-10-07 14:17:41 +02002716 VkDisplayPlaneAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2717 VkDisplayPlaneAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002718 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2719 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2720 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2721 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2722 };
2723 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2724 if (planeCaps.supportedAlpha & alphaModes[i]) {
2725 alphaMode = alphaModes[i];
2726 break;
2727 }
2728 }
Damien Leone600c3052017-01-31 10:26:07 -07002729 image_extent.width = mode_props.parameters.visibleRegion.width;
2730 image_extent.height = mode_props.parameters.visibleRegion.height;
2731
2732 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2733 create_info.pNext = NULL;
2734 create_info.flags = 0;
2735 create_info.displayMode = mode_props.displayMode;
2736 create_info.planeIndex = plane_index;
2737 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2738 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002739 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002740 create_info.globalAlpha = 1.0f;
2741 create_info.imageExtent = image_extent;
2742
2743 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2744}
2745
Dave Houlton5fa47912018-02-16 11:02:26 -07002746static void demo_run_display(struct demo *demo) {
Damien Leone600c3052017-01-31 10:26:07 -07002747 while (!demo->quit) {
2748 demo_draw(demo);
2749 demo->curFrame++;
2750
2751 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2752 demo->quit = true;
2753 }
2754 }
2755}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002756#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002757
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002758/*
2759 * Return 1 (true) if all layer names specified in check_names
2760 * can be found in given layer properties.
2761 */
Dave Houlton5fa47912018-02-16 11:02:26 -07002762static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002763 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002764 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002765 for (uint32_t j = 0; j < layer_count; j++) {
2766 if (!strcmp(check_names[i], layers[j].layerName)) {
2767 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002768 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002769 }
2770 }
2771 if (!found) {
2772 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2773 return 0;
2774 }
2775 }
2776 return 1;
2777}
2778
Karl Schultze4dc75c2016-02-02 15:37:51 -07002779static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002780 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002781 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002782 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002783 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002784 char **instance_validation_layers = NULL;
2785 demo->enabled_extension_count = 0;
2786 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002787
Dave Houlton5fa47912018-02-16 11:02:26 -07002788 char *instance_validation_layers_alt1[] = {"VK_LAYER_LUNARG_standard_validation"};
Karl Schultz7c50ad62016-04-01 12:07:03 -06002789
Dave Houlton5fa47912018-02-16 11:02:26 -07002790 char *instance_validation_layers_alt2[] = {"VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2791 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
2792 "VK_LAYER_GOOGLE_unique_objects"};
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002793
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002794 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002795 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002796 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06002797 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002798 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002799
Karl Schultz7c50ad62016-04-01 12:07:03 -06002800 instance_validation_layers = instance_validation_layers_alt1;
2801 if (instance_layer_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002802 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2803 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002804 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002805
Dave Houlton5fa47912018-02-16 11:02:26 -07002806 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt1), instance_validation_layers,
2807 instance_layer_count, instance_layers);
Karl Schultz7c50ad62016-04-01 12:07:03 -06002808 if (validation_found) {
2809 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002810 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2811 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002812 } else {
2813 // use alternative set of validation layers
2814 instance_validation_layers = instance_validation_layers_alt2;
2815 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Dave Houlton5fa47912018-02-16 11:02:26 -07002816 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers_alt2), instance_validation_layers,
2817 instance_layer_count, instance_layers);
2818 validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002819 for (uint32_t i = 0; i < validation_layer_count; i++) {
2820 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002821 }
2822 }
2823 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002824 }
Dustin Graves7c287352016-01-27 13:48:06 -07002825
Karl Schultz7c50ad62016-04-01 12:07:03 -06002826 if (!validation_found) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002827 ERR_EXIT(
2828 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
2829 "Please look at the Getting Started guide for additional information.\n",
2830 "vkCreateInstance Failure");
Karl Schultz7c50ad62016-04-01 12:07:03 -06002831 }
Dustin Graves7c287352016-01-27 13:48:06 -07002832 }
2833
2834 /* Look for instance extensions */
2835 VkBool32 surfaceExtFound = 0;
2836 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002837 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07002838
Dave Houlton5fa47912018-02-16 11:02:26 -07002839 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002840 assert(!err);
2841
Dustin Graves7c287352016-01-27 13:48:06 -07002842 if (instance_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002843 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2844 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07002845 assert(!err);
2846 for (uint32_t i = 0; i < instance_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002847 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002848 surfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002849 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002850 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002851#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002852 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002853 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002854 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002855 }
Tony Barbour153cb062016-12-07 13:43:36 -07002856#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002857 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Tony Barbour2593b182016-04-19 10:57:58 -06002858 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002859 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
Tony Barbour2593b182016-04-19 10:57:58 -06002860 }
Tony Barbour153cb062016-12-07 13:43:36 -07002861#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002862 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002863 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002864 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002865 }
Tony Barbour153cb062016-12-07 13:43:36 -07002866#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002867 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002868 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002869 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002870 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002871#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002872#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002873 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Damien Leone600c3052017-01-31 10:26:07 -07002874 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002875 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
Damien Leone600c3052017-01-31 10:26:07 -07002876 }
Tony Barbour153cb062016-12-07 13:43:36 -07002877#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002878 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002879 platformSurfaceExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07002880 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002881 }
Bill Hollingsf4352142017-02-14 22:58:56 -05002882#elif defined(VK_USE_PLATFORM_IOS_MVK)
2883 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2884 platformSurfaceExtFound = 1;
2885 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
2886 }
2887#elif defined(VK_USE_PLATFORM_MACOS_MVK)
2888 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
2889 platformSurfaceExtFound = 1;
2890 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
2891 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002892#endif
Dave Houlton5fa47912018-02-16 11:02:26 -07002893 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07002894 if (demo->validate) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002895 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07002896 }
2897 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06002898 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002899 }
Dustin Graves7c287352016-01-27 13:48:06 -07002900
2901 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002902 }
Dustin Graves7c287352016-01-27 13:48:06 -07002903
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002904 if (!surfaceExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07002905 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
2906 " extension.\n\n"
2907 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2908 "Please look at the Getting Started guide for additional information.\n",
Ian Elliott65152912015-04-28 13:22:33 -06002909 "vkCreateInstance Failure");
2910 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002911 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002912#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002913 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
2914 " extension.\n\n"
2915 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2916 "Please look at the Getting Started guide for additional information.\n",
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07002917 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002918#elif defined(VK_USE_PLATFORM_IOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07002919 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_IOS_SURFACE_EXTENSION_NAME
2920 " extension.\n\n"
2921 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2922 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06002923 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05002924#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07002925 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_MVK_MACOS_SURFACE_EXTENSION_NAME
2926 " extension.\n\n"
2927 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2928 "Please look at the Getting Started guide for additional information.\n",
Tony Barbourc65cd752017-03-13 15:29:35 -06002929 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002930#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002931 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
2932 " extension.\n\n"
2933 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2934 "Please look at the Getting Started guide for additional information.\n",
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07002935 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002936#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002937 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_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",
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002941 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002942#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002943#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002944 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
2945 " extension.\n\n"
2946 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2947 "Please look at the Getting Started guide for additional information.\n",
Damien Leone600c3052017-01-31 10:26:07 -07002948 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002949#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002950 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
2951 " extension.\n\n"
2952 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2953 "Please look at the Getting Started guide for additional information.\n",
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002954 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07002955#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Dave Houlton5fa47912018-02-16 11:02:26 -07002956 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
2957 " extension.\n\n"
2958 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
2959 "Please look at the Getting Started guide for additional information.\n",
Tony Barbour2593b182016-04-19 10:57:58 -06002960 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002961#endif
Tony Barbour153cb062016-12-07 13:43:36 -07002962 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002963 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002964 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002965 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002966 .pApplicationName = APP_SHORT_NAME,
2967 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002968 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002969 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06002970 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002971 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002972 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002973 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002974 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002975 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06002976 .enabledLayerCount = demo->enabled_layer_count,
2977 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
2978 .enabledExtensionCount = demo->enabled_extension_count,
2979 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002980 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06002981
2982 /*
2983 * This is info for a temp callback to use during CreateInstance.
2984 * After the instance is created, we use the instance-based
2985 * function to register the final callback.
2986 */
Karl Schultza2615462017-01-20 13:19:20 -07002987 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06002988 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06002989 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07002990 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
2991 dbgCreateInfoTemp.pNext = NULL;
2992 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
2993 dbgCreateInfoTemp.pUserData = demo;
Dave Houlton5fa47912018-02-16 11:02:26 -07002994 dbgCreateInfoTemp.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06002995 if (demo->validate_checks_disabled) {
2996 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
2997 val_flags.pNext = NULL;
2998 val_flags.disabledValidationCheckCount = 1;
2999 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
3000 val_flags.pDisabledValidationChecks = &disabled_check;
Dave Houlton5fa47912018-02-16 11:02:26 -07003001 dbgCreateInfoTemp.pNext = (void *)&val_flags;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003002 }
Karl Schultza2615462017-01-20 13:19:20 -07003003 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003004 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003005
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003006 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003007
Chia-I Wu63636062015-10-26 21:10:41 +08003008 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003009 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003010 ERR_EXIT(
3011 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3012 "Please look at the Getting Started guide for additional information.\n",
3013 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003014 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003015 ERR_EXIT(
3016 "Cannot find a specified extension library.\n"
3017 "Make sure your layers path is set appropriately.\n",
3018 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003019 } else if (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003020 ERR_EXIT(
3021 "vkCreateInstance failed.\n\n"
3022 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3023 "Please look at the Getting Started guide for additional information.\n",
3024 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003025 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003026
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003027 /* Make initial call to query gpu_count, then second call for gpu info*/
3028 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3029 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003030
3031 if (gpu_count > 0) {
3032 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3033 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3034 assert(!err);
3035 /* For cube demo we just grab the first physical device */
3036 demo->gpu = physical_devices[0];
3037 free(physical_devices);
3038 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003039 ERR_EXIT(
3040 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3041 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3042 "Please look at the Getting Started guide for additional information.\n",
3043 "vkEnumeratePhysicalDevices Failure");
Dustin Graves7c287352016-01-27 13:48:06 -07003044 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003045
Dustin Graves7c287352016-01-27 13:48:06 -07003046 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003047 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003048 VkBool32 swapchainExtFound = 0;
3049 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003050 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003051
Dave Houlton5fa47912018-02-16 11:02:26 -07003052 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003053 assert(!err);
3054
Dustin Graves7c287352016-01-27 13:48:06 -07003055 if (device_extension_count > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003056 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3057 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
Dustin Graves7c287352016-01-27 13:48:06 -07003058 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003059
Dustin Graves7c287352016-01-27 13:48:06 -07003060 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003061 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
Dustin Graves7c287352016-01-27 13:48:06 -07003062 swapchainExtFound = 1;
Dave Houlton5fa47912018-02-16 11:02:26 -07003063 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
Dustin Graves7c287352016-01-27 13:48:06 -07003064 }
3065 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003066 }
Dustin Graves7c287352016-01-27 13:48:06 -07003067
Ian Elliott53622a72017-03-28 11:06:33 -06003068 if (demo->VK_KHR_incremental_present_enabled) {
3069 // Even though the user "enabled" the extension via the command
3070 // line, we must make sure that it's enumerated for use with the
3071 // device. Therefore, disable it here, and re-enable it again if
3072 // enumerated.
3073 demo->VK_KHR_incremental_present_enabled = false;
3074 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003075 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3076 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
Ian Elliott53622a72017-03-28 11:06:33 -06003077 demo->VK_KHR_incremental_present_enabled = true;
3078 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3079 }
3080 assert(demo->enabled_extension_count < 64);
3081 }
3082 if (!demo->VK_KHR_incremental_present_enabled) {
3083 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3084 }
3085 }
3086
Ian Elliottd940ca22017-03-22 08:31:27 -06003087 if (demo->VK_GOOGLE_display_timing_enabled) {
3088 // Even though the user "enabled" the extension via the command
3089 // line, we must make sure that it's enumerated for use with the
3090 // device. Therefore, disable it here, and re-enable it again if
3091 // enumerated.
3092 demo->VK_GOOGLE_display_timing_enabled = false;
3093 for (uint32_t i = 0; i < device_extension_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003094 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3095 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
Ian Elliottd940ca22017-03-22 08:31:27 -06003096 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003097 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003098 }
3099 assert(demo->enabled_extension_count < 64);
3100 }
3101 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003102 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003103 }
3104 }
3105
Dustin Graves7c287352016-01-27 13:48:06 -07003106 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003107 }
Dustin Graves7c287352016-01-27 13:48:06 -07003108
Tony Barbourcc69f452015-10-08 13:59:42 -06003109 if (!swapchainExtFound) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003110 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3111 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3112 "Please look at the Getting Started guide for additional information.\n",
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003113 "vkCreateInstance Failure");
3114 }
3115
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003116 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003117 demo->CreateDebugReportCallback =
Dave Houlton5fa47912018-02-16 11:02:26 -07003118 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugReportCallbackEXT");
Karl Schultze4dc75c2016-02-02 15:37:51 -07003119 demo->DestroyDebugReportCallback =
Dave Houlton5fa47912018-02-16 11:02:26 -07003120 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003121 if (!demo->CreateDebugReportCallback) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003122 ERR_EXIT("GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n", "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003123 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003124 if (!demo->DestroyDebugReportCallback) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003125 ERR_EXIT("GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n", "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003126 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003127 demo->DebugReportMessage = (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003128 if (!demo->DebugReportMessage) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003129 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n", "vkGetProcAddr Failure");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003130 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003131
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003132 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003133 PFN_vkDebugReportCallbackEXT callback;
3134 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003135 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003136 dbgCreateInfo.pNext = NULL;
3137 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003138 dbgCreateInfo.pUserData = demo;
Dave Houlton5fa47912018-02-16 11:02:26 -07003139 dbgCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
3140 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL, &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003141 switch (err) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003142 case VK_SUCCESS:
3143 break;
3144 case VK_ERROR_OUT_OF_HOST_MEMORY:
3145 ERR_EXIT("CreateDebugReportCallback: out of host memory\n", "CreateDebugReportCallback Failure");
3146 break;
3147 default:
3148 ERR_EXIT("CreateDebugReportCallback: unknown failure\n", "CreateDebugReportCallback Failure");
3149 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003150 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003151 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003152 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003153
3154 /* Call with NULL data to get count */
Dave Houlton5fa47912018-02-16 11:02:26 -07003155 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
Tony Barbourab2a0362016-09-06 11:40:12 -06003156 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003157
Dave Houlton5fa47912018-02-16 11:02:26 -07003158 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3159 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
Tony Barbourab2a0362016-09-06 11:40:12 -06003160
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003161 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003162 // If app has specific feature requirements it should check supported
3163 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003164 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003165 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003166
Tony Barbourda2bad52016-01-22 14:36:40 -07003167 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3168 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3169 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3170 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3171 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3172}
3173
Karl Schultze4dc75c2016-02-02 15:37:51 -07003174static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003175 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003176 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003177 VkDeviceQueueCreateInfo queues[2];
3178 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3179 queues[0].pNext = NULL;
3180 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3181 queues[0].queueCount = 1;
3182 queues[0].pQueuePriorities = queue_priorities;
3183 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003184
3185 VkDeviceCreateInfo device = {
3186 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3187 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003188 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003189 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003190 .enabledLayerCount = 0,
3191 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003192 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003193 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Dave Houlton5fa47912018-02-16 11:02:26 -07003194 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003195 };
Tony Barbour22e06272016-09-07 16:07:56 -06003196 if (demo->separate_present_queue) {
3197 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3198 queues[1].pNext = NULL;
3199 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3200 queues[1].queueCount = 1;
3201 queues[1].pQueuePriorities = queue_priorities;
3202 queues[1].flags = 0;
3203 device.queueCreateInfoCount = 2;
3204 }
Chia-I Wu63636062015-10-26 21:10:41 +08003205 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003206 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003207}
3208
Karl Schultze4dc75c2016-02-02 15:37:51 -07003209static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003210 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003211
Karl Schultze4dc75c2016-02-02 15:37:51 -07003212// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003213#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003214 VkWin32SurfaceCreateInfoKHR createInfo;
3215 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3216 createInfo.pNext = NULL;
3217 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003218 createInfo.hinstance = demo->connection;
3219 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003220
Dave Houlton5fa47912018-02-16 11:02:26 -07003221 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003222#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003223 VkWaylandSurfaceCreateInfoKHR createInfo;
3224 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3225 createInfo.pNext = NULL;
3226 createInfo.flags = 0;
3227 createInfo.display = demo->display;
3228 createInfo.surface = demo->window;
3229
Dave Houlton5fa47912018-02-16 11:02:26 -07003230 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003231#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003232#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3233 VkAndroidSurfaceCreateInfoKHR createInfo;
3234 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3235 createInfo.pNext = NULL;
3236 createInfo.flags = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003237 createInfo.window = (ANativeWindow *)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003238
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003239 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003240#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3241 VkXlibSurfaceCreateInfoKHR createInfo;
3242 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3243 createInfo.pNext = NULL;
3244 createInfo.flags = 0;
3245 createInfo.dpy = demo->display;
3246 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003247
Dave Houlton5fa47912018-02-16 11:02:26 -07003248 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003249#elif defined(VK_USE_PLATFORM_XCB_KHR)
3250 VkXcbSurfaceCreateInfoKHR createInfo;
3251 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3252 createInfo.pNext = NULL;
3253 createInfo.flags = 0;
3254 createInfo.connection = demo->connection;
3255 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003256
Tony Barbour153cb062016-12-07 13:43:36 -07003257 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003258#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3259 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003260#elif defined(VK_USE_PLATFORM_IOS_MVK)
3261 VkIOSSurfaceCreateInfoMVK surface;
3262 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3263 surface.pNext = NULL;
3264 surface.flags = 0;
3265 surface.pView = demo->window;
3266
3267 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3268#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3269 VkMacOSSurfaceCreateInfoMVK surface;
3270 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3271 surface.pNext = NULL;
3272 surface.flags = 0;
3273 surface.pView = demo->window;
3274
3275 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003276#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003277 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003278
Tony Barbourcc69f452015-10-08 13:59:42 -06003279 // Iterate over each queue to learn whether it supports presenting:
Dave Houlton5fa47912018-02-16 11:02:26 -07003280 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003281 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003282 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003283 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003284
3285 // Search for a graphics and a present queue in the array of queue
3286 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003287 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3288 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003289 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003290 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3291 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3292 graphicsQueueFamilyIndex = i;
3293 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003294
Tony Barbourab2a0362016-09-06 11:40:12 -06003295 if (supportsPresent[i] == VK_TRUE) {
3296 graphicsQueueFamilyIndex = i;
3297 presentQueueFamilyIndex = i;
3298 break;
3299 }
3300 }
3301 }
3302
3303 if (presentQueueFamilyIndex == UINT32_MAX) {
3304 // If didn't find a queue that supports both graphics and present, then
3305 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003306 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003307 if (supportsPresent[i] == VK_TRUE) {
3308 presentQueueFamilyIndex = i;
3309 break;
3310 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003311 }
3312 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003313
3314 // Generate error if could not find both a graphics and a present queue
Dave Houlton5fa47912018-02-16 11:02:26 -07003315 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3316 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003317 }
3318
Tony Barbourab2a0362016-09-06 11:40:12 -06003319 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3320 demo->present_queue_family_index = presentQueueFamilyIndex;
Dave Houlton5fa47912018-02-16 11:02:26 -07003321 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003322 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003323
Tony Barbourda2bad52016-01-22 14:36:40 -07003324 demo_create_device(demo);
3325
3326 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3327 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3328 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3329 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3330 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003331 if (demo->VK_GOOGLE_display_timing_enabled) {
3332 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3333 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3334 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003335
Dave Houlton5fa47912018-02-16 11:02:26 -07003336 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003337
Tony Barbour22e06272016-09-07 16:07:56 -06003338 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003339 demo->present_queue = demo->graphics_queue;
3340 } else {
Dave Houlton5fa47912018-02-16 11:02:26 -07003341 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
Tony Barboura2a67812016-07-18 13:21:06 -06003342 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003343
Ian Elliottaaae5352015-07-06 14:27:58 -06003344 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003345 uint32_t formatCount;
Dave Houlton5fa47912018-02-16 11:02:26 -07003346 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003347 assert(!err);
Dave Houlton5fa47912018-02-16 11:02:26 -07003348 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3349 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003350 assert(!err);
3351 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3352 // the surface has no preferred format. Otherwise, at least one
3353 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003354 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003355 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003356 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003357 assert(formatCount >= 1);
3358 demo->format = surfFormats[0].format;
3359 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003360 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003361
David Pinedo2bb7c932015-06-18 17:03:14 -06003362 demo->quit = false;
3363 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003364
Tony Barbource7524e2016-07-28 12:01:45 -06003365 // Create semaphores to synchronize acquiring presentable buffers before
3366 // rendering and waiting for drawing to be complete before presenting
3367 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3368 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3369 .pNext = NULL,
3370 .flags = 0,
3371 };
Tony Barbource7524e2016-07-28 12:01:45 -06003372
Tony Barbour66a56c32016-09-21 13:10:56 -06003373 // Create fences that we can use to throttle if we get too far
3374 // ahead of the image presents
3375 VkFenceCreateInfo fence_ci = {
Dave Houlton5fa47912018-02-16 11:02:26 -07003376 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
Tony Barbour66a56c32016-09-21 13:10:56 -06003377 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003378 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3379 assert(!err);
3380
Dave Houlton5fa47912018-02-16 11:02:26 -07003381 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003382 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003383
Dave Houlton5fa47912018-02-16 11:02:26 -07003384 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003385 assert(!err);
3386
3387 if (demo->separate_present_queue) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003388 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
Tony Barbour66a56c32016-09-21 13:10:56 -06003389 assert(!err);
3390 }
Tony Barbour22e06272016-09-07 16:07:56 -06003391 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003392 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003393
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003394 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003395 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003396}
3397
Tony Barbour153cb062016-12-07 13:43:36 -07003398#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003399static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3400 wl_fixed_t sy) {}
3401
3402static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3403
3404static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3405
3406static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3407 uint32_t state) {
3408 struct demo *demo = data;
3409 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3410 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
3411 }
3412}
3413
3414static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3415
3416static const struct wl_pointer_listener pointer_listener = {
3417 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3418};
3419
3420static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3421
3422static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3423 struct wl_array *keys) {}
3424
3425static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3426
3427static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3428 uint32_t state) {
3429 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3430 struct demo *demo = data;
3431 switch (key) {
3432 case KEY_ESC: // Escape
3433 demo->quit = true;
3434 break;
3435 case KEY_LEFT: // left arrow key
3436 demo->spin_angle -= demo->spin_increment;
3437 break;
3438 case KEY_RIGHT: // right arrow key
3439 demo->spin_angle += demo->spin_increment;
3440 break;
3441 case KEY_SPACE: // space bar
3442 demo->pause = !demo->pause;
3443 break;
3444 }
3445}
3446
3447static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3448 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3449
3450static const struct wl_keyboard_listener keyboard_listener = {
3451 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3452};
3453
3454static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3455 // Subscribe to pointer events
3456 struct demo *demo = data;
3457 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3458 demo->pointer = wl_seat_get_pointer(seat);
3459 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3460 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3461 wl_pointer_destroy(demo->pointer);
3462 demo->pointer = NULL;
3463 }
3464 // Subscribe to keyboard events
3465 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3466 demo->keyboard = wl_seat_get_keyboard(seat);
3467 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3468 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3469 wl_keyboard_destroy(demo->keyboard);
3470 demo->keyboard = NULL;
3471 }
3472}
3473
3474static const struct wl_seat_listener seat_listener = {
3475 seat_handle_capabilities,
3476};
3477
3478static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003479 uint32_t version UNUSED) {
3480 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003481 // pickup wayland objects when they appear
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003482 if (strcmp(interface, "wl_compositor") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003483 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003484 } else if (strcmp(interface, "wl_shell") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003485 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3486 } else if (strcmp(interface, "wl_seat") == 0) {
3487 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3488 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003489 }
3490}
3491
Dave Houlton5fa47912018-02-16 11:02:26 -07003492static 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 +09003493
Dave Houlton5fa47912018-02-16 11:02:26 -07003494static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003495#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003496#endif
3497
Karl Schultze4dc75c2016-02-02 15:37:51 -07003498static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003499#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003500 const xcb_setup_t *setup;
3501 xcb_screen_iterator_t iter;
3502 int scr;
3503
Mike Weiblen5d2ad542018-02-13 13:56:13 -07003504 const char *display_envar = getenv("DISPLAY");
3505 if (display_envar == NULL || display_envar[0] == '\0') {
3506 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3507 fflush(stdout);
3508 exit(1);
3509 }
3510
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003511 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003512 if (xcb_connection_has_error(demo->connection) > 0) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003513 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06003514 fflush(stdout);
3515 exit(1);
3516 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003517
3518 setup = xcb_get_setup(demo->connection);
3519 iter = xcb_setup_roots_iterator(setup);
Dave Houlton5fa47912018-02-16 11:02:26 -07003520 while (scr-- > 0) xcb_screen_next(&iter);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003521
3522 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003523#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3524 demo->display = wl_display_connect(NULL);
3525
3526 if (demo->display == NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003527 printf("Cannot find a compatible Vulkan installable client driver (ICD).\nExiting ...\n");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003528 fflush(stdout);
3529 exit(1);
3530 }
3531
3532 demo->registry = wl_display_get_registry(demo->display);
3533 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3534 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003535#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003536#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003537}
3538
Karl Schultze4dc75c2016-02-02 15:37:51 -07003539static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003540 vec3 eye = {0.0f, 3.0f, 5.0f};
3541 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003542 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003543
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003544 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003545 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003546 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003547
Piers Daniell735ee532015-02-23 16:23:13 -07003548 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003549 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003550 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003551 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003552 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003553 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3554 demo->presentMode = atoi(argv[i + 1]);
Tony Barbour291d57b2016-10-21 14:47:07 -06003555 i++;
3556 continue;
3557 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003558 if (strcmp(argv[i], "--break") == 0) {
3559 demo->use_break = true;
3560 continue;
3561 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003562 if (strcmp(argv[i], "--validate") == 0) {
3563 demo->validate = true;
3564 continue;
3565 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003566 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3567 demo->validate = true;
3568 demo->validate_checks_disabled = true;
3569 continue;
3570 }
Tony Barbour2593b182016-04-19 10:57:58 -06003571 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003572 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003573 continue;
3574 }
Dave Houlton5fa47912018-02-16 11:02:26 -07003575 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3576 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003577 i++;
3578 continue;
3579 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003580 if (strcmp(argv[i], "--suppress_popups") == 0) {
3581 demo->suppress_popups = true;
3582 continue;
3583 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003584 if (strcmp(argv[i], "--display_timing") == 0) {
3585 demo->VK_GOOGLE_display_timing_enabled = true;
3586 continue;
3587 }
Ian Elliott53622a72017-03-28 11:06:33 -06003588 if (strcmp(argv[i], "--incremental_present") == 0) {
3589 demo->VK_KHR_incremental_present_enabled = true;
3590 continue;
3591 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003592
Cody Northropaf28a532016-09-27 10:50:57 -06003593#if defined(ANDROID)
3594 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3595#else
Mike Schuchardt9a881512017-11-01 16:16:22 -06003596 fprintf(stderr,
3597 "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled]\n"
3598 " [--break] [--c <framecount>] [--suppress_popups]\n"
3599 " [--incremental_present] [--display_timing]\n"
3600 " [--present_mode {0,1,2,3}]\n"
3601 "\n"
3602 "Options for --present_mode:\n"
3603 " %d: VK_PRESENT_MODE_IMMEDIATE_KHR\n"
3604 " %d: VK_PRESENT_MODE_MAILBOX_KHR\n"
3605 " %d: VK_PRESENT_MODE_FIFO_KHR (default)\n"
3606 " %d: VK_PRESENT_MODE_FIFO_RELAXED_KHR\n",
3607 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
3608 VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003609 fflush(stderr);
3610 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003611#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003612 }
3613
Tony Barbour153cb062016-12-07 13:43:36 -07003614 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003615
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003616 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003617
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003618 demo->width = 500;
3619 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003620
Tony Barbour66a56c32016-09-21 13:10:56 -06003621 demo->spin_angle = 4.0f;
3622 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003623 demo->pause = false;
3624
Dave Houlton5fa47912018-02-16 11:02:26 -07003625 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003626 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3627 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003628
Dave Houlton5fa47912018-02-16 11:02:26 -07003629 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003630}
3631
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003632#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003633// Include header required for parsing the command line options.
3634#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003635
Dave Houlton5fa47912018-02-16 11:02:26 -07003636int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
3637 MSG msg; // message
3638 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003639 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003640 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003641
Jamie Madillb2ff6502017-03-15 16:17:46 -04003642 // Ensure wParam is initialized.
3643 msg.wParam = 0;
3644
Karl Schultze4dc75c2016-02-02 15:37:51 -07003645 // Use the CommandLine functions to get the command line arguments.
3646 // Unfortunately, Microsoft outputs
3647 // this information as wide characters for Unicode, and we simply want the
3648 // Ascii version to be compatible
3649 // with the non-Windows side. So, we have to convert the information to
3650 // Ascii character strings.
3651 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3652 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003653 argc = 0;
3654 }
3655
Karl Schultze4dc75c2016-02-02 15:37:51 -07003656 if (argc > 0) {
3657 argv = (char **)malloc(sizeof(char *) * argc);
3658 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003659 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003660 } else {
3661 for (int iii = 0; iii < argc; iii++) {
3662 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003663 size_t numConverted = 0;
3664
Karl Schultze4dc75c2016-02-02 15:37:51 -07003665 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3666 if (argv[iii] != NULL) {
Dave Houlton5fa47912018-02-16 11:02:26 -07003667 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003668 }
3669 }
3670 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003671 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003672 argv = NULL;
3673 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003674
3675 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003676
3677 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003678 if (argc > 0 && argv != NULL) {
3679 for (int iii = 0; iii < argc; iii++) {
3680 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003681 free(argv[iii]);
3682 }
3683 }
3684 free(argv);
3685 }
3686
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003687 demo.connection = hInstance;
3688 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003689 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003690 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003691
3692 demo_prepare(&demo);
3693
Dave Houlton5fa47912018-02-16 11:02:26 -07003694 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003695
3696 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003697 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003698 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Dave Houlton5fa47912018-02-16 11:02:26 -07003699 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003700 {
Dave Houlton5fa47912018-02-16 11:02:26 -07003701 done = true; // if found, quit app
Karl Schultze4dc75c2016-02-02 15:37:51 -07003702 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003703 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003704 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003705 DispatchMessage(&msg);
3706 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003707 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003708 }
3709
3710 demo_cleanup(&demo);
3711
Karl Schultze4dc75c2016-02-02 15:37:51 -07003712 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003713}
Bill Hollingsf4352142017-02-14 22:58:56 -05003714
3715#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
Dave Houlton5fa47912018-02-16 11:02:26 -07003716static void demo_main(struct demo *demo, void *view) {
3717 const char *argv[] = {"CubeSample"};
3718 int argc = sizeof(argv) / sizeof(char *);
Bill Hollingsf4352142017-02-14 22:58:56 -05003719
Dave Houlton5fa47912018-02-16 11:02:26 -07003720 demo_init(demo, argc, (char **)argv);
Tony Barbourc65cd752017-03-13 15:29:35 -06003721 demo->window = view;
3722 demo_init_vk_swapchain(demo);
3723 demo_prepare(demo);
3724 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003725}
3726
3727static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003728 // Wait for work to finish before updating MVP.
3729 vkDeviceWaitIdle(demo->device);
3730 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003731
Tony Barbourc65cd752017-03-13 15:29:35 -06003732 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003733}
3734
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003735#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3736#include <android/log.h>
3737#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003738#include "android_util.h"
3739
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003740static bool initialized = false;
3741static bool active = false;
3742struct demo demo;
3743
Dave Houlton5fa47912018-02-16 11:02:26 -07003744static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003745
Dave Houlton5fa47912018-02-16 11:02:26 -07003746static void processCommand(struct android_app *app, int32_t cmd) {
3747 switch (cmd) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003748 case APP_CMD_INIT_WINDOW: {
3749 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003750 // We're getting a new window. If the app is starting up, we
3751 // need to initialize. If the app has already been
3752 // initialized, that means that we lost our previous window,
3753 // which means that we have a lot of work to do. At a minimum,
3754 // we need to destroy the swapchain and surface associated with
3755 // the old window, and create a new surface and swapchain.
3756 // However, since there are a lot of other objects/state that
3757 // is tied to the swapchain, it's easiest to simply cleanup and
3758 // start over (i.e. use a brute-force approach of re-starting
3759 // the app)
3760 if (demo.prepared) {
3761 demo_cleanup(&demo);
3762 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003763
3764 // Parse Intents into argc, argv
3765 // Use the following key to send arguments, i.e.
3766 // --es args "--validate"
3767 const char key[] = "args";
Dave Houlton5fa47912018-02-16 11:02:26 -07003768 char *appTag = (char *)APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003769 int argc = 0;
Dave Houlton5fa47912018-02-16 11:02:26 -07003770 char **argv = get_args(app, key, appTag, &argc);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003771
3772 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
Dave Houlton5fa47912018-02-16 11:02:26 -07003773 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 -06003774
3775 demo_init(&demo, argc, argv);
3776
3777 // Free the argv malloc'd by get_args
Dave Houlton5fa47912018-02-16 11:02:26 -07003778 for (int i = 0; i < argc; i++) free(argv[i]);
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003779
Dave Houlton5fa47912018-02-16 11:02:26 -07003780 demo.window = (void *)app->window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003781 demo_init_vk_swapchain(&demo);
3782 demo_prepare(&demo);
3783 initialized = true;
3784 }
3785 break;
3786 }
3787 case APP_CMD_GAINED_FOCUS: {
3788 active = true;
3789 break;
3790 }
3791 case APP_CMD_LOST_FOCUS: {
3792 active = false;
3793 break;
3794 }
3795 }
3796}
3797
Dave Houlton5fa47912018-02-16 11:02:26 -07003798void android_main(struct android_app *app) {
Cody Northrop8707a6e2016-04-26 19:59:19 -06003799#ifdef ANDROID
3800 int vulkanSupport = InitVulkan();
Dave Houlton5fa47912018-02-16 11:02:26 -07003801 if (vulkanSupport == 0) return;
Cody Northrop8707a6e2016-04-26 19:59:19 -06003802#endif
3803
Ian Elliottf05d5d12016-05-17 12:03:35 -06003804 demo.prepared = false;
3805
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003806 app->onAppCmd = processCommand;
3807 app->onInputEvent = processInput;
3808
Dave Houlton5fa47912018-02-16 11:02:26 -07003809 while (1) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003810 int events;
Dave Houlton5fa47912018-02-16 11:02:26 -07003811 struct android_poll_source *source;
3812 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003813 if (source) {
3814 source->process(app, source);
3815 }
3816
3817 if (app->destroyRequested != 0) {
3818 demo_cleanup(&demo);
3819 return;
3820 }
3821 }
3822 if (initialized && active) {
3823 demo_run(&demo);
3824 }
3825 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003826}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06003827#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07003828int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003829 struct demo demo;
3830
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003831 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07003832#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003833 demo_create_xcb_window(&demo);
3834#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3835 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003836#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3837 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003838#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003839#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003840
Tony Barbourcc69f452015-10-08 13:59:42 -06003841 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003842
3843 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003844
Tony Barbour153cb062016-12-07 13:43:36 -07003845#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06003846 demo_run_xcb(&demo);
3847#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3848 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003849#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3850 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003851#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003852#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3853 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003854#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003855
3856 demo_cleanup(&demo);
3857
Karl Schultz0218c002016-03-22 17:06:13 -06003858 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003859}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003860#endif