blob: 0fd29f912f0de5b643864fccd1cf28df148e3692 [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 };
1253 for (uint32_t i = 0; i < sizeof(compositeAlphaFlags); i++) {
1254 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,
1633 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1634 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,
Tony Barbour5ff13182016-10-19 13:58:29 -06001656 VK_ACCESS_HOST_WRITE_BIT,
1657 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1658 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 Barbour5ff13182016-10-19 13:58:29 -06001664 VK_ACCESS_HOST_WRITE_BIT,
1665 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
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001927//TODO: Merge shader reading
1928#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001929static VkShaderModule
1930demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001931 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001932 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001933 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001934
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001935 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1936 moduleCreateInfo.pNext = NULL;
1937
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001938 moduleCreateInfo.codeSize = size;
1939 moduleCreateInfo.pCode = code;
1940 moduleCreateInfo.flags = 0;
1941 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1942 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001943
1944 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001945}
1946
Karl Schultze4dc75c2016-02-02 15:37:51 -07001947char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001948 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001949 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001950 void *shader_code;
1951
Bill Hollingsf4352142017-02-14 22:58:56 -05001952#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001953 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001954#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001955
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001956 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001957 if (!fp)
1958 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001959
1960 fseek(fp, 0L, SEEK_END);
1961 size = ftell(fp);
1962
1963 fseek(fp, 0L, SEEK_SET);
1964
1965 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001966 retval = fread(shader_code, size, 1, fp);
1967 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001968
1969 *psize = size;
1970
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001971 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001972 return shader_code;
1973}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001974#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001975
Karl Schultze4dc75c2016-02-02 15:37:51 -07001976static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001977#ifdef __ANDROID__
1978 VkShaderModuleCreateInfo sh_info = {};
1979 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1980
1981#include "cube.vert.h"
1982 sh_info.codeSize = sizeof(cube_vert);
1983 sh_info.pCode = cube_vert;
1984 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1985 assert(!err);
1986#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001987 void *vertShaderCode;
1988 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001989
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001990 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001991 if (!vertShaderCode) {
1992 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
1993 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001994
Karl Schultze4dc75c2016-02-02 15:37:51 -07001995 demo->vert_shader_module =
1996 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001997
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001998 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001999#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08002000
2001 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002002}
2003
Karl Schultze4dc75c2016-02-02 15:37:51 -07002004static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002005#ifdef __ANDROID__
2006 VkShaderModuleCreateInfo sh_info = {};
2007 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
2008
2009#include "cube.frag.h"
2010 sh_info.codeSize = sizeof(cube_frag);
2011 sh_info.pCode = cube_frag;
2012 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
2013 assert(!err);
2014#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002015 void *fragShaderCode;
2016 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002017
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002018 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08002019 if (!fragShaderCode) {
2020 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
2021 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002022
Karl Schultze4dc75c2016-02-02 15:37:51 -07002023 demo->frag_shader_module =
2024 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002025
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002026 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002027#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08002028
2029 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030}
2031
Karl Schultze4dc75c2016-02-02 15:37:51 -07002032static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002033 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002034 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002035 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002036 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002037 VkPipelineRasterizationStateCreateInfo rs;
2038 VkPipelineColorBlendStateCreateInfo cb;
2039 VkPipelineDepthStencilStateCreateInfo ds;
2040 VkPipelineViewportStateCreateInfo vp;
2041 VkPipelineMultisampleStateCreateInfo ms;
2042 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
2043 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06002044 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002045
Piers Daniellba839d12015-09-29 13:01:09 -06002046 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2047 memset(&dynamicState, 0, sizeof dynamicState);
2048 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2049 dynamicState.pDynamicStates = dynamicStateEnables;
2050
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002051 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002052 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002053 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002054
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002055 memset(&vi, 0, sizeof(vi));
2056 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2057
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002058 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002059 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002060 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002061
2062 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002063 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2064 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002065 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002066 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002067 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002068 rs.rasterizerDiscardEnable = VK_FALSE;
2069 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002070 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002071
2072 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002073 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2074 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002075 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002076 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002077 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002078 cb.attachmentCount = 1;
2079 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002080
Tony Barbour29645d02015-01-16 14:27:35 -07002081 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002082 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002083 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002084 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2085 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002086 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002087 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2088 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002089
2090 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002091 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002092 ds.depthTestEnable = VK_TRUE;
2093 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002094 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002095 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002096 ds.back.failOp = VK_STENCIL_OP_KEEP;
2097 ds.back.passOp = VK_STENCIL_OP_KEEP;
2098 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002099 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002100 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002101
Tony Barbour29645d02015-01-16 14:27:35 -07002102 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002103 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002104 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002105 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002106
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002107 // Two stages: vs and fs
2108 pipeline.stageCount = 2;
2109 VkPipelineShaderStageCreateInfo shaderStages[2];
2110 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2111
Karl Schultze4dc75c2016-02-02 15:37:51 -07002112 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2113 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002114 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002115 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002116
Karl Schultze4dc75c2016-02-02 15:37:51 -07002117 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2118 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002119 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002120 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002121
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002122 memset(&pipelineCache, 0, sizeof(pipelineCache));
2123 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002124
Karl Schultze4dc75c2016-02-02 15:37:51 -07002125 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
2126 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002127 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002128
Karl Schultze4dc75c2016-02-02 15:37:51 -07002129 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002130 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002131 pipeline.pRasterizationState = &rs;
2132 pipeline.pColorBlendState = &cb;
2133 pipeline.pMultisampleState = &ms;
2134 pipeline.pViewportState = &vp;
2135 pipeline.pDepthStencilState = &ds;
2136 pipeline.pStages = shaderStages;
2137 pipeline.renderPass = demo->render_pass;
2138 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002139
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002140 pipeline.renderPass = demo->render_pass;
2141
Karl Schultze4dc75c2016-02-02 15:37:51 -07002142 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
2143 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002144 assert(!err);
2145
Chia-I Wu63636062015-10-26 21:10:41 +08002146 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2147 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002148}
2149
Karl Schultze4dc75c2016-02-02 15:37:51 -07002150static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002151 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002152 [0] =
2153 {
2154 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07002155 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002156 },
2157 [1] =
2158 {
2159 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07002160 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002161 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002162 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002163 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002164 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002165 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002166 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002167 .poolSizeCount = 2,
2168 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002169 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002170 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002171
Karl Schultze4dc75c2016-02-02 15:37:51 -07002172 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
2173 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002174 assert(!err);
2175}
2176
Karl Schultze4dc75c2016-02-02 15:37:51 -07002177static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002178 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002179 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002180 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002181
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002182 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002183 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06002184 .pNext = NULL,
2185 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002186 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002187 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002188
2189 VkDescriptorBufferInfo buffer_info;
2190 buffer_info.offset = 0;
2191 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002192
Chia-I Wuae721ba2015-05-25 16:27:55 +08002193 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002194 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002195 tex_descs[i].sampler = demo->textures[i].sampler;
2196 tex_descs[i].imageView = demo->textures[i].view;
2197 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002198 }
2199
2200 memset(&writes, 0, sizeof(writes));
2201
2202 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002203 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002204 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002205 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002206
2207 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002208 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002209 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002210 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002211 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002212
Tony Barboura72d9492017-01-11 13:35:09 -07002213 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2214 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2215 assert(!err);
2216 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2217 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2218 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2219 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2220 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002221}
2222
Karl Schultze4dc75c2016-02-02 15:37:51 -07002223static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002224 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002225 attachments[1] = demo->depth.view;
2226
Chia-I Wuf973e322015-07-08 13:34:24 +08002227 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002228 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2229 .pNext = NULL,
2230 .renderPass = demo->render_pass,
2231 .attachmentCount = 2,
2232 .pAttachments = attachments,
2233 .width = demo->width,
2234 .height = demo->height,
2235 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002236 };
2237 VkResult U_ASSERT_ONLY err;
2238 uint32_t i;
2239
Tony Barbourd8e504f2015-10-08 14:26:24 -06002240 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002241 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002242 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002243 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002244 assert(!err);
2245 }
2246}
2247
Karl Schultze4dc75c2016-02-02 15:37:51 -07002248static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002249 VkResult U_ASSERT_ONLY err;
2250
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002251 const VkCommandPoolCreateInfo cmd_pool_info = {
2252 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06002253 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002254 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06002255 .flags = 0,
2256 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07002257 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
2258 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06002259 assert(!err);
2260
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002261 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002262 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002263 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002264 .commandPool = demo->cmd_pool,
2265 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002266 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002267 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002268 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2269 assert(!err);
2270 VkCommandBufferBeginInfo cmd_buf_info = {
2271 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2272 .pNext = NULL,
2273 .flags = 0,
2274 .pInheritanceInfo = NULL,
2275 };
2276 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2277 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002278
2279 demo_prepare_buffers(demo);
2280 demo_prepare_depth(demo);
2281 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002282 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002283
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002284 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002285 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002286 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002287
Ian Elliotta0781972015-08-21 15:09:33 -06002288 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002289 err =
Tony Barboura72d9492017-01-11 13:35:09 -07002290 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002291 assert(!err);
2292 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002293
Tony Barbour22e06272016-09-07 16:07:56 -06002294 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002295 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002296 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2297 .pNext = NULL,
2298 .queueFamilyIndex = demo->present_queue_family_index,
2299 .flags = 0,
2300 };
Karl Schultza2615462017-01-20 13:19:20 -07002301 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06002302 &demo->present_cmd_pool);
2303 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002304 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002305 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2306 .pNext = NULL,
2307 .commandPool = demo->present_cmd_pool,
2308 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2309 .commandBufferCount = 1,
2310 };
2311 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2312 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07002313 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002314 assert(!err);
2315 demo_build_image_ownership_cmd(demo, i);
2316 }
2317 }
2318
Chia-I Wu63ea9262015-03-26 13:14:16 +08002319 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002320 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002321
Chia-I Wuf973e322015-07-08 13:34:24 +08002322 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002323
Ian Elliotta0781972015-08-21 15:09:33 -06002324 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002325 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002326 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002327 }
2328
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002329 /*
2330 * Prepare functions above may generate pipeline commands
2331 * that need to be flushed before beginning the render loop.
2332 */
2333 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002334 if (demo->staging_texture.image) {
2335 demo_destroy_texture_image(demo, &demo->staging_texture);
2336 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002337
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002338 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002339 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002340}
2341
Karl Schultze4dc75c2016-02-02 15:37:51 -07002342static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002343 uint32_t i;
2344
2345 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002346 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002347
Tony Barbour66a56c32016-09-21 13:10:56 -06002348 // Wait for fences from present operations
2349 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002350 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002351 vkDestroyFence(demo->device, demo->fences[i], NULL);
2352 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2353 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2354 if (demo->separate_present_queue) {
2355 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2356 }
2357 }
2358
Tony Barbourd8e504f2015-10-08 14:26:24 -06002359 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002360 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002361 }
Chia-I Wu63636062015-10-26 21:10:41 +08002362 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002363
Chia-I Wu63636062015-10-26 21:10:41 +08002364 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2365 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2366 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2367 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2368 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002369
2370 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002371 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2372 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2373 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2374 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002375 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002376 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002377
Chia-I Wu63636062015-10-26 21:10:41 +08002378 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2379 vkDestroyImage(demo->device, demo->depth.image, NULL);
2380 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002381
Ian Elliotta0781972015-08-21 15:09:33 -06002382 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002383 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002384 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002385 &demo->swapchain_image_resources[i].cmd);
2386 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2387 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002388 }
Tony Barboura72d9492017-01-11 13:35:09 -07002389 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002390 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002391 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002392
Tony Barbour22e06272016-09-07 16:07:56 -06002393 if (demo->separate_present_queue) {
2394 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002395 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002396 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002397 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002398 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002399 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002400 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002401 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002402
Tony Barbour153cb062016-12-07 13:43:36 -07002403#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002404 XDestroyWindow(demo->display, demo->xlib_window);
2405 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002406#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002407 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002408 xcb_disconnect(demo->connection);
2409 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002410#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06002411 wl_keyboard_destroy(demo->keyboard);
2412 wl_pointer_destroy(demo->pointer);
2413 wl_seat_destroy(demo->seat);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002414 wl_shell_surface_destroy(demo->shell_surface);
2415 wl_surface_destroy(demo->window);
2416 wl_shell_destroy(demo->shell);
2417 wl_compositor_destroy(demo->compositor);
2418 wl_registry_destroy(demo->registry);
2419 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002420#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002421#endif
Karl Schultz86429112017-06-20 11:27:59 -06002422
2423 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002424}
2425
Karl Schultze4dc75c2016-02-02 15:37:51 -07002426static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002427 uint32_t i;
2428
Mike Stroyanbb60b382015-10-28 09:46:57 -06002429 // Don't react to resize until after first initialization.
2430 if (!demo->prepared) {
2431 return;
2432 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002433 // In order to properly resize the window, we must re-create the swapchain
2434 // AND redo the command buffers, etc.
2435 //
2436 // First, perform part of the demo_cleanup() function:
2437 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002438 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002439
2440 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002441 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002442 }
Chia-I Wu63636062015-10-26 21:10:41 +08002443 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002444
Chia-I Wu63636062015-10-26 21:10:41 +08002445 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2446 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2447 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2448 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2449 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002450
2451 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002452 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2453 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2454 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2455 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002456 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002457
Chia-I Wu63636062015-10-26 21:10:41 +08002458 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2459 vkDestroyImage(demo->device, demo->depth.image, NULL);
2460 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002461
Ian Elliottcf074d32015-10-16 18:02:43 -06002462 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002463 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002464 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002465 &demo->swapchain_image_resources[i].cmd);
2466 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2467 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002468 }
Chia-I Wu63636062015-10-26 21:10:41 +08002469 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002470 if (demo->separate_present_queue) {
2471 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2472 }
Tony Barboura72d9492017-01-11 13:35:09 -07002473 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002474
Ian Elliottcf074d32015-10-16 18:02:43 -06002475 // Second, re-perform the demo_prepare() function, which will re-create the
2476 // swapchain:
2477 demo_prepare(demo);
2478}
2479
David Pinedo2bb7c932015-06-18 17:03:14 -06002480// On MS-Windows, make this a global, so it's available to WndProc()
2481struct demo demo;
2482
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002483#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002484static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002485 if (!demo->prepared)
2486 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002487
Ian Elliott639ca472015-04-16 15:23:05 -06002488 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002489 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002490 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002491 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002492 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002493}
Ian Elliott639ca472015-04-16 15:23:05 -06002494
2495// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002496LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2497 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002498 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002499 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002500 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002501 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002502 // The validation callback calls MessageBox which can generate paint
2503 // events - don't make more Vulkan calls if we got here from the
2504 // callback
2505 if (!in_callback) {
2506 demo_run(&demo);
2507 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002508 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002509 case WM_GETMINMAXINFO: // set window's minimum size
2510 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2511 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002512 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002513 // Resize the application to the new window size, except when
2514 // it was minimized. Vulkan doesn't support images or swapchains
2515 // with width=0 and height=0.
2516 if (wParam != SIZE_MINIMIZED) {
2517 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002518 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002519 demo_resize(&demo);
2520 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002521 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002522 default:
2523 break;
2524 }
2525 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2526}
2527
Karl Schultze4dc75c2016-02-02 15:37:51 -07002528static void demo_create_window(struct demo *demo) {
2529 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002530
2531 // Initialize the window class structure:
2532 win_class.cbSize = sizeof(WNDCLASSEX);
2533 win_class.style = CS_HREDRAW | CS_VREDRAW;
2534 win_class.lpfnWndProc = WndProc;
2535 win_class.cbClsExtra = 0;
2536 win_class.cbWndExtra = 0;
2537 win_class.hInstance = demo->connection; // hInstance
2538 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2539 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2540 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2541 win_class.lpszMenuName = NULL;
2542 win_class.lpszClassName = demo->name;
2543 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2544 // Register window class:
2545 if (!RegisterClassEx(&win_class)) {
2546 // It didn't work, so try to give a useful error:
2547 printf("Unexpected error trying to start the application!\n");
2548 fflush(stdout);
2549 exit(1);
2550 }
2551 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002552 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002553 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002554 demo->window = CreateWindowEx(0,
2555 demo->name, // class name
2556 demo->name, // app name
2557 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002558 WS_VISIBLE | WS_SYSMENU,
2559 100, 100, // x/y coords
2560 wr.right - wr.left, // width
2561 wr.bottom - wr.top, // height
2562 NULL, // handle to parent
2563 NULL, // handle to menu
2564 demo->connection, // hInstance
2565 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002566 if (!demo->window) {
2567 // It didn't work, so try to give a useful error:
2568 printf("Cannot create a window in which to draw!\n");
2569 fflush(stdout);
2570 exit(1);
2571 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002572 // Window client area size must be at least 1 pixel high, to prevent crash.
2573 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2574 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002575}
Tony Barbour8295ac42016-08-08 11:04:17 -06002576#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002577static void demo_create_xlib_window(struct demo *demo) {
2578
Tony Barbouree9cd372017-01-31 16:09:58 -07002579 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002580 demo->display = XOpenDisplay(NULL);
2581 long visualMask = VisualScreenMask;
2582 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002583 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002584 vInfoTemplate.screen = DefaultScreen(demo->display);
2585 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2586 &vInfoTemplate, &numberOfVisuals);
2587
2588 Colormap colormap = XCreateColormap(
2589 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2590 visualInfo->visual, AllocNone);
2591
Rene Lindsay11612722016-06-13 17:20:39 -06002592 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002593 windowAttributes.colormap = colormap;
2594 windowAttributes.background_pixel = 0xFFFFFFFF;
2595 windowAttributes.border_pixel = 0;
2596 windowAttributes.event_mask =
2597 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2598
2599 demo->xlib_window = XCreateWindow(
2600 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2601 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2602 visualInfo->visual,
2603 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2604
2605 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2606 XMapWindow(demo->display, demo->xlib_window);
2607 XFlush(demo->display);
2608 demo->xlib_wm_delete_window =
2609 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2610}
2611static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2612 switch(event->type) {
2613 case ClientMessage:
2614 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2615 demo->quit = true;
2616 break;
2617 case KeyPress:
2618 switch (event->xkey.keycode) {
2619 case 0x9: // Escape
2620 demo->quit = true;
2621 break;
2622 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002623 demo->spin_angle -= demo->spin_increment;
2624 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002625 case 0x72: // right arrow key
2626 demo->spin_angle += demo->spin_increment;
2627 break;
2628 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002629 demo->pause = !demo->pause;
2630 break;
2631 }
2632 break;
2633 case ConfigureNotify:
2634 if ((demo->width != event->xconfigure.width) ||
2635 (demo->height != event->xconfigure.height)) {
2636 demo->width = event->xconfigure.width;
2637 demo->height = event->xconfigure.height;
2638 demo_resize(demo);
2639 }
2640 break;
2641 default:
2642 break;
2643 }
2644
2645}
2646
2647static void demo_run_xlib(struct demo *demo) {
2648
2649 while (!demo->quit) {
2650 XEvent event;
2651
2652 if (demo->pause) {
2653 XNextEvent(demo->display, &event);
2654 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002655 }
2656 while (XPending(demo->display) > 0) {
2657 XNextEvent(demo->display, &event);
2658 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002659 }
2660
Tony Barbour2593b182016-04-19 10:57:58 -06002661 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002662 demo->curFrame++;
2663 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2664 demo->quit = true;
2665 }
2666}
Tony Barbour153cb062016-12-07 13:43:36 -07002667#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002668static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002669 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002670 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002671 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002672 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002673 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002674 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002675 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002676 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2677 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002678 demo->quit = true;
2679 }
2680 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002681 case XCB_KEY_RELEASE: {
2682 const xcb_key_release_event_t *key =
2683 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002684
Karl Schultze4dc75c2016-02-02 15:37:51 -07002685 switch (key->detail) {
2686 case 0x9: // Escape
2687 demo->quit = true;
2688 break;
2689 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002690 demo->spin_angle -= demo->spin_increment;
2691 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002692 case 0x72: // right arrow key
2693 demo->spin_angle += demo->spin_increment;
2694 break;
2695 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002696 demo->pause = !demo->pause;
2697 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002698 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002699 } break;
2700 case XCB_CONFIGURE_NOTIFY: {
2701 const xcb_configure_notify_event_t *cfg =
2702 (const xcb_configure_notify_event_t *)event;
2703 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002704 demo->width = cfg->width;
2705 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002706 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002707 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002708 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002709 default:
2710 break;
2711 }
2712}
2713
Tony Barbour2593b182016-04-19 10:57:58 -06002714static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002715 xcb_flush(demo->connection);
2716
2717 while (!demo->quit) {
2718 xcb_generic_event_t *event;
2719
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002720 if (demo->pause) {
2721 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002722 }
2723 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002724 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002725 }
2726 while (event) {
2727 demo_handle_xcb_event(demo, event);
2728 free(event);
2729 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002730 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002731
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002732 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002733 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002734 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002735 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002736 }
2737}
2738
Tony Barbour2593b182016-04-19 10:57:58 -06002739static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002740 uint32_t value_mask, value_list[32];
2741
Tony Barbour2593b182016-04-19 10:57:58 -06002742 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002743
2744 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2745 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002746 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002747 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002748
Tony Barbour2593b182016-04-19 10:57:58 -06002749 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002750 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2751 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2752 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002753
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002754 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002755 xcb_intern_atom_cookie_t cookie =
2756 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2757 xcb_intern_atom_reply_t *reply =
2758 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002759
Karl Schultze4dc75c2016-02-02 15:37:51 -07002760 xcb_intern_atom_cookie_t cookie2 =
2761 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2762 demo->atom_wm_delete_window =
2763 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002764
Tony Barbour2593b182016-04-19 10:57:58 -06002765 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002766 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002767 &(*demo->atom_wm_delete_window).atom);
2768 free(reply);
2769
Tony Barbour2593b182016-04-19 10:57:58 -06002770 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002771
Karl Schultze4dc75c2016-02-02 15:37:51 -07002772 // Force the x/y coordinates to 100,100 results are identical in consecutive
2773 // runs
2774 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002775 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002776 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002777}
Tony Barbour6b131662016-08-25 13:14:45 -06002778// VK_USE_PLATFORM_XCB_KHR
2779#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002780static void demo_run(struct demo *demo) {
2781 while (!demo->quit) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06002782 if (demo->pause) {
2783 wl_display_dispatch(demo->display); // block and wait for input
2784 }
2785 else {
2786 wl_display_dispatch_pending(demo->display); // don't block
2787 demo_draw(demo);
2788 demo->curFrame++;
2789 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2790 }
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002791 }
2792}
2793
2794static void handle_ping(void *data UNUSED,
2795 struct wl_shell_surface *shell_surface,
2796 uint32_t serial) {
2797 wl_shell_surface_pong(shell_surface, serial);
2798}
2799
2800static void handle_configure(void *data UNUSED,
2801 struct wl_shell_surface *shell_surface UNUSED,
2802 uint32_t edges UNUSED, int32_t width UNUSED,
2803 int32_t height UNUSED) {}
2804
2805static void handle_popup_done(void *data UNUSED,
2806 struct wl_shell_surface *shell_surface UNUSED) {}
2807
2808static const struct wl_shell_surface_listener shell_surface_listener = {
2809 handle_ping, handle_configure, handle_popup_done};
2810
2811static void demo_create_window(struct demo *demo) {
2812 demo->window = wl_compositor_create_surface(demo->compositor);
2813 if (!demo->window) {
2814 printf("Can not create wayland_surface from compositor!\n");
2815 fflush(stdout);
2816 exit(1);
2817 }
2818
2819 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2820 if (!demo->shell_surface) {
2821 printf("Can not get shell_surface from wayland_surface!\n");
2822 fflush(stdout);
2823 exit(1);
2824 }
2825 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2826 demo);
2827 wl_shell_surface_set_toplevel(demo->shell_surface);
2828 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2829}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002830#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2831static void demo_run(struct demo *demo) {
2832 if (!demo->prepared)
2833 return;
2834
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002835 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002836 demo->curFrame++;
2837}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002838#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002839#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2840static VkResult demo_create_display_surface(struct demo *demo) {
2841 VkResult U_ASSERT_ONLY err;
2842 uint32_t display_count;
2843 uint32_t mode_count;
2844 uint32_t plane_count;
2845 VkDisplayPropertiesKHR display_props;
2846 VkDisplayKHR display;
2847 VkDisplayModePropertiesKHR mode_props;
2848 VkDisplayPlanePropertiesKHR *plane_props;
2849 VkBool32 found_plane = VK_FALSE;
2850 uint32_t plane_index;
2851 VkExtent2D image_extent;
2852 VkDisplaySurfaceCreateInfoKHR create_info;
2853
2854 // Get the first display
2855 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2856 assert(!err);
2857
2858 if (display_count == 0) {
2859 printf("Cannot find any display!\n");
2860 fflush(stdout);
2861 exit(1);
2862 }
2863
2864 display_count = 1;
2865 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2866 assert(!err || (err == VK_INCOMPLETE));
2867
2868 display = display_props.display;
2869
2870 // Get the first mode of the display
2871 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2872 assert(!err);
2873
2874 if (mode_count == 0) {
2875 printf("Cannot find any mode for the display!\n");
2876 fflush(stdout);
2877 exit(1);
2878 }
2879
2880 mode_count = 1;
2881 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2882 assert(!err || (err == VK_INCOMPLETE));
2883
2884 // Get the list of planes
2885 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2886 assert(!err);
2887
2888 if (plane_count == 0) {
2889 printf("Cannot find any plane!\n");
2890 fflush(stdout);
2891 exit(1);
2892 }
2893
2894 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2895 assert(plane_props);
2896
2897 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2898 assert(!err);
2899
2900 // Find a plane compatible with the display
2901 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2902 uint32_t supported_count;
2903 VkDisplayKHR *supported_displays;
2904
2905 // Disqualify planes that are bound to a different display
2906 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2907 (plane_props[plane_index].currentDisplay != display)) {
2908 continue;
2909 }
2910
2911 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2912 assert(!err);
2913
2914 if (supported_count == 0) {
2915 continue;
2916 }
2917
2918 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2919 assert(supported_displays);
2920
2921 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2922 assert(!err);
2923
2924 for (uint32_t i = 0; i < supported_count; i++) {
2925 if (supported_displays[i] == display) {
2926 found_plane = VK_TRUE;
2927 break;
2928 }
2929 }
2930
2931 free(supported_displays);
2932
2933 if (found_plane) {
2934 break;
2935 }
2936 }
2937
2938 if (!found_plane) {
2939 printf("Cannot find a plane compatible with the display!\n");
2940 fflush(stdout);
2941 exit(1);
2942 }
2943
2944 free(plane_props);
2945
Tony Barbour820b55b2017-03-14 08:55:46 -06002946 VkDisplayPlaneCapabilitiesKHR planeCaps;
2947 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2948 // Find a supported alpha mode
2949 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2950 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
2951 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2952 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2953 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2954 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2955 };
2956 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2957 if (planeCaps.supportedAlpha & alphaModes[i]) {
2958 alphaMode = alphaModes[i];
2959 break;
2960 }
2961 }
Damien Leone600c3052017-01-31 10:26:07 -07002962 image_extent.width = mode_props.parameters.visibleRegion.width;
2963 image_extent.height = mode_props.parameters.visibleRegion.height;
2964
2965 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2966 create_info.pNext = NULL;
2967 create_info.flags = 0;
2968 create_info.displayMode = mode_props.displayMode;
2969 create_info.planeIndex = plane_index;
2970 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2971 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002972 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002973 create_info.globalAlpha = 1.0f;
2974 create_info.imageExtent = image_extent;
2975
2976 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2977}
2978
2979static void demo_run_display(struct demo *demo)
2980{
2981 while (!demo->quit) {
2982 demo_draw(demo);
2983 demo->curFrame++;
2984
2985 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2986 demo->quit = true;
2987 }
2988 }
2989}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002990#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002991
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002992/*
2993 * Return 1 (true) if all layer names specified in check_names
2994 * can be found in given layer properties.
2995 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002996static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002997 uint32_t layer_count,
2998 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002999 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003000 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003001 for (uint32_t j = 0; j < layer_count; j++) {
3002 if (!strcmp(check_names[i], layers[j].layerName)) {
3003 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07003004 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003005 }
3006 }
3007 if (!found) {
3008 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
3009 return 0;
3010 }
3011 }
3012 return 1;
3013}
3014
Karl Schultze4dc75c2016-02-02 15:37:51 -07003015static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003016 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003017 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003018 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06003019 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003020 char **instance_validation_layers = NULL;
3021 demo->enabled_extension_count = 0;
3022 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003023
Karl Schultz7c50ad62016-04-01 12:07:03 -06003024 char *instance_validation_layers_alt1[] = {
3025 "VK_LAYER_LUNARG_standard_validation"
3026 };
3027
3028 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07003029 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
3030 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
Mark Lobodzinski1392e812017-06-09 17:02:11 -06003031 "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003032 };
3033
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003034 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003035 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003036 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003037
Karl Schultz7c50ad62016-04-01 12:07:03 -06003038 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07003039 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003040
Karl Schultz7c50ad62016-04-01 12:07:03 -06003041 instance_validation_layers = instance_validation_layers_alt1;
3042 if (instance_layer_count > 0) {
3043 VkLayerProperties *instance_layers =
3044 malloc(sizeof (VkLayerProperties) * instance_layer_count);
3045 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
3046 instance_layers);
3047 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003048
Karl Schultz7c50ad62016-04-01 12:07:03 -06003049
3050 validation_found = demo_check_layers(
3051 ARRAY_SIZE(instance_validation_layers_alt1),
3052 instance_validation_layers, instance_layer_count,
3053 instance_layers);
3054 if (validation_found) {
3055 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003056 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
3057 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003058 } else {
3059 // use alternative set of validation layers
3060 instance_validation_layers = instance_validation_layers_alt2;
3061 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
3062 validation_found = demo_check_layers(
3063 ARRAY_SIZE(instance_validation_layers_alt2),
3064 instance_validation_layers, instance_layer_count,
3065 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003066 validation_layer_count =
3067 ARRAY_SIZE(instance_validation_layers_alt2);
3068 for (uint32_t i = 0; i < validation_layer_count; i++) {
3069 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06003070 }
3071 }
3072 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003073 }
Dustin Graves7c287352016-01-27 13:48:06 -07003074
Karl Schultz7c50ad62016-04-01 12:07:03 -06003075 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06003076 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06003077 "required validation layer.\n\n"
3078 "Please look at the Getting Started guide for additional "
3079 "information.\n",
3080 "vkCreateInstance Failure");
3081 }
Dustin Graves7c287352016-01-27 13:48:06 -07003082 }
3083
3084 /* Look for instance extensions */
3085 VkBool32 surfaceExtFound = 0;
3086 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003087 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003088
Karl Schultze4dc75c2016-02-02 15:37:51 -07003089 err = vkEnumerateInstanceExtensionProperties(
3090 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003091 assert(!err);
3092
Dustin Graves7c287352016-01-27 13:48:06 -07003093 if (instance_extension_count > 0) {
3094 VkExtensionProperties *instance_extensions =
3095 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3096 err = vkEnumerateInstanceExtensionProperties(
3097 NULL, &instance_extension_count, instance_extensions);
3098 assert(!err);
3099 for (uint32_t i = 0; i < instance_extension_count; i++) {
3100 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
3101 instance_extensions[i].extensionName)) {
3102 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003103 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003104 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003105 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003106#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003107 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
3108 instance_extensions[i].extensionName)) {
3109 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003110 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003111 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
3112 }
Tony Barbour153cb062016-12-07 13:43:36 -07003113#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003114 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
3115 instance_extensions[i].extensionName)) {
3116 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06003117 demo->extension_names[demo->enabled_extension_count++] =
3118 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
3119 }
Tony Barbour153cb062016-12-07 13:43:36 -07003120#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003121 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
3122 instance_extensions[i].extensionName)) {
3123 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003124 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003125 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
3126 }
Tony Barbour153cb062016-12-07 13:43:36 -07003127#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003128 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
3129 instance_extensions[i].extensionName)) {
3130 platformSurfaceExtFound = 1;
3131 demo->extension_names[demo->enabled_extension_count++] =
3132 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
3133 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003134#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003135#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3136 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
3137 instance_extensions[i].extensionName)) {
3138 platformSurfaceExtFound = 1;
3139 demo->extension_names[demo->enabled_extension_count++] =
3140 VK_KHR_DISPLAY_EXTENSION_NAME;
3141 }
Tony Barbour153cb062016-12-07 13:43:36 -07003142#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003143 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
3144 instance_extensions[i].extensionName)) {
3145 platformSurfaceExtFound = 1;
3146 demo->extension_names[demo->enabled_extension_count++] =
3147 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
3148 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003149#elif defined(VK_USE_PLATFORM_IOS_MVK)
3150 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3151 platformSurfaceExtFound = 1;
3152 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3153 }
3154#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3155 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3156 platformSurfaceExtFound = 1;
3157 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3158 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003159#endif
Dustin Graves7c287352016-01-27 13:48:06 -07003160 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
3161 instance_extensions[i].extensionName)) {
3162 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003163 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003164 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
3165 }
3166 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003167 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003168 }
Dustin Graves7c287352016-01-27 13:48:06 -07003169
3170 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003171 }
Dustin Graves7c287352016-01-27 13:48:06 -07003172
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003173 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003174 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3175 "the " VK_KHR_SURFACE_EXTENSION_NAME
3176 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06003177 "Vulkan installable client driver (ICD) installed?\nPlease "
3178 "look at the Getting Started guide for additional "
3179 "information.\n",
3180 "vkCreateInstance Failure");
3181 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003182 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003183#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003184 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3185 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3186 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003187 "Vulkan installable client driver (ICD) installed?\nPlease "
3188 "look at the Getting Started guide for additional "
3189 "information.\n",
3190 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003191#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003192 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3193 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3194 "Vulkan installable client driver (ICD) installed?\nPlease "
3195 "look at the Getting Started guide for additional "
3196 "information.\n",
3197 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003198#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003199 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3200 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3201 "Vulkan installable client driver (ICD) installed?\nPlease "
3202 "look at the Getting Started guide for additional "
3203 "information.\n",
3204 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003205#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003206 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3207 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3208 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003209 "Vulkan installable client driver (ICD) installed?\nPlease "
3210 "look at the Getting Started guide for additional "
3211 "information.\n",
3212 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003213#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3214 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3215 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3216 " extension.\n\nDo you have a compatible "
3217 "Vulkan installable client driver (ICD) installed?\nPlease "
3218 "look at the Getting Started guide for additional "
3219 "information.\n",
3220 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003221#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003222#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3223 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3224 "the " VK_KHR_DISPLAY_EXTENSION_NAME
3225 " extension.\n\nDo you have a compatible "
3226 "Vulkan installable client driver (ICD) installed?\nPlease "
3227 "look at the Getting Started guide for additional "
3228 "information.\n",
3229 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003230#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3231 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3232 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3233 " extension.\n\nDo you have a compatible "
3234 "Vulkan installable client driver (ICD) installed?\nPlease "
3235 "look at the Getting Started guide for additional "
3236 "information.\n",
3237 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003238#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003239 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3240 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3241 " extension.\n\nDo you have a compatible "
3242 "Vulkan installable client driver (ICD) installed?\nPlease "
3243 "look at the Getting Started guide for additional "
3244 "information.\n",
3245 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003246#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003247 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003248 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003249 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003250 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003251 .pApplicationName = APP_SHORT_NAME,
3252 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003253 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003254 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003255 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003256 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003257 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003258 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003259 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003260 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003261 .enabledLayerCount = demo->enabled_layer_count,
3262 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3263 .enabledExtensionCount = demo->enabled_extension_count,
3264 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003265 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003266
3267 /*
3268 * This is info for a temp callback to use during CreateInstance.
3269 * After the instance is created, we use the instance-based
3270 * function to register the final callback.
3271 */
Karl Schultza2615462017-01-20 13:19:20 -07003272 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003273 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003274 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07003275 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
3276 dbgCreateInfoTemp.pNext = NULL;
3277 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
3278 dbgCreateInfoTemp.pUserData = demo;
3279 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06003280 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003281 if (demo->validate_checks_disabled) {
3282 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
3283 val_flags.pNext = NULL;
3284 val_flags.disabledValidationCheckCount = 1;
3285 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
3286 val_flags.pDisabledValidationChecks = &disabled_check;
3287 dbgCreateInfoTemp.pNext = (void*)&val_flags;
3288 }
Karl Schultza2615462017-01-20 13:19:20 -07003289 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003290 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003291
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003292 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003293
Chia-I Wu63636062015-10-26 21:10:41 +08003294 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003295 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
3296 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06003297 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06003298 "additional information.\n",
3299 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003300 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003301 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07003302 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003303 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003304 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06003305 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
3306 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06003307 "the Getting Started guide for additional information.\n",
3308 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003309 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003310
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003311 /* Make initial call to query gpu_count, then second call for gpu info*/
3312 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3313 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003314
3315 if (gpu_count > 0) {
3316 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3317 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3318 assert(!err);
3319 /* For cube demo we just grab the first physical device */
3320 demo->gpu = physical_devices[0];
3321 free(physical_devices);
3322 } else {
3323 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3324 "Do you have a compatible Vulkan installable client driver (ICD) "
3325 "installed?\nPlease look at the Getting Started guide for "
3326 "additional information.\n",
3327 "vkEnumeratePhysicalDevices Failure");
3328 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003329
Dustin Graves7c287352016-01-27 13:48:06 -07003330 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003331 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003332 VkBool32 swapchainExtFound = 0;
3333 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003334 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003335
Karl Schultze4dc75c2016-02-02 15:37:51 -07003336 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
3337 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003338 assert(!err);
3339
Dustin Graves7c287352016-01-27 13:48:06 -07003340 if (device_extension_count > 0) {
3341 VkExtensionProperties *device_extensions =
3342 malloc(sizeof(VkExtensionProperties) * device_extension_count);
3343 err = vkEnumerateDeviceExtensionProperties(
3344 demo->gpu, NULL, &device_extension_count, device_extensions);
3345 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003346
Dustin Graves7c287352016-01-27 13:48:06 -07003347 for (uint32_t i = 0; i < device_extension_count; i++) {
3348 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3349 device_extensions[i].extensionName)) {
3350 swapchainExtFound = 1;
3351 demo->extension_names[demo->enabled_extension_count++] =
3352 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3353 }
3354 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003355 }
Dustin Graves7c287352016-01-27 13:48:06 -07003356
Ian Elliott53622a72017-03-28 11:06:33 -06003357 if (demo->VK_KHR_incremental_present_enabled) {
3358 // Even though the user "enabled" the extension via the command
3359 // line, we must make sure that it's enumerated for use with the
3360 // device. Therefore, disable it here, and re-enable it again if
3361 // enumerated.
3362 demo->VK_KHR_incremental_present_enabled = false;
3363 for (uint32_t i = 0; i < device_extension_count; i++) {
3364 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
3365 device_extensions[i].extensionName)) {
3366 demo->extension_names[demo->enabled_extension_count++] =
3367 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
3368 demo->VK_KHR_incremental_present_enabled = true;
3369 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3370 }
3371 assert(demo->enabled_extension_count < 64);
3372 }
3373 if (!demo->VK_KHR_incremental_present_enabled) {
3374 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3375 }
3376 }
3377
Ian Elliottd940ca22017-03-22 08:31:27 -06003378 if (demo->VK_GOOGLE_display_timing_enabled) {
3379 // Even though the user "enabled" the extension via the command
3380 // line, we must make sure that it's enumerated for use with the
3381 // device. Therefore, disable it here, and re-enable it again if
3382 // enumerated.
3383 demo->VK_GOOGLE_display_timing_enabled = false;
3384 for (uint32_t i = 0; i < device_extension_count; i++) {
3385 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
3386 device_extensions[i].extensionName)) {
3387 demo->extension_names[demo->enabled_extension_count++] =
3388 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
3389 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003390 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003391 }
3392 assert(demo->enabled_extension_count < 64);
3393 }
3394 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003395 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003396 }
3397 }
3398
Dustin Graves7c287352016-01-27 13:48:06 -07003399 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003400 }
Dustin Graves7c287352016-01-27 13:48:06 -07003401
Tony Barbourcc69f452015-10-08 13:59:42 -06003402 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003403 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3404 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3405 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003406 "Vulkan installable client driver (ICD) installed?\nPlease "
3407 "look at the Getting Started guide for additional "
3408 "information.\n",
3409 "vkCreateInstance Failure");
3410 }
3411
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003412 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003413 demo->CreateDebugReportCallback =
3414 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3415 demo->inst, "vkCreateDebugReportCallbackEXT");
3416 demo->DestroyDebugReportCallback =
3417 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3418 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003419 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003420 ERR_EXIT(
3421 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3422 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003423 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003424 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003425 ERR_EXIT(
3426 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3427 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003428 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003429 demo->DebugReportMessage =
3430 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3431 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003432 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003433 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003434 "vkGetProcAddr Failure");
3435 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003436
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003437 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003438 PFN_vkDebugReportCallbackEXT callback;
3439 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003440 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003441 dbgCreateInfo.pNext = NULL;
3442 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003443 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003444 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003445 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003446 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3447 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003448 switch (err) {
3449 case VK_SUCCESS:
3450 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003451 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003452 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3453 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003454 break;
3455 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003456 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3457 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003458 break;
3459 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003460 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003461 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003462
3463 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003464 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3465 &demo->queue_family_count, NULL);
3466 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003467
Karl Schultze4dc75c2016-02-02 15:37:51 -07003468 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003469 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3470 vkGetPhysicalDeviceQueueFamilyProperties(
3471 demo->gpu, &demo->queue_family_count, demo->queue_props);
3472
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003473 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003474 // If app has specific feature requirements it should check supported
3475 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003476 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003477 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003478
Tony Barbourda2bad52016-01-22 14:36:40 -07003479 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3480 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3481 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3482 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3483 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3484}
3485
Karl Schultze4dc75c2016-02-02 15:37:51 -07003486static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003487 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003488 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003489 VkDeviceQueueCreateInfo queues[2];
3490 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3491 queues[0].pNext = NULL;
3492 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3493 queues[0].queueCount = 1;
3494 queues[0].pQueuePriorities = queue_priorities;
3495 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003496
3497 VkDeviceCreateInfo device = {
3498 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3499 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003500 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003501 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003502 .enabledLayerCount = 0,
3503 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003504 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003505 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3506 .pEnabledFeatures =
3507 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003508 };
Tony Barbour22e06272016-09-07 16:07:56 -06003509 if (demo->separate_present_queue) {
3510 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3511 queues[1].pNext = NULL;
3512 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3513 queues[1].queueCount = 1;
3514 queues[1].pQueuePriorities = queue_priorities;
3515 queues[1].flags = 0;
3516 device.queueCreateInfoCount = 2;
3517 }
Chia-I Wu63636062015-10-26 21:10:41 +08003518 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003519 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003520}
3521
Karl Schultze4dc75c2016-02-02 15:37:51 -07003522static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003523 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003524
Karl Schultze4dc75c2016-02-02 15:37:51 -07003525// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003526#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003527 VkWin32SurfaceCreateInfoKHR createInfo;
3528 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3529 createInfo.pNext = NULL;
3530 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003531 createInfo.hinstance = demo->connection;
3532 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003533
Karl Schultze4dc75c2016-02-02 15:37:51 -07003534 err =
3535 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003536#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003537 VkWaylandSurfaceCreateInfoKHR createInfo;
3538 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3539 createInfo.pNext = NULL;
3540 createInfo.flags = 0;
3541 createInfo.display = demo->display;
3542 createInfo.surface = demo->window;
3543
3544 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3545 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003546#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003547#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3548 VkAndroidSurfaceCreateInfoKHR createInfo;
3549 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3550 createInfo.pNext = NULL;
3551 createInfo.flags = 0;
3552 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003553
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003554 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003555#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3556 VkXlibSurfaceCreateInfoKHR createInfo;
3557 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3558 createInfo.pNext = NULL;
3559 createInfo.flags = 0;
3560 createInfo.dpy = demo->display;
3561 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003562
Tony Barbour153cb062016-12-07 13:43:36 -07003563 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003564 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003565#elif defined(VK_USE_PLATFORM_XCB_KHR)
3566 VkXcbSurfaceCreateInfoKHR createInfo;
3567 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3568 createInfo.pNext = NULL;
3569 createInfo.flags = 0;
3570 createInfo.connection = demo->connection;
3571 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003572
Tony Barbour153cb062016-12-07 13:43:36 -07003573 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003574#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3575 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003576#elif defined(VK_USE_PLATFORM_IOS_MVK)
3577 VkIOSSurfaceCreateInfoMVK surface;
3578 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3579 surface.pNext = NULL;
3580 surface.flags = 0;
3581 surface.pView = demo->window;
3582
3583 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3584#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3585 VkMacOSSurfaceCreateInfoMVK surface;
3586 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3587 surface.pNext = NULL;
3588 surface.flags = 0;
3589 surface.pView = demo->window;
3590
3591 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003592#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003593 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003594
Tony Barbourcc69f452015-10-08 13:59:42 -06003595 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003596 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003597 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003598 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003599 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003600 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003601 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003602
3603 // Search for a graphics and a present queue in the array of queue
3604 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003605 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3606 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003607 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003608 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3609 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3610 graphicsQueueFamilyIndex = i;
3611 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003612
Tony Barbourab2a0362016-09-06 11:40:12 -06003613 if (supportsPresent[i] == VK_TRUE) {
3614 graphicsQueueFamilyIndex = i;
3615 presentQueueFamilyIndex = i;
3616 break;
3617 }
3618 }
3619 }
3620
3621 if (presentQueueFamilyIndex == UINT32_MAX) {
3622 // If didn't find a queue that supports both graphics and present, then
3623 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003624 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003625 if (supportsPresent[i] == VK_TRUE) {
3626 presentQueueFamilyIndex = i;
3627 break;
3628 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003629 }
3630 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003631
3632 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003633 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3634 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003635 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003636 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003637 }
3638
Tony Barbourab2a0362016-09-06 11:40:12 -06003639 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3640 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003641 demo->separate_present_queue =
3642 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003643 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003644
Tony Barbourda2bad52016-01-22 14:36:40 -07003645 demo_create_device(demo);
3646
3647 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3648 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3649 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3650 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3651 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003652 if (demo->VK_GOOGLE_display_timing_enabled) {
3653 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3654 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3655 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003656
Tony Barboura2a67812016-07-18 13:21:06 -06003657 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3658 &demo->graphics_queue);
3659
Tony Barbour22e06272016-09-07 16:07:56 -06003660 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003661 demo->present_queue = demo->graphics_queue;
3662 } else {
3663 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3664 &demo->present_queue);
3665 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003666
Ian Elliottaaae5352015-07-06 14:27:58 -06003667 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003668 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003669 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003670 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003671 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003672 VkSurfaceFormatKHR *surfFormats =
3673 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003674 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003675 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003676 assert(!err);
3677 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3678 // the surface has no preferred format. Otherwise, at least one
3679 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003680 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003681 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003682 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003683 assert(formatCount >= 1);
3684 demo->format = surfFormats[0].format;
3685 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003686 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003687
David Pinedo2bb7c932015-06-18 17:03:14 -06003688 demo->quit = false;
3689 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003690
Tony Barbource7524e2016-07-28 12:01:45 -06003691 // Create semaphores to synchronize acquiring presentable buffers before
3692 // rendering and waiting for drawing to be complete before presenting
3693 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3694 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3695 .pNext = NULL,
3696 .flags = 0,
3697 };
Tony Barbource7524e2016-07-28 12:01:45 -06003698
Tony Barbour66a56c32016-09-21 13:10:56 -06003699 // Create fences that we can use to throttle if we get too far
3700 // ahead of the image presents
3701 VkFenceCreateInfo fence_ci = {
3702 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3703 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003704 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003705 };
3706 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003707 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3708 assert(!err);
3709
Tony Barbour22e06272016-09-07 16:07:56 -06003710 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003711 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003712 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003713
3714 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3715 &demo->draw_complete_semaphores[i]);
3716 assert(!err);
3717
3718 if (demo->separate_present_queue) {
3719 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3720 &demo->image_ownership_semaphores[i]);
3721 assert(!err);
3722 }
Tony Barbour22e06272016-09-07 16:07:56 -06003723 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003724 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003725
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003726 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003727 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003728}
3729
Tony Barbour153cb062016-12-07 13:43:36 -07003730#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Joey Bzdek15eb0702017-06-07 09:40:36 -06003731static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3732 wl_fixed_t sy) {}
3733
3734static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3735
3736static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3737
3738static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3739 uint32_t state) {
3740 struct demo *demo = data;
3741 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3742 wl_shell_surface_move(demo->shell_surface, demo->seat, serial);
3743 }
3744}
3745
3746static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3747
3748static const struct wl_pointer_listener pointer_listener = {
3749 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3750};
3751
3752static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3753
3754static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3755 struct wl_array *keys) {}
3756
3757static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3758
3759static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3760 uint32_t state) {
3761 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3762 struct demo *demo = data;
3763 switch (key) {
3764 case KEY_ESC: // Escape
3765 demo->quit = true;
3766 break;
3767 case KEY_LEFT: // left arrow key
3768 demo->spin_angle -= demo->spin_increment;
3769 break;
3770 case KEY_RIGHT: // right arrow key
3771 demo->spin_angle += demo->spin_increment;
3772 break;
3773 case KEY_SPACE: // space bar
3774 demo->pause = !demo->pause;
3775 break;
3776 }
3777}
3778
3779static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3780 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3781
3782static const struct wl_keyboard_listener keyboard_listener = {
3783 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3784};
3785
3786static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3787 // Subscribe to pointer events
3788 struct demo *demo = data;
3789 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3790 demo->pointer = wl_seat_get_pointer(seat);
3791 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3792 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3793 wl_pointer_destroy(demo->pointer);
3794 demo->pointer = NULL;
3795 }
3796 // Subscribe to keyboard events
3797 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3798 demo->keyboard = wl_seat_get_keyboard(seat);
3799 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3800 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3801 wl_keyboard_destroy(demo->keyboard);
3802 demo->keyboard = NULL;
3803 }
3804}
3805
3806static const struct wl_seat_listener seat_listener = {
3807 seat_handle_capabilities,
3808};
3809
3810static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003811 uint32_t version UNUSED) {
3812 struct demo *demo = data;
Joey Bzdek15eb0702017-06-07 09:40:36 -06003813 // pickup wayland objects when they appear
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003814 if (strcmp(interface, "wl_compositor") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003815 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003816 } else if (strcmp(interface, "wl_shell") == 0) {
Joey Bzdek15eb0702017-06-07 09:40:36 -06003817 demo->shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
3818 } else if (strcmp(interface, "wl_seat") == 0) {
3819 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3820 wl_seat_add_listener(demo->seat, &seat_listener, demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003821 }
3822}
3823
3824static void registry_handle_global_remove(void *data UNUSED,
3825 struct wl_registry *registry UNUSED,
3826 uint32_t name UNUSED) {}
3827
3828static const struct wl_registry_listener registry_listener = {
3829 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003830#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003831#endif
3832
Karl Schultze4dc75c2016-02-02 15:37:51 -07003833static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003834#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003835 const xcb_setup_t *setup;
3836 xcb_screen_iterator_t iter;
3837 int scr;
3838
3839 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003840 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003841 printf("Cannot find a compatible Vulkan installable client driver "
3842 "(ICD).\nExiting ...\n");
3843 fflush(stdout);
3844 exit(1);
3845 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003846
3847 setup = xcb_get_setup(demo->connection);
3848 iter = xcb_setup_roots_iterator(setup);
3849 while (scr-- > 0)
3850 xcb_screen_next(&iter);
3851
3852 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003853#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3854 demo->display = wl_display_connect(NULL);
3855
3856 if (demo->display == NULL) {
3857 printf("Cannot find a compatible Vulkan installable client driver "
3858 "(ICD).\nExiting ...\n");
3859 fflush(stdout);
3860 exit(1);
3861 }
3862
3863 demo->registry = wl_display_get_registry(demo->display);
3864 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3865 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003866#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003867#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003868}
3869
Karl Schultze4dc75c2016-02-02 15:37:51 -07003870static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003871 vec3 eye = {0.0f, 3.0f, 5.0f};
3872 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003873 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003874
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003875 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003876 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003877 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003878
Piers Daniell735ee532015-02-23 16:23:13 -07003879 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003880 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003881 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003882 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003883 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003884 if ((strcmp(argv[i], "--present_mode") == 0) &&
3885 (i < argc - 1)) {
3886 demo->presentMode = atoi(argv[i+1]);
3887 i++;
3888 continue;
3889 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003890 if (strcmp(argv[i], "--break") == 0) {
3891 demo->use_break = true;
3892 continue;
3893 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003894 if (strcmp(argv[i], "--validate") == 0) {
3895 demo->validate = true;
3896 continue;
3897 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003898 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3899 demo->validate = true;
3900 demo->validate_checks_disabled = true;
3901 continue;
3902 }
Tony Barbour2593b182016-04-19 10:57:58 -06003903 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003904 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003905 continue;
3906 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003907 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3908 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3909 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003910 i++;
3911 continue;
3912 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003913 if (strcmp(argv[i], "--suppress_popups") == 0) {
3914 demo->suppress_popups = true;
3915 continue;
3916 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003917 if (strcmp(argv[i], "--display_timing") == 0) {
3918 demo->VK_GOOGLE_display_timing_enabled = true;
3919 continue;
3920 }
Ian Elliott53622a72017-03-28 11:06:33 -06003921 if (strcmp(argv[i], "--incremental_present") == 0) {
3922 demo->VK_KHR_incremental_present_enabled = true;
3923 continue;
3924 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003925
Cody Northropaf28a532016-09-27 10:50:57 -06003926#if defined(ANDROID)
3927 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3928#else
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003929 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled] [--break] "
Ian Elliott53622a72017-03-28 11:06:33 -06003930 "[--c <framecount>] [--suppress_popups] [--incremental_present] [--display_timing] [--present_mode <present mode enum>]\n"
Tony Barbour291d57b2016-10-21 14:47:07 -06003931 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3932 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3933 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3934 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3935 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3936 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003937 fflush(stderr);
3938 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003939#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003940 }
3941
Tony Barbour153cb062016-12-07 13:43:36 -07003942 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003943
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003944 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003945
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003946 demo->width = 500;
3947 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003948
Tony Barbour66a56c32016-09-21 13:10:56 -06003949 demo->spin_angle = 4.0f;
3950 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003951 demo->pause = false;
3952
Karl Schultze4dc75c2016-02-02 15:37:51 -07003953 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3954 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003955 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3956 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003957
3958 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003959}
3960
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003961#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003962// Include header required for parsing the command line options.
3963#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003964
Karl Schultze4dc75c2016-02-02 15:37:51 -07003965int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3966 int nCmdShow) {
3967 MSG msg; // message
3968 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003969 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003970 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003971
Jamie Madillb2ff6502017-03-15 16:17:46 -04003972 // Ensure wParam is initialized.
3973 msg.wParam = 0;
3974
Karl Schultze4dc75c2016-02-02 15:37:51 -07003975 // Use the CommandLine functions to get the command line arguments.
3976 // Unfortunately, Microsoft outputs
3977 // this information as wide characters for Unicode, and we simply want the
3978 // Ascii version to be compatible
3979 // with the non-Windows side. So, we have to convert the information to
3980 // Ascii character strings.
3981 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3982 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003983 argc = 0;
3984 }
3985
Karl Schultze4dc75c2016-02-02 15:37:51 -07003986 if (argc > 0) {
3987 argv = (char **)malloc(sizeof(char *) * argc);
3988 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003989 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003990 } else {
3991 for (int iii = 0; iii < argc; iii++) {
3992 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003993 size_t numConverted = 0;
3994
Karl Schultze4dc75c2016-02-02 15:37:51 -07003995 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3996 if (argv[iii] != NULL) {
3997 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3998 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003999 }
4000 }
4001 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07004002 } else {
Mark Young418b9d12016-01-06 14:26:04 -07004003 argv = NULL;
4004 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06004005
4006 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07004007
4008 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07004009 if (argc > 0 && argv != NULL) {
4010 for (int iii = 0; iii < argc; iii++) {
4011 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07004012 free(argv[iii]);
4013 }
4014 }
4015 free(argv);
4016 }
4017
Tony Barbour3dddd5d2015-04-29 16:19:20 -06004018 demo.connection = hInstance;
4019 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06004020 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06004021 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06004022
4023 demo_prepare(&demo);
4024
Karl Schultze4dc75c2016-02-02 15:37:51 -07004025 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07004026
4027 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07004028 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06004029 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07004030 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06004031 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07004032 done = true; // if found, quit app
4033 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06004034 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07004035 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06004036 DispatchMessage(&msg);
4037 }
Tony Barbourc8a59892015-10-20 12:49:46 -06004038 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06004039 }
4040
4041 demo_cleanup(&demo);
4042
Karl Schultze4dc75c2016-02-02 15:37:51 -07004043 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06004044}
Bill Hollingsf4352142017-02-14 22:58:56 -05004045
4046#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
4047static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06004048 const char* argv[] = { "CubeSample" };
4049 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05004050
Tony Barbourc65cd752017-03-13 15:29:35 -06004051 demo_init(demo, argc, (char**)argv);
4052 demo->window = view;
4053 demo_init_vk_swapchain(demo);
4054 demo_prepare(demo);
4055 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05004056}
4057
4058static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06004059 // Wait for work to finish before updating MVP.
4060 vkDeviceWaitIdle(demo->device);
4061 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05004062
Tony Barbourc65cd752017-03-13 15:29:35 -06004063 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05004064}
4065
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004066#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
4067#include <android/log.h>
4068#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004069#include "android_util.h"
4070
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004071static bool initialized = false;
4072static bool active = false;
4073struct demo demo;
4074
4075static int32_t processInput(struct android_app* app, AInputEvent* event) {
4076 return 0;
4077}
4078
4079static void processCommand(struct android_app* app, int32_t cmd) {
4080 switch(cmd) {
4081 case APP_CMD_INIT_WINDOW: {
4082 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06004083 // We're getting a new window. If the app is starting up, we
4084 // need to initialize. If the app has already been
4085 // initialized, that means that we lost our previous window,
4086 // which means that we have a lot of work to do. At a minimum,
4087 // we need to destroy the swapchain and surface associated with
4088 // the old window, and create a new surface and swapchain.
4089 // However, since there are a lot of other objects/state that
4090 // is tied to the swapchain, it's easiest to simply cleanup and
4091 // start over (i.e. use a brute-force approach of re-starting
4092 // the app)
4093 if (demo.prepared) {
4094 demo_cleanup(&demo);
4095 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004096
4097 // Parse Intents into argc, argv
4098 // Use the following key to send arguments, i.e.
4099 // --es args "--validate"
4100 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06004101 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004102 int argc = 0;
4103 char** argv = get_args(app, key, appTag, &argc);
4104
4105 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
4106 for (int i = 0; i < argc; i++)
4107 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
4108
4109 demo_init(&demo, argc, argv);
4110
4111 // Free the argv malloc'd by get_args
4112 for (int i = 0; i < argc; i++)
4113 free(argv[i]);
4114
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004115 demo.window = (void*)app->window;
4116 demo_init_vk_swapchain(&demo);
4117 demo_prepare(&demo);
4118 initialized = true;
4119 }
4120 break;
4121 }
4122 case APP_CMD_GAINED_FOCUS: {
4123 active = true;
4124 break;
4125 }
4126 case APP_CMD_LOST_FOCUS: {
4127 active = false;
4128 break;
4129 }
4130 }
4131}
4132
4133void android_main(struct android_app *app)
4134{
4135 app_dummy();
4136
Cody Northrop8707a6e2016-04-26 19:59:19 -06004137#ifdef ANDROID
4138 int vulkanSupport = InitVulkan();
4139 if (vulkanSupport == 0)
4140 return;
4141#endif
4142
Ian Elliottf05d5d12016-05-17 12:03:35 -06004143 demo.prepared = false;
4144
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004145 app->onAppCmd = processCommand;
4146 app->onInputEvent = processInput;
4147
4148 while(1) {
4149 int events;
4150 struct android_poll_source* source;
4151 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
4152 if (source) {
4153 source->process(app, source);
4154 }
4155
4156 if (app->destroyRequested != 0) {
4157 demo_cleanup(&demo);
4158 return;
4159 }
4160 }
4161 if (initialized && active) {
4162 demo_run(&demo);
4163 }
4164 }
4165
4166}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004167#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004168int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004169 struct demo demo;
4170
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004171 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004172#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004173 demo_create_xcb_window(&demo);
4174#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4175 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004176#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4177 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004178#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004179#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004180
Tony Barbourcc69f452015-10-08 13:59:42 -06004181 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004182
4183 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004184
Tony Barbour153cb062016-12-07 13:43:36 -07004185#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004186 demo_run_xcb(&demo);
4187#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4188 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004189#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4190 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004191#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07004192#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4193 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004194#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004195
4196 demo_cleanup(&demo);
4197
Karl Schultz0218c002016-03-22 17:06:13 -06004198 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004199}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004200#endif