blob: e2e40c3d2e96de80ff4653e92b2eb69548f4ef72 [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>
38#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060039
Ian Elliott639ca472015-04-16 15:23:05 -060040#ifdef _WIN32
41#pragma comment(linker, "/subsystem:windows")
Ian Elliott639ca472015-04-16 15:23:05 -060042#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070043#endif // _WIN32
Ian Elliott639ca472015-04-16 15:23:05 -060044
Tony Barbourefd0c5a2016-12-07 14:45:12 -070045#if defined(VK_USE_PLATFORM_MIR_KHR)
46#warning "Cube does not have code for Mir at this time"
47#endif
48
Cody Northrop8707a6e2016-04-26 19:59:19 -060049#ifdef ANDROID
50#include "vulkan_wrapper.h"
51#else
David Pinedoc5193c12015-11-06 12:54:48 -070052#include <vulkan/vulkan.h>
Cody Northrop8707a6e2016-04-26 19:59:19 -060053#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -070054
David Pinedo541526f2015-11-24 09:00:24 -070055#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060056#include "linmath.h"
57
Ian Elliottd940ca22017-03-22 08:31:27 -060058#include "gettime.h"
59#include "inttypes.h"
60#define MILLION 1000000L
61#define BILLION 1000000000L
62
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060063#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060064#define APP_SHORT_NAME "cube"
65#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060066
Tony Barbour66a56c32016-09-21 13:10:56 -060067// Allow a maximum of two outstanding presentation operations.
68#define FRAME_LAG 2
69
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060070#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
71
Tony Barbourfdc2d352015-04-22 09:02:32 -060072#if defined(NDEBUG) && defined(__GNUC__)
73#define U_ASSERT_ONLY __attribute__((unused))
74#else
75#define U_ASSERT_ONLY
76#endif
77
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +090078#if defined(__GNUC__)
79#define UNUSED __attribute__((unused))
80#else
81#define UNUSED
82#endif
83
Ian Elliott07264132015-04-28 11:35:02 -060084#ifdef _WIN32
Tony Barbour602ca4f2016-09-12 14:02:43 -060085bool in_callback = false;
Mark Lobodzinski85dbd822017-01-26 13:34:13 -070086#define ERR_EXIT(err_msg, err_class) \
87 do { \
88 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
89 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -070090 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -060091void DbgMsg(char *fmt, ...) {
92 va_list va;
93 va_start(va, fmt);
94 printf(fmt, va);
95 fflush(stdout);
96 va_end(va);
97}
Ian Elliott07264132015-04-28 11:35:02 -060098
Michael Lentinec6cde4b2016-04-18 13:20:45 -050099#elif defined __ANDROID__
100#include <android/log.h>
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700101#define ERR_EXIT(err_msg, err_class) \
102 do { \
103 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", err_msg)); \
104 exit(1); \
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500105 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600106#ifdef VARARGS_WORKS_ON_ANDROID
107void DbgMsg(const char *fmt, ...) {
108 va_list va;
109 va_start(va, fmt);
110 __android_log_print(ANDROID_LOG_INFO, "Cube", fmt, va);
111 va_end(va);
112}
113#else // VARARGS_WORKS_ON_ANDROID
114#define DbgMsg(fmt, ...) \
115 do { \
116 ((void)__android_log_print(ANDROID_LOG_INFO, "Cube", fmt, ##__VA_ARGS__)); \
117 } while (0)
118#endif // VARARGS_WORKS_ON_ANDROID
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500119#else
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700120#define ERR_EXIT(err_msg, err_class) \
121 do { \
Robert Morell4ccc6522017-02-01 14:51:00 -0800122 printf("%s\n", err_msg); \
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700123 fflush(stdout); \
124 exit(1); \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700125 } while (0)
Ian Elliottd940ca22017-03-22 08:31:27 -0600126void DbgMsg(char *fmt, ...) {
127 va_list va;
128 va_start(va, fmt);
129 printf(fmt, va);
130 fflush(stdout);
131 va_end(va);
132}
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500133#endif
Ian Elliott07264132015-04-28 11:35:02 -0600134
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700135#define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
136 { \
137 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
138 if (demo->fp##entrypoint == NULL) { \
139 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
140 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700141 }
Ian Elliottaaae5352015-07-06 14:27:58 -0600142
Jon Ashburn7d8342c2015-10-08 15:58:23 -0600143static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
144
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700145#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
146 { \
147 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
148 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
149 if (demo->fp##entrypoint == NULL) { \
150 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
151 } \
Karl Schultze4dc75c2016-02-02 15:37:51 -0700152 }
Ian Elliott673898b2015-06-22 15:07:49 -0600153
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600154/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700155 * structure to track all objects related to a texture.
156 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600157struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600158 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700159
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600160 VkImage image;
161 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600162
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800163 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500164 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600165 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700166 int32_t tex_width, tex_height;
167};
168
Karl Schultze4dc75c2016-02-02 15:37:51 -0700169static char *tex_files[] = {"lunarg.ppm"};
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170
Karl Schultz0218c002016-03-22 17:06:13 -0600171static int validation_error = 0;
172
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600173struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600174 // Must start with MVP
Karl Schultze4dc75c2016-02-02 15:37:51 -0700175 float mvp[4][4];
176 float position[12 * 3][4];
177 float attr[12 * 3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600178};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600179
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600180//--------------------------------------------------------------------------------------
181// Mesh and VertexFormat Data
182//--------------------------------------------------------------------------------------
Karl Schultze4dc75c2016-02-02 15:37:51 -0700183// clang-format off
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600184static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800185 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600186 -1.0f,-1.0f, 1.0f,
187 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800188 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600189 -1.0f, 1.0f,-1.0f,
190 -1.0f,-1.0f,-1.0f,
191
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800192 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600193 1.0f, 1.0f,-1.0f,
194 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800195 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600196 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600197 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600198
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800199 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600200 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600201 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800202 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600203 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600204 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600205
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600207 -1.0f, 1.0f, 1.0f,
208 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800209 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600211 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800213 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214 1.0f, 1.0f, 1.0f,
215 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 1.0f,-1.0f,-1.0f,
218 1.0f, 1.0f,-1.0f,
219
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600222 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800223 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f,-1.0f, 1.0f,
225 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600226};
227
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600228static const float g_uv_buffer_data[] = {
Rene Lindsay76867e82016-07-29 10:49:11 -0700229 0.0f, 1.0f, // -X side
230 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800231 1.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800232 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600233 0.0f, 0.0f,
234 0.0f, 1.0f,
235
Rene Lindsay76867e82016-07-29 10:49:11 -0700236 1.0f, 1.0f, // -Z side
237 0.0f, 0.0f,
238 0.0f, 1.0f,
239 1.0f, 1.0f,
240 1.0f, 0.0f,
241 0.0f, 0.0f,
242
243 1.0f, 0.0f, // -Y side
244 1.0f, 1.0f,
245 0.0f, 1.0f,
246 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600247 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800248 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600249
Rene Lindsay76867e82016-07-29 10:49:11 -0700250 1.0f, 0.0f, // +Y side
251 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800252 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600253 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700254 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600255 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600256
Rene Lindsay76867e82016-07-29 10:49:11 -0700257 1.0f, 0.0f, // +X side
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800258 0.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700259 0.0f, 1.0f,
260 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600261 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800262 1.0f, 0.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700263
264 0.0f, 0.0f, // +Z side
265 0.0f, 1.0f,
266 1.0f, 0.0f,
267 0.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600268 1.0f, 1.0f,
Rene Lindsay76867e82016-07-29 10:49:11 -0700269 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600270};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700271// clang-format on
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600272
Karl Schultze4dc75c2016-02-02 15:37:51 -0700273void dumpMatrix(const char *note, mat4x4 MVP) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600274 int i;
275
276 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700277 for (i = 0; i < 4; i++) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600278 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
279 }
280 printf("\n");
281 fflush(stdout);
282}
283
Karl Schultze4dc75c2016-02-02 15:37:51 -0700284void dumpVec4(const char *note, vec4 vector) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600285 printf("%s: \n", note);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700286 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600287 printf("\n");
288 fflush(stdout);
289}
290
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700291VKAPI_ATTR VkBool32 VKAPI_CALL BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject,
292 size_t location, int32_t msgCode, const char *pLayerPrefix, const char *pMsg,
293 void *pUserData) {
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -0700294#ifndef WIN32
295 raise(SIGTRAP);
296#else
297 DebugBreak();
298#endif
299
300 return false;
301}
302
Mark Lobodzinski4c62d002016-05-19 17:22:25 -0600303typedef struct {
Ian Elliottaaae5352015-07-06 14:27:58 -0600304 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800305 VkCommandBuffer cmd;
Tony Barbour22e06272016-09-07 16:07:56 -0600306 VkCommandBuffer graphics_to_present_cmd;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600307 VkImageView view;
Tony Barboura72d9492017-01-11 13:35:09 -0700308 VkBuffer uniform_buffer;
309 VkDeviceMemory uniform_memory;
310 VkFramebuffer framebuffer;
311 VkDescriptorSet descriptor_set;
Tony Barboura72d9492017-01-11 13:35:09 -0700312} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600313
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600314struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500315#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600316#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700317 HINSTANCE connection; // hInstance - Windows Instance
318 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
319 HWND window; // hWnd - window handle
320 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700321#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700322 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600323 Window xlib_window;
324 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700325#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700326 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327 xcb_connection_t *connection;
328 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600329 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800330 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900331#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
332 struct wl_display *display;
333 struct wl_registry *registry;
334 struct wl_compositor *compositor;
335 struct wl_surface *window;
336 struct wl_shell *shell;
337 struct wl_shell_surface *shell_surface;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700338#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500339#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700340 ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500341#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
342 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500343#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700344 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600345 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700346 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600347 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600348
Ian Elliott53622a72017-03-28 11:06:33 -0600349 bool VK_KHR_incremental_present_enabled;
350
Ian Elliottd940ca22017-03-22 08:31:27 -0600351 bool VK_GOOGLE_display_timing_enabled;
352 bool syncd_with_actual_presents;
353 uint64_t refresh_duration;
354 uint64_t refresh_duration_multiplier;
355 uint64_t target_IPD; // image present duration (inverse of frame rate)
356 uint64_t prev_desired_present_time;
357 uint32_t next_present_id;
358 uint32_t last_early_id; // 0 if no early images
359 uint32_t last_late_id; // 0 if no late images
360
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600361 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600362 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600363 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600364 VkQueue graphics_queue;
365 VkQueue present_queue;
366 uint32_t graphics_queue_family_index;
367 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600368 VkSemaphore image_acquired_semaphores[FRAME_LAG];
369 VkSemaphore draw_complete_semaphores[FRAME_LAG];
370 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600371 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600372 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600373 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600374
Tony Barbourda2bad52016-01-22 14:36:40 -0700375 uint32_t enabled_extension_count;
376 uint32_t enabled_layer_count;
377 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600378 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700379
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600380 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600381 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600382 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700384 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
385 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
386 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
387 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600388 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
389 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
390 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
391 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
392 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600393 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
394 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600395 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600396 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700397 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600398 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600399 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600400 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600401
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800402 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600403 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600404
405 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600406 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600407
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600408 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800409 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500410 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600411 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600412 } depth;
413
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600414 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600415 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600416
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700417 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500418 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600419 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600420 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800421 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600422 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600423
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600424 mat4x4 projection_matrix;
425 mat4x4 view_matrix;
426 mat4x4 model_matrix;
427
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600428 float spin_angle;
429 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600430 bool pause;
431
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600432 VkShaderModule vert_shader_module;
433 VkShaderModule frag_shader_module;
434
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600435 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800436
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600437 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600438 int32_t curFrame;
439 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600440 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600441 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600442 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600443 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700444 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
445 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
446 VkDebugReportCallbackEXT msg_callback;
447 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600448
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600449 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600450 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600451};
452
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700453VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
454 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600455 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600456 char *message = (char *)malloc(strlen(pMsg) + 100);
457
458 assert(message);
459
Cody Northropaf28a532016-09-27 10:50:57 -0600460 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
461 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600462 validation_error = 1;
463 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600464 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
465 validation_error = 1;
466 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600467 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600468 validation_error = 1;
469 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
470 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
471 validation_error = 1;
472 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
473 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600474 validation_error = 1;
475 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600476 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600477 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600478 }
479
480#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600481
Tony Barbour602ca4f2016-09-12 14:02:43 -0600482 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600483 struct demo *demo = (struct demo*) pUserData;
484 if (!demo->suppress_popups)
485 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600486 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600487
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600488#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600489
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600490 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600491 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600492 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600493 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600494 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600495 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600496 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600497 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600498 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600499 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600500 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600501 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600502 }
Cody Northropaf28a532016-09-27 10:50:57 -0600503
lenny-lunargfbe22392016-06-06 11:07:53 -0600504#else
Cody Northropaf28a532016-09-27 10:50:57 -0600505
lenny-lunargfbe22392016-06-06 11:07:53 -0600506 printf("%s\n", message);
507 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600508
lenny-lunargfbe22392016-06-06 11:07:53 -0600509#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600510
lenny-lunargfbe22392016-06-06 11:07:53 -0600511 free(message);
512
Cody Northropaf28a532016-09-27 10:50:57 -0600513 //clang-format on
514
lenny-lunargfbe22392016-06-06 11:07:53 -0600515 /*
516 * false indicates that layer should not bail-out of an
517 * API call that had validation failures. This may mean that the
518 * app dies inside the driver due to invalid parameter(s).
519 * That's what would happen without validation layers, so we'll
520 * keep that behavior here.
521 */
522 return false;
523}
524
Ian Elliottd940ca22017-03-22 08:31:27 -0600525bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
526 // The desired time was the earliest time that the present should have
527 // occured. In almost every case, the actual time should be later than the
528 // desired time. We should only consider the actual time "late" if it is
529 // after "desired + rdur".
530 if (actual <= desired) {
531 // The actual time was before or equal to the desired time. This will
532 // probably never happen, but in case it does, return false since the
533 // present was obviously NOT late.
534 return false;
535 }
536 uint64_t deadline = actual + rdur;
537 if (actual > deadline) {
538 return true;
539 } else {
540 return false;
541 }
542}
543bool CanPresentEarlier(uint64_t earliest,
544 uint64_t actual,
545 uint64_t margin,
546 uint64_t rdur) {
547 if (earliest < actual) {
548 // Consider whether this present could have occured earlier. Make sure
549 // that earliest time was at least 2msec earlier than actual time, and
550 // that the margin was at least 2msec:
551 uint64_t diff = actual - earliest;
552 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
553 // This present could have occured earlier because both: 1) the
554 // earliest time was at least 2 msec before actual time, and 2) the
555 // margin was at least 2msec.
556 return true;
557 }
558 }
559 return false;
560}
561
Ian Elliottcf074d32015-10-16 18:02:43 -0600562// Forward declaration:
563static void demo_resize(struct demo *demo);
564
Karl Schultze4dc75c2016-02-02 15:37:51 -0700565static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
566 VkFlags requirements_mask,
567 uint32_t *typeIndex) {
568 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600569 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700570 if ((typeBits & 1) == 1) {
571 // Type is available, does it match user properties?
572 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
573 requirements_mask) == requirements_mask) {
574 *typeIndex = i;
575 return true;
576 }
577 }
578 typeBits >>= 1;
579 }
580 // No memory types matched, return failure
581 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600582}
583
Karl Schultze4dc75c2016-02-02 15:37:51 -0700584static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600585 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600586
Tony Barbource7524e2016-07-28 12:01:45 -0600587 // This function could get called twice if the texture uses a staging buffer
588 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600589 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600590 return;
591
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600592 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600593 assert(!err);
594
Tony Barbource7524e2016-07-28 12:01:45 -0600595 VkFence fence;
596 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
597 .pNext = NULL,
598 .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700599 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
600 assert(!err);
601
Karl Schultze4dc75c2016-02-02 15:37:51 -0700602 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700603 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
604 .pNext = NULL,
605 .waitSemaphoreCount = 0,
606 .pWaitSemaphores = NULL,
607 .pWaitDstStageMask = NULL,
608 .commandBufferCount = 1,
609 .pCommandBuffers = cmd_bufs,
610 .signalSemaphoreCount = 0,
611 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600612
Tony Barbource7524e2016-07-28 12:01:45 -0600613 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600614 assert(!err);
615
Tony Barbource7524e2016-07-28 12:01:45 -0600616 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600617 assert(!err);
618
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600619 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600620 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600621 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600622}
623
Karl Schultze4dc75c2016-02-02 15:37:51 -0700624static void demo_set_image_layout(struct demo *demo, VkImage image,
625 VkImageAspectFlags aspectMask,
626 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700627 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600628 VkAccessFlagBits srcAccessMask,
629 VkPipelineStageFlags src_stages,
630 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600631 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600632
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600633 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600634 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600635 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700636 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800637 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600638 .oldLayout = old_image_layout,
639 .newLayout = new_image_layout,
640 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700641 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600642
Tony Barboureb5077b2016-10-19 15:23:36 -0600643 switch (new_image_layout) {
644 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600645 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600646 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
647 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600648
Tony Barboureb5077b2016-10-19 15:23:36 -0600649 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700650 image_memory_barrier.dstAccessMask =
651 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600652 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700653
Tony Barboureb5077b2016-10-19 15:23:36 -0600654 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700655 image_memory_barrier.dstAccessMask =
656 VK_ACCESS_DEPTH_STENCIL_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_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700660 image_memory_barrier.dstAccessMask =
661 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600662 break;
663
664 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
665 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
666 break;
667
668 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
669 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
670 break;
671
672 default:
673 image_memory_barrier.dstAccessMask = 0;
674 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600675 }
676
Tony Barbourf165b5d2016-10-03 16:01:41 -0600677
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600678 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600679
Karl Schultze4dc75c2016-02-02 15:37:51 -0700680 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
681 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600682}
683
Karl Schultze4dc75c2016-02-02 15:37:51 -0700684static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700685 const VkCommandBufferBeginInfo cmd_buf_info = {
686 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
687 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600688 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700689 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700690 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800691 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700692 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
693 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800694 };
695 const VkRenderPassBeginInfo rp_begin = {
696 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
697 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800698 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700699 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800700 .renderArea.offset.x = 0,
701 .renderArea.offset.y = 0,
702 .renderArea.extent.width = demo->width,
703 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600704 .clearValueCount = 2,
705 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700706 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800707 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600708
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600709 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600710 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800711 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700712 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
713 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Tony Barboura72d9492017-01-11 13:35:09 -0700714 demo->pipeline_layout, 0, 1,
715 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set,
716 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600717 VkViewport viewport;
718 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700719 viewport.height = (float)demo->height;
720 viewport.width = (float)demo->width;
721 viewport.minDepth = (float)0.0f;
722 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700723 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600724
725 VkRect2D scissor;
726 memset(&scissor, 0, sizeof(scissor));
727 scissor.extent.width = demo->width;
728 scissor.extent.height = demo->height;
729 scissor.offset.x = 0;
730 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700731 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600732 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600733 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600734 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800735 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600736
Tony Barbour22e06272016-09-07 16:07:56 -0600737 if (demo->separate_present_queue) {
738 // We have to transfer ownership from the graphics queue family to the
739 // present queue family to be able to present. Note that we don't have
740 // to transfer from present queue family back to graphics queue family at
741 // the start of the next frame because we don't care about the image's
742 // contents at that point.
743 VkImageMemoryBarrier image_ownership_barrier = {
744 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
745 .pNext = NULL,
746 .srcAccessMask = 0,
747 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
748 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
749 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
750 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
751 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700752 .image = demo->swapchain_image_resources[demo->current_buffer].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600753 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
754
755 vkCmdPipelineBarrier(cmd_buf,
756 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600757 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600758 0, NULL, 0, NULL, 1, &image_ownership_barrier);
759 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600760 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600761 assert(!err);
762}
763
Tony Barbour22e06272016-09-07 16:07:56 -0600764void demo_build_image_ownership_cmd(struct demo *demo, int i) {
765 VkResult U_ASSERT_ONLY err;
766
767 const VkCommandBufferBeginInfo cmd_buf_info = {
768 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
769 .pNext = NULL,
770 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
771 .pInheritanceInfo = NULL,
772 };
Tony Barboura72d9492017-01-11 13:35:09 -0700773 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600774 &cmd_buf_info);
775 assert(!err);
776
777 VkImageMemoryBarrier image_ownership_barrier = {
778 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
779 .pNext = NULL,
780 .srcAccessMask = 0,
781 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
782 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
783 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
784 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
785 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700786 .image = demo->swapchain_image_resources[i].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600787 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
788
Tony Barboura72d9492017-01-11 13:35:09 -0700789 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600790 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
791 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
792 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700793 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600794 assert(!err);
795}
796
Karl Schultze4dc75c2016-02-02 15:37:51 -0700797void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600798 mat4x4 MVP, Model, VP;
799 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600800 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600801 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600802
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600803 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600804
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700805 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600806 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700807 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
808 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600809 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600810
Tony Barboura72d9492017-01-11 13:35:09 -0700811 err = vkMapMemory(demo->device,
812 demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0,
813 VK_WHOLE_SIZE, 0, (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600814 assert(!err);
815
Karl Schultze4dc75c2016-02-02 15:37:51 -0700816 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600817
Tony Barboura72d9492017-01-11 13:35:09 -0700818 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819}
820
Ian Elliottd940ca22017-03-22 08:31:27 -0600821void DemoUpdateTargetIPD(struct demo *demo) {
822 // Look at what happened to previous presents, and make appropriate
823 // adjustments in timing:
824 VkResult U_ASSERT_ONLY err;
825 VkPastPresentationTimingGOOGLE* past = NULL;
826 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600827
828 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
829 demo->swapchain,
830 &count,
831 NULL);
832 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600833 if (count) {
834 past = (VkPastPresentationTimingGOOGLE*) malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
835 assert(past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600836 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
837 demo->swapchain,
838 &count,
839 past);
840 assert(!err);
841
842 bool early = false;
843 bool late = false;
844 bool calibrate_next = false;
Ian Elliottd940ca22017-03-22 08:31:27 -0600845 for (uint32_t i = 0 ; i < count ; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600846 if (!demo->syncd_with_actual_presents) {
847 // This is the first time that we've received an
848 // actualPresentTime for this swapchain. In order to not
849 // perceive these early frames as "late", we need to sync-up
850 // our future desiredPresentTime's with the
851 // actualPresentTime(s) that we're receiving now.
852 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600853
Ian Elliottd940ca22017-03-22 08:31:27 -0600854 // So that we don't suspect any pending presents as late,
855 // record them all as suspected-late presents:
856 demo->last_late_id = demo->next_present_id - 1;
857 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600858 demo->syncd_with_actual_presents = true;
859 break;
860 } else if (CanPresentEarlier(past[i].earliestPresentTime,
861 past[i].actualPresentTime,
862 past[i].presentMargin,
863 demo->refresh_duration)) {
864 // This image could have been presented earlier. We don't want
865 // to decrease the target_IPD until we've seen early presents
866 // for at least two seconds.
867 if (demo->last_early_id == past[i].presentID) {
868 // We've now seen two seconds worth of early presents.
869 // Flag it as such, and reset the counter:
870 early = true;
871 demo->last_early_id = 0;
872 } else if (demo->last_early_id == 0) {
873 // This is the first early present we've seen.
874 // Calculate the presentID for two seconds from now.
875 uint64_t lastEarlyTime =
876 past[i].actualPresentTime + (2 * BILLION);
877 uint32_t howManyPresents =
878 (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
879 demo->last_early_id = past[i].presentID + howManyPresents;
880 } else {
881 // We are in the midst of a set of early images,
882 // and so we won't do anything.
883 }
884 late = false;
885 demo->last_late_id = 0;
886 } else if (ActualTimeLate(past[i].desiredPresentTime,
887 past[i].actualPresentTime,
888 demo->refresh_duration)) {
889 // This image was presented after its desired time. Since
890 // there's a delay between calling vkQueuePresentKHR and when
891 // we get the timing data, several presents may have been late.
892 // Thus, we need to threat all of the outstanding presents as
893 // being likely late, so that we only increase the target_IPD
894 // once for all of those presents.
895 if ((demo->last_late_id == 0) ||
896 (demo->last_late_id < past[i].presentID)) {
897 late = true;
898 // Record the last suspected-late present:
899 demo->last_late_id = demo->next_present_id - 1;
900 } else {
901 // We are in the midst of a set of likely-late images,
902 // and so we won't do anything.
903 }
904 early = false;
905 demo->last_early_id = 0;
906 } else {
907 // Since this image was not presented early or late, reset
908 // any sets of early or late presentIDs:
909 early = false;
910 late = false;
911 calibrate_next = true;
912 demo->last_early_id = 0;
913 demo->last_late_id = 0;
914 }
915 }
916
917 if (early) {
918 // Since we've seen at least two-seconds worth of presnts that
919 // could have occured earlier than desired, let's decrease the
920 // target_IPD (i.e. increase the frame rate):
921 //
922 // TODO(ianelliott): Try to calculate a better target_IPD based
923 // on the most recently-seen present (this is overly-simplistic).
924 demo->refresh_duration_multiplier--;
925 if (demo->refresh_duration_multiplier == 0) {
926 // This should never happen, but in case it does, don't
927 // try to go faster.
928 demo->refresh_duration_multiplier = 1;
929 }
930 demo->target_IPD =
931 demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600932 }
933 if (late) {
934 // Since we found a new instance of a late present, we want to
935 // increase the target_IPD (i.e. decrease the frame rate):
936 //
937 // TODO(ianelliott): Try to calculate a better target_IPD based
938 // on the most recently-seen present (this is overly-simplistic).
939 demo->refresh_duration_multiplier++;
940 demo->target_IPD =
941 demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600942 }
943
944 if (calibrate_next) {
945 int64_t multiple = demo->next_present_id - past[count-1].presentID;
946 demo->prev_desired_present_time =
947 (past[count-1].actualPresentTime +
948 (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600949 }
950 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600951}
952
Karl Schultze4dc75c2016-02-02 15:37:51 -0700953static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600954 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600955
Damien Leonec336d822017-04-14 16:10:14 -0700956 // Ensure no more than FRAME_LAG renderings are outstanding
szdarkhackd322ff02016-10-08 09:51:22 +0300957 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
958 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600959
Ian Elliottaaae5352015-07-06 14:27:58 -0600960 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700961 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barboura72d9492017-01-11 13:35:09 -0700962 demo->image_acquired_semaphores[demo->frame_index],
Damien Leonec336d822017-04-14 16:10:14 -0700963 VK_NULL_HANDLE, &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600964
Tony Barboura72d9492017-01-11 13:35:09 -0700965 demo_update_data_buffer(demo);
966
Ian Elliottcf074d32015-10-16 18:02:43 -0600967 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
968 // demo->swapchain is out of date (e.g. the window was resized) and
969 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600970 demo->frame_index += 1;
971 demo->frame_index %= FRAME_LAG;
972
Ian Elliottcf074d32015-10-16 18:02:43 -0600973 demo_resize(demo);
974 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600975 return;
976 } else if (err == VK_SUBOPTIMAL_KHR) {
977 // demo->swapchain is not as optimal as it could be, but the platform's
978 // presentation engine will still present the image correctly.
979 } else {
980 assert(!err);
981 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600982 if (demo->VK_GOOGLE_display_timing_enabled) {
983 // Look at what happened to previous presents, and make appropriate
984 // adjustments in timing:
985 DemoUpdateTargetIPD(demo);
986
987 // Note: a real application would position its geometry to that it's in
988 // the correct locatoin for when the next image is presented. It might
989 // also wait, so that there's less latency between any input and when
990 // the next image is rendered/presented. This demo program is so
991 // simple that it doesn't do either of those.
992 }
993
Tony Barbource7524e2016-07-28 12:01:45 -0600994 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600995 // that the image won't be rendered to until the presentation
996 // engine has fully released ownership to the application, and it is
997 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -0600998 VkPipelineStageFlags pipe_stage_flags;
999 VkSubmitInfo submit_info;
1000 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1001 submit_info.pNext = NULL;
1002 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1003 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1004 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001005 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001006 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001007 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001008 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001009 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura72d9492017-01-11 13:35:09 -07001010 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info,
Damien Leonec336d822017-04-14 16:10:14 -07001011 demo->fences[demo->frame_index]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001012 assert(!err);
1013
Tony Barbour22e06272016-09-07 16:07:56 -06001014 if (demo->separate_present_queue) {
1015 // If we are using separate queues, change image ownership to the
1016 // present queue before presenting, waiting for the draw complete
1017 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001018 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001019 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1020 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001021 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001022 submit_info.commandBufferCount = 1;
1023 submit_info.pCommandBuffers =
Tony Barboura72d9492017-01-11 13:35:09 -07001024 &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001025 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001026 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001027 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1028 assert(!err);
1029 }
1030
1031 // If we are using separate queues we have to wait for image ownership,
1032 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001033 VkPresentInfoKHR present = {
1034 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001035 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001036 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06001037 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -06001038 ? &demo->image_ownership_semaphores[demo->frame_index]
1039 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001040 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001041 .pSwapchains = &demo->swapchain,
1042 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001043 };
1044
Ian Elliott53622a72017-03-28 11:06:33 -06001045 if (demo->VK_KHR_incremental_present_enabled) {
1046 // If using VK_KHR_incremental_present, we provide a hint of the region
1047 // that contains changed content relative to the previously-presented
1048 // image. The implementation can use this hint in order to save
1049 // work/power (by only copying the region in the hint). The
1050 // implementation is free to ignore the hint though, and so we must
1051 // ensure that the entire image has the correctly-drawn content.
1052 uint32_t eighthOfWidth = demo->width / 8;
1053 uint32_t eighthOfHeight = demo->height / 8;
1054 VkRectLayerKHR rect = {
1055 .offset.x = eighthOfWidth,
1056 .offset.y = eighthOfHeight,
1057 .extent.width = eighthOfWidth * 6,
1058 .extent.height = eighthOfHeight * 6,
1059 .layer = 0,
1060 };
1061 VkPresentRegionKHR region = {
1062 .rectangleCount = 1,
1063 .pRectangles = &rect,
1064 };
1065 VkPresentRegionsKHR regions = {
1066 .sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1067 .pNext = present.pNext,
1068 .swapchainCount = present.swapchainCount,
1069 .pRegions = &region,
1070 };
1071 present.pNext = &regions;
Ian Elliott53622a72017-03-28 11:06:33 -06001072 }
1073
Ian Elliottd940ca22017-03-22 08:31:27 -06001074 if (demo->VK_GOOGLE_display_timing_enabled) {
1075 VkPresentTimeGOOGLE ptime;
1076 if (demo->prev_desired_present_time == 0) {
1077 // This must be the first present for this swapchain.
1078 //
1079 // We don't know where we are relative to the presentation engine's
1080 // display's refresh cycle. We also don't know how long rendering
1081 // takes. Let's make a grossly-simplified assumption that the
1082 // desiredPresentTime should be half way between now and
1083 // now+target_IPD. We will adjust over time.
1084 uint64_t curtime = getTimeInNanoseconds();
1085 if (curtime == 0) {
1086 // Since we didn't find out the current time, don't give a
1087 // desiredPresentTime:
1088 ptime.desiredPresentTime = 0;
1089 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001090 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1091 }
1092 } else {
1093 ptime.desiredPresentTime = (demo->prev_desired_present_time +
1094 demo->target_IPD);
1095 }
1096 ptime.presentID = demo->next_present_id++;
1097 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001098
1099 VkPresentTimesInfoGOOGLE present_time = {
1100 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1101 .pNext = present.pNext,
1102 .swapchainCount = present.swapchainCount,
1103 .pTimes = &ptime,
1104 };
1105 if (demo->VK_GOOGLE_display_timing_enabled) {
1106 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001107 }
1108 }
1109
Tony Barboura2a67812016-07-18 13:21:06 -06001110 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001111 demo->frame_index += 1;
1112 demo->frame_index %= FRAME_LAG;
1113
Ian Elliottcf074d32015-10-16 18:02:43 -06001114 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1115 // demo->swapchain is out of date (e.g. the window was resized) and
1116 // must be recreated:
1117 demo_resize(demo);
1118 } else if (err == VK_SUBOPTIMAL_KHR) {
1119 // demo->swapchain is not as optimal as it could be, but the platform's
1120 // presentation engine will still present the image correctly.
1121 } else {
1122 assert(!err);
1123 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001124}
1125
Karl Schultze4dc75c2016-02-02 15:37:51 -07001126static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001127 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001128 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001129
Ian Elliottb08e0d92015-11-20 11:55:46 -07001130 // Check the surface capabilities and formats
1131 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001132 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
1133 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001134 assert(!err);
1135
Ian Elliott8528eff2015-08-07 15:56:59 -06001136 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001137 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1138 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001139 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06001140 VkPresentModeKHR *presentModes =
1141 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001142 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001143 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1144 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001145 assert(!err);
1146
Ian Elliotta0781972015-08-21 15:09:33 -06001147 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001148 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1149 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1150 // If the surface size is undefined, the size is set to the size
1151 // of the images requested, which must fit within the minimum and
1152 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001153 swapchainExtent.width = demo->width;
1154 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001155
1156 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1157 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1158 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1159 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1160 }
1161
1162 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1163 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1164 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1165 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1166 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001167 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001168 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001169 swapchainExtent = surfCapabilities.currentExtent;
1170 demo->width = surfCapabilities.currentExtent.width;
1171 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001172 }
1173
Tony Barbour66a56c32016-09-21 13:10:56 -06001174 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001175 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001176 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001177
Ian Elliottb18fdde2016-10-03 12:11:12 -06001178 // There are times when you may wish to use another present mode. The
1179 // following code shows how to select them, and the comments provide some
1180 // reasons you may wish to use them.
1181 //
1182 // It should be noted that Vulkan 1.0 doesn't provide a method for
1183 // synchronizing rendering with the presentation engine's display. There
1184 // is a method provided for throttling rendering with the display, but
1185 // there are some presentation engines for which this method will not work.
1186 // If an application doesn't throttle its rendering, and if it renders much
1187 // faster than the refresh rate of the display, this can waste power on
1188 // mobile devices. That is because power is being spent rendering images
1189 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001190
Ian Elliottb18fdde2016-10-03 12:11:12 -06001191 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1192 // tearing, or have some way of synchronizing their rendering with the
1193 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001194 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1195 // generally render a new presentable image every refresh cycle, but are
1196 // occasionally early. In this case, the application wants the new image
1197 // to be displayed instead of the previously-queued-for-presentation image
1198 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001199 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1200 // render a new presentable image every refresh cycle, but are occasionally
1201 // late. In this case (perhaps because of stuttering/latency concerns),
1202 // the application wants the late image to be immediately displayed, even
1203 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001204
1205 if (demo->presentMode != swapchainPresentMode) {
1206
1207 for (size_t i = 0; i < presentModeCount; ++i) {
1208 if (presentModes[i] == demo->presentMode) {
1209 swapchainPresentMode = demo->presentMode;
1210 break;
1211 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001212 }
1213 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001214 if (swapchainPresentMode != demo->presentMode) {
1215 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1216 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001217
Tony Barbour84376fe2017-01-06 15:54:22 -07001218 // Determine the number of VkImages to use in the swap chain.
1219 // Application desires to acquire 3 images at a time for triple
1220 // buffering
1221 uint32_t desiredNumOfSwapchainImages = 3;
1222 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1223 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1224 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001225 // If maxImageCount is 0, we can ask for as many images as we want;
1226 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -07001227 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -06001228 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001229 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001230 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001231 }
1232
Tony Barbour9fd6d352015-09-16 15:01:32 -06001233 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001234 if (surfCapabilities.supportedTransforms &
1235 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001236 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001237 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001238 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001239 }
1240
Tony Barbour820b55b2017-03-14 08:55:46 -06001241 // Find a supported composite alpha mode - one of these is guaranteed to be set
1242 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1243 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1244 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1245 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1246 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1247 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1248 };
1249 for (uint32_t i = 0; i < sizeof(compositeAlphaFlags); i++) {
1250 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1251 compositeAlpha = compositeAlphaFlags[i];
1252 break;
1253 }
1254 }
1255
Tony Barboura2a67812016-07-18 13:21:06 -06001256 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001257 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001258 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001259 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001260 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001261 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001262 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001263 .imageExtent =
1264 {
1265 .width = swapchainExtent.width, .height = swapchainExtent.height,
1266 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001267 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001268 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001269 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001270 .imageArrayLayers = 1,
1271 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1272 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001273 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001274 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001275 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001276 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001277 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001278 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -06001279 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001280 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001281 assert(!err);
1282
Ian Elliottcf074d32015-10-16 18:02:43 -06001283 // If we just re-created an existing swapchain, we should destroy the old
1284 // swapchain at this point.
1285 // Note: destroying the swapchain also cleans up all its associated
1286 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001287 if (oldSwapchain != VK_NULL_HANDLE) {
Tony Barboura90a4f92017-03-23 13:25:36 -06001288 // AMD driver times out waiting on fences used in AcquireNextImage on
1289 // a swapchain that is subsequently destroyed before the wait.
1290 vkWaitForFences(demo->device, FRAME_LAG, demo->fences, VK_TRUE, UINT64_MAX);
Ian Elliottb08e0d92015-11-20 11:55:46 -07001291 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001292 }
1293
1294 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001295 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001296 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001297
Karl Schultze4dc75c2016-02-02 15:37:51 -07001298 VkImage *swapchainImages =
1299 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001300 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001301 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001302 &demo->swapchainImageCount,
1303 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001304 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001305
Tony Barboura72d9492017-01-11 13:35:09 -07001306 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -07001307 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001308 assert(demo->swapchain_image_resources);
1309
Ian Elliotta0781972015-08-21 15:09:33 -06001310 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001311 VkImageViewCreateInfo color_image_view = {
1312 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001313 .pNext = NULL,
1314 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001315 .components =
1316 {
1317 .r = VK_COMPONENT_SWIZZLE_R,
1318 .g = VK_COMPONENT_SWIZZLE_G,
1319 .b = VK_COMPONENT_SWIZZLE_B,
1320 .a = VK_COMPONENT_SWIZZLE_A,
1321 },
1322 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1323 .baseMipLevel = 0,
1324 .levelCount = 1,
1325 .baseArrayLayer = 0,
1326 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001327 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1328 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001329 };
1330
Tony Barboura72d9492017-01-11 13:35:09 -07001331 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001332
Tony Barboura72d9492017-01-11 13:35:09 -07001333 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001334
Karl Schultze4dc75c2016-02-02 15:37:51 -07001335 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001336 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001337 assert(!err);
1338 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001339
Ian Elliottd940ca22017-03-22 08:31:27 -06001340 if (demo->VK_GOOGLE_display_timing_enabled) {
1341 VkRefreshCycleDurationGOOGLE rc_dur;
1342 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device,
1343 demo->swapchain,
1344 &rc_dur);
1345 assert(!err);
1346 demo->refresh_duration = rc_dur.refreshDuration;
1347
1348 demo->syncd_with_actual_presents = false;
1349 // Initially target 1X the refresh duration:
1350 demo->target_IPD = demo->refresh_duration;
1351 demo->refresh_duration_multiplier = 1;
1352 demo->prev_desired_present_time = 0;
1353 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001354 }
1355
Mark Young04fd3d82016-01-25 14:43:50 -07001356 if (NULL != presentModes) {
1357 free(presentModes);
1358 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001359}
1360
Karl Schultze4dc75c2016-02-02 15:37:51 -07001361static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001362 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001363 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001364 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001366 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001367 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001368 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001369 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001370 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001371 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001372 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001373 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001374 .flags = 0,
1375 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001376
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001377 VkImageViewCreateInfo view = {
1378 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001379 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001380 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001381 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001382 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1383 .baseMipLevel = 0,
1384 .levelCount = 1,
1385 .baseArrayLayer = 0,
1386 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001387 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001388 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001390
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001391 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001392 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001393 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001394
1395 demo->depth.format = depth_format;
1396
1397 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001398 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001399 assert(!err);
1400
Karl Schultze4dc75c2016-02-02 15:37:51 -07001401 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001402 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001403
Chia-I Wuf48700b2015-11-10 16:21:09 +08001404 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001405 demo->depth.mem_alloc.pNext = NULL;
1406 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1407 demo->depth.mem_alloc.memoryTypeIndex = 0;
1408
Karl Schultze4dc75c2016-02-02 15:37:51 -07001409 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
Jeremy Hayes1273a382017-02-22 08:53:13 -07001410 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001411 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001412 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001414 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001415 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1416 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001417 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001419 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001420 err =
1421 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001422 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423
1424 /* create image view */
1425 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001426 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428}
1429
Tony Barbour1a86d032015-09-21 15:17:33 -06001430/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001431bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001432 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001433
1434#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001435 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001436#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001437
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001438#ifdef __ANDROID__
1439#include <lunarg.ppm.h>
1440 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001441 cPtr = (char*)lunarg_ppm;
1442 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001443 return false;
1444 }
1445 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001446 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001447 if (rgba_data == NULL) {
1448 return true;
1449 }
1450 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001451 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001452 return false;
1453 }
1454 while(strncmp(cPtr++, "\n", 1));
1455
1456 for (int y = 0; y < *height; y++) {
1457 uint8_t *rowPtr = rgba_data;
1458 for (int x = 0; x < *width; x++) {
1459 memcpy(rowPtr, cPtr, 3);
1460 rowPtr[3] = 255; /* Alpha of 1 */
1461 rowPtr += 4;
1462 cPtr += 3;
1463 }
1464 rgba_data += layout->rowPitch;
1465 }
1466
1467 return true;
1468#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001469 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001470 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001471
Tony Barbour1a86d032015-09-21 15:17:33 -06001472 if (!fPtr)
1473 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001474
Tony Barbour1a86d032015-09-21 15:17:33 -06001475 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001476 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1477 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001478 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001479 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001480
Tony Barbour1a86d032015-09-21 15:17:33 -06001481 do {
1482 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001483 if (cPtr == NULL) {
1484 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001485 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001486 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001487 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001488
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001489 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001490 if (rgba_data == NULL) {
1491 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001492 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001493 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001494 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001495 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001496 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1497 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001498 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001499 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001500
Karl Schultze4dc75c2016-02-02 15:37:51 -07001501 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001502 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001503 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001504 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001505 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001506 rowPtr[3] = 255; /* Alpha of 1 */
1507 rowPtr += 4;
1508 }
1509 rgba_data += layout->rowPitch;
1510 }
1511 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001512 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001513#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001514}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001515
Karl Schultze4dc75c2016-02-02 15:37:51 -07001516static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001517 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001518 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001519 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001520 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001521 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001522 int32_t tex_width;
1523 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001524 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001525 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001526
Karl Schultze4dc75c2016-02-02 15:37:51 -07001527 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001528 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001529 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001530
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001531 tex_obj->tex_width = tex_width;
1532 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001533
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001534 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001535 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001536 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001537 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001538 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001539 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001540 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001541 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001542 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001543 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001544 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001545 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001546 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001547 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001548
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001549 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001550
Karl Schultze4dc75c2016-02-02 15:37:51 -07001551 err =
1552 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001553 assert(!err);
1554
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001555 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001556
Chia-I Wuf48700b2015-11-10 16:21:09 +08001557 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001558 tex_obj->mem_alloc.pNext = NULL;
1559 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1560 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001561
Karl Schultze4dc75c2016-02-02 15:37:51 -07001562 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1563 required_props,
1564 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001565 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001566
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001567 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001568 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001569 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001570 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001571
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001572 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001573 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001574 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001575
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001576 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001577 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001578 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001579 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001580 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001581 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001582 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001583 void *data;
1584
Karl Schultze4dc75c2016-02-02 15:37:51 -07001585 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1586 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001587
Karl Schultze4dc75c2016-02-02 15:37:51 -07001588 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1589 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001590 assert(!err);
1591
1592 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1593 fprintf(stderr, "Error loading texture: %s\n", filename);
1594 }
1595
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001596 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001597 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001598
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001599 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001600}
1601
Karl Schultze4dc75c2016-02-02 15:37:51 -07001602static void demo_destroy_texture_image(struct demo *demo,
1603 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001604 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001605 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1606 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001607}
1608
Karl Schultze4dc75c2016-02-02 15:37:51 -07001609static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001610 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001611 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001612 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001613
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001614 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001615
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001616 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001617 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001618
Karl Schultze4dc75c2016-02-02 15:37:51 -07001619 if ((props.linearTilingFeatures &
1620 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1621 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001622 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001623 demo_prepare_texture_image(
1624 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1625 VK_IMAGE_USAGE_SAMPLED_BIT,
1626 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1627 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001628 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1629 // shader to run until layout transition completes
1630 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1631 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1632 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1633 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001634 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001635 } else if (props.optimalTilingFeatures &
1636 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001637 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001638
Tony Barbour16156cb2016-10-19 14:15:49 -06001639 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001640 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001641 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001642 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1643 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1644 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001645
Karl Schultze4dc75c2016-02-02 15:37:51 -07001646 demo_prepare_texture_image(
1647 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1648 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1649 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001650
Tony Barbour16156cb2016-10-19 14:15:49 -06001651 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001652 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001653 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001654 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001655 VK_ACCESS_HOST_WRITE_BIT,
1656 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1657 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001658
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001659 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001660 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001661 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001662 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001663 VK_ACCESS_HOST_WRITE_BIT,
1664 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1665 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001666
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001667 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001668 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1669 .srcOffset = {0, 0, 0},
1670 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1671 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001672 .extent = {demo->staging_texture.tex_width,
1673 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001674 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001675 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001676 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001677 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1678 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001679
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001680 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001681 VK_IMAGE_ASPECT_COLOR_BIT,
1682 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001683 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001684 VK_ACCESS_TRANSFER_WRITE_BIT,
1685 VK_PIPELINE_STAGE_TRANSFER_BIT,
1686 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001687
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001688 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001689 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1690 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001691 }
1692
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001693 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001694 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001695 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001696 .magFilter = VK_FILTER_NEAREST,
1697 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001698 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001699 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1700 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1701 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001702 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001703 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001704 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001705 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001706 .minLod = 0.0f,
1707 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001708 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001709 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001710 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001711
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001712 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001713 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001714 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001715 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001716 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001717 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001718 .components =
1719 {
1720 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1721 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1722 },
1723 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001724 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001725 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001726
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001728 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001729 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001730 assert(!err);
1731
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001732 /* create image view */
1733 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001734 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001735 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001736 assert(!err);
1737 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001738}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001739
Tony Barboura72d9492017-01-11 13:35:09 -07001740void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001741 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001742 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001743 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001744 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001745 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001746 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001747 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001748 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001749
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001750 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001751 mat4x4_mul(MVP, VP, demo->model_matrix);
1752 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001753 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001754
Karl Schultza2615462017-01-20 13:19:20 -07001755 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001756 data.position[i][0] = g_vertex_buffer_data[i * 3];
1757 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1758 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001759 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001760 data.attr[i][0] = g_uv_buffer_data[2 * i];
1761 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001762 data.attr[i][2] = 0;
1763 data.attr[i][3] = 0;
1764 }
1765
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001766 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001767 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001768 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001769 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001770
Tony Barboura72d9492017-01-11 13:35:09 -07001771 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1772 err =
1773 vkCreateBuffer(demo->device, &buf_info, NULL,
1774 &demo->swapchain_image_resources[i].uniform_buffer);
1775 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001776
Tony Barboura72d9492017-01-11 13:35:09 -07001777 vkGetBufferMemoryRequirements(demo->device,
1778 demo->swapchain_image_resources[i].uniform_buffer,
1779 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001780
Tony Barboura72d9492017-01-11 13:35:09 -07001781 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1782 mem_alloc.pNext = NULL;
1783 mem_alloc.allocationSize = mem_reqs.size;
1784 mem_alloc.memoryTypeIndex = 0;
1785
1786 pass = memory_type_from_properties(
1787 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001788 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001789 &mem_alloc.memoryTypeIndex);
1790 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001791
Tony Barboura72d9492017-01-11 13:35:09 -07001792 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1793 &demo->swapchain_image_resources[i].uniform_memory);
1794 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001795
Tony Barboura72d9492017-01-11 13:35:09 -07001796 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1797 VK_WHOLE_SIZE, 0, (void **)&pData);
1798 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001799
Tony Barboura72d9492017-01-11 13:35:09 -07001800 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001801
Tony Barboura72d9492017-01-11 13:35:09 -07001802 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001803
Tony Barboura72d9492017-01-11 13:35:09 -07001804 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1805 demo->swapchain_image_resources[i].uniform_memory, 0);
1806 assert(!err);
1807 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001808}
1809
Karl Schultze4dc75c2016-02-02 15:37:51 -07001810static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001811 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001812 [0] =
1813 {
1814 .binding = 0,
1815 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1816 .descriptorCount = 1,
1817 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1818 .pImmutableSamplers = NULL,
1819 },
1820 [1] =
1821 {
1822 .binding = 1,
1823 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1824 .descriptorCount = DEMO_TEXTURE_COUNT,
1825 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1826 .pImmutableSamplers = NULL,
1827 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001828 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001829 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001830 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001831 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001832 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001833 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001834 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001835 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001836
Karl Schultze4dc75c2016-02-02 15:37:51 -07001837 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1838 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001839 assert(!err);
1840
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001841 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001842 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1843 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001844 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001845 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001846 };
1847
Karl Schultze4dc75c2016-02-02 15:37:51 -07001848 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001849 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001850 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001851}
1852
Karl Schultze4dc75c2016-02-02 15:37:51 -07001853static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001854 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1855 // because at the start of the renderpass, we don't care about their contents.
1856 // At the start of the subpass, the color attachment's layout will be transitioned
1857 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1858 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1859 // the renderpass, the color attachment's layout will be transitioned to
1860 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1861 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001862 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001863 [0] =
1864 {
1865 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001866 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001867 .samples = VK_SAMPLE_COUNT_1_BIT,
1868 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1869 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1870 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1871 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001872 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001873 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001874 },
1875 [1] =
1876 {
1877 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001878 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001879 .samples = VK_SAMPLE_COUNT_1_BIT,
1880 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1881 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1882 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1883 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1884 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001885 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001886 .finalLayout =
1887 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1888 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001889 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001890 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001891 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001892 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001893 const VkAttachmentReference depth_reference = {
1894 .attachment = 1,
1895 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1896 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001897 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001898 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1899 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001900 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001901 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001902 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001903 .pColorAttachments = &color_reference,
1904 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001905 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001906 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001907 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001908 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001909 const VkRenderPassCreateInfo rp_info = {
1910 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1911 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001912 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001913 .attachmentCount = 2,
1914 .pAttachments = attachments,
1915 .subpassCount = 1,
1916 .pSubpasses = &subpass,
1917 .dependencyCount = 0,
1918 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001919 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001920 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001921
Chia-I Wu63636062015-10-26 21:10:41 +08001922 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001923 assert(!err);
1924}
1925
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001926//TODO: Merge shader reading
1927#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001928static VkShaderModule
1929demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001930 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001931 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001932 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001933
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001934 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1935 moduleCreateInfo.pNext = NULL;
1936
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001937 moduleCreateInfo.codeSize = size;
1938 moduleCreateInfo.pCode = code;
1939 moduleCreateInfo.flags = 0;
1940 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1941 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001942
1943 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001944}
1945
Karl Schultze4dc75c2016-02-02 15:37:51 -07001946char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001947 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001948 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001949 void *shader_code;
1950
Bill Hollingsf4352142017-02-14 22:58:56 -05001951#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001952 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001953#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001954
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001955 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001956 if (!fp)
1957 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001958
1959 fseek(fp, 0L, SEEK_END);
1960 size = ftell(fp);
1961
1962 fseek(fp, 0L, SEEK_SET);
1963
1964 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001965 retval = fread(shader_code, size, 1, fp);
1966 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001967
1968 *psize = size;
1969
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001970 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001971 return shader_code;
1972}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001973#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001974
Karl Schultze4dc75c2016-02-02 15:37:51 -07001975static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001976#ifdef __ANDROID__
1977 VkShaderModuleCreateInfo sh_info = {};
1978 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1979
1980#include "cube.vert.h"
1981 sh_info.codeSize = sizeof(cube_vert);
1982 sh_info.pCode = cube_vert;
1983 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1984 assert(!err);
1985#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001986 void *vertShaderCode;
1987 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001988
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001989 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001990 if (!vertShaderCode) {
1991 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
1992 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001993
Karl Schultze4dc75c2016-02-02 15:37:51 -07001994 demo->vert_shader_module =
1995 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001996
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001997 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001998#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001999
2000 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002001}
2002
Karl Schultze4dc75c2016-02-02 15:37:51 -07002003static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002004#ifdef __ANDROID__
2005 VkShaderModuleCreateInfo sh_info = {};
2006 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
2007
2008#include "cube.frag.h"
2009 sh_info.codeSize = sizeof(cube_frag);
2010 sh_info.pCode = cube_frag;
2011 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
2012 assert(!err);
2013#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002014 void *fragShaderCode;
2015 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002016
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002017 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08002018 if (!fragShaderCode) {
2019 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
2020 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002021
Karl Schultze4dc75c2016-02-02 15:37:51 -07002022 demo->frag_shader_module =
2023 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002024
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002025 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002026#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08002027
2028 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002029}
2030
Karl Schultze4dc75c2016-02-02 15:37:51 -07002031static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002032 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002033 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002034 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002035 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002036 VkPipelineRasterizationStateCreateInfo rs;
2037 VkPipelineColorBlendStateCreateInfo cb;
2038 VkPipelineDepthStencilStateCreateInfo ds;
2039 VkPipelineViewportStateCreateInfo vp;
2040 VkPipelineMultisampleStateCreateInfo ms;
2041 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
2042 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06002043 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002044
Piers Daniellba839d12015-09-29 13:01:09 -06002045 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2046 memset(&dynamicState, 0, sizeof dynamicState);
2047 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2048 dynamicState.pDynamicStates = dynamicStateEnables;
2049
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002051 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002052 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002053
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002054 memset(&vi, 0, sizeof(vi));
2055 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2056
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002057 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002058 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002059 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002060
2061 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002062 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2063 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002064 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002065 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002066 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002067 rs.rasterizerDiscardEnable = VK_FALSE;
2068 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002069 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002070
2071 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002072 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2073 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002074 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002075 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002076 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002077 cb.attachmentCount = 1;
2078 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002079
Tony Barbour29645d02015-01-16 14:27:35 -07002080 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002081 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002082 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002083 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2084 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002085 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002086 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2087 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002088
2089 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002090 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002091 ds.depthTestEnable = VK_TRUE;
2092 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002093 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002094 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002095 ds.back.failOp = VK_STENCIL_OP_KEEP;
2096 ds.back.passOp = VK_STENCIL_OP_KEEP;
2097 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002098 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002099 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002100
Tony Barbour29645d02015-01-16 14:27:35 -07002101 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002102 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002103 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002104 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002105
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002106 // Two stages: vs and fs
2107 pipeline.stageCount = 2;
2108 VkPipelineShaderStageCreateInfo shaderStages[2];
2109 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2110
Karl Schultze4dc75c2016-02-02 15:37:51 -07002111 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2112 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002113 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002114 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002115
Karl Schultze4dc75c2016-02-02 15:37:51 -07002116 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2117 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002118 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002119 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002120
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002121 memset(&pipelineCache, 0, sizeof(pipelineCache));
2122 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002123
Karl Schultze4dc75c2016-02-02 15:37:51 -07002124 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
2125 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002126 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002127
Karl Schultze4dc75c2016-02-02 15:37:51 -07002128 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002129 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002130 pipeline.pRasterizationState = &rs;
2131 pipeline.pColorBlendState = &cb;
2132 pipeline.pMultisampleState = &ms;
2133 pipeline.pViewportState = &vp;
2134 pipeline.pDepthStencilState = &ds;
2135 pipeline.pStages = shaderStages;
2136 pipeline.renderPass = demo->render_pass;
2137 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002138
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002139 pipeline.renderPass = demo->render_pass;
2140
Karl Schultze4dc75c2016-02-02 15:37:51 -07002141 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
2142 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002143 assert(!err);
2144
Chia-I Wu63636062015-10-26 21:10:41 +08002145 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2146 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002147}
2148
Karl Schultze4dc75c2016-02-02 15:37:51 -07002149static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002150 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002151 [0] =
2152 {
2153 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07002154 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002155 },
2156 [1] =
2157 {
2158 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07002159 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002160 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002161 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002162 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002163 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002164 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002165 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002166 .poolSizeCount = 2,
2167 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002168 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002169 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002170
Karl Schultze4dc75c2016-02-02 15:37:51 -07002171 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
2172 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002173 assert(!err);
2174}
2175
Karl Schultze4dc75c2016-02-02 15:37:51 -07002176static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002177 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002178 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002179 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002180
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002181 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002182 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06002183 .pNext = NULL,
2184 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002185 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002186 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002187
2188 VkDescriptorBufferInfo buffer_info;
2189 buffer_info.offset = 0;
2190 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002191
Chia-I Wuae721ba2015-05-25 16:27:55 +08002192 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002193 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002194 tex_descs[i].sampler = demo->textures[i].sampler;
2195 tex_descs[i].imageView = demo->textures[i].view;
2196 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002197 }
2198
2199 memset(&writes, 0, sizeof(writes));
2200
2201 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002202 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002203 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002204 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002205
2206 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002207 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002208 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002209 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002210 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002211
Tony Barboura72d9492017-01-11 13:35:09 -07002212 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2213 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2214 assert(!err);
2215 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2216 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2217 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2218 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2219 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002220}
2221
Karl Schultze4dc75c2016-02-02 15:37:51 -07002222static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002223 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002224 attachments[1] = demo->depth.view;
2225
Chia-I Wuf973e322015-07-08 13:34:24 +08002226 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002227 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2228 .pNext = NULL,
2229 .renderPass = demo->render_pass,
2230 .attachmentCount = 2,
2231 .pAttachments = attachments,
2232 .width = demo->width,
2233 .height = demo->height,
2234 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002235 };
2236 VkResult U_ASSERT_ONLY err;
2237 uint32_t i;
2238
Tony Barbourd8e504f2015-10-08 14:26:24 -06002239 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002240 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002241 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002242 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002243 assert(!err);
2244 }
2245}
2246
Karl Schultze4dc75c2016-02-02 15:37:51 -07002247static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002248 VkResult U_ASSERT_ONLY err;
2249
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002250 const VkCommandPoolCreateInfo cmd_pool_info = {
2251 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06002252 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002253 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06002254 .flags = 0,
2255 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07002256 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
2257 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06002258 assert(!err);
2259
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002260 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002261 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002262 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002263 .commandPool = demo->cmd_pool,
2264 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002265 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002266 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002267 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2268 assert(!err);
2269 VkCommandBufferBeginInfo cmd_buf_info = {
2270 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2271 .pNext = NULL,
2272 .flags = 0,
2273 .pInheritanceInfo = NULL,
2274 };
2275 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2276 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002277
2278 demo_prepare_buffers(demo);
2279 demo_prepare_depth(demo);
2280 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002281 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002282
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002283 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002284 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002285 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002286
Ian Elliotta0781972015-08-21 15:09:33 -06002287 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002288 err =
Tony Barboura72d9492017-01-11 13:35:09 -07002289 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002290 assert(!err);
2291 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002292
Tony Barbour22e06272016-09-07 16:07:56 -06002293 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002294 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002295 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2296 .pNext = NULL,
2297 .queueFamilyIndex = demo->present_queue_family_index,
2298 .flags = 0,
2299 };
Karl Schultza2615462017-01-20 13:19:20 -07002300 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06002301 &demo->present_cmd_pool);
2302 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002303 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002304 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2305 .pNext = NULL,
2306 .commandPool = demo->present_cmd_pool,
2307 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2308 .commandBufferCount = 1,
2309 };
2310 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2311 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07002312 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002313 assert(!err);
2314 demo_build_image_ownership_cmd(demo, i);
2315 }
2316 }
2317
Chia-I Wu63ea9262015-03-26 13:14:16 +08002318 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002319 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002320
Chia-I Wuf973e322015-07-08 13:34:24 +08002321 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002322
Ian Elliotta0781972015-08-21 15:09:33 -06002323 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002324 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002325 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002326 }
2327
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002328 /*
2329 * Prepare functions above may generate pipeline commands
2330 * that need to be flushed before beginning the render loop.
2331 */
2332 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002333 if (demo->staging_texture.image) {
2334 demo_destroy_texture_image(demo, &demo->staging_texture);
2335 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002336
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002337 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002338 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002339}
2340
Karl Schultze4dc75c2016-02-02 15:37:51 -07002341static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002342 uint32_t i;
2343
2344 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002345 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002346
Tony Barbour66a56c32016-09-21 13:10:56 -06002347 // Wait for fences from present operations
2348 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002349 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002350 vkDestroyFence(demo->device, demo->fences[i], NULL);
2351 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2352 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2353 if (demo->separate_present_queue) {
2354 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2355 }
2356 }
2357
Tony Barbourd8e504f2015-10-08 14:26:24 -06002358 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002359 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002360 }
Chia-I Wu63636062015-10-26 21:10:41 +08002361 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002362
Chia-I Wu63636062015-10-26 21:10:41 +08002363 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2364 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2365 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2366 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2367 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002368
2369 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002370 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2371 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2372 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2373 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002374 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002375 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002376
Chia-I Wu63636062015-10-26 21:10:41 +08002377 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2378 vkDestroyImage(demo->device, demo->depth.image, NULL);
2379 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002380
Ian Elliotta0781972015-08-21 15:09:33 -06002381 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002382 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002383 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002384 &demo->swapchain_image_resources[i].cmd);
2385 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2386 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002387 }
Tony Barboura72d9492017-01-11 13:35:09 -07002388 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002389 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002390 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002391
Tony Barbour22e06272016-09-07 16:07:56 -06002392 if (demo->separate_present_queue) {
2393 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002394 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002395 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002396 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002397 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002398 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002399 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002400 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002401 vkDestroyInstance(demo->inst, 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)
2411 wl_shell_surface_destroy(demo->shell_surface);
2412 wl_surface_destroy(demo->window);
2413 wl_shell_destroy(demo->shell);
2414 wl_compositor_destroy(demo->compositor);
2415 wl_registry_destroy(demo->registry);
2416 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002417#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002418#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002419}
2420
Karl Schultze4dc75c2016-02-02 15:37:51 -07002421static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002422 uint32_t i;
2423
Mike Stroyanbb60b382015-10-28 09:46:57 -06002424 // Don't react to resize until after first initialization.
2425 if (!demo->prepared) {
2426 return;
2427 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002428 // In order to properly resize the window, we must re-create the swapchain
2429 // AND redo the command buffers, etc.
2430 //
2431 // First, perform part of the demo_cleanup() function:
2432 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002433 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002434
2435 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002436 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002437 }
Chia-I Wu63636062015-10-26 21:10:41 +08002438 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002439
Chia-I Wu63636062015-10-26 21:10:41 +08002440 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2441 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2442 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2443 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2444 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002445
2446 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002447 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2448 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2449 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2450 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002451 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002452
Chia-I Wu63636062015-10-26 21:10:41 +08002453 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2454 vkDestroyImage(demo->device, demo->depth.image, NULL);
2455 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002456
Ian Elliottcf074d32015-10-16 18:02:43 -06002457 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002458 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002459 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002460 &demo->swapchain_image_resources[i].cmd);
2461 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2462 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002463 }
Chia-I Wu63636062015-10-26 21:10:41 +08002464 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002465 if (demo->separate_present_queue) {
2466 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2467 }
Tony Barboura72d9492017-01-11 13:35:09 -07002468 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002469
Ian Elliottcf074d32015-10-16 18:02:43 -06002470 // Second, re-perform the demo_prepare() function, which will re-create the
2471 // swapchain:
2472 demo_prepare(demo);
2473}
2474
David Pinedo2bb7c932015-06-18 17:03:14 -06002475// On MS-Windows, make this a global, so it's available to WndProc()
2476struct demo demo;
2477
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002478#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002479static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002480 if (!demo->prepared)
2481 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002482
Ian Elliott639ca472015-04-16 15:23:05 -06002483 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002484 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002485 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002486 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002487 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002488}
Ian Elliott639ca472015-04-16 15:23:05 -06002489
2490// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002491LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2492 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002493 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002494 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002495 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002496 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002497 // The validation callback calls MessageBox which can generate paint
2498 // events - don't make more Vulkan calls if we got here from the
2499 // callback
2500 if (!in_callback) {
2501 demo_run(&demo);
2502 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002503 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002504 case WM_GETMINMAXINFO: // set window's minimum size
2505 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2506 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002507 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002508 // Resize the application to the new window size, except when
2509 // it was minimized. Vulkan doesn't support images or swapchains
2510 // with width=0 and height=0.
2511 if (wParam != SIZE_MINIMIZED) {
2512 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002513 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002514 demo_resize(&demo);
2515 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002516 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002517 default:
2518 break;
2519 }
2520 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2521}
2522
Karl Schultze4dc75c2016-02-02 15:37:51 -07002523static void demo_create_window(struct demo *demo) {
2524 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002525
2526 // Initialize the window class structure:
2527 win_class.cbSize = sizeof(WNDCLASSEX);
2528 win_class.style = CS_HREDRAW | CS_VREDRAW;
2529 win_class.lpfnWndProc = WndProc;
2530 win_class.cbClsExtra = 0;
2531 win_class.cbWndExtra = 0;
2532 win_class.hInstance = demo->connection; // hInstance
2533 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2534 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2535 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2536 win_class.lpszMenuName = NULL;
2537 win_class.lpszClassName = demo->name;
2538 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2539 // Register window class:
2540 if (!RegisterClassEx(&win_class)) {
2541 // It didn't work, so try to give a useful error:
2542 printf("Unexpected error trying to start the application!\n");
2543 fflush(stdout);
2544 exit(1);
2545 }
2546 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002547 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002548 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002549 demo->window = CreateWindowEx(0,
2550 demo->name, // class name
2551 demo->name, // app name
2552 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002553 WS_VISIBLE | WS_SYSMENU,
2554 100, 100, // x/y coords
2555 wr.right - wr.left, // width
2556 wr.bottom - wr.top, // height
2557 NULL, // handle to parent
2558 NULL, // handle to menu
2559 demo->connection, // hInstance
2560 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002561 if (!demo->window) {
2562 // It didn't work, so try to give a useful error:
2563 printf("Cannot create a window in which to draw!\n");
2564 fflush(stdout);
2565 exit(1);
2566 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002567 // Window client area size must be at least 1 pixel high, to prevent crash.
2568 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2569 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002570}
Tony Barbour8295ac42016-08-08 11:04:17 -06002571#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002572static void demo_create_xlib_window(struct demo *demo) {
2573
Tony Barbouree9cd372017-01-31 16:09:58 -07002574 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002575 demo->display = XOpenDisplay(NULL);
2576 long visualMask = VisualScreenMask;
2577 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002578 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002579 vInfoTemplate.screen = DefaultScreen(demo->display);
2580 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2581 &vInfoTemplate, &numberOfVisuals);
2582
2583 Colormap colormap = XCreateColormap(
2584 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2585 visualInfo->visual, AllocNone);
2586
Rene Lindsay11612722016-06-13 17:20:39 -06002587 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002588 windowAttributes.colormap = colormap;
2589 windowAttributes.background_pixel = 0xFFFFFFFF;
2590 windowAttributes.border_pixel = 0;
2591 windowAttributes.event_mask =
2592 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2593
2594 demo->xlib_window = XCreateWindow(
2595 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2596 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2597 visualInfo->visual,
2598 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2599
2600 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2601 XMapWindow(demo->display, demo->xlib_window);
2602 XFlush(demo->display);
2603 demo->xlib_wm_delete_window =
2604 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2605}
2606static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2607 switch(event->type) {
2608 case ClientMessage:
2609 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2610 demo->quit = true;
2611 break;
2612 case KeyPress:
2613 switch (event->xkey.keycode) {
2614 case 0x9: // Escape
2615 demo->quit = true;
2616 break;
2617 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002618 demo->spin_angle -= demo->spin_increment;
2619 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002620 case 0x72: // right arrow key
2621 demo->spin_angle += demo->spin_increment;
2622 break;
2623 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002624 demo->pause = !demo->pause;
2625 break;
2626 }
2627 break;
2628 case ConfigureNotify:
2629 if ((demo->width != event->xconfigure.width) ||
2630 (demo->height != event->xconfigure.height)) {
2631 demo->width = event->xconfigure.width;
2632 demo->height = event->xconfigure.height;
2633 demo_resize(demo);
2634 }
2635 break;
2636 default:
2637 break;
2638 }
2639
2640}
2641
2642static void demo_run_xlib(struct demo *demo) {
2643
2644 while (!demo->quit) {
2645 XEvent event;
2646
2647 if (demo->pause) {
2648 XNextEvent(demo->display, &event);
2649 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002650 }
2651 while (XPending(demo->display) > 0) {
2652 XNextEvent(demo->display, &event);
2653 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002654 }
2655
Tony Barbour2593b182016-04-19 10:57:58 -06002656 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002657 demo->curFrame++;
2658 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2659 demo->quit = true;
2660 }
2661}
Tony Barbour153cb062016-12-07 13:43:36 -07002662#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002663static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002664 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002665 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002666 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002667 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002668 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002669 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002670 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002671 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2672 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002673 demo->quit = true;
2674 }
2675 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002676 case XCB_KEY_RELEASE: {
2677 const xcb_key_release_event_t *key =
2678 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002679
Karl Schultze4dc75c2016-02-02 15:37:51 -07002680 switch (key->detail) {
2681 case 0x9: // Escape
2682 demo->quit = true;
2683 break;
2684 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002685 demo->spin_angle -= demo->spin_increment;
2686 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002687 case 0x72: // right arrow key
2688 demo->spin_angle += demo->spin_increment;
2689 break;
2690 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002691 demo->pause = !demo->pause;
2692 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002693 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002694 } break;
2695 case XCB_CONFIGURE_NOTIFY: {
2696 const xcb_configure_notify_event_t *cfg =
2697 (const xcb_configure_notify_event_t *)event;
2698 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002699 demo->width = cfg->width;
2700 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002701 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002702 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002703 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002704 default:
2705 break;
2706 }
2707}
2708
Tony Barbour2593b182016-04-19 10:57:58 -06002709static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002710 xcb_flush(demo->connection);
2711
2712 while (!demo->quit) {
2713 xcb_generic_event_t *event;
2714
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002715 if (demo->pause) {
2716 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002717 }
2718 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002719 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002720 }
2721 while (event) {
2722 demo_handle_xcb_event(demo, event);
2723 free(event);
2724 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002725 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002726
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002727 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002728 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002729 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002730 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002731 }
2732}
2733
Tony Barbour2593b182016-04-19 10:57:58 -06002734static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002735 uint32_t value_mask, value_list[32];
2736
Tony Barbour2593b182016-04-19 10:57:58 -06002737 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002738
2739 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2740 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002741 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002742 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002743
Tony Barbour2593b182016-04-19 10:57:58 -06002744 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002745 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2746 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2747 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002748
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002749 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002750 xcb_intern_atom_cookie_t cookie =
2751 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2752 xcb_intern_atom_reply_t *reply =
2753 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002754
Karl Schultze4dc75c2016-02-02 15:37:51 -07002755 xcb_intern_atom_cookie_t cookie2 =
2756 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2757 demo->atom_wm_delete_window =
2758 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002759
Tony Barbour2593b182016-04-19 10:57:58 -06002760 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002761 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002762 &(*demo->atom_wm_delete_window).atom);
2763 free(reply);
2764
Tony Barbour2593b182016-04-19 10:57:58 -06002765 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002766
Karl Schultze4dc75c2016-02-02 15:37:51 -07002767 // Force the x/y coordinates to 100,100 results are identical in consecutive
2768 // runs
2769 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002770 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002771 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002772}
Tony Barbour6b131662016-08-25 13:14:45 -06002773// VK_USE_PLATFORM_XCB_KHR
2774#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002775static void demo_run(struct demo *demo) {
2776 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002777 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002778 demo->curFrame++;
2779 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2780 demo->quit = true;
2781 }
2782}
2783
2784static void handle_ping(void *data UNUSED,
2785 struct wl_shell_surface *shell_surface,
2786 uint32_t serial) {
2787 wl_shell_surface_pong(shell_surface, serial);
2788}
2789
2790static void handle_configure(void *data UNUSED,
2791 struct wl_shell_surface *shell_surface UNUSED,
2792 uint32_t edges UNUSED, int32_t width UNUSED,
2793 int32_t height UNUSED) {}
2794
2795static void handle_popup_done(void *data UNUSED,
2796 struct wl_shell_surface *shell_surface UNUSED) {}
2797
2798static const struct wl_shell_surface_listener shell_surface_listener = {
2799 handle_ping, handle_configure, handle_popup_done};
2800
2801static void demo_create_window(struct demo *demo) {
2802 demo->window = wl_compositor_create_surface(demo->compositor);
2803 if (!demo->window) {
2804 printf("Can not create wayland_surface from compositor!\n");
2805 fflush(stdout);
2806 exit(1);
2807 }
2808
2809 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2810 if (!demo->shell_surface) {
2811 printf("Can not get shell_surface from wayland_surface!\n");
2812 fflush(stdout);
2813 exit(1);
2814 }
2815 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2816 demo);
2817 wl_shell_surface_set_toplevel(demo->shell_surface);
2818 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2819}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002820#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2821static void demo_run(struct demo *demo) {
2822 if (!demo->prepared)
2823 return;
2824
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002825 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002826 demo->curFrame++;
2827}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002828#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002829#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2830static VkResult demo_create_display_surface(struct demo *demo) {
2831 VkResult U_ASSERT_ONLY err;
2832 uint32_t display_count;
2833 uint32_t mode_count;
2834 uint32_t plane_count;
2835 VkDisplayPropertiesKHR display_props;
2836 VkDisplayKHR display;
2837 VkDisplayModePropertiesKHR mode_props;
2838 VkDisplayPlanePropertiesKHR *plane_props;
2839 VkBool32 found_plane = VK_FALSE;
2840 uint32_t plane_index;
2841 VkExtent2D image_extent;
2842 VkDisplaySurfaceCreateInfoKHR create_info;
2843
2844 // Get the first display
2845 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2846 assert(!err);
2847
2848 if (display_count == 0) {
2849 printf("Cannot find any display!\n");
2850 fflush(stdout);
2851 exit(1);
2852 }
2853
2854 display_count = 1;
2855 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2856 assert(!err || (err == VK_INCOMPLETE));
2857
2858 display = display_props.display;
2859
2860 // Get the first mode of the display
2861 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2862 assert(!err);
2863
2864 if (mode_count == 0) {
2865 printf("Cannot find any mode for the display!\n");
2866 fflush(stdout);
2867 exit(1);
2868 }
2869
2870 mode_count = 1;
2871 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2872 assert(!err || (err == VK_INCOMPLETE));
2873
2874 // Get the list of planes
2875 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2876 assert(!err);
2877
2878 if (plane_count == 0) {
2879 printf("Cannot find any plane!\n");
2880 fflush(stdout);
2881 exit(1);
2882 }
2883
2884 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2885 assert(plane_props);
2886
2887 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2888 assert(!err);
2889
2890 // Find a plane compatible with the display
2891 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2892 uint32_t supported_count;
2893 VkDisplayKHR *supported_displays;
2894
2895 // Disqualify planes that are bound to a different display
2896 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2897 (plane_props[plane_index].currentDisplay != display)) {
2898 continue;
2899 }
2900
2901 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2902 assert(!err);
2903
2904 if (supported_count == 0) {
2905 continue;
2906 }
2907
2908 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2909 assert(supported_displays);
2910
2911 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2912 assert(!err);
2913
2914 for (uint32_t i = 0; i < supported_count; i++) {
2915 if (supported_displays[i] == display) {
2916 found_plane = VK_TRUE;
2917 break;
2918 }
2919 }
2920
2921 free(supported_displays);
2922
2923 if (found_plane) {
2924 break;
2925 }
2926 }
2927
2928 if (!found_plane) {
2929 printf("Cannot find a plane compatible with the display!\n");
2930 fflush(stdout);
2931 exit(1);
2932 }
2933
2934 free(plane_props);
2935
Tony Barbour820b55b2017-03-14 08:55:46 -06002936 VkDisplayPlaneCapabilitiesKHR planeCaps;
2937 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2938 // Find a supported alpha mode
2939 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2940 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
2941 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2942 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2943 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2944 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2945 };
2946 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2947 if (planeCaps.supportedAlpha & alphaModes[i]) {
2948 alphaMode = alphaModes[i];
2949 break;
2950 }
2951 }
Damien Leone600c3052017-01-31 10:26:07 -07002952 image_extent.width = mode_props.parameters.visibleRegion.width;
2953 image_extent.height = mode_props.parameters.visibleRegion.height;
2954
2955 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2956 create_info.pNext = NULL;
2957 create_info.flags = 0;
2958 create_info.displayMode = mode_props.displayMode;
2959 create_info.planeIndex = plane_index;
2960 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2961 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002962 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002963 create_info.globalAlpha = 1.0f;
2964 create_info.imageExtent = image_extent;
2965
2966 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2967}
2968
2969static void demo_run_display(struct demo *demo)
2970{
2971 while (!demo->quit) {
2972 demo_draw(demo);
2973 demo->curFrame++;
2974
2975 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2976 demo->quit = true;
2977 }
2978 }
2979}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002980#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002981
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002982/*
2983 * Return 1 (true) if all layer names specified in check_names
2984 * can be found in given layer properties.
2985 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002986static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002987 uint32_t layer_count,
2988 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002989 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002990 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002991 for (uint32_t j = 0; j < layer_count; j++) {
2992 if (!strcmp(check_names[i], layers[j].layerName)) {
2993 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002994 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002995 }
2996 }
2997 if (!found) {
2998 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2999 return 0;
3000 }
3001 }
3002 return 1;
3003}
3004
Karl Schultze4dc75c2016-02-02 15:37:51 -07003005static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003006 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003007 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003008 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06003009 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003010 char **instance_validation_layers = NULL;
3011 demo->enabled_extension_count = 0;
3012 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003013
Karl Schultz7c50ad62016-04-01 12:07:03 -06003014 char *instance_validation_layers_alt1[] = {
3015 "VK_LAYER_LUNARG_standard_validation"
3016 };
3017
3018 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07003019 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
3020 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
3021 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003022 };
3023
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003024 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003025 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003026 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003027
Karl Schultz7c50ad62016-04-01 12:07:03 -06003028 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07003029 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003030
Karl Schultz7c50ad62016-04-01 12:07:03 -06003031 instance_validation_layers = instance_validation_layers_alt1;
3032 if (instance_layer_count > 0) {
3033 VkLayerProperties *instance_layers =
3034 malloc(sizeof (VkLayerProperties) * instance_layer_count);
3035 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
3036 instance_layers);
3037 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003038
Karl Schultz7c50ad62016-04-01 12:07:03 -06003039
3040 validation_found = demo_check_layers(
3041 ARRAY_SIZE(instance_validation_layers_alt1),
3042 instance_validation_layers, instance_layer_count,
3043 instance_layers);
3044 if (validation_found) {
3045 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003046 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
3047 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003048 } else {
3049 // use alternative set of validation layers
3050 instance_validation_layers = instance_validation_layers_alt2;
3051 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
3052 validation_found = demo_check_layers(
3053 ARRAY_SIZE(instance_validation_layers_alt2),
3054 instance_validation_layers, instance_layer_count,
3055 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003056 validation_layer_count =
3057 ARRAY_SIZE(instance_validation_layers_alt2);
3058 for (uint32_t i = 0; i < validation_layer_count; i++) {
3059 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06003060 }
3061 }
3062 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003063 }
Dustin Graves7c287352016-01-27 13:48:06 -07003064
Karl Schultz7c50ad62016-04-01 12:07:03 -06003065 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06003066 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06003067 "required validation layer.\n\n"
3068 "Please look at the Getting Started guide for additional "
3069 "information.\n",
3070 "vkCreateInstance Failure");
3071 }
Dustin Graves7c287352016-01-27 13:48:06 -07003072 }
3073
3074 /* Look for instance extensions */
3075 VkBool32 surfaceExtFound = 0;
3076 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003077 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003078
Karl Schultze4dc75c2016-02-02 15:37:51 -07003079 err = vkEnumerateInstanceExtensionProperties(
3080 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003081 assert(!err);
3082
Dustin Graves7c287352016-01-27 13:48:06 -07003083 if (instance_extension_count > 0) {
3084 VkExtensionProperties *instance_extensions =
3085 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3086 err = vkEnumerateInstanceExtensionProperties(
3087 NULL, &instance_extension_count, instance_extensions);
3088 assert(!err);
3089 for (uint32_t i = 0; i < instance_extension_count; i++) {
3090 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
3091 instance_extensions[i].extensionName)) {
3092 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003093 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003094 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003095 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003096#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003097 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
3098 instance_extensions[i].extensionName)) {
3099 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003100 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003101 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
3102 }
Tony Barbour153cb062016-12-07 13:43:36 -07003103#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003104 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
3105 instance_extensions[i].extensionName)) {
3106 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06003107 demo->extension_names[demo->enabled_extension_count++] =
3108 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
3109 }
Tony Barbour153cb062016-12-07 13:43:36 -07003110#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003111 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME,
3112 instance_extensions[i].extensionName)) {
3113 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003114 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003115 VK_KHR_XCB_SURFACE_EXTENSION_NAME;
3116 }
Tony Barbour153cb062016-12-07 13:43:36 -07003117#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003118 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
3119 instance_extensions[i].extensionName)) {
3120 platformSurfaceExtFound = 1;
3121 demo->extension_names[demo->enabled_extension_count++] =
3122 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
3123 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003124#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003125#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3126 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
3127 instance_extensions[i].extensionName)) {
3128 platformSurfaceExtFound = 1;
3129 demo->extension_names[demo->enabled_extension_count++] =
3130 VK_KHR_DISPLAY_EXTENSION_NAME;
3131 }
Tony Barbour153cb062016-12-07 13:43:36 -07003132#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003133 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
3134 instance_extensions[i].extensionName)) {
3135 platformSurfaceExtFound = 1;
3136 demo->extension_names[demo->enabled_extension_count++] =
3137 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
3138 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003139#elif defined(VK_USE_PLATFORM_IOS_MVK)
3140 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3141 platformSurfaceExtFound = 1;
3142 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3143 }
3144#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3145 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3146 platformSurfaceExtFound = 1;
3147 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3148 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003149#endif
Dustin Graves7c287352016-01-27 13:48:06 -07003150 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
3151 instance_extensions[i].extensionName)) {
3152 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003153 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003154 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
3155 }
3156 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003157 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003158 }
Dustin Graves7c287352016-01-27 13:48:06 -07003159
3160 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003161 }
Dustin Graves7c287352016-01-27 13:48:06 -07003162
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003163 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003164 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3165 "the " VK_KHR_SURFACE_EXTENSION_NAME
3166 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06003167 "Vulkan installable client driver (ICD) installed?\nPlease "
3168 "look at the Getting Started guide for additional "
3169 "information.\n",
3170 "vkCreateInstance Failure");
3171 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003172 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003173#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003174 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3175 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3176 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003177 "Vulkan installable client driver (ICD) installed?\nPlease "
3178 "look at the Getting Started guide for additional "
3179 "information.\n",
3180 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003181#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003182 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3183 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3184 "Vulkan installable client driver (ICD) installed?\nPlease "
3185 "look at the Getting Started guide for additional "
3186 "information.\n",
3187 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003188#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003189 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3190 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3191 "Vulkan installable client driver (ICD) installed?\nPlease "
3192 "look at the Getting Started guide for additional "
3193 "information.\n",
3194 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003195#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003196 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3197 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3198 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003199 "Vulkan installable client driver (ICD) installed?\nPlease "
3200 "look at the Getting Started guide for additional "
3201 "information.\n",
3202 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003203#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3204 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3205 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3206 " extension.\n\nDo you have a compatible "
3207 "Vulkan installable client driver (ICD) installed?\nPlease "
3208 "look at the Getting Started guide for additional "
3209 "information.\n",
3210 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003211#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003212#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3213 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3214 "the " VK_KHR_DISPLAY_EXTENSION_NAME
3215 " extension.\n\nDo you have a compatible "
3216 "Vulkan installable client driver (ICD) installed?\nPlease "
3217 "look at the Getting Started guide for additional "
3218 "information.\n",
3219 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003220#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3221 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3222 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3223 " extension.\n\nDo you have a compatible "
3224 "Vulkan installable client driver (ICD) installed?\nPlease "
3225 "look at the Getting Started guide for additional "
3226 "information.\n",
3227 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003228#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003229 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3230 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3231 " extension.\n\nDo you have a compatible "
3232 "Vulkan installable client driver (ICD) installed?\nPlease "
3233 "look at the Getting Started guide for additional "
3234 "information.\n",
3235 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003236#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003237 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003238 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003239 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003240 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003241 .pApplicationName = APP_SHORT_NAME,
3242 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003243 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003244 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003245 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003246 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003247 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003248 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003249 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003250 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003251 .enabledLayerCount = demo->enabled_layer_count,
3252 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3253 .enabledExtensionCount = demo->enabled_extension_count,
3254 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003255 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003256
3257 /*
3258 * This is info for a temp callback to use during CreateInstance.
3259 * After the instance is created, we use the instance-based
3260 * function to register the final callback.
3261 */
Karl Schultza2615462017-01-20 13:19:20 -07003262 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003263 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003264 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07003265 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
3266 dbgCreateInfoTemp.pNext = NULL;
3267 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
3268 dbgCreateInfoTemp.pUserData = demo;
3269 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06003270 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003271 if (demo->validate_checks_disabled) {
3272 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
3273 val_flags.pNext = NULL;
3274 val_flags.disabledValidationCheckCount = 1;
3275 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
3276 val_flags.pDisabledValidationChecks = &disabled_check;
3277 dbgCreateInfoTemp.pNext = (void*)&val_flags;
3278 }
Karl Schultza2615462017-01-20 13:19:20 -07003279 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003280 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003281
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003282 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003283
Chia-I Wu63636062015-10-26 21:10:41 +08003284 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003285 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
3286 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06003287 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06003288 "additional information.\n",
3289 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003290 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003291 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07003292 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003293 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003294 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06003295 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
3296 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06003297 "the Getting Started guide for additional information.\n",
3298 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003299 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003300
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003301 /* Make initial call to query gpu_count, then second call for gpu info*/
3302 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3303 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003304
3305 if (gpu_count > 0) {
3306 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3307 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3308 assert(!err);
3309 /* For cube demo we just grab the first physical device */
3310 demo->gpu = physical_devices[0];
3311 free(physical_devices);
3312 } else {
3313 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3314 "Do you have a compatible Vulkan installable client driver (ICD) "
3315 "installed?\nPlease look at the Getting Started guide for "
3316 "additional information.\n",
3317 "vkEnumeratePhysicalDevices Failure");
3318 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003319
Dustin Graves7c287352016-01-27 13:48:06 -07003320 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003321 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003322 VkBool32 swapchainExtFound = 0;
3323 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003324 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003325
Karl Schultze4dc75c2016-02-02 15:37:51 -07003326 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
3327 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003328 assert(!err);
3329
Dustin Graves7c287352016-01-27 13:48:06 -07003330 if (device_extension_count > 0) {
3331 VkExtensionProperties *device_extensions =
3332 malloc(sizeof(VkExtensionProperties) * device_extension_count);
3333 err = vkEnumerateDeviceExtensionProperties(
3334 demo->gpu, NULL, &device_extension_count, device_extensions);
3335 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003336
Dustin Graves7c287352016-01-27 13:48:06 -07003337 for (uint32_t i = 0; i < device_extension_count; i++) {
3338 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3339 device_extensions[i].extensionName)) {
3340 swapchainExtFound = 1;
3341 demo->extension_names[demo->enabled_extension_count++] =
3342 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3343 }
3344 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003345 }
Dustin Graves7c287352016-01-27 13:48:06 -07003346
Ian Elliott53622a72017-03-28 11:06:33 -06003347 if (demo->VK_KHR_incremental_present_enabled) {
3348 // Even though the user "enabled" the extension via the command
3349 // line, we must make sure that it's enumerated for use with the
3350 // device. Therefore, disable it here, and re-enable it again if
3351 // enumerated.
3352 demo->VK_KHR_incremental_present_enabled = false;
3353 for (uint32_t i = 0; i < device_extension_count; i++) {
3354 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
3355 device_extensions[i].extensionName)) {
3356 demo->extension_names[demo->enabled_extension_count++] =
3357 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
3358 demo->VK_KHR_incremental_present_enabled = true;
3359 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3360 }
3361 assert(demo->enabled_extension_count < 64);
3362 }
3363 if (!demo->VK_KHR_incremental_present_enabled) {
3364 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3365 }
3366 }
3367
Ian Elliottd940ca22017-03-22 08:31:27 -06003368 if (demo->VK_GOOGLE_display_timing_enabled) {
3369 // Even though the user "enabled" the extension via the command
3370 // line, we must make sure that it's enumerated for use with the
3371 // device. Therefore, disable it here, and re-enable it again if
3372 // enumerated.
3373 demo->VK_GOOGLE_display_timing_enabled = false;
3374 for (uint32_t i = 0; i < device_extension_count; i++) {
3375 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
3376 device_extensions[i].extensionName)) {
3377 demo->extension_names[demo->enabled_extension_count++] =
3378 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
3379 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003380 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003381 }
3382 assert(demo->enabled_extension_count < 64);
3383 }
3384 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003385 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003386 }
3387 }
3388
Dustin Graves7c287352016-01-27 13:48:06 -07003389 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003390 }
Dustin Graves7c287352016-01-27 13:48:06 -07003391
Tony Barbourcc69f452015-10-08 13:59:42 -06003392 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003393 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3394 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3395 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003396 "Vulkan installable client driver (ICD) installed?\nPlease "
3397 "look at the Getting Started guide for additional "
3398 "information.\n",
3399 "vkCreateInstance Failure");
3400 }
3401
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003402 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003403 demo->CreateDebugReportCallback =
3404 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3405 demo->inst, "vkCreateDebugReportCallbackEXT");
3406 demo->DestroyDebugReportCallback =
3407 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3408 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003409 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003410 ERR_EXIT(
3411 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3412 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003413 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003414 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003415 ERR_EXIT(
3416 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3417 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003418 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003419 demo->DebugReportMessage =
3420 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3421 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003422 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003423 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003424 "vkGetProcAddr Failure");
3425 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003426
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003427 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003428 PFN_vkDebugReportCallbackEXT callback;
3429 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003430 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003431 dbgCreateInfo.pNext = NULL;
3432 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003433 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003434 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003435 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003436 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3437 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003438 switch (err) {
3439 case VK_SUCCESS:
3440 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003441 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003442 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3443 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003444 break;
3445 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003446 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3447 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003448 break;
3449 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003450 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003451 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003452
3453 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003454 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3455 &demo->queue_family_count, NULL);
3456 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003457
Karl Schultze4dc75c2016-02-02 15:37:51 -07003458 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003459 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3460 vkGetPhysicalDeviceQueueFamilyProperties(
3461 demo->gpu, &demo->queue_family_count, demo->queue_props);
3462
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003463 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003464 // If app has specific feature requirements it should check supported
3465 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003466 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003467 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003468
Tony Barbourda2bad52016-01-22 14:36:40 -07003469 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3470 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3471 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3472 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3473 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3474}
3475
Karl Schultze4dc75c2016-02-02 15:37:51 -07003476static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003477 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003478 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003479 VkDeviceQueueCreateInfo queues[2];
3480 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3481 queues[0].pNext = NULL;
3482 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3483 queues[0].queueCount = 1;
3484 queues[0].pQueuePriorities = queue_priorities;
3485 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003486
3487 VkDeviceCreateInfo device = {
3488 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3489 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003490 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003491 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003492 .enabledLayerCount = 0,
3493 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003494 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003495 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3496 .pEnabledFeatures =
3497 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003498 };
Tony Barbour22e06272016-09-07 16:07:56 -06003499 if (demo->separate_present_queue) {
3500 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3501 queues[1].pNext = NULL;
3502 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3503 queues[1].queueCount = 1;
3504 queues[1].pQueuePriorities = queue_priorities;
3505 queues[1].flags = 0;
3506 device.queueCreateInfoCount = 2;
3507 }
Chia-I Wu63636062015-10-26 21:10:41 +08003508 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003509 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003510}
3511
Karl Schultze4dc75c2016-02-02 15:37:51 -07003512static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003513 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003514
Karl Schultze4dc75c2016-02-02 15:37:51 -07003515// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003516#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003517 VkWin32SurfaceCreateInfoKHR createInfo;
3518 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3519 createInfo.pNext = NULL;
3520 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003521 createInfo.hinstance = demo->connection;
3522 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003523
Karl Schultze4dc75c2016-02-02 15:37:51 -07003524 err =
3525 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003526#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003527 VkWaylandSurfaceCreateInfoKHR createInfo;
3528 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3529 createInfo.pNext = NULL;
3530 createInfo.flags = 0;
3531 createInfo.display = demo->display;
3532 createInfo.surface = demo->window;
3533
3534 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3535 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003536#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003537#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3538 VkAndroidSurfaceCreateInfoKHR createInfo;
3539 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3540 createInfo.pNext = NULL;
3541 createInfo.flags = 0;
3542 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003543
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003544 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003545#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3546 VkXlibSurfaceCreateInfoKHR createInfo;
3547 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3548 createInfo.pNext = NULL;
3549 createInfo.flags = 0;
3550 createInfo.dpy = demo->display;
3551 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003552
Tony Barbour153cb062016-12-07 13:43:36 -07003553 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003554 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003555#elif defined(VK_USE_PLATFORM_XCB_KHR)
3556 VkXcbSurfaceCreateInfoKHR createInfo;
3557 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3558 createInfo.pNext = NULL;
3559 createInfo.flags = 0;
3560 createInfo.connection = demo->connection;
3561 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003562
Tony Barbour153cb062016-12-07 13:43:36 -07003563 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003564#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3565 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003566#elif defined(VK_USE_PLATFORM_IOS_MVK)
3567 VkIOSSurfaceCreateInfoMVK surface;
3568 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3569 surface.pNext = NULL;
3570 surface.flags = 0;
3571 surface.pView = demo->window;
3572
3573 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3574#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3575 VkMacOSSurfaceCreateInfoMVK surface;
3576 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3577 surface.pNext = NULL;
3578 surface.flags = 0;
3579 surface.pView = demo->window;
3580
3581 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003582#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003583 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003584
Tony Barbourcc69f452015-10-08 13:59:42 -06003585 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003586 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003587 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003588 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003589 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003590 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003591 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003592
3593 // Search for a graphics and a present queue in the array of queue
3594 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003595 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3596 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003597 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003598 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3599 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3600 graphicsQueueFamilyIndex = i;
3601 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003602
Tony Barbourab2a0362016-09-06 11:40:12 -06003603 if (supportsPresent[i] == VK_TRUE) {
3604 graphicsQueueFamilyIndex = i;
3605 presentQueueFamilyIndex = i;
3606 break;
3607 }
3608 }
3609 }
3610
3611 if (presentQueueFamilyIndex == UINT32_MAX) {
3612 // If didn't find a queue that supports both graphics and present, then
3613 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003614 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003615 if (supportsPresent[i] == VK_TRUE) {
3616 presentQueueFamilyIndex = i;
3617 break;
3618 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003619 }
3620 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003621
3622 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003623 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3624 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003625 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003626 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003627 }
3628
Tony Barbourab2a0362016-09-06 11:40:12 -06003629 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3630 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003631 demo->separate_present_queue =
3632 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003633 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003634
Tony Barbourda2bad52016-01-22 14:36:40 -07003635 demo_create_device(demo);
3636
3637 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3638 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3639 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3640 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3641 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003642 if (demo->VK_GOOGLE_display_timing_enabled) {
3643 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3644 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3645 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003646
Tony Barboura2a67812016-07-18 13:21:06 -06003647 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3648 &demo->graphics_queue);
3649
Tony Barbour22e06272016-09-07 16:07:56 -06003650 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003651 demo->present_queue = demo->graphics_queue;
3652 } else {
3653 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3654 &demo->present_queue);
3655 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003656
Ian Elliottaaae5352015-07-06 14:27:58 -06003657 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003658 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003659 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003660 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003661 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003662 VkSurfaceFormatKHR *surfFormats =
3663 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003664 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003665 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003666 assert(!err);
3667 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3668 // the surface has no preferred format. Otherwise, at least one
3669 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003670 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003671 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003672 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003673 assert(formatCount >= 1);
3674 demo->format = surfFormats[0].format;
3675 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003676 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003677
David Pinedo2bb7c932015-06-18 17:03:14 -06003678 demo->quit = false;
3679 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003680
Tony Barbource7524e2016-07-28 12:01:45 -06003681 // Create semaphores to synchronize acquiring presentable buffers before
3682 // rendering and waiting for drawing to be complete before presenting
3683 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3684 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3685 .pNext = NULL,
3686 .flags = 0,
3687 };
Tony Barbource7524e2016-07-28 12:01:45 -06003688
Tony Barbour66a56c32016-09-21 13:10:56 -06003689 // Create fences that we can use to throttle if we get too far
3690 // ahead of the image presents
3691 VkFenceCreateInfo fence_ci = {
3692 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3693 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003694 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003695 };
3696 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003697 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3698 assert(!err);
3699
Tony Barbour22e06272016-09-07 16:07:56 -06003700 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003701 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003702 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003703
3704 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3705 &demo->draw_complete_semaphores[i]);
3706 assert(!err);
3707
3708 if (demo->separate_present_queue) {
3709 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3710 &demo->image_ownership_semaphores[i]);
3711 assert(!err);
3712 }
Tony Barbour22e06272016-09-07 16:07:56 -06003713 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003714 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003715
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003716 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003717 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003718}
3719
Tony Barbour153cb062016-12-07 13:43:36 -07003720#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003721static void registry_handle_global(void *data, struct wl_registry *registry,
3722 uint32_t name, const char *interface,
3723 uint32_t version UNUSED) {
3724 struct demo *demo = data;
3725 if (strcmp(interface, "wl_compositor") == 0) {
3726 demo->compositor =
3727 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3728 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3729 * tp xdg_shell */
3730 } else if (strcmp(interface, "wl_shell") == 0) {
3731 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3732 }
3733}
3734
3735static void registry_handle_global_remove(void *data UNUSED,
3736 struct wl_registry *registry UNUSED,
3737 uint32_t name UNUSED) {}
3738
3739static const struct wl_registry_listener registry_listener = {
3740 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003741#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003742#endif
3743
Karl Schultze4dc75c2016-02-02 15:37:51 -07003744static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003745#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003746 const xcb_setup_t *setup;
3747 xcb_screen_iterator_t iter;
3748 int scr;
3749
3750 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003751 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003752 printf("Cannot find a compatible Vulkan installable client driver "
3753 "(ICD).\nExiting ...\n");
3754 fflush(stdout);
3755 exit(1);
3756 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003757
3758 setup = xcb_get_setup(demo->connection);
3759 iter = xcb_setup_roots_iterator(setup);
3760 while (scr-- > 0)
3761 xcb_screen_next(&iter);
3762
3763 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003764#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3765 demo->display = wl_display_connect(NULL);
3766
3767 if (demo->display == NULL) {
3768 printf("Cannot find a compatible Vulkan installable client driver "
3769 "(ICD).\nExiting ...\n");
3770 fflush(stdout);
3771 exit(1);
3772 }
3773
3774 demo->registry = wl_display_get_registry(demo->display);
3775 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3776 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003777#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003778#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003779}
3780
Karl Schultze4dc75c2016-02-02 15:37:51 -07003781static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003782 vec3 eye = {0.0f, 3.0f, 5.0f};
3783 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003784 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003785
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003786 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003787 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003788 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003789
Piers Daniell735ee532015-02-23 16:23:13 -07003790 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003791 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003792 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003793 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003794 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003795 if ((strcmp(argv[i], "--present_mode") == 0) &&
3796 (i < argc - 1)) {
3797 demo->presentMode = atoi(argv[i+1]);
3798 i++;
3799 continue;
3800 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003801 if (strcmp(argv[i], "--break") == 0) {
3802 demo->use_break = true;
3803 continue;
3804 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003805 if (strcmp(argv[i], "--validate") == 0) {
3806 demo->validate = true;
3807 continue;
3808 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003809 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3810 demo->validate = true;
3811 demo->validate_checks_disabled = true;
3812 continue;
3813 }
Tony Barbour2593b182016-04-19 10:57:58 -06003814 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003815 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003816 continue;
3817 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003818 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3819 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3820 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003821 i++;
3822 continue;
3823 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003824 if (strcmp(argv[i], "--suppress_popups") == 0) {
3825 demo->suppress_popups = true;
3826 continue;
3827 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003828 if (strcmp(argv[i], "--display_timing") == 0) {
3829 demo->VK_GOOGLE_display_timing_enabled = true;
3830 continue;
3831 }
Ian Elliott53622a72017-03-28 11:06:33 -06003832 if (strcmp(argv[i], "--incremental_present") == 0) {
3833 demo->VK_KHR_incremental_present_enabled = true;
3834 continue;
3835 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003836
Cody Northropaf28a532016-09-27 10:50:57 -06003837#if defined(ANDROID)
3838 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3839#else
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003840 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled] [--break] "
Ian Elliott53622a72017-03-28 11:06:33 -06003841 "[--c <framecount>] [--suppress_popups] [--incremental_present] [--display_timing] [--present_mode <present mode enum>]\n"
Tony Barbour291d57b2016-10-21 14:47:07 -06003842 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3843 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3844 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3845 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3846 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3847 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003848 fflush(stderr);
3849 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003850#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003851 }
3852
Tony Barbour153cb062016-12-07 13:43:36 -07003853 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003854
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003855 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003856
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003857 demo->width = 500;
3858 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003859
Tony Barbour66a56c32016-09-21 13:10:56 -06003860 demo->spin_angle = 4.0f;
3861 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003862 demo->pause = false;
3863
Karl Schultze4dc75c2016-02-02 15:37:51 -07003864 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3865 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003866 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3867 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003868
3869 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003870}
3871
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003872#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003873// Include header required for parsing the command line options.
3874#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003875
Karl Schultze4dc75c2016-02-02 15:37:51 -07003876int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3877 int nCmdShow) {
3878 MSG msg; // message
3879 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003880 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003881 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003882
Jamie Madillb2ff6502017-03-15 16:17:46 -04003883 // Ensure wParam is initialized.
3884 msg.wParam = 0;
3885
Karl Schultze4dc75c2016-02-02 15:37:51 -07003886 // Use the CommandLine functions to get the command line arguments.
3887 // Unfortunately, Microsoft outputs
3888 // this information as wide characters for Unicode, and we simply want the
3889 // Ascii version to be compatible
3890 // with the non-Windows side. So, we have to convert the information to
3891 // Ascii character strings.
3892 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3893 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003894 argc = 0;
3895 }
3896
Karl Schultze4dc75c2016-02-02 15:37:51 -07003897 if (argc > 0) {
3898 argv = (char **)malloc(sizeof(char *) * argc);
3899 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003900 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003901 } else {
3902 for (int iii = 0; iii < argc; iii++) {
3903 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003904 size_t numConverted = 0;
3905
Karl Schultze4dc75c2016-02-02 15:37:51 -07003906 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3907 if (argv[iii] != NULL) {
3908 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3909 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003910 }
3911 }
3912 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003913 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003914 argv = NULL;
3915 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003916
3917 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003918
3919 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003920 if (argc > 0 && argv != NULL) {
3921 for (int iii = 0; iii < argc; iii++) {
3922 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003923 free(argv[iii]);
3924 }
3925 }
3926 free(argv);
3927 }
3928
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003929 demo.connection = hInstance;
3930 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003931 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003932 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003933
3934 demo_prepare(&demo);
3935
Karl Schultze4dc75c2016-02-02 15:37:51 -07003936 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003937
3938 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003939 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003940 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003941 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003942 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003943 done = true; // if found, quit app
3944 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003945 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003946 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003947 DispatchMessage(&msg);
3948 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003949 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003950 }
3951
3952 demo_cleanup(&demo);
3953
Karl Schultze4dc75c2016-02-02 15:37:51 -07003954 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003955}
Bill Hollingsf4352142017-02-14 22:58:56 -05003956
3957#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
3958static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003959 const char* argv[] = { "CubeSample" };
3960 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05003961
Tony Barbourc65cd752017-03-13 15:29:35 -06003962 demo_init(demo, argc, (char**)argv);
3963 demo->window = view;
3964 demo_init_vk_swapchain(demo);
3965 demo_prepare(demo);
3966 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003967}
3968
3969static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003970 // Wait for work to finish before updating MVP.
3971 vkDeviceWaitIdle(demo->device);
3972 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003973
Tony Barbourc65cd752017-03-13 15:29:35 -06003974 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003975}
3976
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003977#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3978#include <android/log.h>
3979#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003980#include "android_util.h"
3981
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003982static bool initialized = false;
3983static bool active = false;
3984struct demo demo;
3985
3986static int32_t processInput(struct android_app* app, AInputEvent* event) {
3987 return 0;
3988}
3989
3990static void processCommand(struct android_app* app, int32_t cmd) {
3991 switch(cmd) {
3992 case APP_CMD_INIT_WINDOW: {
3993 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003994 // We're getting a new window. If the app is starting up, we
3995 // need to initialize. If the app has already been
3996 // initialized, that means that we lost our previous window,
3997 // which means that we have a lot of work to do. At a minimum,
3998 // we need to destroy the swapchain and surface associated with
3999 // the old window, and create a new surface and swapchain.
4000 // However, since there are a lot of other objects/state that
4001 // is tied to the swapchain, it's easiest to simply cleanup and
4002 // start over (i.e. use a brute-force approach of re-starting
4003 // the app)
4004 if (demo.prepared) {
4005 demo_cleanup(&demo);
4006 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004007
4008 // Parse Intents into argc, argv
4009 // Use the following key to send arguments, i.e.
4010 // --es args "--validate"
4011 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06004012 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06004013 int argc = 0;
4014 char** argv = get_args(app, key, appTag, &argc);
4015
4016 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
4017 for (int i = 0; i < argc; i++)
4018 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
4019
4020 demo_init(&demo, argc, argv);
4021
4022 // Free the argv malloc'd by get_args
4023 for (int i = 0; i < argc; i++)
4024 free(argv[i]);
4025
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004026 demo.window = (void*)app->window;
4027 demo_init_vk_swapchain(&demo);
4028 demo_prepare(&demo);
4029 initialized = true;
4030 }
4031 break;
4032 }
4033 case APP_CMD_GAINED_FOCUS: {
4034 active = true;
4035 break;
4036 }
4037 case APP_CMD_LOST_FOCUS: {
4038 active = false;
4039 break;
4040 }
4041 }
4042}
4043
4044void android_main(struct android_app *app)
4045{
4046 app_dummy();
4047
Cody Northrop8707a6e2016-04-26 19:59:19 -06004048#ifdef ANDROID
4049 int vulkanSupport = InitVulkan();
4050 if (vulkanSupport == 0)
4051 return;
4052#endif
4053
Ian Elliottf05d5d12016-05-17 12:03:35 -06004054 demo.prepared = false;
4055
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004056 app->onAppCmd = processCommand;
4057 app->onInputEvent = processInput;
4058
4059 while(1) {
4060 int events;
4061 struct android_poll_source* source;
4062 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
4063 if (source) {
4064 source->process(app, source);
4065 }
4066
4067 if (app->destroyRequested != 0) {
4068 demo_cleanup(&demo);
4069 return;
4070 }
4071 }
4072 if (initialized && active) {
4073 demo_run(&demo);
4074 }
4075 }
4076
4077}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004078#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004079int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004080 struct demo demo;
4081
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004082 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004083#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004084 demo_create_xcb_window(&demo);
4085#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4086 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004087#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4088 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004089#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004090#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004091
Tony Barbourcc69f452015-10-08 13:59:42 -06004092 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004093
4094 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004095
Tony Barbour153cb062016-12-07 13:43:36 -07004096#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004097 demo_run_xcb(&demo);
4098#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4099 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004100#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4101 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004102#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07004103#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4104 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004105#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004106
4107 demo_cleanup(&demo);
4108
Karl Schultz0218c002016-03-22 17:06:13 -06004109 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004110}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004111#endif