blob: e5b10b0507cffff1951ced3f6f43d62c9cfcd009 [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 /*
521 * 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 */
527 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}
548bool CanPresentEarlier(uint64_t earliest,
549 uint64_t actual,
550 uint64_t margin,
551 uint64_t rdur) {
552 if (earliest < actual) {
553 // Consider whether this present could have occured earlier. Make sure
554 // that earliest time was at least 2msec earlier than actual time, and
555 // that the margin was at least 2msec:
556 uint64_t diff = actual - earliest;
557 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
558 // This present could have occured earlier because both: 1) the
559 // earliest time was at least 2 msec before actual time, and 2) the
560 // margin was at least 2msec.
561 return true;
562 }
563 }
564 return false;
565}
566
Ian Elliottcf074d32015-10-16 18:02:43 -0600567// Forward declaration:
568static void demo_resize(struct demo *demo);
569
Karl Schultze4dc75c2016-02-02 15:37:51 -0700570static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
571 VkFlags requirements_mask,
572 uint32_t *typeIndex) {
573 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600574 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700575 if ((typeBits & 1) == 1) {
576 // Type is available, does it match user properties?
577 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
578 requirements_mask) == requirements_mask) {
579 *typeIndex = i;
580 return true;
581 }
582 }
583 typeBits >>= 1;
584 }
585 // No memory types matched, return failure
586 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600587}
588
Karl Schultze4dc75c2016-02-02 15:37:51 -0700589static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600590 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600591
Tony Barbource7524e2016-07-28 12:01:45 -0600592 // This function could get called twice if the texture uses a staging buffer
593 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600594 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600595 return;
596
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600597 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600598 assert(!err);
599
Tony Barbource7524e2016-07-28 12:01:45 -0600600 VkFence fence;
601 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
602 .pNext = NULL,
603 .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700604 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
605 assert(!err);
606
Karl Schultze4dc75c2016-02-02 15:37:51 -0700607 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700608 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
609 .pNext = NULL,
610 .waitSemaphoreCount = 0,
611 .pWaitSemaphores = NULL,
612 .pWaitDstStageMask = NULL,
613 .commandBufferCount = 1,
614 .pCommandBuffers = cmd_bufs,
615 .signalSemaphoreCount = 0,
616 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600617
Tony Barbource7524e2016-07-28 12:01:45 -0600618 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600619 assert(!err);
620
Tony Barbource7524e2016-07-28 12:01:45 -0600621 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600622 assert(!err);
623
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600624 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600625 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600626 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600627}
628
Karl Schultze4dc75c2016-02-02 15:37:51 -0700629static void demo_set_image_layout(struct demo *demo, VkImage image,
630 VkImageAspectFlags aspectMask,
631 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700632 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600633 VkAccessFlagBits srcAccessMask,
634 VkPipelineStageFlags src_stages,
635 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600636 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600637
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600638 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600639 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600640 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700641 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800642 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600643 .oldLayout = old_image_layout,
644 .newLayout = new_image_layout,
645 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700646 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600647
Tony Barboureb5077b2016-10-19 15:23:36 -0600648 switch (new_image_layout) {
649 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600650 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600651 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
652 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600653
Tony Barboureb5077b2016-10-19 15:23:36 -0600654 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700655 image_memory_barrier.dstAccessMask =
656 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600657 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700658
Tony Barboureb5077b2016-10-19 15:23:36 -0600659 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700660 image_memory_barrier.dstAccessMask =
661 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600662 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700663
Tony Barboureb5077b2016-10-19 15:23:36 -0600664 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700665 image_memory_barrier.dstAccessMask =
666 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600667 break;
668
669 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
670 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
671 break;
672
673 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
674 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
675 break;
676
677 default:
678 image_memory_barrier.dstAccessMask = 0;
679 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600680 }
681
Tony Barbourf165b5d2016-10-03 16:01:41 -0600682
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600683 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600684
Karl Schultze4dc75c2016-02-02 15:37:51 -0700685 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
686 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600687}
688
Karl Schultze4dc75c2016-02-02 15:37:51 -0700689static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700690 const VkCommandBufferBeginInfo cmd_buf_info = {
691 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
692 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600693 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700694 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700695 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800696 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700697 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
698 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800699 };
700 const VkRenderPassBeginInfo rp_begin = {
701 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
702 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800703 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700704 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800705 .renderArea.offset.x = 0,
706 .renderArea.offset.y = 0,
707 .renderArea.extent.width = demo->width,
708 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600709 .clearValueCount = 2,
710 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700711 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800712 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600713
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600714 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600715 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800716 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700717 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
718 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Tony Barboura72d9492017-01-11 13:35:09 -0700719 demo->pipeline_layout, 0, 1,
720 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set,
721 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600722 VkViewport viewport;
723 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700724 viewport.height = (float)demo->height;
725 viewport.width = (float)demo->width;
726 viewport.minDepth = (float)0.0f;
727 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700728 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600729
730 VkRect2D scissor;
731 memset(&scissor, 0, sizeof(scissor));
732 scissor.extent.width = demo->width;
733 scissor.extent.height = demo->height;
734 scissor.offset.x = 0;
735 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700736 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600737 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600738 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600739 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800740 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600741
Tony Barbour22e06272016-09-07 16:07:56 -0600742 if (demo->separate_present_queue) {
743 // We have to transfer ownership from the graphics queue family to the
744 // present queue family to be able to present. Note that we don't have
745 // to transfer from present queue family back to graphics queue family at
746 // the start of the next frame because we don't care about the image's
747 // contents at that point.
748 VkImageMemoryBarrier image_ownership_barrier = {
749 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
750 .pNext = NULL,
751 .srcAccessMask = 0,
752 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
753 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
754 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
755 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
756 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700757 .image = demo->swapchain_image_resources[demo->current_buffer].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600758 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
759
760 vkCmdPipelineBarrier(cmd_buf,
761 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600762 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600763 0, NULL, 0, NULL, 1, &image_ownership_barrier);
764 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600765 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600766 assert(!err);
767}
768
Tony Barbour22e06272016-09-07 16:07:56 -0600769void demo_build_image_ownership_cmd(struct demo *demo, int i) {
770 VkResult U_ASSERT_ONLY err;
771
772 const VkCommandBufferBeginInfo cmd_buf_info = {
773 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
774 .pNext = NULL,
775 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
776 .pInheritanceInfo = NULL,
777 };
Tony Barboura72d9492017-01-11 13:35:09 -0700778 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600779 &cmd_buf_info);
780 assert(!err);
781
782 VkImageMemoryBarrier image_ownership_barrier = {
783 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
784 .pNext = NULL,
785 .srcAccessMask = 0,
786 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
787 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
788 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
789 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
790 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700791 .image = demo->swapchain_image_resources[i].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600792 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
793
Tony Barboura72d9492017-01-11 13:35:09 -0700794 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600795 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
796 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
797 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700798 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600799 assert(!err);
800}
801
Karl Schultze4dc75c2016-02-02 15:37:51 -0700802void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600803 mat4x4 MVP, Model, VP;
804 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600805 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600806 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600807
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600808 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600809
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700810 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600811 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700812 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
813 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600814 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600815
Tony Barboura72d9492017-01-11 13:35:09 -0700816 err = vkMapMemory(demo->device,
817 demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0,
818 VK_WHOLE_SIZE, 0, (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819 assert(!err);
820
Karl Schultze4dc75c2016-02-02 15:37:51 -0700821 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600822
Tony Barboura72d9492017-01-11 13:35:09 -0700823 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600824}
825
Ian Elliottd940ca22017-03-22 08:31:27 -0600826void DemoUpdateTargetIPD(struct demo *demo) {
827 // Look at what happened to previous presents, and make appropriate
828 // adjustments in timing:
829 VkResult U_ASSERT_ONLY err;
830 VkPastPresentationTimingGOOGLE* past = NULL;
831 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600832
833 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
834 demo->swapchain,
835 &count,
836 NULL);
837 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600838 if (count) {
839 past = (VkPastPresentationTimingGOOGLE*) malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
840 assert(past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600841 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
842 demo->swapchain,
843 &count,
844 past);
845 assert(!err);
846
847 bool early = false;
848 bool late = false;
849 bool calibrate_next = false;
Ian Elliottd940ca22017-03-22 08:31:27 -0600850 for (uint32_t i = 0 ; i < count ; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600851 if (!demo->syncd_with_actual_presents) {
852 // This is the first time that we've received an
853 // actualPresentTime for this swapchain. In order to not
854 // perceive these early frames as "late", we need to sync-up
855 // our future desiredPresentTime's with the
856 // actualPresentTime(s) that we're receiving now.
857 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600858
Ian Elliottd940ca22017-03-22 08:31:27 -0600859 // So that we don't suspect any pending presents as late,
860 // record them all as suspected-late presents:
861 demo->last_late_id = demo->next_present_id - 1;
862 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600863 demo->syncd_with_actual_presents = true;
864 break;
865 } else if (CanPresentEarlier(past[i].earliestPresentTime,
866 past[i].actualPresentTime,
867 past[i].presentMargin,
868 demo->refresh_duration)) {
869 // This image could have been presented earlier. We don't want
870 // to decrease the target_IPD until we've seen early presents
871 // for at least two seconds.
872 if (demo->last_early_id == past[i].presentID) {
873 // We've now seen two seconds worth of early presents.
874 // Flag it as such, and reset the counter:
875 early = true;
876 demo->last_early_id = 0;
877 } else if (demo->last_early_id == 0) {
878 // This is the first early present we've seen.
879 // Calculate the presentID for two seconds from now.
880 uint64_t lastEarlyTime =
881 past[i].actualPresentTime + (2 * BILLION);
882 uint32_t howManyPresents =
883 (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
884 demo->last_early_id = past[i].presentID + howManyPresents;
885 } else {
886 // We are in the midst of a set of early images,
887 // and so we won't do anything.
888 }
889 late = false;
890 demo->last_late_id = 0;
891 } else if (ActualTimeLate(past[i].desiredPresentTime,
892 past[i].actualPresentTime,
893 demo->refresh_duration)) {
894 // This image was presented after its desired time. Since
895 // there's a delay between calling vkQueuePresentKHR and when
896 // we get the timing data, several presents may have been late.
897 // Thus, we need to threat all of the outstanding presents as
898 // being likely late, so that we only increase the target_IPD
899 // once for all of those presents.
900 if ((demo->last_late_id == 0) ||
901 (demo->last_late_id < past[i].presentID)) {
902 late = true;
903 // Record the last suspected-late present:
904 demo->last_late_id = demo->next_present_id - 1;
905 } else {
906 // We are in the midst of a set of likely-late images,
907 // and so we won't do anything.
908 }
909 early = false;
910 demo->last_early_id = 0;
911 } else {
912 // Since this image was not presented early or late, reset
913 // any sets of early or late presentIDs:
914 early = false;
915 late = false;
916 calibrate_next = true;
917 demo->last_early_id = 0;
918 demo->last_late_id = 0;
919 }
920 }
921
922 if (early) {
923 // Since we've seen at least two-seconds worth of presnts that
924 // could have occured earlier than desired, let's decrease the
925 // target_IPD (i.e. increase the frame rate):
926 //
927 // TODO(ianelliott): Try to calculate a better target_IPD based
928 // on the most recently-seen present (this is overly-simplistic).
929 demo->refresh_duration_multiplier--;
930 if (demo->refresh_duration_multiplier == 0) {
931 // This should never happen, but in case it does, don't
932 // try to go faster.
933 demo->refresh_duration_multiplier = 1;
934 }
935 demo->target_IPD =
936 demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600937 }
938 if (late) {
939 // Since we found a new instance of a late present, we want to
940 // increase the target_IPD (i.e. decrease the frame rate):
941 //
942 // TODO(ianelliott): Try to calculate a better target_IPD based
943 // on the most recently-seen present (this is overly-simplistic).
944 demo->refresh_duration_multiplier++;
945 demo->target_IPD =
946 demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600947 }
948
949 if (calibrate_next) {
950 int64_t multiple = demo->next_present_id - past[count-1].presentID;
951 demo->prev_desired_present_time =
952 (past[count-1].actualPresentTime +
953 (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600954 }
955 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600956}
957
Karl Schultze4dc75c2016-02-02 15:37:51 -0700958static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600959 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600960
Damien Leonec336d822017-04-14 16:10:14 -0700961 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +0300962 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
963 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600964
Tony Barbour8b7c9112017-06-29 13:34:41 -0600965 do {
Tony Barbour6914e6d2017-06-02 12:11:29 -0600966 // Get the index of the next available swapchain image:
967 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
968 demo->image_acquired_semaphores[demo->frame_index],
969 VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600970
Tony Barbour6914e6d2017-06-02 12:11:29 -0600971 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
972 // demo->swapchain is out of date (e.g. the window was resized) and
973 // must be recreated:
974 demo_resize(demo);
975 } else if (err == VK_SUBOPTIMAL_KHR) {
976 // demo->swapchain is not as optimal as it could be, but the platform's
977 // presentation engine will still present the image correctly.
978 break;
979 } else {
980 assert(!err);
981 }
Tony Barbour8b7c9112017-06-29 13:34:41 -0600982 } while (err != VK_SUCCESS);
Tony Barbour6914e6d2017-06-02 12:11:29 -0600983
Tony Barbour3eafe1f2017-06-14 11:59:55 -0600984 demo_update_data_buffer(demo);
985
Ian Elliottd940ca22017-03-22 08:31:27 -0600986 if (demo->VK_GOOGLE_display_timing_enabled) {
987 // Look at what happened to previous presents, and make appropriate
988 // adjustments in timing:
989 DemoUpdateTargetIPD(demo);
990
991 // Note: a real application would position its geometry to that it's in
992 // the correct locatoin for when the next image is presented. It might
993 // also wait, so that there's less latency between any input and when
994 // the next image is rendered/presented. This demo program is so
995 // simple that it doesn't do either of those.
996 }
997
Tony Barbource7524e2016-07-28 12:01:45 -0600998 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600999 // that the image won't be rendered to until the presentation
1000 // engine has fully released ownership to the application, and it is
1001 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001002 VkPipelineStageFlags pipe_stage_flags;
1003 VkSubmitInfo submit_info;
1004 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1005 submit_info.pNext = NULL;
1006 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1007 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1008 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001009 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001010 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001011 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001012 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001013 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura72d9492017-01-11 13:35:09 -07001014 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info,
Damien Leonec336d822017-04-14 16:10:14 -07001015 demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001016 assert(!err);
1017
Tony Barbour22e06272016-09-07 16:07:56 -06001018 if (demo->separate_present_queue) {
1019 // If we are using separate queues, change image ownership to the
1020 // present queue before presenting, waiting for the draw complete
1021 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001022 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001023 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1024 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001025 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001026 submit_info.commandBufferCount = 1;
1027 submit_info.pCommandBuffers =
Tony Barboura72d9492017-01-11 13:35:09 -07001028 &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001029 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001030 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001031 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1032 assert(!err);
1033 }
1034
1035 // If we are using separate queues we have to wait for image ownership,
1036 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001037 VkPresentInfoKHR present = {
1038 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001039 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001040 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06001041 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -06001042 ? &demo->image_ownership_semaphores[demo->frame_index]
1043 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001044 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001045 .pSwapchains = &demo->swapchain,
1046 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001047 };
1048
Ian Elliott53622a72017-03-28 11:06:33 -06001049 if (demo->VK_KHR_incremental_present_enabled) {
1050 // If using VK_KHR_incremental_present, we provide a hint of the region
1051 // that contains changed content relative to the previously-presented
1052 // image. The implementation can use this hint in order to save
1053 // work/power (by only copying the region in the hint). The
1054 // implementation is free to ignore the hint though, and so we must
1055 // ensure that the entire image has the correctly-drawn content.
1056 uint32_t eighthOfWidth = demo->width / 8;
1057 uint32_t eighthOfHeight = demo->height / 8;
1058 VkRectLayerKHR rect = {
1059 .offset.x = eighthOfWidth,
1060 .offset.y = eighthOfHeight,
1061 .extent.width = eighthOfWidth * 6,
1062 .extent.height = eighthOfHeight * 6,
1063 .layer = 0,
1064 };
1065 VkPresentRegionKHR region = {
1066 .rectangleCount = 1,
1067 .pRectangles = &rect,
1068 };
1069 VkPresentRegionsKHR regions = {
1070 .sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1071 .pNext = present.pNext,
1072 .swapchainCount = present.swapchainCount,
1073 .pRegions = &region,
1074 };
1075 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001076 }
1077
Ian Elliottd940ca22017-03-22 08:31:27 -06001078 if (demo->VK_GOOGLE_display_timing_enabled) {
1079 VkPresentTimeGOOGLE ptime;
1080 if (demo->prev_desired_present_time == 0) {
1081 // This must be the first present for this swapchain.
1082 //
1083 // We don't know where we are relative to the presentation engine's
1084 // display's refresh cycle. We also don't know how long rendering
1085 // takes. Let's make a grossly-simplified assumption that the
1086 // desiredPresentTime should be half way between now and
1087 // now+target_IPD. We will adjust over time.
1088 uint64_t curtime = getTimeInNanoseconds();
1089 if (curtime == 0) {
1090 // Since we didn't find out the current time, don't give a
1091 // desiredPresentTime:
1092 ptime.desiredPresentTime = 0;
1093 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001094 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1095 }
1096 } else {
1097 ptime.desiredPresentTime = (demo->prev_desired_present_time +
1098 demo->target_IPD);
1099 }
1100 ptime.presentID = demo->next_present_id++;
1101 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001102
1103 VkPresentTimesInfoGOOGLE present_time = {
1104 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1105 .pNext = present.pNext,
1106 .swapchainCount = present.swapchainCount,
1107 .pTimes = &ptime,
1108 };
1109 if (demo->VK_GOOGLE_display_timing_enabled) {
1110 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001111 }
1112 }
1113
Tony Barboura2a67812016-07-18 13:21:06 -06001114 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001115 demo->frame_index += 1;
1116 demo->frame_index %= FRAME_LAG;
1117
Ian Elliottcf074d32015-10-16 18:02:43 -06001118 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1119 // demo->swapchain is out of date (e.g. the window was resized) and
1120 // must be recreated:
1121 demo_resize(demo);
1122 } else if (err == VK_SUBOPTIMAL_KHR) {
1123 // demo->swapchain is not as optimal as it could be, but the platform's
1124 // presentation engine will still present the image correctly.
1125 } else {
1126 assert(!err);
1127 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001128}
1129
Karl Schultze4dc75c2016-02-02 15:37:51 -07001130static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001131 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001132 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001133
Ian Elliottb08e0d92015-11-20 11:55:46 -07001134 // Check the surface capabilities and formats
1135 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001136 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
1137 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001138 assert(!err);
1139
Ian Elliott8528eff2015-08-07 15:56:59 -06001140 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001141 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1142 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001143 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06001144 VkPresentModeKHR *presentModes =
1145 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001146 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001147 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1148 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001149 assert(!err);
1150
Ian Elliotta0781972015-08-21 15:09:33 -06001151 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001152 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1153 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1154 // If the surface size is undefined, the size is set to the size
1155 // of the images requested, which must fit within the minimum and
1156 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001157 swapchainExtent.width = demo->width;
1158 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001159
1160 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1161 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1162 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1163 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1164 }
1165
1166 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1167 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1168 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1169 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1170 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001171 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001172 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001173 swapchainExtent = surfCapabilities.currentExtent;
1174 demo->width = surfCapabilities.currentExtent.width;
1175 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001176 }
1177
Tony Barbour66a56c32016-09-21 13:10:56 -06001178 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001179 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001180 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001181
Ian Elliottb18fdde2016-10-03 12:11:12 -06001182 // There are times when you may wish to use another present mode. The
1183 // following code shows how to select them, and the comments provide some
1184 // reasons you may wish to use them.
1185 //
1186 // It should be noted that Vulkan 1.0 doesn't provide a method for
1187 // synchronizing rendering with the presentation engine's display. There
1188 // is a method provided for throttling rendering with the display, but
1189 // there are some presentation engines for which this method will not work.
1190 // If an application doesn't throttle its rendering, and if it renders much
1191 // faster than the refresh rate of the display, this can waste power on
1192 // mobile devices. That is because power is being spent rendering images
1193 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001194
Ian Elliottb18fdde2016-10-03 12:11:12 -06001195 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1196 // tearing, or have some way of synchronizing their rendering with the
1197 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001198 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1199 // generally render a new presentable image every refresh cycle, but are
1200 // occasionally early. In this case, the application wants the new image
1201 // to be displayed instead of the previously-queued-for-presentation image
1202 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001203 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1204 // render a new presentable image every refresh cycle, but are occasionally
1205 // late. In this case (perhaps because of stuttering/latency concerns),
1206 // the application wants the late image to be immediately displayed, even
1207 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001208
1209 if (demo->presentMode != swapchainPresentMode) {
1210
1211 for (size_t i = 0; i < presentModeCount; ++i) {
1212 if (presentModes[i] == demo->presentMode) {
1213 swapchainPresentMode = demo->presentMode;
1214 break;
1215 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001216 }
1217 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001218 if (swapchainPresentMode != demo->presentMode) {
1219 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1220 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001221
Tony Barbour84376fe2017-01-06 15:54:22 -07001222 // Determine the number of VkImages to use in the swap chain.
1223 // Application desires to acquire 3 images at a time for triple
1224 // buffering
1225 uint32_t desiredNumOfSwapchainImages = 3;
1226 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1227 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1228 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001229 // If maxImageCount is 0, we can ask for as many images as we want;
1230 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -07001231 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -06001232 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001233 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001234 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001235 }
1236
Tony Barbour9fd6d352015-09-16 15:01:32 -06001237 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001238 if (surfCapabilities.supportedTransforms &
1239 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001240 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001241 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001242 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001243 }
1244
Tony Barbour820b55b2017-03-14 08:55:46 -06001245 // Find a supported composite alpha mode - one of these is guaranteed to be set
1246 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1247 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1248 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1249 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1250 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1251 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1252 };
Tony Barbour34ea9cf2017-08-14 12:02:30 -06001253 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
Tony Barbour820b55b2017-03-14 08:55:46 -06001254 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1255 compositeAlpha = compositeAlphaFlags[i];
1256 break;
1257 }
1258 }
1259
Tony Barboura2a67812016-07-18 13:21:06 -06001260 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001261 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001262 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001263 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001264 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001265 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001266 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001267 .imageExtent =
1268 {
1269 .width = swapchainExtent.width, .height = swapchainExtent.height,
1270 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001271 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001272 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001273 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001274 .imageArrayLayers = 1,
1275 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1276 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001277 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001278 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001279 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001280 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001281 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001282 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -06001283 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001284 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001285 assert(!err);
1286
Ian Elliottcf074d32015-10-16 18:02:43 -06001287 // If we just re-created an existing swapchain, we should destroy the old
1288 // swapchain at this point.
1289 // Note: destroying the swapchain also cleans up all its associated
1290 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001291 if (oldSwapchain != VK_NULL_HANDLE) {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001292 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001293 }
1294
1295 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001296 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001297 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001298
Karl Schultze4dc75c2016-02-02 15:37:51 -07001299 VkImage *swapchainImages =
1300 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001301 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001302 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001303 &demo->swapchainImageCount,
1304 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001305 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001306
Tony Barboura72d9492017-01-11 13:35:09 -07001307 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -07001308 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001309 assert(demo->swapchain_image_resources);
1310
Ian Elliotta0781972015-08-21 15:09:33 -06001311 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001312 VkImageViewCreateInfo color_image_view = {
1313 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001314 .pNext = NULL,
1315 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001316 .components =
1317 {
1318 .r = VK_COMPONENT_SWIZZLE_R,
1319 .g = VK_COMPONENT_SWIZZLE_G,
1320 .b = VK_COMPONENT_SWIZZLE_B,
1321 .a = VK_COMPONENT_SWIZZLE_A,
1322 },
1323 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1324 .baseMipLevel = 0,
1325 .levelCount = 1,
1326 .baseArrayLayer = 0,
1327 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001328 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1329 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001330 };
1331
Tony Barboura72d9492017-01-11 13:35:09 -07001332 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001333
Tony Barboura72d9492017-01-11 13:35:09 -07001334 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335
Karl Schultze4dc75c2016-02-02 15:37:51 -07001336 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001337 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001338 assert(!err);
1339 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001340
Ian Elliottd940ca22017-03-22 08:31:27 -06001341 if (demo->VK_GOOGLE_display_timing_enabled) {
1342 VkRefreshCycleDurationGOOGLE rc_dur;
1343 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device,
1344 demo->swapchain,
1345 &rc_dur);
1346 assert(!err);
1347 demo->refresh_duration = rc_dur.refreshDuration;
1348
1349 demo->syncd_with_actual_presents = false;
1350 // Initially target 1X the refresh duration:
1351 demo->target_IPD = demo->refresh_duration;
1352 demo->refresh_duration_multiplier = 1;
1353 demo->prev_desired_present_time = 0;
1354 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001355 }
1356
Mark Young04fd3d82016-01-25 14:43:50 -07001357 if (NULL != presentModes) {
1358 free(presentModes);
1359 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001360}
1361
Karl Schultze4dc75c2016-02-02 15:37:51 -07001362static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001363 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001364 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001365 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001366 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001367 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001368 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001369 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001370 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001371 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001372 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001373 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001374 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001375 .flags = 0,
1376 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001377
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001378 VkImageViewCreateInfo view = {
1379 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001381 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001382 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001383 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1384 .baseMipLevel = 0,
1385 .levelCount = 1,
1386 .baseArrayLayer = 0,
1387 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001389 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001390 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001391
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001392 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001393 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001394 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001395
1396 demo->depth.format = depth_format;
1397
1398 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001399 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 assert(!err);
1401
Karl Schultze4dc75c2016-02-02 15:37:51 -07001402 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001403 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001404
Chia-I Wuf48700b2015-11-10 16:21:09 +08001405 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001406 demo->depth.mem_alloc.pNext = NULL;
1407 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1408 demo->depth.mem_alloc.memoryTypeIndex = 0;
1409
Karl Schultze4dc75c2016-02-02 15:37:51 -07001410 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
Jeremy Hayes1273a382017-02-22 08:53:13 -07001411 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001412 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001413 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001415 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001416 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1417 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001418 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001420 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001421 err =
1422 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001423 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001424
1425 /* create image view */
1426 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001427 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429}
1430
Tony Barbour1a86d032015-09-21 15:17:33 -06001431/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001432bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001433 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001434
1435#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001436 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001437#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001438
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001439#ifdef __ANDROID__
1440#include <lunarg.ppm.h>
1441 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001442 cPtr = (char*)lunarg_ppm;
1443 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001444 return false;
1445 }
1446 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001447 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001448 if (rgba_data == NULL) {
1449 return true;
1450 }
1451 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001452 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001453 return false;
1454 }
1455 while(strncmp(cPtr++, "\n", 1));
1456
1457 for (int y = 0; y < *height; y++) {
1458 uint8_t *rowPtr = rgba_data;
1459 for (int x = 0; x < *width; x++) {
1460 memcpy(rowPtr, cPtr, 3);
1461 rowPtr[3] = 255; /* Alpha of 1 */
1462 rowPtr += 4;
1463 cPtr += 3;
1464 }
1465 rgba_data += layout->rowPitch;
1466 }
1467
1468 return true;
1469#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001470 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001471 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001472
Tony Barbour1a86d032015-09-21 15:17:33 -06001473 if (!fPtr)
1474 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001475
Tony Barbour1a86d032015-09-21 15:17:33 -06001476 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001477 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1478 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001479 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001480 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001481
Tony Barbour1a86d032015-09-21 15:17:33 -06001482 do {
1483 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001484 if (cPtr == NULL) {
1485 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001486 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001487 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001488 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001489
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001490 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001491 if (rgba_data == NULL) {
1492 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001493 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001494 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001495 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001496 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001497 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1498 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001499 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001500 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001501
Karl Schultze4dc75c2016-02-02 15:37:51 -07001502 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001503 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001504 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001505 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001506 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001507 rowPtr[3] = 255; /* Alpha of 1 */
1508 rowPtr += 4;
1509 }
1510 rgba_data += layout->rowPitch;
1511 }
1512 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001513 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001514#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001515}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001516
Karl Schultze4dc75c2016-02-02 15:37:51 -07001517static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001518 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001519 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001520 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001521 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001522 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001523 int32_t tex_width;
1524 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001525 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001526 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001527
Karl Schultze4dc75c2016-02-02 15:37:51 -07001528 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001529 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001530 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001531
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001532 tex_obj->tex_width = tex_width;
1533 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001534
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001535 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001536 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001537 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001538 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001539 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001540 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001541 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001542 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001543 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001544 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001545 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001546 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001547 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001548 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001549
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001550 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001551
Karl Schultze4dc75c2016-02-02 15:37:51 -07001552 err =
1553 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001554 assert(!err);
1555
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001556 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001557
Chia-I Wuf48700b2015-11-10 16:21:09 +08001558 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001559 tex_obj->mem_alloc.pNext = NULL;
1560 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1561 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001562
Karl Schultze4dc75c2016-02-02 15:37:51 -07001563 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1564 required_props,
1565 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001566 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001567
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001568 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001569 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001570 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001571 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001572
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001573 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001574 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001575 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001576
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001577 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001578 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001579 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001580 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001581 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001582 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001583 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001584 void *data;
1585
Karl Schultze4dc75c2016-02-02 15:37:51 -07001586 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1587 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001588
Karl Schultze4dc75c2016-02-02 15:37:51 -07001589 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1590 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001591 assert(!err);
1592
1593 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1594 fprintf(stderr, "Error loading texture: %s\n", filename);
1595 }
1596
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001597 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001598 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001599
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001600 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001601}
1602
Karl Schultze4dc75c2016-02-02 15:37:51 -07001603static void demo_destroy_texture_image(struct demo *demo,
1604 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001605 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001606 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1607 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001608}
1609
Karl Schultze4dc75c2016-02-02 15:37:51 -07001610static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001611 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001612 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001613 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001614
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001615 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001616
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001617 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001618 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001619
Karl Schultze4dc75c2016-02-02 15:37:51 -07001620 if ((props.linearTilingFeatures &
1621 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1622 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001623 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001624 demo_prepare_texture_image(
1625 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1626 VK_IMAGE_USAGE_SAMPLED_BIT,
1627 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1628 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001629 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1630 // shader to run until layout transition completes
1631 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1632 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
Karl Schultza0d51532017-11-22 16:43:15 -07001633 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001634 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001635 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001636 } else if (props.optimalTilingFeatures &
1637 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001638 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001639
Tony Barbour16156cb2016-10-19 14:15:49 -06001640 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001641 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001642 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001643 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1644 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1645 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001646
Karl Schultze4dc75c2016-02-02 15:37:51 -07001647 demo_prepare_texture_image(
1648 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1649 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1650 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001651
Tony Barbour16156cb2016-10-19 14:15:49 -06001652 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001653 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001654 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001655 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Karl Schultza0d51532017-11-22 16:43:15 -07001656 0,
1657 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001658 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001659
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001660 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001661 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001662 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001663 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour60c13682017-07-28 09:08:26 -06001664 0,
Tony Barbour5ff13182016-10-19 13:58:29 -06001665 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1666 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001667
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001668 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001669 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1670 .srcOffset = {0, 0, 0},
1671 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1672 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001673 .extent = {demo->staging_texture.tex_width,
1674 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001675 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001676 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001677 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001678 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1679 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001680
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001681 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001682 VK_IMAGE_ASPECT_COLOR_BIT,
1683 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001684 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001685 VK_ACCESS_TRANSFER_WRITE_BIT,
1686 VK_PIPELINE_STAGE_TRANSFER_BIT,
1687 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001688
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001689 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001690 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1691 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001692 }
1693
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001694 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001695 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001696 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001697 .magFilter = VK_FILTER_NEAREST,
1698 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001699 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001700 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1701 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1702 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001703 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001704 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001705 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001706 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707 .minLod = 0.0f,
1708 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001709 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001710 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001711 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001712
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001713 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001714 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001715 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001716 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001717 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001718 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001719 .components =
1720 {
1721 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1722 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1723 },
1724 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001725 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001726 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001727
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001728 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001729 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001730 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001731 assert(!err);
1732
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733 /* create image view */
1734 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001735 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001736 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001737 assert(!err);
1738 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001739}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001740
Tony Barboura72d9492017-01-11 13:35:09 -07001741void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001742 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001743 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001744 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001745 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001746 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001747 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001748 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001749 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001750
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001751 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001752 mat4x4_mul(MVP, VP, demo->model_matrix);
1753 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001754 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001755
Karl Schultza2615462017-01-20 13:19:20 -07001756 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001757 data.position[i][0] = g_vertex_buffer_data[i * 3];
1758 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1759 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001760 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001761 data.attr[i][0] = g_uv_buffer_data[2 * i];
1762 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001763 data.attr[i][2] = 0;
1764 data.attr[i][3] = 0;
1765 }
1766
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001767 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001768 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001769 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001770 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001771
Tony Barboura72d9492017-01-11 13:35:09 -07001772 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1773 err =
1774 vkCreateBuffer(demo->device, &buf_info, NULL,
1775 &demo->swapchain_image_resources[i].uniform_buffer);
1776 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001777
Tony Barboura72d9492017-01-11 13:35:09 -07001778 vkGetBufferMemoryRequirements(demo->device,
1779 demo->swapchain_image_resources[i].uniform_buffer,
1780 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001781
Tony Barboura72d9492017-01-11 13:35:09 -07001782 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1783 mem_alloc.pNext = NULL;
1784 mem_alloc.allocationSize = mem_reqs.size;
1785 mem_alloc.memoryTypeIndex = 0;
1786
1787 pass = memory_type_from_properties(
1788 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001789 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001790 &mem_alloc.memoryTypeIndex);
1791 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001792
Tony Barboura72d9492017-01-11 13:35:09 -07001793 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1794 &demo->swapchain_image_resources[i].uniform_memory);
1795 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001796
Tony Barboura72d9492017-01-11 13:35:09 -07001797 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1798 VK_WHOLE_SIZE, 0, (void **)&pData);
1799 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001800
Tony Barboura72d9492017-01-11 13:35:09 -07001801 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001802
Tony Barboura72d9492017-01-11 13:35:09 -07001803 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001804
Tony Barboura72d9492017-01-11 13:35:09 -07001805 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1806 demo->swapchain_image_resources[i].uniform_memory, 0);
1807 assert(!err);
1808 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001809}
1810
Karl Schultze4dc75c2016-02-02 15:37:51 -07001811static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001812 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001813 [0] =
1814 {
1815 .binding = 0,
1816 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1817 .descriptorCount = 1,
1818 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1819 .pImmutableSamplers = NULL,
1820 },
1821 [1] =
1822 {
1823 .binding = 1,
1824 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1825 .descriptorCount = DEMO_TEXTURE_COUNT,
1826 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1827 .pImmutableSamplers = NULL,
1828 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001829 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001830 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001831 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001832 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001833 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001834 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001835 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001836 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001837
Karl Schultze4dc75c2016-02-02 15:37:51 -07001838 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1839 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001840 assert(!err);
1841
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001842 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001843 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1844 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001845 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001846 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001847 };
1848
Karl Schultze4dc75c2016-02-02 15:37:51 -07001849 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001850 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001851 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852}
1853
Karl Schultze4dc75c2016-02-02 15:37:51 -07001854static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001855 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1856 // because at the start of the renderpass, we don't care about their contents.
1857 // At the start of the subpass, the color attachment's layout will be transitioned
1858 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1859 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1860 // the renderpass, the color attachment's layout will be transitioned to
1861 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1862 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001863 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001864 [0] =
1865 {
1866 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001867 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001868 .samples = VK_SAMPLE_COUNT_1_BIT,
1869 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1870 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1871 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1872 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001873 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001874 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001875 },
1876 [1] =
1877 {
1878 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001879 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001880 .samples = VK_SAMPLE_COUNT_1_BIT,
1881 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1882 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1883 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1884 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1885 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001886 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001887 .finalLayout =
1888 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1889 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001890 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001891 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001892 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001893 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001894 const VkAttachmentReference depth_reference = {
1895 .attachment = 1,
1896 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1897 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001898 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001899 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1900 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001901 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001902 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001903 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001904 .pColorAttachments = &color_reference,
1905 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001906 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001907 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001908 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001909 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001910 const VkRenderPassCreateInfo rp_info = {
1911 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1912 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001913 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001914 .attachmentCount = 2,
1915 .pAttachments = attachments,
1916 .subpassCount = 1,
1917 .pSubpasses = &subpass,
1918 .dependencyCount = 0,
1919 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001920 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001921 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001922
Chia-I Wu63636062015-10-26 21:10:41 +08001923 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001924 assert(!err);
1925}
1926
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001927static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001928 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001929 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001930 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001931
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001932 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1933 moduleCreateInfo.pNext = NULL;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001934 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001935 moduleCreateInfo.codeSize = size;
1936 moduleCreateInfo.pCode = code;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001937
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001938 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1939 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001940
1941 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001942}
1943
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001944static void demo_prepare_vs(struct demo *demo) {
1945 const uint32_t vs_code[] = {
1946#include "cube.vert.inc"
1947 };
1948 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001949}
1950
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01001951static void demo_prepare_fs(struct demo *demo) {
1952 const uint32_t fs_code[] = {
1953#include "cube.frag.inc"
1954 };
1955 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001956}
1957
Karl Schultze4dc75c2016-02-02 15:37:51 -07001958static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001959 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001960 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001961 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06001962 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001963 VkPipelineRasterizationStateCreateInfo rs;
1964 VkPipelineColorBlendStateCreateInfo cb;
1965 VkPipelineDepthStencilStateCreateInfo ds;
1966 VkPipelineViewportStateCreateInfo vp;
1967 VkPipelineMultisampleStateCreateInfo ms;
1968 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
1969 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001970 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971
Piers Daniellba839d12015-09-29 13:01:09 -06001972 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
1973 memset(&dynamicState, 0, sizeof dynamicState);
1974 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
1975 dynamicState.pDynamicStates = dynamicStateEnables;
1976
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001977 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001978 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001979 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001980
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07001981 memset(&vi, 0, sizeof(vi));
1982 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
1983
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001984 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001985 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001986 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987
1988 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08001989 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
1990 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001991 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001992 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06001993 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06001994 rs.rasterizerDiscardEnable = VK_FALSE;
1995 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06001996 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001997
1998 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001999 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2000 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002001 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002002 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002003 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002004 cb.attachmentCount = 1;
2005 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002006
Tony Barbour29645d02015-01-16 14:27:35 -07002007 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002008 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002009 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002010 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2011 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002012 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002013 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2014 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002015
2016 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002017 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002018 ds.depthTestEnable = VK_TRUE;
2019 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002020 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002021 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002022 ds.back.failOp = VK_STENCIL_OP_KEEP;
2023 ds.back.passOp = VK_STENCIL_OP_KEEP;
2024 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002025 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002026 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002027
Tony Barbour29645d02015-01-16 14:27:35 -07002028 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002029 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002030 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002031 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002032
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002033 demo_prepare_vs(demo);
2034 demo_prepare_fs(demo);
2035
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002036 // Two stages: vs and fs
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002037 VkPipelineShaderStageCreateInfo shaderStages[2];
2038 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2039
Karl Schultze4dc75c2016-02-02 15:37:51 -07002040 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2041 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002042 shaderStages[0].module = demo->vert_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002043 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002044
Karl Schultze4dc75c2016-02-02 15:37:51 -07002045 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2046 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002047 shaderStages[1].module = demo->frag_shader_module;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002048 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002049
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002050 memset(&pipelineCache, 0, sizeof(pipelineCache));
2051 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002052
Karl Schultze4dc75c2016-02-02 15:37:51 -07002053 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
2054 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002055 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002056
Karl Schultze4dc75c2016-02-02 15:37:51 -07002057 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002058 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002059 pipeline.pRasterizationState = &rs;
2060 pipeline.pColorBlendState = &cb;
2061 pipeline.pMultisampleState = &ms;
2062 pipeline.pViewportState = &vp;
2063 pipeline.pDepthStencilState = &ds;
Petr Kraus9a4eb6a2017-11-30 14:49:20 +01002064 pipeline.stageCount = ARRAY_SIZE(shaderStages);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002065 pipeline.pStages = shaderStages;
2066 pipeline.renderPass = demo->render_pass;
2067 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002068
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002069 pipeline.renderPass = demo->render_pass;
2070
Karl Schultze4dc75c2016-02-02 15:37:51 -07002071 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
2072 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002073 assert(!err);
2074
Chia-I Wu63636062015-10-26 21:10:41 +08002075 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2076 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002077}
2078
Karl Schultze4dc75c2016-02-02 15:37:51 -07002079static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002080 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002081 [0] =
2082 {
2083 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07002084 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002085 },
2086 [1] =
2087 {
2088 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07002089 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002090 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002091 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002092 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002093 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002094 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002095 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002096 .poolSizeCount = 2,
2097 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002098 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002099 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002100
Karl Schultze4dc75c2016-02-02 15:37:51 -07002101 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
2102 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002103 assert(!err);
2104}
2105
Karl Schultze4dc75c2016-02-02 15:37:51 -07002106static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002107 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002108 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002109 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002110
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002111 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002112 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06002113 .pNext = NULL,
2114 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002115 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002116 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002117
2118 VkDescriptorBufferInfo buffer_info;
2119 buffer_info.offset = 0;
2120 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002121
Chia-I Wuae721ba2015-05-25 16:27:55 +08002122 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002123 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002124 tex_descs[i].sampler = demo->textures[i].sampler;
2125 tex_descs[i].imageView = demo->textures[i].view;
2126 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002127 }
2128
2129 memset(&writes, 0, sizeof(writes));
2130
2131 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002132 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002133 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002134 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002135
2136 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002137 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002138 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002139 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002140 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002141
Tony Barboura72d9492017-01-11 13:35:09 -07002142 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2143 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2144 assert(!err);
2145 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2146 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2147 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2148 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2149 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002150}
2151
Karl Schultze4dc75c2016-02-02 15:37:51 -07002152static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002153 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002154 attachments[1] = demo->depth.view;
2155
Chia-I Wuf973e322015-07-08 13:34:24 +08002156 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002157 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2158 .pNext = NULL,
2159 .renderPass = demo->render_pass,
2160 .attachmentCount = 2,
2161 .pAttachments = attachments,
2162 .width = demo->width,
2163 .height = demo->height,
2164 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002165 };
2166 VkResult U_ASSERT_ONLY err;
2167 uint32_t i;
2168
Tony Barbourd8e504f2015-10-08 14:26:24 -06002169 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002170 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002171 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002172 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002173 assert(!err);
2174 }
2175}
2176
Karl Schultze4dc75c2016-02-02 15:37:51 -07002177static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002178 VkResult U_ASSERT_ONLY err;
2179
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002180 const VkCommandPoolCreateInfo cmd_pool_info = {
2181 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06002182 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002183 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06002184 .flags = 0,
2185 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07002186 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
2187 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06002188 assert(!err);
2189
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002190 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002191 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002192 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002193 .commandPool = demo->cmd_pool,
2194 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002195 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002196 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002197 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2198 assert(!err);
2199 VkCommandBufferBeginInfo cmd_buf_info = {
2200 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2201 .pNext = NULL,
2202 .flags = 0,
2203 .pInheritanceInfo = NULL,
2204 };
2205 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2206 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002207
2208 demo_prepare_buffers(demo);
2209 demo_prepare_depth(demo);
2210 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002211 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002212
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002213 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002214 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002215 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002216
Ian Elliotta0781972015-08-21 15:09:33 -06002217 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002218 err =
Tony Barboura72d9492017-01-11 13:35:09 -07002219 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002220 assert(!err);
2221 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002222
Tony Barbour22e06272016-09-07 16:07:56 -06002223 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002224 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002225 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2226 .pNext = NULL,
2227 .queueFamilyIndex = demo->present_queue_family_index,
2228 .flags = 0,
2229 };
Karl Schultza2615462017-01-20 13:19:20 -07002230 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06002231 &demo->present_cmd_pool);
2232 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002233 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002234 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2235 .pNext = NULL,
2236 .commandPool = demo->present_cmd_pool,
2237 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2238 .commandBufferCount = 1,
2239 };
2240 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2241 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07002242 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002243 assert(!err);
2244 demo_build_image_ownership_cmd(demo, i);
2245 }
2246 }
2247
Chia-I Wu63ea9262015-03-26 13:14:16 +08002248 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002249 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002250
Chia-I Wuf973e322015-07-08 13:34:24 +08002251 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002252
Ian Elliotta0781972015-08-21 15:09:33 -06002253 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002254 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002255 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002256 }
2257
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002258 /*
2259 * Prepare functions above may generate pipeline commands
2260 * that need to be flushed before beginning the render loop.
2261 */
2262 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002263 if (demo->staging_texture.image) {
2264 demo_destroy_texture_image(demo, &demo->staging_texture);
2265 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002266
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002267 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002268 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002269}
2270
Karl Schultze4dc75c2016-02-02 15:37:51 -07002271static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002272 uint32_t i;
2273
2274 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002275 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002276
Tony Barbour66a56c32016-09-21 13:10:56 -06002277 // Wait for fences from present operations
2278 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002279 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002280 vkDestroyFence(demo->device, demo->fences[i], NULL);
2281 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2282 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2283 if (demo->separate_present_queue) {
2284 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2285 }
2286 }
2287
Tony Barbourd8e504f2015-10-08 14:26:24 -06002288 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002289 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002290 }
Chia-I Wu63636062015-10-26 21:10:41 +08002291 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002292
Chia-I Wu63636062015-10-26 21:10:41 +08002293 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2294 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2295 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2296 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2297 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002298
2299 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002300 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2301 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2302 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2303 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002304 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002305 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002306
Chia-I Wu63636062015-10-26 21:10:41 +08002307 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2308 vkDestroyImage(demo->device, demo->depth.image, NULL);
2309 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002310
Ian Elliotta0781972015-08-21 15:09:33 -06002311 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002312 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002313 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002314 &demo->swapchain_image_resources[i].cmd);
2315 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2316 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002317 }
Tony Barboura72d9492017-01-11 13:35:09 -07002318 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002319 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002320 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002321
Tony Barbour22e06272016-09-07 16:07:56 -06002322 if (demo->separate_present_queue) {
2323 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002324 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002325 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002326 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002327 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002328 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002329 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002330 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002331
Tony Barbour153cb062016-12-07 13:43:36 -07002332#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002333 XDestroyWindow(demo->display, demo->xlib_window);
2334 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002335#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002336 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002337 xcb_disconnect(demo->connection);
2338 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002339#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002340 wl_keyboard_destroy(demo->keyboard);
2341 wl_pointer_destroy(demo->pointer);
2342 wl_seat_destroy(demo->seat);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002343 wl_shell_surface_destroy(demo->shell_surface);
2344 wl_surface_destroy(demo->window);
2345 wl_shell_destroy(demo->shell);
2346 wl_compositor_destroy(demo->compositor);
2347 wl_registry_destroy(demo->registry);
2348 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002349#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002350#endif
Karl Schultz86429112017-06-20 11:27:59 -06002351
2352 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002353}
2354
Karl Schultze4dc75c2016-02-02 15:37:51 -07002355static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002356 uint32_t i;
2357
Mike Stroyanbb60b382015-10-28 09:46:57 -06002358 // Don't react to resize until after first initialization.
2359 if (!demo->prepared) {
2360 return;
2361 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002362 // In order to properly resize the window, we must re-create the swapchain
2363 // AND redo the command buffers, etc.
2364 //
2365 // First, perform part of the demo_cleanup() function:
2366 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002367 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002368
2369 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002370 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002371 }
Chia-I Wu63636062015-10-26 21:10:41 +08002372 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002373
Chia-I Wu63636062015-10-26 21:10:41 +08002374 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2375 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2376 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2377 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2378 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002379
2380 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002381 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2382 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2383 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2384 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002385 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002386
Chia-I Wu63636062015-10-26 21:10:41 +08002387 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2388 vkDestroyImage(demo->device, demo->depth.image, NULL);
2389 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002390
Ian Elliottcf074d32015-10-16 18:02:43 -06002391 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002392 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002393 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002394 &demo->swapchain_image_resources[i].cmd);
2395 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2396 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002397 }
Chia-I Wu63636062015-10-26 21:10:41 +08002398 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002399 if (demo->separate_present_queue) {
2400 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2401 }
Tony Barboura72d9492017-01-11 13:35:09 -07002402 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002403
Ian Elliottcf074d32015-10-16 18:02:43 -06002404 // Second, re-perform the demo_prepare() function, which will re-create the
2405 // swapchain:
2406 demo_prepare(demo);
2407}
2408
David Pinedo2bb7c932015-06-18 17:03:14 -06002409// On MS-Windows, make this a global, so it's available to WndProc()
2410struct demo demo;
2411
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002412#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002413static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002414 if (!demo->prepared)
2415 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002416
Ian Elliott639ca472015-04-16 15:23:05 -06002417 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002418 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002419 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002420 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002421 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002422}
Ian Elliott639ca472015-04-16 15:23:05 -06002423
2424// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002425LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2426 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002427 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002428 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002429 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002430 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002431 // The validation callback calls MessageBox which can generate paint
2432 // events - don't make more Vulkan calls if we got here from the
2433 // callback
2434 if (!in_callback) {
2435 demo_run(&demo);
2436 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002437 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002438 case WM_GETMINMAXINFO: // set window's minimum size
2439 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2440 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002441 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002442 // Resize the application to the new window size, except when
2443 // it was minimized. Vulkan doesn't support images or swapchains
2444 // with width=0 and height=0.
2445 if (wParam != SIZE_MINIMIZED) {
2446 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002447 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002448 demo_resize(&demo);
2449 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002450 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002451 default:
2452 break;
2453 }
2454 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2455}
2456
Karl Schultze4dc75c2016-02-02 15:37:51 -07002457static void demo_create_window(struct demo *demo) {
2458 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002459
2460 // Initialize the window class structure:
2461 win_class.cbSize = sizeof(WNDCLASSEX);
2462 win_class.style = CS_HREDRAW | CS_VREDRAW;
2463 win_class.lpfnWndProc = WndProc;
2464 win_class.cbClsExtra = 0;
2465 win_class.cbWndExtra = 0;
2466 win_class.hInstance = demo->connection; // hInstance
2467 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2468 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2469 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2470 win_class.lpszMenuName = NULL;
2471 win_class.lpszClassName = demo->name;
2472 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2473 // Register window class:
2474 if (!RegisterClassEx(&win_class)) {
2475 // It didn't work, so try to give a useful error:
2476 printf("Unexpected error trying to start the application!\n");
2477 fflush(stdout);
2478 exit(1);
2479 }
2480 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002481 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002482 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002483 demo->window = CreateWindowEx(0,
2484 demo->name, // class name
2485 demo->name, // app name
2486 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002487 WS_VISIBLE | WS_SYSMENU,
2488 100, 100, // x/y coords
2489 wr.right - wr.left, // width
2490 wr.bottom - wr.top, // height
2491 NULL, // handle to parent
2492 NULL, // handle to menu
2493 demo->connection, // hInstance
2494 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002495 if (!demo->window) {
2496 // It didn't work, so try to give a useful error:
2497 printf("Cannot create a window in which to draw!\n");
2498 fflush(stdout);
2499 exit(1);
2500 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002501 // Window client area size must be at least 1 pixel high, to prevent crash.
2502 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2503 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002504}
Tony Barbour8295ac42016-08-08 11:04:17 -06002505#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002506static void demo_create_xlib_window(struct demo *demo) {
2507
Tony Barbouree9cd372017-01-31 16:09:58 -07002508 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002509 demo->display = XOpenDisplay(NULL);
2510 long visualMask = VisualScreenMask;
2511 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002512 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002513 vInfoTemplate.screen = DefaultScreen(demo->display);
2514 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2515 &vInfoTemplate, &numberOfVisuals);
2516
2517 Colormap colormap = XCreateColormap(
2518 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2519 visualInfo->visual, AllocNone);
2520
Rene Lindsay11612722016-06-13 17:20:39 -06002521 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002522 windowAttributes.colormap = colormap;
2523 windowAttributes.background_pixel = 0xFFFFFFFF;
2524 windowAttributes.border_pixel = 0;
2525 windowAttributes.event_mask =
2526 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2527
2528 demo->xlib_window = XCreateWindow(
2529 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2530 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2531 visualInfo->visual,
2532 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2533
2534 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2535 XMapWindow(demo->display, demo->xlib_window);
2536 XFlush(demo->display);
2537 demo->xlib_wm_delete_window =
2538 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2539}
2540static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2541 switch(event->type) {
2542 case ClientMessage:
2543 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2544 demo->quit = true;
2545 break;
2546 case KeyPress:
2547 switch (event->xkey.keycode) {
2548 case 0x9: // Escape
2549 demo->quit = true;
2550 break;
2551 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002552 demo->spin_angle -= demo->spin_increment;
2553 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002554 case 0x72: // right arrow key
2555 demo->spin_angle += demo->spin_increment;
2556 break;
2557 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002558 demo->pause = !demo->pause;
2559 break;
2560 }
2561 break;
2562 case ConfigureNotify:
2563 if ((demo->width != event->xconfigure.width) ||
2564 (demo->height != event->xconfigure.height)) {
2565 demo->width = event->xconfigure.width;
2566 demo->height = event->xconfigure.height;
2567 demo_resize(demo);
2568 }
2569 break;
2570 default:
2571 break;
2572 }
2573
2574}
2575
2576static void demo_run_xlib(struct demo *demo) {
2577
2578 while (!demo->quit) {
2579 XEvent event;
2580
2581 if (demo->pause) {
2582 XNextEvent(demo->display, &event);
2583 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002584 }
2585 while (XPending(demo->display) > 0) {
2586 XNextEvent(demo->display, &event);
2587 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002588 }
2589
Tony Barbour2593b182016-04-19 10:57:58 -06002590 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002591 demo->curFrame++;
2592 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2593 demo->quit = true;
2594 }
2595}
Tony Barbour153cb062016-12-07 13:43:36 -07002596#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002597static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002598 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002599 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002600 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002601 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002602 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002603 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002604 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002605 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2606 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002607 demo->quit = true;
2608 }
2609 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002610 case XCB_KEY_RELEASE: {
2611 const xcb_key_release_event_t *key =
2612 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002613
Karl Schultze4dc75c2016-02-02 15:37:51 -07002614 switch (key->detail) {
2615 case 0x9: // Escape
2616 demo->quit = true;
2617 break;
2618 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002619 demo->spin_angle -= demo->spin_increment;
2620 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002621 case 0x72: // right arrow key
2622 demo->spin_angle += demo->spin_increment;
2623 break;
2624 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002625 demo->pause = !demo->pause;
2626 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002627 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002628 } break;
2629 case XCB_CONFIGURE_NOTIFY: {
2630 const xcb_configure_notify_event_t *cfg =
2631 (const xcb_configure_notify_event_t *)event;
2632 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002633 demo->width = cfg->width;
2634 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002635 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002636 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002637 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002638 default:
2639 break;
2640 }
2641}
2642
Tony Barbour2593b182016-04-19 10:57:58 -06002643static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002644 xcb_flush(demo->connection);
2645
2646 while (!demo->quit) {
2647 xcb_generic_event_t *event;
2648
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002649 if (demo->pause) {
2650 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002651 }
2652 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002653 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002654 }
2655 while (event) {
2656 demo_handle_xcb_event(demo, event);
2657 free(event);
2658 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002659 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002660
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002661 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002662 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002663 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002664 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002665 }
2666}
2667
Tony Barbour2593b182016-04-19 10:57:58 -06002668static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002669 uint32_t value_mask, value_list[32];
2670
Tony Barbour2593b182016-04-19 10:57:58 -06002671 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002672
2673 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2674 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002675 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002676 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002677
Tony Barbour2593b182016-04-19 10:57:58 -06002678 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002679 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2680 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2681 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002682
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002683 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002684 xcb_intern_atom_cookie_t cookie =
2685 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2686 xcb_intern_atom_reply_t *reply =
2687 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002688
Karl Schultze4dc75c2016-02-02 15:37:51 -07002689 xcb_intern_atom_cookie_t cookie2 =
2690 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2691 demo->atom_wm_delete_window =
2692 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002693
Tony Barbour2593b182016-04-19 10:57:58 -06002694 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002695 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002696 &(*demo->atom_wm_delete_window).atom);
2697 free(reply);
2698
Tony Barbour2593b182016-04-19 10:57:58 -06002699 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002700
Karl Schultze4dc75c2016-02-02 15:37:51 -07002701 // Force the x/y coordinates to 100,100 results are identical in consecutive
2702 // runs
2703 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002704 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002705 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002706}
Tony Barbour6b131662016-08-25 13:14:45 -06002707// VK_USE_PLATFORM_XCB_KHR
2708#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002709static void demo_run(struct demo *demo) {
2710 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002711 if (demo->pause) {
2712 wl_display_dispatch(demo->display); // block and wait for input
Joey Bzdek33bc5c82017-06-14 10:33:36 -06002713 } else {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002714 wl_display_dispatch_pending(demo->display); // don't block
2715 demo_draw(demo);
2716 demo->curFrame++;
2717 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2718 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002719 }
2720}
2721
2722static void handle_ping(void *data UNUSED,
2723 struct wl_shell_surface *shell_surface,
2724 uint32_t serial) {
2725 wl_shell_surface_pong(shell_surface, serial);
2726}
2727
2728static void handle_configure(void *data UNUSED,
2729 struct wl_shell_surface *shell_surface UNUSED,
2730 uint32_t edges UNUSED, int32_t width UNUSED,
2731 int32_t height UNUSED) {}
2732
2733static void handle_popup_done(void *data UNUSED,
2734 struct wl_shell_surface *shell_surface UNUSED) {}
2735
2736static const struct wl_shell_surface_listener shell_surface_listener = {
2737 handle_ping, handle_configure, handle_popup_done};
2738
2739static void demo_create_window(struct demo *demo) {
2740 demo->window = wl_compositor_create_surface(demo->compositor);
2741 if (!demo->window) {
2742 printf("Can not create wayland_surface from compositor!\n");
2743 fflush(stdout);
2744 exit(1);
2745 }
2746
2747 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2748 if (!demo->shell_surface) {
2749 printf("Can not get shell_surface from wayland_surface!\n");
2750 fflush(stdout);
2751 exit(1);
2752 }
2753 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2754 demo);
2755 wl_shell_surface_set_toplevel(demo->shell_surface);
2756 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2757}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002758#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2759static void demo_run(struct demo *demo) {
2760 if (!demo->prepared)
2761 return;
2762
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002763 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002764 demo->curFrame++;
2765}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002766#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002767#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2768static VkResult demo_create_display_surface(struct demo *demo) {
2769 VkResult U_ASSERT_ONLY err;
2770 uint32_t display_count;
2771 uint32_t mode_count;
2772 uint32_t plane_count;
2773 VkDisplayPropertiesKHR display_props;
2774 VkDisplayKHR display;
2775 VkDisplayModePropertiesKHR mode_props;
2776 VkDisplayPlanePropertiesKHR *plane_props;
2777 VkBool32 found_plane = VK_FALSE;
2778 uint32_t plane_index;
2779 VkExtent2D image_extent;
2780 VkDisplaySurfaceCreateInfoKHR create_info;
2781
2782 // Get the first display
2783 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2784 assert(!err);
2785
2786 if (display_count == 0) {
2787 printf("Cannot find any display!\n");
2788 fflush(stdout);
2789 exit(1);
2790 }
2791
2792 display_count = 1;
2793 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2794 assert(!err || (err == VK_INCOMPLETE));
2795
2796 display = display_props.display;
2797
2798 // Get the first mode of the display
2799 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2800 assert(!err);
2801
2802 if (mode_count == 0) {
2803 printf("Cannot find any mode for the display!\n");
2804 fflush(stdout);
2805 exit(1);
2806 }
2807
2808 mode_count = 1;
2809 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2810 assert(!err || (err == VK_INCOMPLETE));
2811
2812 // Get the list of planes
2813 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2814 assert(!err);
2815
2816 if (plane_count == 0) {
2817 printf("Cannot find any plane!\n");
2818 fflush(stdout);
2819 exit(1);
2820 }
2821
2822 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2823 assert(plane_props);
2824
2825 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2826 assert(!err);
2827
2828 // Find a plane compatible with the display
2829 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2830 uint32_t supported_count;
2831 VkDisplayKHR *supported_displays;
2832
2833 // Disqualify planes that are bound to a different display
2834 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2835 (plane_props[plane_index].currentDisplay != display)) {
2836 continue;
2837 }
2838
2839 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2840 assert(!err);
2841
2842 if (supported_count == 0) {
2843 continue;
2844 }
2845
2846 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2847 assert(supported_displays);
2848
2849 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2850 assert(!err);
2851
2852 for (uint32_t i = 0; i < supported_count; i++) {
2853 if (supported_displays[i] == display) {
2854 found_plane = VK_TRUE;
2855 break;
2856 }
2857 }
2858
2859 free(supported_displays);
2860
2861 if (found_plane) {
2862 break;
2863 }
2864 }
2865
2866 if (!found_plane) {
2867 printf("Cannot find a plane compatible with the display!\n");
2868 fflush(stdout);
2869 exit(1);
2870 }
2871
2872 free(plane_props);
2873
Tony Barbour820b55b2017-03-14 08:55:46 -06002874 VkDisplayPlaneCapabilitiesKHR planeCaps;
2875 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2876 // Find a supported alpha mode
Wladimir J. van der Laan8ec4fce2017-10-07 14:17:41 +02002877 VkDisplayPlaneAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2878 VkDisplayPlaneAlphaFlagBitsKHR alphaModes[4] = {
Tony Barbour820b55b2017-03-14 08:55:46 -06002879 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2880 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2881 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2882 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2883 };
2884 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2885 if (planeCaps.supportedAlpha & alphaModes[i]) {
2886 alphaMode = alphaModes[i];
2887 break;
2888 }
2889 }
Damien Leone600c3052017-01-31 10:26:07 -07002890 image_extent.width = mode_props.parameters.visibleRegion.width;
2891 image_extent.height = mode_props.parameters.visibleRegion.height;
2892
2893 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2894 create_info.pNext = NULL;
2895 create_info.flags = 0;
2896 create_info.displayMode = mode_props.displayMode;
2897 create_info.planeIndex = plane_index;
2898 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2899 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002900 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002901 create_info.globalAlpha = 1.0f;
2902 create_info.imageExtent = image_extent;
2903
2904 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2905}
2906
2907static void demo_run_display(struct demo *demo)
2908{
2909 while (!demo->quit) {
2910 demo_draw(demo);
2911 demo->curFrame++;
2912
2913 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2914 demo->quit = true;
2915 }
2916 }
2917}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002918#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002919
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002920/*
2921 * Return 1 (true) if all layer names specified in check_names
2922 * can be found in given layer properties.
2923 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002924static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002925 uint32_t layer_count,
2926 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002927 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002928 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002929 for (uint32_t j = 0; j < layer_count; j++) {
2930 if (!strcmp(check_names[i], layers[j].layerName)) {
2931 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002932 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002933 }
2934 }
2935 if (!found) {
2936 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2937 return 0;
2938 }
2939 }
2940 return 1;
2941}
2942
Karl Schultze4dc75c2016-02-02 15:37:51 -07002943static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002944 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002945 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002946 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002947 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002948 char **instance_validation_layers = NULL;
2949 demo->enabled_extension_count = 0;
2950 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002951
Karl Schultz7c50ad62016-04-01 12:07:03 -06002952 char *instance_validation_layers_alt1[] = {
2953 "VK_LAYER_LUNARG_standard_validation"
2954 };
2955
2956 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07002957 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
2958 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
Mark Lobodzinski1392e812017-06-09 17:02:11 -06002959 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002960 };
2961
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002962 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002963 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002964 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002965
Karl Schultz7c50ad62016-04-01 12:07:03 -06002966 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07002967 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002968
Karl Schultz7c50ad62016-04-01 12:07:03 -06002969 instance_validation_layers = instance_validation_layers_alt1;
2970 if (instance_layer_count > 0) {
2971 VkLayerProperties *instance_layers =
2972 malloc(sizeof (VkLayerProperties) * instance_layer_count);
2973 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
2974 instance_layers);
2975 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07002976
Karl Schultz7c50ad62016-04-01 12:07:03 -06002977
2978 validation_found = demo_check_layers(
2979 ARRAY_SIZE(instance_validation_layers_alt1),
2980 instance_validation_layers, instance_layer_count,
2981 instance_layers);
2982 if (validation_found) {
2983 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002984 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
2985 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002986 } else {
2987 // use alternative set of validation layers
2988 instance_validation_layers = instance_validation_layers_alt2;
2989 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
2990 validation_found = demo_check_layers(
2991 ARRAY_SIZE(instance_validation_layers_alt2),
2992 instance_validation_layers, instance_layer_count,
2993 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06002994 validation_layer_count =
2995 ARRAY_SIZE(instance_validation_layers_alt2);
2996 for (uint32_t i = 0; i < validation_layer_count; i++) {
2997 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06002998 }
2999 }
3000 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003001 }
Dustin Graves7c287352016-01-27 13:48:06 -07003002
Karl Schultz7c50ad62016-04-01 12:07:03 -06003003 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06003004 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06003005 "required validation layer.\n\n"
3006 "Please look at the Getting Started guide for additional "
3007 "information.\n",
3008 "vkCreateInstance Failure");
3009 }
Dustin Graves7c287352016-01-27 13:48:06 -07003010 }
3011
3012 /* Look for instance extensions */
3013 VkBool32 surfaceExtFound = 0;
3014 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003015 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003016
Karl Schultze4dc75c2016-02-02 15:37:51 -07003017 err = vkEnumerateInstanceExtensionProperties(
3018 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003019 assert(!err);
3020
Dustin Graves7c287352016-01-27 13:48:06 -07003021 if (instance_extension_count > 0) {
3022 VkExtensionProperties *instance_extensions =
3023 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3024 err = vkEnumerateInstanceExtensionProperties(
3025 NULL, &instance_extension_count, instance_extensions);
3026 assert(!err);
3027 for (uint32_t i = 0; i < instance_extension_count; i++) {
3028 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
3029 instance_extensions[i].extensionName)) {
3030 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003031 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003032 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003033 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003034#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003035 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
3036 instance_extensions[i].extensionName)) {
3037 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003038 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003039 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
3040 }
Tony Barbour153cb062016-12-07 13:43:36 -07003041#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003042 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
3043 instance_extensions[i].extensionName)) {
3044 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06003045 demo->extension_names[demo->enabled_extension_count++] =
3046 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
3047 }
Tony Barbour153cb062016-12-07 13:43:36 -07003048#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003049 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
3050 instance_extensions[i].extensionName)) {
3051 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003052 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003053 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
3054 }
Tony Barbour153cb062016-12-07 13:43:36 -07003055#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003056 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
3057 instance_extensions[i].extensionName)) {
3058 platformSurfaceExtFound = 1;
3059 demo->extension_names[demo->enabled_extension_count++] =
3060 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
3061 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003062#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003063#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3064 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
3065 instance_extensions[i].extensionName)) {
3066 platformSurfaceExtFound = 1;
3067 demo->extension_names[demo->enabled_extension_count++] =
3068 VK_KHR_DISPLAY_EXTENSION_NAME;
3069 }
Tony Barbour153cb062016-12-07 13:43:36 -07003070#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003071 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
3072 instance_extensions[i].extensionName)) {
3073 platformSurfaceExtFound = 1;
3074 demo->extension_names[demo->enabled_extension_count++] =
3075 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
3076 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003077#elif defined(VK_USE_PLATFORM_IOS_MVK)
3078 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3079 platformSurfaceExtFound = 1;
3080 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3081 }
3082#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3083 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3084 platformSurfaceExtFound = 1;
3085 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3086 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003087#endif
Dustin Graves7c287352016-01-27 13:48:06 -07003088 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
3089 instance_extensions[i].extensionName)) {
3090 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003091 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003092 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
3093 }
3094 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003095 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003096 }
Dustin Graves7c287352016-01-27 13:48:06 -07003097
3098 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003099 }
Dustin Graves7c287352016-01-27 13:48:06 -07003100
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003101 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003102 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3103 "the " VK_KHR_SURFACE_EXTENSION_NAME
3104 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06003105 "Vulkan installable client driver (ICD) installed?\nPlease "
3106 "look at the Getting Started guide for additional "
3107 "information.\n",
3108 "vkCreateInstance Failure");
3109 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003110 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003111#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003112 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3113 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3114 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003115 "Vulkan installable client driver (ICD) installed?\nPlease "
3116 "look at the Getting Started guide for additional "
3117 "information.\n",
3118 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003119#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003120 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3121 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3122 "Vulkan installable client driver (ICD) installed?\nPlease "
3123 "look at the Getting Started guide for additional "
3124 "information.\n",
3125 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003126#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003127 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3128 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3129 "Vulkan installable client driver (ICD) installed?\nPlease "
3130 "look at the Getting Started guide for additional "
3131 "information.\n",
3132 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003133#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003134 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3135 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3136 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003137 "Vulkan installable client driver (ICD) installed?\nPlease "
3138 "look at the Getting Started guide for additional "
3139 "information.\n",
3140 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003141#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3142 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3143 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3144 " extension.\n\nDo you have a compatible "
3145 "Vulkan installable client driver (ICD) installed?\nPlease "
3146 "look at the Getting Started guide for additional "
3147 "information.\n",
3148 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003149#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003150#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3151 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3152 "the " VK_KHR_DISPLAY_EXTENSION_NAME
3153 " extension.\n\nDo you have a compatible "
3154 "Vulkan installable client driver (ICD) installed?\nPlease "
3155 "look at the Getting Started guide for additional "
3156 "information.\n",
3157 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003158#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3159 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3160 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3161 " extension.\n\nDo you have a compatible "
3162 "Vulkan installable client driver (ICD) installed?\nPlease "
3163 "look at the Getting Started guide for additional "
3164 "information.\n",
3165 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003166#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003167 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3168 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3169 " extension.\n\nDo you have a compatible "
3170 "Vulkan installable client driver (ICD) installed?\nPlease "
3171 "look at the Getting Started guide for additional "
3172 "information.\n",
3173 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003174#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003175 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003176 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003177 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003178 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003179 .pApplicationName = APP_SHORT_NAME,
3180 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003181 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003182 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003183 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003184 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003185 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003186 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003187 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003188 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003189 .enabledLayerCount = demo->enabled_layer_count,
3190 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3191 .enabledExtensionCount = demo->enabled_extension_count,
3192 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003193 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003194
3195 /*
3196 * This is info for a temp callback to use during CreateInstance.
3197 * After the instance is created, we use the instance-based
3198 * function to register the final callback.
3199 */
Karl Schultza2615462017-01-20 13:19:20 -07003200 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003201 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003202 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07003203 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
3204 dbgCreateInfoTemp.pNext = NULL;
3205 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
3206 dbgCreateInfoTemp.pUserData = demo;
3207 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06003208 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003209 if (demo->validate_checks_disabled) {
3210 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
3211 val_flags.pNext = NULL;
3212 val_flags.disabledValidationCheckCount = 1;
3213 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
3214 val_flags.pDisabledValidationChecks = &disabled_check;
3215 dbgCreateInfoTemp.pNext = (void*)&val_flags;
3216 }
Karl Schultza2615462017-01-20 13:19:20 -07003217 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003218 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003219
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003220 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003221
Chia-I Wu63636062015-10-26 21:10:41 +08003222 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003223 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
3224 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06003225 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06003226 "additional information.\n",
3227 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003228 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003229 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07003230 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003231 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003232 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06003233 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
3234 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06003235 "the Getting Started guide for additional information.\n",
3236 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003237 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003238
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003239 /* Make initial call to query gpu_count, then second call for gpu info*/
3240 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3241 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003242
3243 if (gpu_count > 0) {
3244 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3245 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3246 assert(!err);
3247 /* For cube demo we just grab the first physical device */
3248 demo->gpu = physical_devices[0];
3249 free(physical_devices);
3250 } else {
3251 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3252 "Do you have a compatible Vulkan installable client driver (ICD) "
3253 "installed?\nPlease look at the Getting Started guide for "
3254 "additional information.\n",
3255 "vkEnumeratePhysicalDevices Failure");
3256 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003257
Dustin Graves7c287352016-01-27 13:48:06 -07003258 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003259 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003260 VkBool32 swapchainExtFound = 0;
3261 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003262 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003263
Karl Schultze4dc75c2016-02-02 15:37:51 -07003264 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
3265 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003266 assert(!err);
3267
Dustin Graves7c287352016-01-27 13:48:06 -07003268 if (device_extension_count > 0) {
3269 VkExtensionProperties *device_extensions =
3270 malloc(sizeof(VkExtensionProperties) * device_extension_count);
3271 err = vkEnumerateDeviceExtensionProperties(
3272 demo->gpu, NULL, &device_extension_count, device_extensions);
3273 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003274
Dustin Graves7c287352016-01-27 13:48:06 -07003275 for (uint32_t i = 0; i < device_extension_count; i++) {
3276 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3277 device_extensions[i].extensionName)) {
3278 swapchainExtFound = 1;
3279 demo->extension_names[demo->enabled_extension_count++] =
3280 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3281 }
3282 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003283 }
Dustin Graves7c287352016-01-27 13:48:06 -07003284
Ian Elliott53622a72017-03-28 11:06:33 -06003285 if (demo->VK_KHR_incremental_present_enabled) {
3286 // Even though the user "enabled" the extension via the command
3287 // line, we must make sure that it's enumerated for use with the
3288 // device. Therefore, disable it here, and re-enable it again if
3289 // enumerated.
3290 demo->VK_KHR_incremental_present_enabled = false;
3291 for (uint32_t i = 0; i < device_extension_count; i++) {
3292 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
3293 device_extensions[i].extensionName)) {
3294 demo->extension_names[demo->enabled_extension_count++] =
3295 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
3296 demo->VK_KHR_incremental_present_enabled = true;
3297 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3298 }
3299 assert(demo->enabled_extension_count < 64);
3300 }
3301 if (!demo->VK_KHR_incremental_present_enabled) {
3302 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3303 }
3304 }
3305
Ian Elliottd940ca22017-03-22 08:31:27 -06003306 if (demo->VK_GOOGLE_display_timing_enabled) {
3307 // Even though the user "enabled" the extension via the command
3308 // line, we must make sure that it's enumerated for use with the
3309 // device. Therefore, disable it here, and re-enable it again if
3310 // enumerated.
3311 demo->VK_GOOGLE_display_timing_enabled = false;
3312 for (uint32_t i = 0; i < device_extension_count; i++) {
3313 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
3314 device_extensions[i].extensionName)) {
3315 demo->extension_names[demo->enabled_extension_count++] =
3316 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
3317 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003318 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003319 }
3320 assert(demo->enabled_extension_count < 64);
3321 }
3322 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003323 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003324 }
3325 }
3326
Dustin Graves7c287352016-01-27 13:48:06 -07003327 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003328 }
Dustin Graves7c287352016-01-27 13:48:06 -07003329
Tony Barbourcc69f452015-10-08 13:59:42 -06003330 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003331 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3332 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3333 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003334 "Vulkan installable client driver (ICD) installed?\nPlease "
3335 "look at the Getting Started guide for additional "
3336 "information.\n",
3337 "vkCreateInstance Failure");
3338 }
3339
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003340 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003341 demo->CreateDebugReportCallback =
3342 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3343 demo->inst, "vkCreateDebugReportCallbackEXT");
3344 demo->DestroyDebugReportCallback =
3345 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3346 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003347 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003348 ERR_EXIT(
3349 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3350 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003351 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003352 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003353 ERR_EXIT(
3354 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3355 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003356 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003357 demo->DebugReportMessage =
3358 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3359 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003360 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003361 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003362 "vkGetProcAddr Failure");
3363 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003364
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003365 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003366 PFN_vkDebugReportCallbackEXT callback;
3367 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003368 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003369 dbgCreateInfo.pNext = NULL;
3370 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003371 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003372 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003373 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003374 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3375 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003376 switch (err) {
3377 case VK_SUCCESS:
3378 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003379 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003380 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3381 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003382 break;
3383 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003384 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3385 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003386 break;
3387 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003388 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003389 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003390
3391 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003392 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3393 &demo->queue_family_count, NULL);
3394 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003395
Karl Schultze4dc75c2016-02-02 15:37:51 -07003396 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003397 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3398 vkGetPhysicalDeviceQueueFamilyProperties(
3399 demo->gpu, &demo->queue_family_count, demo->queue_props);
3400
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003401 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003402 // If app has specific feature requirements it should check supported
3403 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003404 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003405 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003406
Tony Barbourda2bad52016-01-22 14:36:40 -07003407 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3408 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3409 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3410 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3411 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3412}
3413
Karl Schultze4dc75c2016-02-02 15:37:51 -07003414static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003415 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003416 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003417 VkDeviceQueueCreateInfo queues[2];
3418 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3419 queues[0].pNext = NULL;
3420 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3421 queues[0].queueCount = 1;
3422 queues[0].pQueuePriorities = queue_priorities;
3423 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003424
3425 VkDeviceCreateInfo device = {
3426 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3427 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003428 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003429 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003430 .enabledLayerCount = 0,
3431 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003432 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003433 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3434 .pEnabledFeatures =
3435 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003436 };
Tony Barbour22e06272016-09-07 16:07:56 -06003437 if (demo->separate_present_queue) {
3438 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3439 queues[1].pNext = NULL;
3440 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3441 queues[1].queueCount = 1;
3442 queues[1].pQueuePriorities = queue_priorities;
3443 queues[1].flags = 0;
3444 device.queueCreateInfoCount = 2;
3445 }
Chia-I Wu63636062015-10-26 21:10:41 +08003446 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003447 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003448}
3449
Karl Schultze4dc75c2016-02-02 15:37:51 -07003450static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003451 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003452
Karl Schultze4dc75c2016-02-02 15:37:51 -07003453// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003454#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003455 VkWin32SurfaceCreateInfoKHR createInfo;
3456 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3457 createInfo.pNext = NULL;
3458 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003459 createInfo.hinstance = demo->connection;
3460 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003461
Karl Schultze4dc75c2016-02-02 15:37:51 -07003462 err =
3463 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003464#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003465 VkWaylandSurfaceCreateInfoKHR createInfo;
3466 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3467 createInfo.pNext = NULL;
3468 createInfo.flags = 0;
3469 createInfo.display = demo->display;
3470 createInfo.surface = demo->window;
3471
3472 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3473 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003474#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003475#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3476 VkAndroidSurfaceCreateInfoKHR createInfo;
3477 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3478 createInfo.pNext = NULL;
3479 createInfo.flags = 0;
3480 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003481
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003482 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003483#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3484 VkXlibSurfaceCreateInfoKHR createInfo;
3485 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3486 createInfo.pNext = NULL;
3487 createInfo.flags = 0;
3488 createInfo.dpy = demo->display;
3489 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003490
Tony Barbour153cb062016-12-07 13:43:36 -07003491 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003492 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003493#elif defined(VK_USE_PLATFORM_XCB_KHR)
3494 VkXcbSurfaceCreateInfoKHR createInfo;
3495 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3496 createInfo.pNext = NULL;
3497 createInfo.flags = 0;
3498 createInfo.connection = demo->connection;
3499 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003500
Tony Barbour153cb062016-12-07 13:43:36 -07003501 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003502#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3503 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003504#elif defined(VK_USE_PLATFORM_IOS_MVK)
3505 VkIOSSurfaceCreateInfoMVK surface;
3506 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3507 surface.pNext = NULL;
3508 surface.flags = 0;
3509 surface.pView = demo->window;
3510
3511 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3512#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3513 VkMacOSSurfaceCreateInfoMVK surface;
3514 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3515 surface.pNext = NULL;
3516 surface.flags = 0;
3517 surface.pView = demo->window;
3518
3519 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003520#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003521 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003522
Tony Barbourcc69f452015-10-08 13:59:42 -06003523 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003524 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003525 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003526 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003527 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003528 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003529 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003530
3531 // Search for a graphics and a present queue in the array of queue
3532 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003533 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3534 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003535 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003536 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3537 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3538 graphicsQueueFamilyIndex = i;
3539 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003540
Tony Barbourab2a0362016-09-06 11:40:12 -06003541 if (supportsPresent[i] == VK_TRUE) {
3542 graphicsQueueFamilyIndex = i;
3543 presentQueueFamilyIndex = i;
3544 break;
3545 }
3546 }
3547 }
3548
3549 if (presentQueueFamilyIndex == UINT32_MAX) {
3550 // If didn't find a queue that supports both graphics and present, then
3551 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003552 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003553 if (supportsPresent[i] == VK_TRUE) {
3554 presentQueueFamilyIndex = i;
3555 break;
3556 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003557 }
3558 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003559
3560 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003561 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3562 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003563 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003564 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003565 }
3566
Tony Barbourab2a0362016-09-06 11:40:12 -06003567 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3568 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003569 demo->separate_present_queue =
3570 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003571 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003572
Tony Barbourda2bad52016-01-22 14:36:40 -07003573 demo_create_device(demo);
3574
3575 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3576 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3577 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3578 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3579 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003580 if (demo->VK_GOOGLE_display_timing_enabled) {
3581 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3582 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3583 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003584
Tony Barboura2a67812016-07-18 13:21:06 -06003585 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3586 &demo->graphics_queue);
3587
Tony Barbour22e06272016-09-07 16:07:56 -06003588 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003589 demo->present_queue = demo->graphics_queue;
3590 } else {
3591 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3592 &demo->present_queue);
3593 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003594
Ian Elliottaaae5352015-07-06 14:27:58 -06003595 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003596 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003597 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003598 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003599 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003600 VkSurfaceFormatKHR *surfFormats =
3601 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003602 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003603 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003604 assert(!err);
3605 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3606 // the surface has no preferred format. Otherwise, at least one
3607 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003608 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003609 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003610 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003611 assert(formatCount >= 1);
3612 demo->format = surfFormats[0].format;
3613 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003614 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003615
David Pinedo2bb7c932015-06-18 17:03:14 -06003616 demo->quit = false;
3617 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003618
Tony Barbource7524e2016-07-28 12:01:45 -06003619 // Create semaphores to synchronize acquiring presentable buffers before
3620 // rendering and waiting for drawing to be complete before presenting
3621 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3622 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3623 .pNext = NULL,
3624 .flags = 0,
3625 };
Tony Barbource7524e2016-07-28 12:01:45 -06003626
Tony Barbour66a56c32016-09-21 13:10:56 -06003627 // Create fences that we can use to throttle if we get too far
3628 // ahead of the image presents
3629 VkFenceCreateInfo fence_ci = {
3630 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3631 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003632 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003633 };
3634 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003635 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3636 assert(!err);
3637
Tony Barbour22e06272016-09-07 16:07:56 -06003638 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003639 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003640 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003641
3642 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3643 &demo->draw_complete_semaphores[i]);
3644 assert(!err);
3645
3646 if (demo->separate_present_queue) {
3647 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3648 &demo->image_ownership_semaphores[i]);
3649 assert(!err);
3650 }
Tony Barbour22e06272016-09-07 16:07:56 -06003651 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003652 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003653
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003654 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003655 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003656}
3657
Tony Barbour153cb062016-12-07 13:43:36 -07003658#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003659static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3660 wl_fixed_t sy) {}
3661
3662static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3663
3664static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3665
3666static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3667 uint32_t state) {
3668 struct demo *demo = data;
3669 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3670 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
3671 }
3672}
3673
3674static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3675
3676static const struct wl_pointer_listener pointer_listener = {
3677 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3678};
3679
3680static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3681
3682static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3683 struct wl_array *keys) {}
3684
3685static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3686
3687static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3688 uint32_t state) {
3689 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3690 struct demo *demo = data;
3691 switch (key) {
3692 case KEY_ESC: // Escape
3693 demo->quit = true;
3694 break;
3695 case KEY_LEFT: // left arrow key
3696 demo->spin_angle -= demo->spin_increment;
3697 break;
3698 case KEY_RIGHT: // right arrow key
3699 demo->spin_angle += demo->spin_increment;
3700 break;
3701 case KEY_SPACE: // space bar
3702 demo->pause = !demo->pause;
3703 break;
3704 }
3705}
3706
3707static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3708 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3709
3710static const struct wl_keyboard_listener keyboard_listener = {
3711 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3712};
3713
3714static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3715 // Subscribe to pointer events
3716 struct demo *demo = data;
3717 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3718 demo->pointer = wl_seat_get_pointer(seat);
3719 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3720 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3721 wl_pointer_destroy(demo->pointer);
3722 demo->pointer = NULL;
3723 }
3724 // Subscribe to keyboard events
3725 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3726 demo->keyboard = wl_seat_get_keyboard(seat);
3727 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3728 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3729 wl_keyboard_destroy(demo->keyboard);
3730 demo->keyboard = NULL;
3731 }
3732}
3733
3734static const struct wl_seat_listener seat_listener = {
3735 seat_handle_capabilities,
3736};
3737
3738static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003739 uint32_t version UNUSED) {
3740 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003741 // pickup wayland objects when they appear
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003742 if (strcmp(interface, "wl_compositor") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003743 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003744 } else if (strcmp(interface, "wl_shell") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003745 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3746 } else if (strcmp(interface, "wl_seat") == 0) {
3747 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3748 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003749 }
3750}
3751
3752static void registry_handle_global_remove(void *data UNUSED,
3753 struct wl_registry *registry UNUSED,
3754 uint32_t name UNUSED) {}
3755
3756static const struct wl_registry_listener registry_listener = {
3757 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003758#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003759#endif
3760
Karl Schultze4dc75c2016-02-02 15:37:51 -07003761static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003762#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003763 const xcb_setup_t *setup;
3764 xcb_screen_iterator_t iter;
3765 int scr;
3766
3767 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003768 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003769 printf("Cannot find a compatible Vulkan installable client driver "
3770 "(ICD).\nExiting ...\n");
3771 fflush(stdout);
3772 exit(1);
3773 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003774
3775 setup = xcb_get_setup(demo->connection);
3776 iter = xcb_setup_roots_iterator(setup);
3777 while (scr-- > 0)
3778 xcb_screen_next(&iter);
3779
3780 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003781#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3782 demo->display = wl_display_connect(NULL);
3783
3784 if (demo->display == NULL) {
3785 printf("Cannot find a compatible Vulkan installable client driver "
3786 "(ICD).\nExiting ...\n");
3787 fflush(stdout);
3788 exit(1);
3789 }
3790
3791 demo->registry = wl_display_get_registry(demo->display);
3792 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3793 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003794#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003795#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003796}
3797
Karl Schultze4dc75c2016-02-02 15:37:51 -07003798static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003799 vec3 eye = {0.0f, 3.0f, 5.0f};
3800 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003801 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003802
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003803 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003804 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003805 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003806
Piers Daniell735ee532015-02-23 16:23:13 -07003807 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003808 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003809 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003810 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003811 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003812 if ((strcmp(argv[i], "--present_mode") == 0) &&
3813 (i < argc - 1)) {
3814 demo->presentMode = atoi(argv[i+1]);
3815 i++;
3816 continue;
3817 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003818 if (strcmp(argv[i], "--break") == 0) {
3819 demo->use_break = true;
3820 continue;
3821 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003822 if (strcmp(argv[i], "--validate") == 0) {
3823 demo->validate = true;
3824 continue;
3825 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003826 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3827 demo->validate = true;
3828 demo->validate_checks_disabled = true;
3829 continue;
3830 }
Tony Barbour2593b182016-04-19 10:57:58 -06003831 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003832 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003833 continue;
3834 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003835 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3836 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3837 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003838 i++;
3839 continue;
3840 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003841 if (strcmp(argv[i], "--suppress_popups") == 0) {
3842 demo->suppress_popups = true;
3843 continue;
3844 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003845 if (strcmp(argv[i], "--display_timing") == 0) {
3846 demo->VK_GOOGLE_display_timing_enabled = true;
3847 continue;
3848 }
Ian Elliott53622a72017-03-28 11:06:33 -06003849 if (strcmp(argv[i], "--incremental_present") == 0) {
3850 demo->VK_KHR_incremental_present_enabled = true;
3851 continue;
3852 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003853
Cody Northropaf28a532016-09-27 10:50:57 -06003854#if defined(ANDROID)
3855 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3856#else
Mike Schuchardt9a881512017-11-01 16:16:22 -06003857 fprintf(stderr,
3858 "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled]\n"
3859 " [--break] [--c <framecount>] [--suppress_popups]\n"
3860 " [--incremental_present] [--display_timing]\n"
3861 " [--present_mode {0,1,2,3}]\n"
3862 "\n"
3863 "Options for --present_mode:\n"
3864 " %d: VK_PRESENT_MODE_IMMEDIATE_KHR\n"
3865 " %d: VK_PRESENT_MODE_MAILBOX_KHR\n"
3866 " %d: VK_PRESENT_MODE_FIFO_KHR (default)\n"
3867 " %d: VK_PRESENT_MODE_FIFO_RELAXED_KHR\n",
3868 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_FIFO_KHR,
3869 VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003870 fflush(stderr);
3871 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003872#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003873 }
3874
Tony Barbour153cb062016-12-07 13:43:36 -07003875 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003876
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003877 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003878
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003879 demo->width = 500;
3880 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003881
Tony Barbour66a56c32016-09-21 13:10:56 -06003882 demo->spin_angle = 4.0f;
3883 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003884 demo->pause = false;
3885
Karl Schultze4dc75c2016-02-02 15:37:51 -07003886 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3887 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003888 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3889 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003890
3891 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003892}
3893
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003894#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003895// Include header required for parsing the command line options.
3896#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003897
Karl Schultze4dc75c2016-02-02 15:37:51 -07003898int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3899 int nCmdShow) {
3900 MSG msg; // message
3901 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003902 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003903 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003904
Jamie Madillb2ff6502017-03-15 16:17:46 -04003905 // Ensure wParam is initialized.
3906 msg.wParam = 0;
3907
Karl Schultze4dc75c2016-02-02 15:37:51 -07003908 // Use the CommandLine functions to get the command line arguments.
3909 // Unfortunately, Microsoft outputs
3910 // this information as wide characters for Unicode, and we simply want the
3911 // Ascii version to be compatible
3912 // with the non-Windows side. So, we have to convert the information to
3913 // Ascii character strings.
3914 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3915 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003916 argc = 0;
3917 }
3918
Karl Schultze4dc75c2016-02-02 15:37:51 -07003919 if (argc > 0) {
3920 argv = (char **)malloc(sizeof(char *) * argc);
3921 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003922 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003923 } else {
3924 for (int iii = 0; iii < argc; iii++) {
3925 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003926 size_t numConverted = 0;
3927
Karl Schultze4dc75c2016-02-02 15:37:51 -07003928 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3929 if (argv[iii] != NULL) {
3930 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3931 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003932 }
3933 }
3934 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003935 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003936 argv = NULL;
3937 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003938
3939 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003940
3941 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003942 if (argc > 0 && argv != NULL) {
3943 for (int iii = 0; iii < argc; iii++) {
3944 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003945 free(argv[iii]);
3946 }
3947 }
3948 free(argv);
3949 }
3950
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003951 demo.connection = hInstance;
3952 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003953 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003954 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003955
3956 demo_prepare(&demo);
3957
Karl Schultze4dc75c2016-02-02 15:37:51 -07003958 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003959
3960 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003961 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003962 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003963 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003964 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003965 done = true; // if found, quit app
3966 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003967 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003968 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003969 DispatchMessage(&msg);
3970 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003971 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003972 }
3973
3974 demo_cleanup(&demo);
3975
Karl Schultze4dc75c2016-02-02 15:37:51 -07003976 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003977}
Bill Hollingsf4352142017-02-14 22:58:56 -05003978
3979#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
3980static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003981 const char* argv[] = { "CubeSample" };
3982 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05003983
Tony Barbourc65cd752017-03-13 15:29:35 -06003984 demo_init(demo, argc, (char**)argv);
3985 demo->window = view;
3986 demo_init_vk_swapchain(demo);
3987 demo_prepare(demo);
3988 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003989}
3990
3991static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003992 // Wait for work to finish before updating MVP.
3993 vkDeviceWaitIdle(demo->device);
3994 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003995
Tony Barbourc65cd752017-03-13 15:29:35 -06003996 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003997}
3998
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003999#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
4000#include <android/log.h>
4001#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004002#include "android_util.h"
4003
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004004static bool initialized = false;
4005static bool active = false;
4006struct demo demo;
4007
4008static int32_t processInput(struct android_app* app, AInputEvent* event) {
4009 return 0;
4010}
4011
4012static void processCommand(struct android_app* app, int32_t cmd) {
4013 switch(cmd) {
4014 case APP_CMD_INIT_WINDOW: {
4015 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06004016 // We're getting a new window. If the app is starting up, we
4017 // need to initialize. If the app has already been
4018 // initialized, that means that we lost our previous window,
4019 // which means that we have a lot of work to do. At a minimum,
4020 // we need to destroy the swapchain and surface associated with
4021 // the old window, and create a new surface and swapchain.
4022 // However, since there are a lot of other objects/state that
4023 // is tied to the swapchain, it's easiest to simply cleanup and
4024 // start over (i.e. use a brute-force approach of re-starting
4025 // the app)
4026 if (demo.prepared) {
4027 demo_cleanup(&demo);
4028 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004029
4030 // Parse Intents into argc, argv
4031 // Use the following key to send arguments, i.e.
4032 // --es args "--validate"
4033 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06004034 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004035 int argc = 0;
4036 char** argv = get_args(app, key, appTag, &argc);
4037
4038 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
4039 for (int i = 0; i < argc; i++)
4040 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
4041
4042 demo_init(&demo, argc, argv);
4043
4044 // Free the argv malloc'd by get_args
4045 for (int i = 0; i < argc; i++)
4046 free(argv[i]);
4047
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004048 demo.window = (void*)app->window;
4049 demo_init_vk_swapchain(&demo);
4050 demo_prepare(&demo);
4051 initialized = true;
4052 }
4053 break;
4054 }
4055 case APP_CMD_GAINED_FOCUS: {
4056 active = true;
4057 break;
4058 }
4059 case APP_CMD_LOST_FOCUS: {
4060 active = false;
4061 break;
4062 }
4063 }
4064}
4065
4066void android_main(struct android_app *app)
4067{
Cody Northrop8707a6e2016-04-26 19:59:19 -06004068#ifdef ANDROID
4069 int vulkanSupport = InitVulkan();
4070 if (vulkanSupport == 0)
4071 return;
4072#endif
4073
Ian Elliottf05d5d12016-05-17 12:03:35 -06004074 demo.prepared = false;
4075
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004076 app->onAppCmd = processCommand;
4077 app->onInputEvent = processInput;
4078
4079 while(1) {
4080 int events;
4081 struct android_poll_source* source;
4082 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
4083 if (source) {
4084 source->process(app, source);
4085 }
4086
4087 if (app->destroyRequested != 0) {
4088 demo_cleanup(&demo);
4089 return;
4090 }
4091 }
4092 if (initialized && active) {
4093 demo_run(&demo);
4094 }
4095 }
4096
4097}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004098#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004099int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004100 struct demo demo;
4101
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004102 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004103#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004104 demo_create_xcb_window(&demo);
4105#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4106 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004107#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4108 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004109#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004110#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004111
Tony Barbourcc69f452015-10-08 13:59:42 -06004112 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004113
4114 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004115
Tony Barbour153cb062016-12-07 13:43:36 -07004116#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004117 demo_run_xcb(&demo);
4118#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4119 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004120#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4121 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004122#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07004123#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4124 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004125#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004126
4127 demo_cleanup(&demo);
4128
Karl Schultz0218c002016-03-22 17:06:13 -06004129 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004130}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004131#endif