blob: 9b4980e7675768d4c4adc8f02b0945e5b6704992 [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;
312 VkFence fence;
313} SwapchainImageResources;
Ian Elliottaaae5352015-07-06 14:27:58 -0600314
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600315struct demo {
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500316#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliott639ca472015-04-16 15:23:05 -0600317#define APP_NAME_STR_LEN 80
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700318 HINSTANCE connection; // hInstance - Windows Instance
319 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
320 HWND window; // hWnd - window handle
321 POINT minsize; // minimum window size
Tony Barbour153cb062016-12-07 13:43:36 -0700322#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700323 Display *display;
Tony Barbour2593b182016-04-19 10:57:58 -0600324 Window xlib_window;
325 Atom xlib_wm_delete_window;
Tony Barbour153cb062016-12-07 13:43:36 -0700326#elif defined(VK_USE_PLATFORM_XCB_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700327 Display *display;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600328 xcb_connection_t *connection;
329 xcb_screen_t *screen;
Tony Barbour2593b182016-04-19 10:57:58 -0600330 xcb_window_t xcb_window;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800331 xcb_intern_atom_reply_t *atom_wm_delete_window;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +0900332#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
333 struct wl_display *display;
334 struct wl_registry *registry;
335 struct wl_compositor *compositor;
336 struct wl_surface *window;
337 struct wl_shell *shell;
338 struct wl_shell_surface *shell_surface;
Tony Barbourefd0c5a2016-12-07 14:45:12 -0700339#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500340#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700341 ANativeWindow *window;
Bill Hollingsf4352142017-02-14 22:58:56 -0500342#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
343 void *window;
Michael Lentinec6cde4b2016-04-18 13:20:45 -0500344#endif
Ian Elliottb08e0d92015-11-20 11:55:46 -0700345 VkSurfaceKHR surface;
Cody Northrop1fedb212015-05-28 11:27:16 -0600346 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700347 bool use_staging_buffer;
Tony Barbour22e06272016-09-07 16:07:56 -0600348 bool separate_present_queue;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349
Ian Elliottd940ca22017-03-22 08:31:27 -0600350 bool VK_GOOGLE_display_timing_enabled;
351 bool syncd_with_actual_presents;
352 uint64_t refresh_duration;
353 uint64_t refresh_duration_multiplier;
354 uint64_t target_IPD; // image present duration (inverse of frame rate)
355 uint64_t prev_desired_present_time;
356 uint32_t next_present_id;
357 uint32_t last_early_id; // 0 if no early images
358 uint32_t last_late_id; // 0 if no late images
359
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600361 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600362 VkDevice device;
Tony Barboura2a67812016-07-18 13:21:06 -0600363 VkQueue graphics_queue;
364 VkQueue present_queue;
365 uint32_t graphics_queue_family_index;
366 uint32_t present_queue_family_index;
Tony Barbour66a56c32016-09-21 13:10:56 -0600367 VkSemaphore image_acquired_semaphores[FRAME_LAG];
368 VkSemaphore draw_complete_semaphores[FRAME_LAG];
369 VkSemaphore image_ownership_semaphores[FRAME_LAG];
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600370 VkPhysicalDeviceProperties gpu_props;
Cody Northrop4d3c11d2015-08-03 17:04:53 -0600371 VkQueueFamilyProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600372 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600373
Tony Barbourda2bad52016-01-22 14:36:40 -0700374 uint32_t enabled_extension_count;
375 uint32_t enabled_layer_count;
376 char *extension_names[64];
Karl Schultz5b423ea2016-06-20 19:08:43 -0600377 char *enabled_layers[64];
Tony Barbourda2bad52016-01-22 14:36:40 -0700378
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 VkFormat format;
Ian Elliotta0781972015-08-21 15:09:33 -0600381 VkColorSpaceKHR color_space;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600382
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700383 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
384 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
385 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
386 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
Ian Elliotta0781972015-08-21 15:09:33 -0600387 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
388 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
389 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
390 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
391 PFN_vkQueuePresentKHR fpQueuePresentKHR;
Ian Elliottd940ca22017-03-22 08:31:27 -0600392 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
393 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
Ian Elliotta0781972015-08-21 15:09:33 -0600394 uint32_t swapchainImageCount;
Ian Elliottcf074d32015-10-16 18:02:43 -0600395 VkSwapchainKHR swapchain;
Tony Barboura72d9492017-01-11 13:35:09 -0700396 SwapchainImageResources *swapchain_image_resources;
Tony Barbour291d57b2016-10-21 14:47:07 -0600397 VkPresentModeKHR presentMode;
Tony Barbour66a56c32016-09-21 13:10:56 -0600398 VkFence fences[FRAME_LAG];
Tony Barbour66a56c32016-09-21 13:10:56 -0600399 int frame_index;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600400
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800401 VkCommandPool cmd_pool;
Tony Barbour22e06272016-09-07 16:07:56 -0600402 VkCommandPool present_cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600403
404 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600405 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600406
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 VkImage image;
Chia-I Wua8fe78a2015-10-27 18:04:07 +0800408 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500409 VkDeviceMemory mem;
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -0600410 VkImageView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600411 } depth;
412
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 struct texture_object textures[DEMO_TEXTURE_COUNT];
Tony Barbour16156cb2016-10-19 14:15:49 -0600414 struct texture_object staging_texture;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600415
Mark Lobodzinski85dbd822017-01-26 13:34:13 -0700416 VkCommandBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500417 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600418 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600419 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800420 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600421 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600422
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600423 mat4x4 projection_matrix;
424 mat4x4 view_matrix;
425 mat4x4 model_matrix;
426
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600427 float spin_angle;
428 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600429 bool pause;
430
Tony Barbourb6dba5c2015-07-21 09:04:41 -0600431 VkShaderModule vert_shader_module;
432 VkShaderModule frag_shader_module;
433
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600434 VkDescriptorPool desc_pool;
Chia-I Wuf973e322015-07-08 13:34:24 +0800435
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600436 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600437 int32_t curFrame;
438 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600439 bool validate;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -0600440 bool validate_checks_disabled;
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -0600441 bool use_break;
lenny-lunargfbe22392016-06-06 11:07:53 -0600442 bool suppress_popups;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -0700443 PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
444 PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
445 VkDebugReportCallbackEXT msg_callback;
446 PFN_vkDebugReportMessageEXT DebugReportMessage;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600447
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600448 uint32_t current_buffer;
Tony Barbourab2a0362016-09-06 11:40:12 -0600449 uint32_t queue_family_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600450};
451
Mark Lobodzinski2dbc2662017-01-26 12:16:30 -0700452VKAPI_ATTR VkBool32 VKAPI_CALL dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location,
453 int32_t msgCode, const char *pLayerPrefix, const char *pMsg, void *pUserData) {
Cody Northropaf28a532016-09-27 10:50:57 -0600454 // clang-format off
lenny-lunargfbe22392016-06-06 11:07:53 -0600455 char *message = (char *)malloc(strlen(pMsg) + 100);
456
457 assert(message);
458
Cody Northropaf28a532016-09-27 10:50:57 -0600459 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
460 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600461 validation_error = 1;
462 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600463 sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
464 validation_error = 1;
465 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Mike Weiblendb0b6642016-10-17 18:52:05 -0600466 sprintf(message, "PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Cody Northropaf28a532016-09-27 10:50:57 -0600467 validation_error = 1;
468 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
469 sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
470 validation_error = 1;
471 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
472 sprintf(message, "DEBUG: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600473 validation_error = 1;
474 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600475 sprintf(message, "INFORMATION: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
lenny-lunargfbe22392016-06-06 11:07:53 -0600476 validation_error = 1;
lenny-lunargfbe22392016-06-06 11:07:53 -0600477 }
478
479#ifdef _WIN32
Cody Northropaf28a532016-09-27 10:50:57 -0600480
Tony Barbour602ca4f2016-09-12 14:02:43 -0600481 in_callback = true;
lenny-lunargfbe22392016-06-06 11:07:53 -0600482 struct demo *demo = (struct demo*) pUserData;
483 if (!demo->suppress_popups)
484 MessageBox(NULL, message, "Alert", MB_OK);
Tony Barbour602ca4f2016-09-12 14:02:43 -0600485 in_callback = false;
Cody Northropaf28a532016-09-27 10:50:57 -0600486
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600487#elif defined(ANDROID)
Cody Northropaf28a532016-09-27 10:50:57 -0600488
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600489 if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600490 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600491 } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600492 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600493 } else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600494 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600495 } else if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600496 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600497 } else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
Cody Northropaf28a532016-09-27 10:50:57 -0600498 __android_log_print(ANDROID_LOG_DEBUG, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600499 } else {
Cody Northropaf28a532016-09-27 10:50:57 -0600500 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
Cody Northropc5e5a8c2016-09-26 21:05:00 -0600501 }
Cody Northropaf28a532016-09-27 10:50:57 -0600502
lenny-lunargfbe22392016-06-06 11:07:53 -0600503#else
Cody Northropaf28a532016-09-27 10:50:57 -0600504
lenny-lunargfbe22392016-06-06 11:07:53 -0600505 printf("%s\n", message);
506 fflush(stdout);
Cody Northropaf28a532016-09-27 10:50:57 -0600507
lenny-lunargfbe22392016-06-06 11:07:53 -0600508#endif
Cody Northropaf28a532016-09-27 10:50:57 -0600509
lenny-lunargfbe22392016-06-06 11:07:53 -0600510 free(message);
511
Cody Northropaf28a532016-09-27 10:50:57 -0600512 //clang-format on
513
lenny-lunargfbe22392016-06-06 11:07:53 -0600514 /*
515 * false indicates that layer should not bail-out of an
516 * API call that had validation failures. This may mean that the
517 * app dies inside the driver due to invalid parameter(s).
518 * That's what would happen without validation layers, so we'll
519 * keep that behavior here.
520 */
521 return false;
522}
523
Ian Elliottd940ca22017-03-22 08:31:27 -0600524bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
525 // The desired time was the earliest time that the present should have
526 // occured. In almost every case, the actual time should be later than the
527 // desired time. We should only consider the actual time "late" if it is
528 // after "desired + rdur".
529 if (actual <= desired) {
530 // The actual time was before or equal to the desired time. This will
531 // probably never happen, but in case it does, return false since the
532 // present was obviously NOT late.
533 return false;
534 }
535 uint64_t deadline = actual + rdur;
536 if (actual > deadline) {
537 return true;
538 } else {
539 return false;
540 }
541}
542bool CanPresentEarlier(uint64_t earliest,
543 uint64_t actual,
544 uint64_t margin,
545 uint64_t rdur) {
546 if (earliest < actual) {
547 // Consider whether this present could have occured earlier. Make sure
548 // that earliest time was at least 2msec earlier than actual time, and
549 // that the margin was at least 2msec:
550 uint64_t diff = actual - earliest;
551 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
552 // This present could have occured earlier because both: 1) the
553 // earliest time was at least 2 msec before actual time, and 2) the
554 // margin was at least 2msec.
555 return true;
556 }
557 }
558 return false;
559}
560
Ian Elliottcf074d32015-10-16 18:02:43 -0600561// Forward declaration:
562static void demo_resize(struct demo *demo);
563
Karl Schultze4dc75c2016-02-02 15:37:51 -0700564static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits,
565 VkFlags requirements_mask,
566 uint32_t *typeIndex) {
567 // Search memtypes to find first index with those properties
Alexandre BACQUART89eb8df2016-04-28 14:25:09 -0600568 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700569 if ((typeBits & 1) == 1) {
570 // Type is available, does it match user properties?
571 if ((demo->memory_properties.memoryTypes[i].propertyFlags &
572 requirements_mask) == requirements_mask) {
573 *typeIndex = i;
574 return true;
575 }
576 }
577 typeBits >>= 1;
578 }
579 // No memory types matched, return failure
580 return false;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600581}
582
Karl Schultze4dc75c2016-02-02 15:37:51 -0700583static void demo_flush_init_cmd(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600584 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585
Tony Barbource7524e2016-07-28 12:01:45 -0600586 // This function could get called twice if the texture uses a staging buffer
587 // In that case the second call should be ignored
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600588 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600589 return;
590
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600591 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600592 assert(!err);
593
Tony Barbource7524e2016-07-28 12:01:45 -0600594 VkFence fence;
595 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
596 .pNext = NULL,
597 .flags = 0};
Tony Barboura72d9492017-01-11 13:35:09 -0700598 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
599 assert(!err);
600
Karl Schultze4dc75c2016-02-02 15:37:51 -0700601 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
Karl Schultze4dc75c2016-02-02 15:37:51 -0700602 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
603 .pNext = NULL,
604 .waitSemaphoreCount = 0,
605 .pWaitSemaphores = NULL,
606 .pWaitDstStageMask = NULL,
607 .commandBufferCount = 1,
608 .pCommandBuffers = cmd_bufs,
609 .signalSemaphoreCount = 0,
610 .pSignalSemaphores = NULL};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600611
Tony Barbource7524e2016-07-28 12:01:45 -0600612 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600613 assert(!err);
614
Tony Barbource7524e2016-07-28 12:01:45 -0600615 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600616 assert(!err);
617
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -0600618 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
Tony Barbource7524e2016-07-28 12:01:45 -0600619 vkDestroyFence(demo->device, fence, NULL);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600620 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600621}
622
Karl Schultze4dc75c2016-02-02 15:37:51 -0700623static void demo_set_image_layout(struct demo *demo, VkImage image,
624 VkImageAspectFlags aspectMask,
625 VkImageLayout old_image_layout,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700626 VkImageLayout new_image_layout,
Tony Barbour5ff13182016-10-19 13:58:29 -0600627 VkAccessFlagBits srcAccessMask,
628 VkPipelineStageFlags src_stages,
629 VkPipelineStageFlags dest_stages) {
Tony Barbour6db4fcb2016-10-19 13:41:16 -0600630 assert(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600631
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600632 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600633 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600634 .pNext = NULL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -0700635 .srcAccessMask = srcAccessMask,
Chia-I Wu19574622015-10-27 19:54:37 +0800636 .dstAccessMask = 0,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600637 .oldLayout = old_image_layout,
638 .newLayout = new_image_layout,
639 .image = image,
Karl Schultze4dc75c2016-02-02 15:37:51 -0700640 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600641
Tony Barboureb5077b2016-10-19 15:23:36 -0600642 switch (new_image_layout) {
643 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600644 /* Make sure anything that was copying from this image has completed */
Tony Barboureb5077b2016-10-19 15:23:36 -0600645 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
646 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600647
Tony Barboureb5077b2016-10-19 15:23:36 -0600648 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700649 image_memory_barrier.dstAccessMask =
650 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600651 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700652
Tony Barboureb5077b2016-10-19 15:23:36 -0600653 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700654 image_memory_barrier.dstAccessMask =
655 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600656 break;
Mark Lobodzinski9e0be702015-11-04 13:49:48 -0700657
Tony Barboureb5077b2016-10-19 15:23:36 -0600658 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700659 image_memory_barrier.dstAccessMask =
660 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
Tony Barboureb5077b2016-10-19 15:23:36 -0600661 break;
662
663 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
664 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
665 break;
666
667 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
668 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
669 break;
670
671 default:
672 image_memory_barrier.dstAccessMask = 0;
673 break;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600674 }
675
Tony Barbourf165b5d2016-10-03 16:01:41 -0600676
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600677 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600678
Karl Schultze4dc75c2016-02-02 15:37:51 -0700679 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0,
680 NULL, 1, pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600681}
682
Karl Schultze4dc75c2016-02-02 15:37:51 -0700683static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700684 const VkCommandBufferBeginInfo cmd_buf_info = {
685 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
686 .pNext = NULL,
Tony Barbource7524e2016-07-28 12:01:45 -0600687 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
Rene Lindsayd787e3a2016-07-07 16:00:14 -0700688 .pInheritanceInfo = NULL,
Jon Ashburn0bec67e2016-01-11 13:12:43 -0700689 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800690 const VkClearValue clear_values[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -0700691 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
692 [1] = {.depthStencil = {1.0f, 0}},
Chia-I Wua74c5b22015-07-07 11:50:03 +0800693 };
694 const VkRenderPassBeginInfo rp_begin = {
695 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
696 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800697 .renderPass = demo->render_pass,
Tony Barboura72d9492017-01-11 13:35:09 -0700698 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
Chia-I Wua74c5b22015-07-07 11:50:03 +0800699 .renderArea.offset.x = 0,
700 .renderArea.offset.y = 0,
701 .renderArea.extent.width = demo->width,
702 .renderArea.extent.height = demo->height,
Cody Northrop372bbae2015-08-04 11:51:03 -0600703 .clearValueCount = 2,
704 .pClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700705 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800706 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600707
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600708 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600709 assert(!err);
Chia-I Wuf051d262015-10-27 19:25:11 +0800710 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700711 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
712 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Tony Barboura72d9492017-01-11 13:35:09 -0700713 demo->pipeline_layout, 0, 1,
714 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set,
715 0, NULL);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600716 VkViewport viewport;
717 memset(&viewport, 0, sizeof(viewport));
Karl Schultze4dc75c2016-02-02 15:37:51 -0700718 viewport.height = (float)demo->height;
719 viewport.width = (float)demo->width;
720 viewport.minDepth = (float)0.0f;
721 viewport.maxDepth = (float)1.0f;
Jon Ashburn19255f82015-12-30 14:06:55 -0700722 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
Courtney Goeltzenleuchter4cbf78b2015-09-17 15:06:17 -0600723
724 VkRect2D scissor;
725 memset(&scissor, 0, sizeof(scissor));
726 scissor.extent.width = demo->width;
727 scissor.extent.height = demo->height;
728 scissor.offset.x = 0;
729 scissor.offset.y = 0;
Jon Ashburn19255f82015-12-30 14:06:55 -0700730 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
Courtney Goeltzenleuchter332a3672015-09-23 12:31:50 -0600731 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
Tony Barbour98390392016-07-28 10:50:13 -0600732 // Note that ending the renderpass changes the image's layout from
Tony Barbour22e06272016-09-07 16:07:56 -0600733 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
Chia-I Wu57b23b42015-06-26 15:34:39 +0800734 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600735
Tony Barbour22e06272016-09-07 16:07:56 -0600736 if (demo->separate_present_queue) {
737 // We have to transfer ownership from the graphics queue family to the
738 // present queue family to be able to present. Note that we don't have
739 // to transfer from present queue family back to graphics queue family at
740 // the start of the next frame because we don't care about the image's
741 // contents at that point.
742 VkImageMemoryBarrier image_ownership_barrier = {
743 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
744 .pNext = NULL,
745 .srcAccessMask = 0,
746 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
747 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
748 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
749 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
750 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700751 .image = demo->swapchain_image_resources[demo->current_buffer].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600752 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
753
754 vkCmdPipelineBarrier(cmd_buf,
755 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
Tony Barbourdb30e4b2016-10-11 11:41:05 -0600756 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0,
Tony Barbour22e06272016-09-07 16:07:56 -0600757 0, NULL, 0, NULL, 1, &image_ownership_barrier);
758 }
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600759 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600760 assert(!err);
761}
762
Tony Barbour22e06272016-09-07 16:07:56 -0600763void demo_build_image_ownership_cmd(struct demo *demo, int i) {
764 VkResult U_ASSERT_ONLY err;
765
766 const VkCommandBufferBeginInfo cmd_buf_info = {
767 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
768 .pNext = NULL,
769 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
770 .pInheritanceInfo = NULL,
771 };
Tony Barboura72d9492017-01-11 13:35:09 -0700772 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600773 &cmd_buf_info);
774 assert(!err);
775
776 VkImageMemoryBarrier image_ownership_barrier = {
777 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
778 .pNext = NULL,
779 .srcAccessMask = 0,
780 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
781 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
782 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
783 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
784 .dstQueueFamilyIndex = demo->present_queue_family_index,
Tony Barboura72d9492017-01-11 13:35:09 -0700785 .image = demo->swapchain_image_resources[i].image,
Tony Barbour22e06272016-09-07 16:07:56 -0600786 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
787
Tony Barboura72d9492017-01-11 13:35:09 -0700788 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd,
Tony Barbour22e06272016-09-07 16:07:56 -0600789 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
790 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0,
791 NULL, 0, NULL, 1, &image_ownership_barrier);
Tony Barboura72d9492017-01-11 13:35:09 -0700792 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -0600793 assert(!err);
794}
795
Karl Schultze4dc75c2016-02-02 15:37:51 -0700796void demo_update_data_buffer(struct demo *demo) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600797 mat4x4 MVP, Model, VP;
798 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600799 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600800 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600801
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600802 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600803
Karl Schultz6ddf1e02017-01-04 10:31:20 -0700804 // Rotate around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600805 mat4x4_dup(Model, demo->model_matrix);
Karl Schultze4dc75c2016-02-02 15:37:51 -0700806 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f,
807 (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600808 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600809
Tony Barboura72d9492017-01-11 13:35:09 -0700810 vkWaitForFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence,
811 VK_TRUE, UINT64_MAX);
812
813 err = vkMapMemory(demo->device,
814 demo->swapchain_image_resources[demo->current_buffer].uniform_memory, 0,
815 VK_WHOLE_SIZE, 0, (void **)&pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600816 assert(!err);
817
Karl Schultze4dc75c2016-02-02 15:37:51 -0700818 memcpy(pData, (const void *)&MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819
Tony Barboura72d9492017-01-11 13:35:09 -0700820 vkUnmapMemory(demo->device, demo->swapchain_image_resources[demo->current_buffer].uniform_memory);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600821}
822
Ian Elliottd940ca22017-03-22 08:31:27 -0600823void DemoUpdateTargetIPD(struct demo *demo) {
824 // Look at what happened to previous presents, and make appropriate
825 // adjustments in timing:
826 VkResult U_ASSERT_ONLY err;
827 VkPastPresentationTimingGOOGLE* past = NULL;
828 uint32_t count = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600829
830 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
831 demo->swapchain,
832 &count,
833 NULL);
834 assert(!err);
Ian Elliottd940ca22017-03-22 08:31:27 -0600835 if (count) {
836 past = (VkPastPresentationTimingGOOGLE*) malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
837 assert(past);
Ian Elliottd940ca22017-03-22 08:31:27 -0600838 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device,
839 demo->swapchain,
840 &count,
841 past);
842 assert(!err);
843
844 bool early = false;
845 bool late = false;
846 bool calibrate_next = false;
Ian Elliottd940ca22017-03-22 08:31:27 -0600847 for (uint32_t i = 0 ; i < count ; i++) {
Ian Elliottd940ca22017-03-22 08:31:27 -0600848 if (!demo->syncd_with_actual_presents) {
849 // This is the first time that we've received an
850 // actualPresentTime for this swapchain. In order to not
851 // perceive these early frames as "late", we need to sync-up
852 // our future desiredPresentTime's with the
853 // actualPresentTime(s) that we're receiving now.
854 calibrate_next = true;
Ian Elliottd940ca22017-03-22 08:31:27 -0600855
Ian Elliottd940ca22017-03-22 08:31:27 -0600856 // So that we don't suspect any pending presents as late,
857 // record them all as suspected-late presents:
858 demo->last_late_id = demo->next_present_id - 1;
859 demo->last_early_id = 0;
Ian Elliottd940ca22017-03-22 08:31:27 -0600860 demo->syncd_with_actual_presents = true;
861 break;
862 } else if (CanPresentEarlier(past[i].earliestPresentTime,
863 past[i].actualPresentTime,
864 past[i].presentMargin,
865 demo->refresh_duration)) {
866 // This image could have been presented earlier. We don't want
867 // to decrease the target_IPD until we've seen early presents
868 // for at least two seconds.
869 if (demo->last_early_id == past[i].presentID) {
870 // We've now seen two seconds worth of early presents.
871 // Flag it as such, and reset the counter:
872 early = true;
873 demo->last_early_id = 0;
874 } else if (demo->last_early_id == 0) {
875 // This is the first early present we've seen.
876 // Calculate the presentID for two seconds from now.
877 uint64_t lastEarlyTime =
878 past[i].actualPresentTime + (2 * BILLION);
879 uint32_t howManyPresents =
880 (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
881 demo->last_early_id = past[i].presentID + howManyPresents;
882 } else {
883 // We are in the midst of a set of early images,
884 // and so we won't do anything.
885 }
886 late = false;
887 demo->last_late_id = 0;
888 } else if (ActualTimeLate(past[i].desiredPresentTime,
889 past[i].actualPresentTime,
890 demo->refresh_duration)) {
891 // This image was presented after its desired time. Since
892 // there's a delay between calling vkQueuePresentKHR and when
893 // we get the timing data, several presents may have been late.
894 // Thus, we need to threat all of the outstanding presents as
895 // being likely late, so that we only increase the target_IPD
896 // once for all of those presents.
897 if ((demo->last_late_id == 0) ||
898 (demo->last_late_id < past[i].presentID)) {
899 late = true;
900 // Record the last suspected-late present:
901 demo->last_late_id = demo->next_present_id - 1;
902 } else {
903 // We are in the midst of a set of likely-late images,
904 // and so we won't do anything.
905 }
906 early = false;
907 demo->last_early_id = 0;
908 } else {
909 // Since this image was not presented early or late, reset
910 // any sets of early or late presentIDs:
911 early = false;
912 late = false;
913 calibrate_next = true;
914 demo->last_early_id = 0;
915 demo->last_late_id = 0;
916 }
917 }
918
919 if (early) {
920 // Since we've seen at least two-seconds worth of presnts that
921 // could have occured earlier than desired, let's decrease the
922 // target_IPD (i.e. increase the frame rate):
923 //
924 // TODO(ianelliott): Try to calculate a better target_IPD based
925 // on the most recently-seen present (this is overly-simplistic).
926 demo->refresh_duration_multiplier--;
927 if (demo->refresh_duration_multiplier == 0) {
928 // This should never happen, but in case it does, don't
929 // try to go faster.
930 demo->refresh_duration_multiplier = 1;
931 }
932 demo->target_IPD =
933 demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600934 }
935 if (late) {
936 // Since we found a new instance of a late present, we want to
937 // increase the target_IPD (i.e. decrease the frame rate):
938 //
939 // TODO(ianelliott): Try to calculate a better target_IPD based
940 // on the most recently-seen present (this is overly-simplistic).
941 demo->refresh_duration_multiplier++;
942 demo->target_IPD =
943 demo->refresh_duration * demo->refresh_duration_multiplier;
Ian Elliottd940ca22017-03-22 08:31:27 -0600944 }
945
946 if (calibrate_next) {
947 int64_t multiple = demo->next_present_id - past[count-1].presentID;
948 demo->prev_desired_present_time =
949 (past[count-1].actualPresentTime +
950 (multiple * demo->target_IPD));
Ian Elliottd940ca22017-03-22 08:31:27 -0600951 }
952 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600953}
954
Karl Schultze4dc75c2016-02-02 15:37:51 -0700955static void demo_draw(struct demo *demo) {
Tony Barbourfdc2d352015-04-22 09:02:32 -0600956 VkResult U_ASSERT_ONLY err;
Tony Barboure35e8aa2016-06-20 10:44:08 -0600957
szdarkhackd322ff02016-10-08 09:51:22 +0300958 // Ensure no more than FRAME_LAG presentations are outstanding
959 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
960 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
Tony Barbour66a56c32016-09-21 13:10:56 -0600961
Ian Elliottaaae5352015-07-06 14:27:58 -0600962 // Get the index of the next available swapchain image:
Karl Schultze4dc75c2016-02-02 15:37:51 -0700963 err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
Tony Barboura72d9492017-01-11 13:35:09 -0700964 demo->image_acquired_semaphores[demo->frame_index],
965 demo->fences[demo->frame_index],
Ian Elliottaaae5352015-07-06 14:27:58 -0600966 &demo->current_buffer);
Tony Barbour66a56c32016-09-21 13:10:56 -0600967
Tony Barboura72d9492017-01-11 13:35:09 -0700968 demo_update_data_buffer(demo);
969
Ian Elliottcf074d32015-10-16 18:02:43 -0600970 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
971 // demo->swapchain is out of date (e.g. the window was resized) and
972 // must be recreated:
Tony Barbour66a56c32016-09-21 13:10:56 -0600973 demo->frame_index += 1;
974 demo->frame_index %= FRAME_LAG;
975
Ian Elliottcf074d32015-10-16 18:02:43 -0600976 demo_resize(demo);
977 demo_draw(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -0600978 return;
979 } else if (err == VK_SUBOPTIMAL_KHR) {
980 // demo->swapchain is not as optimal as it could be, but the platform's
981 // presentation engine will still present the image correctly.
982 } else {
983 assert(!err);
984 }
Ian Elliottd940ca22017-03-22 08:31:27 -0600985 if (demo->VK_GOOGLE_display_timing_enabled) {
986 // Look at what happened to previous presents, and make appropriate
987 // adjustments in timing:
988 DemoUpdateTargetIPD(demo);
989
990 // Note: a real application would position its geometry to that it's in
991 // the correct locatoin for when the next image is presented. It might
992 // also wait, so that there's less latency between any input and when
993 // the next image is rendered/presented. This demo program is so
994 // simple that it doesn't do either of those.
995 }
996
Tony Barbource7524e2016-07-28 12:01:45 -0600997 // Wait for the image acquired semaphore to be signaled to ensure
Courtney Goeltzenleuchter5fa898d2015-10-20 18:04:07 -0600998 // that the image won't be rendered to until the presentation
999 // engine has fully released ownership to the application, and it is
1000 // okay to render to the image.
Tony Barbour22e06272016-09-07 16:07:56 -06001001 VkPipelineStageFlags pipe_stage_flags;
1002 VkSubmitInfo submit_info;
1003 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1004 submit_info.pNext = NULL;
1005 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1006 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1007 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001008 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001009 submit_info.commandBufferCount = 1;
Tony Barboura72d9492017-01-11 13:35:09 -07001010 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001011 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001012 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barboura72d9492017-01-11 13:35:09 -07001013 vkResetFences(demo->device, 1, &demo->swapchain_image_resources[demo->current_buffer].fence);
1014 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info,
1015 demo->swapchain_image_resources[demo->current_buffer].fence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001016 assert(!err);
1017
Tony Barbour22e06272016-09-07 16:07:56 -06001018 if (demo->separate_present_queue) {
1019 // If we are using separate queues, change image ownership to the
1020 // present queue before presenting, waiting for the draw complete
1021 // semaphore and signalling the ownership released semaphore when finished
Tony Barboura72d9492017-01-11 13:35:09 -07001022 VkFence nullFence = VK_NULL_HANDLE;
Tony Barbour22e06272016-09-07 16:07:56 -06001023 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1024 submit_info.waitSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001025 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001026 submit_info.commandBufferCount = 1;
1027 submit_info.pCommandBuffers =
Tony Barboura72d9492017-01-11 13:35:09 -07001028 &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
Tony Barbour22e06272016-09-07 16:07:56 -06001029 submit_info.signalSemaphoreCount = 1;
Tony Barbour66a56c32016-09-21 13:10:56 -06001030 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
Tony Barbour22e06272016-09-07 16:07:56 -06001031 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1032 assert(!err);
1033 }
1034
1035 // If we are using separate queues we have to wait for image ownership,
1036 // otherwise wait for draw complete
Ian Elliotta0781972015-08-21 15:09:33 -06001037 VkPresentInfoKHR present = {
1038 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
Ian Elliottaaae5352015-07-06 14:27:58 -06001039 .pNext = NULL,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001040 .waitSemaphoreCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06001041 .pWaitSemaphores = (demo->separate_present_queue)
Tony Barbour66a56c32016-09-21 13:10:56 -06001042 ? &demo->image_ownership_semaphores[demo->frame_index]
1043 : &demo->draw_complete_semaphores[demo->frame_index],
Ian Elliotta0781972015-08-21 15:09:33 -06001044 .swapchainCount = 1,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001045 .pSwapchains = &demo->swapchain,
1046 .pImageIndices = &demo->current_buffer,
Ian Elliottaaae5352015-07-06 14:27:58 -06001047 };
1048
Ian Elliottd940ca22017-03-22 08:31:27 -06001049 if (demo->VK_GOOGLE_display_timing_enabled) {
1050 VkPresentTimeGOOGLE ptime;
1051 if (demo->prev_desired_present_time == 0) {
1052 // This must be the first present for this swapchain.
1053 //
1054 // We don't know where we are relative to the presentation engine's
1055 // display's refresh cycle. We also don't know how long rendering
1056 // takes. Let's make a grossly-simplified assumption that the
1057 // desiredPresentTime should be half way between now and
1058 // now+target_IPD. We will adjust over time.
1059 uint64_t curtime = getTimeInNanoseconds();
1060 if (curtime == 0) {
1061 // Since we didn't find out the current time, don't give a
1062 // desiredPresentTime:
1063 ptime.desiredPresentTime = 0;
1064 } else {
Ian Elliottd940ca22017-03-22 08:31:27 -06001065 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1066 }
1067 } else {
1068 ptime.desiredPresentTime = (demo->prev_desired_present_time +
1069 demo->target_IPD);
1070 }
1071 ptime.presentID = demo->next_present_id++;
1072 demo->prev_desired_present_time = ptime.desiredPresentTime;
Ian Elliottd940ca22017-03-22 08:31:27 -06001073
1074 VkPresentTimesInfoGOOGLE present_time = {
1075 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1076 .pNext = present.pNext,
1077 .swapchainCount = present.swapchainCount,
1078 .pTimes = &ptime,
1079 };
1080 if (demo->VK_GOOGLE_display_timing_enabled) {
1081 present.pNext = &present_time;
Ian Elliottd940ca22017-03-22 08:31:27 -06001082 }
1083 }
1084
Tony Barboura2a67812016-07-18 13:21:06 -06001085 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
Tony Barbour66a56c32016-09-21 13:10:56 -06001086 demo->frame_index += 1;
1087 demo->frame_index %= FRAME_LAG;
1088
Ian Elliottcf074d32015-10-16 18:02:43 -06001089 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1090 // demo->swapchain is out of date (e.g. the window was resized) and
1091 // must be recreated:
1092 demo_resize(demo);
1093 } else if (err == VK_SUBOPTIMAL_KHR) {
1094 // demo->swapchain is not as optimal as it could be, but the platform's
1095 // presentation engine will still present the image correctly.
1096 } else {
1097 assert(!err);
1098 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001099}
1100
Karl Schultze4dc75c2016-02-02 15:37:51 -07001101static void demo_prepare_buffers(struct demo *demo) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001102 VkResult U_ASSERT_ONLY err;
Ian Elliottcf074d32015-10-16 18:02:43 -06001103 VkSwapchainKHR oldSwapchain = demo->swapchain;
Ian Elliottaaae5352015-07-06 14:27:58 -06001104
Ian Elliottb08e0d92015-11-20 11:55:46 -07001105 // Check the surface capabilities and formats
1106 VkSurfaceCapabilitiesKHR surfCapabilities;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001107 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(
1108 demo->gpu, demo->surface, &surfCapabilities);
Ian Elliottaaae5352015-07-06 14:27:58 -06001109 assert(!err);
1110
Ian Elliott8528eff2015-08-07 15:56:59 -06001111 uint32_t presentModeCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001112 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1113 demo->gpu, demo->surface, &presentModeCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001114 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06001115 VkPresentModeKHR *presentModes =
1116 (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
Ian Elliott8528eff2015-08-07 15:56:59 -06001117 assert(presentModes);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001118 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(
1119 demo->gpu, demo->surface, &presentModeCount, presentModes);
Ian Elliottaaae5352015-07-06 14:27:58 -06001120 assert(!err);
1121
Ian Elliotta0781972015-08-21 15:09:33 -06001122 VkExtent2D swapchainExtent;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001123 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1124 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1125 // If the surface size is undefined, the size is set to the size
1126 // of the images requested, which must fit within the minimum and
1127 // maximum values.
Ian Elliotta0781972015-08-21 15:09:33 -06001128 swapchainExtent.width = demo->width;
1129 swapchainExtent.height = demo->height;
Ian Elliottcf7f7482016-08-19 03:46:58 -06001130
1131 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1132 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1133 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1134 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1135 }
1136
1137 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1138 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1139 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1140 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1141 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001142 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06001143 // If the surface size is defined, the swap chain size must match
Ian Elliottb08e0d92015-11-20 11:55:46 -07001144 swapchainExtent = surfCapabilities.currentExtent;
1145 demo->width = surfCapabilities.currentExtent.width;
1146 demo->height = surfCapabilities.currentExtent.height;
Ian Elliottaaae5352015-07-06 14:27:58 -06001147 }
1148
Tony Barbour66a56c32016-09-21 13:10:56 -06001149 // The FIFO present mode is guaranteed by the spec to be supported
Ian Elliottb18fdde2016-10-03 12:11:12 -06001150 // and to have no tearing. It's a great default present mode to use.
Ian Elliotta0781972015-08-21 15:09:33 -06001151 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour291d57b2016-10-21 14:47:07 -06001152
Ian Elliottb18fdde2016-10-03 12:11:12 -06001153 // There are times when you may wish to use another present mode. The
1154 // following code shows how to select them, and the comments provide some
1155 // reasons you may wish to use them.
1156 //
1157 // It should be noted that Vulkan 1.0 doesn't provide a method for
1158 // synchronizing rendering with the presentation engine's display. There
1159 // is a method provided for throttling rendering with the display, but
1160 // there are some presentation engines for which this method will not work.
1161 // If an application doesn't throttle its rendering, and if it renders much
1162 // faster than the refresh rate of the display, this can waste power on
1163 // mobile devices. That is because power is being spent rendering images
1164 // that may never be seen.
Tony Barbour291d57b2016-10-21 14:47:07 -06001165
Ian Elliottb18fdde2016-10-03 12:11:12 -06001166 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1167 // tearing, or have some way of synchronizing their rendering with the
1168 // display.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001169 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1170 // generally render a new presentable image every refresh cycle, but are
1171 // occasionally early. In this case, the application wants the new image
1172 // to be displayed instead of the previously-queued-for-presentation image
1173 // that has not yet been displayed.
Ian Elliottb18fdde2016-10-03 12:11:12 -06001174 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1175 // render a new presentable image every refresh cycle, but are occasionally
1176 // late. In this case (perhaps because of stuttering/latency concerns),
1177 // the application wants the late image to be immediately displayed, even
1178 // though that may mean some tearing.
Tony Barbour291d57b2016-10-21 14:47:07 -06001179
1180 if (demo->presentMode != swapchainPresentMode) {
1181
1182 for (size_t i = 0; i < presentModeCount; ++i) {
1183 if (presentModes[i] == demo->presentMode) {
1184 swapchainPresentMode = demo->presentMode;
1185 break;
1186 }
Ian Elliottb18fdde2016-10-03 12:11:12 -06001187 }
1188 }
Tony Barbour291d57b2016-10-21 14:47:07 -06001189 if (swapchainPresentMode != demo->presentMode) {
1190 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1191 }
Ian Elliottaaae5352015-07-06 14:27:58 -06001192
Tony Barbour84376fe2017-01-06 15:54:22 -07001193 // Determine the number of VkImages to use in the swap chain.
1194 // Application desires to acquire 3 images at a time for triple
1195 // buffering
1196 uint32_t desiredNumOfSwapchainImages = 3;
1197 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1198 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1199 }
Ian Elliottcf7f7482016-08-19 03:46:58 -06001200 // If maxImageCount is 0, we can ask for as many images as we want;
1201 // otherwise we're limited to maxImageCount
Ian Elliottb08e0d92015-11-20 11:55:46 -07001202 if ((surfCapabilities.maxImageCount > 0) &&
Ian Elliottcf7f7482016-08-19 03:46:58 -06001203 (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
Ian Elliottaaae5352015-07-06 14:27:58 -06001204 // Application must settle for fewer images than desired:
Ian Elliottcf7f7482016-08-19 03:46:58 -06001205 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
Ian Elliottaaae5352015-07-06 14:27:58 -06001206 }
1207
Tony Barbour9fd6d352015-09-16 15:01:32 -06001208 VkSurfaceTransformFlagsKHR preTransform;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001209 if (surfCapabilities.supportedTransforms &
1210 VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
Ian Elliotta7a731c2015-12-11 15:52:12 -07001211 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Ian Elliottaaae5352015-07-06 14:27:58 -06001212 } else {
Ian Elliottb08e0d92015-11-20 11:55:46 -07001213 preTransform = surfCapabilities.currentTransform;
Ian Elliottaaae5352015-07-06 14:27:58 -06001214 }
1215
Tony Barbour820b55b2017-03-14 08:55:46 -06001216 // Find a supported composite alpha mode - one of these is guaranteed to be set
1217 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1218 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1219 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1220 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1221 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1222 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1223 };
1224 for (uint32_t i = 0; i < sizeof(compositeAlphaFlags); i++) {
1225 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1226 compositeAlpha = compositeAlphaFlags[i];
1227 break;
1228 }
1229 }
1230
Tony Barboura2a67812016-07-18 13:21:06 -06001231 VkSwapchainCreateInfoKHR swapchain_ci = {
Ian Elliott5b97eae2015-09-22 10:20:23 -06001232 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001233 .pNext = NULL,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001234 .surface = demo->surface,
Ian Elliottcf7f7482016-08-19 03:46:58 -06001235 .minImageCount = desiredNumOfSwapchainImages,
Chia-I Wucbb564e2015-04-16 22:02:10 +08001236 .imageFormat = demo->format,
Ian Elliott8528eff2015-08-07 15:56:59 -06001237 .imageColorSpace = demo->color_space,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001238 .imageExtent =
1239 {
1240 .width = swapchainExtent.width, .height = swapchainExtent.height,
1241 },
Ian Elliottb08e0d92015-11-20 11:55:46 -07001242 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Ian Elliottaaae5352015-07-06 14:27:58 -06001243 .preTransform = preTransform,
Tony Barbour820b55b2017-03-14 08:55:46 -06001244 .compositeAlpha = compositeAlpha,
Ian Elliottb08e0d92015-11-20 11:55:46 -07001245 .imageArrayLayers = 1,
1246 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1247 .queueFamilyIndexCount = 0,
Ian Elliott8528eff2015-08-07 15:56:59 -06001248 .pQueueFamilyIndices = NULL,
Ian Elliotta0781972015-08-21 15:09:33 -06001249 .presentMode = swapchainPresentMode,
Ian Elliottcf074d32015-10-16 18:02:43 -06001250 .oldSwapchain = oldSwapchain,
Ian Elliottaaae5352015-07-06 14:27:58 -06001251 .clipped = true,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001253 uint32_t i;
Tony Barboura2a67812016-07-18 13:21:06 -06001254 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001255 &demo->swapchain);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001256 assert(!err);
1257
Ian Elliottcf074d32015-10-16 18:02:43 -06001258 // If we just re-created an existing swapchain, we should destroy the old
1259 // swapchain at this point.
1260 // Note: destroying the swapchain also cleans up all its associated
1261 // presentable images once the platform is done with them.
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001262 if (oldSwapchain != VK_NULL_HANDLE) {
Tony Barboura90a4f92017-03-23 13:25:36 -06001263 // AMD driver times out waiting on fences used in AcquireNextImage on
1264 // a swapchain that is subsequently destroyed before the wait.
1265 vkWaitForFences(demo->device, FRAME_LAG, demo->fences, VK_TRUE, UINT64_MAX);
Ian Elliottb08e0d92015-11-20 11:55:46 -07001266 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06001267 }
1268
1269 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001270 &demo->swapchainImageCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06001271 assert(!err);
Chia-I Wucbb564e2015-04-16 22:02:10 +08001272
Karl Schultze4dc75c2016-02-02 15:37:51 -07001273 VkImage *swapchainImages =
1274 (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
Ian Elliotta0781972015-08-21 15:09:33 -06001275 assert(swapchainImages);
Ian Elliottcf074d32015-10-16 18:02:43 -06001276 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain,
Ian Elliotta0781972015-08-21 15:09:33 -06001277 &demo->swapchainImageCount,
1278 swapchainImages);
Ian Elliottaaae5352015-07-06 14:27:58 -06001279 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06001280
Tony Barboura72d9492017-01-11 13:35:09 -07001281 demo->swapchain_image_resources = (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) *
Karl Schultze4dc75c2016-02-02 15:37:51 -07001282 demo->swapchainImageCount);
Tony Barboura72d9492017-01-11 13:35:09 -07001283 assert(demo->swapchain_image_resources);
1284
1285 VkFenceCreateInfo fence_ci = {
1286 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
1287 .pNext = NULL,
1288 .flags = VK_FENCE_CREATE_SIGNALED_BIT
1289 };
Ian Elliottaaae5352015-07-06 14:27:58 -06001290
Ian Elliotta0781972015-08-21 15:09:33 -06001291 for (i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001292 VkImageViewCreateInfo color_image_view = {
1293 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001294 .pNext = NULL,
1295 .format = demo->format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001296 .components =
1297 {
1298 .r = VK_COMPONENT_SWIZZLE_R,
1299 .g = VK_COMPONENT_SWIZZLE_G,
1300 .b = VK_COMPONENT_SWIZZLE_B,
1301 .a = VK_COMPONENT_SWIZZLE_A,
1302 },
1303 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1304 .baseMipLevel = 0,
1305 .levelCount = 1,
1306 .baseArrayLayer = 0,
1307 .layerCount = 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001308 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1309 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310 };
1311
Tony Barboura72d9492017-01-11 13:35:09 -07001312 demo->swapchain_image_resources[i].image = swapchainImages[i];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001313
Tony Barboura72d9492017-01-11 13:35:09 -07001314 color_image_view.image = demo->swapchain_image_resources[i].image;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315
Karl Schultze4dc75c2016-02-02 15:37:51 -07001316 err = vkCreateImageView(demo->device, &color_image_view, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07001317 &demo->swapchain_image_resources[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001318 assert(!err);
Tony Barbour22e06272016-09-07 16:07:56 -06001319
Tony Barboura72d9492017-01-11 13:35:09 -07001320 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->swapchain_image_resources[i].fence);
1321 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001322 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001323
Ian Elliottd940ca22017-03-22 08:31:27 -06001324 if (demo->VK_GOOGLE_display_timing_enabled) {
1325 VkRefreshCycleDurationGOOGLE rc_dur;
1326 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device,
1327 demo->swapchain,
1328 &rc_dur);
1329 assert(!err);
1330 demo->refresh_duration = rc_dur.refreshDuration;
1331
1332 demo->syncd_with_actual_presents = false;
1333 // Initially target 1X the refresh duration:
1334 demo->target_IPD = demo->refresh_duration;
1335 demo->refresh_duration_multiplier = 1;
1336 demo->prev_desired_present_time = 0;
1337 demo->next_present_id = 1;
Ian Elliottd940ca22017-03-22 08:31:27 -06001338 }
1339
Mark Young04fd3d82016-01-25 14:43:50 -07001340 if (NULL != presentModes) {
1341 free(presentModes);
1342 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001343}
1344
Karl Schultze4dc75c2016-02-02 15:37:51 -07001345static void demo_prepare_depth(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001346 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001347 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001348 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001349 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001350 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001351 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001352 .extent = {demo->width, demo->height, 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001354 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001355 .samples = VK_SAMPLE_COUNT_1_BIT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001356 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter4e368ee2015-09-10 14:14:11 -06001357 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001358 .flags = 0,
1359 };
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001360
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001361 VkImageViewCreateInfo view = {
1362 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001363 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001364 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter556ee322015-07-15 17:41:38 -06001365 .format = depth_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001366 .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
1367 .baseMipLevel = 0,
1368 .levelCount = 1,
1369 .baseArrayLayer = 0,
1370 .layerCount = 1},
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371 .flags = 0,
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001372 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001373 };
Mike Stroyanebae8322015-04-17 12:36:38 -06001374
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001375 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001376 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001377 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001378
1379 demo->depth.format = depth_format;
1380
1381 /* create image */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001382 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001383 assert(!err);
1384
Karl Schultze4dc75c2016-02-02 15:37:51 -07001385 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
Tony Barbourcb59a5d2015-10-23 10:53:30 -06001386 assert(!err);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001387
Chia-I Wuf48700b2015-11-10 16:21:09 +08001388 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001389 demo->depth.mem_alloc.pNext = NULL;
1390 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1391 demo->depth.mem_alloc.memoryTypeIndex = 0;
1392
Karl Schultze4dc75c2016-02-02 15:37:51 -07001393 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
Jeremy Hayes1273a382017-02-22 08:53:13 -07001394 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001395 &demo->depth.mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001396 assert(pass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001398 /* allocate memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001399 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL,
1400 &demo->depth.mem);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001401 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001403 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001404 err =
1405 vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001406 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407
1408 /* create image view */
1409 view.image = demo->depth.image;
Chia-I Wu63636062015-10-26 21:10:41 +08001410 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001411 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412}
1413
Tony Barbour1a86d032015-09-21 15:17:33 -06001414/* Load a ppm file into memory */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001415bool loadTexture(const char *filename, uint8_t *rgba_data,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001416 VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
Bill Hollingsf4352142017-02-14 22:58:56 -05001417
1418#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001419 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001420#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001421
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001422#ifdef __ANDROID__
1423#include <lunarg.ppm.h>
1424 char *cPtr;
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001425 cPtr = (char*)lunarg_ppm;
1426 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001427 return false;
1428 }
1429 while(strncmp(cPtr++, "\n", 1));
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001430 sscanf(cPtr, "%u %u", width, height);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001431 if (rgba_data == NULL) {
1432 return true;
1433 }
1434 while(strncmp(cPtr++, "\n", 1));
Mark Lobodzinski4c62d002016-05-19 17:22:25 -06001435 if ((unsigned char*)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001436 return false;
1437 }
1438 while(strncmp(cPtr++, "\n", 1));
1439
1440 for (int y = 0; y < *height; y++) {
1441 uint8_t *rowPtr = rgba_data;
1442 for (int x = 0; x < *width; x++) {
1443 memcpy(rowPtr, cPtr, 3);
1444 rowPtr[3] = 255; /* Alpha of 1 */
1445 rowPtr += 4;
1446 cPtr += 3;
1447 }
1448 rgba_data += layout->rowPitch;
1449 }
1450
1451 return true;
1452#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07001453 FILE *fPtr = fopen(filename, "rb");
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001454 char header[256], *cPtr, *tmp;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001455
Tony Barbour1a86d032015-09-21 15:17:33 -06001456 if (!fPtr)
1457 return false;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001458
Tony Barbour1a86d032015-09-21 15:17:33 -06001459 cPtr = fgets(header, 256, fPtr); // P6
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001460 if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
1461 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001462 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001463 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001464
Tony Barbour1a86d032015-09-21 15:17:33 -06001465 do {
1466 cPtr = fgets(header, 256, fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001467 if (cPtr == NULL) {
1468 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001469 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001470 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07001471 } while (!strncmp(header, "#", 1));
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001472
Alexandre BACQUART9063e2a2016-06-17 11:30:32 -06001473 sscanf(header, "%u %u", width, height);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001474 if (rgba_data == NULL) {
1475 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001476 return true;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001477 }
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001478 tmp = fgets(header, 256, fPtr); // Format
Karl Schultze4dc75c2016-02-02 15:37:51 -07001479 (void)tmp;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001480 if (cPtr == NULL || strncmp(header, "255\n", 3)) {
1481 fclose(fPtr);
Tony Barbour1a86d032015-09-21 15:17:33 -06001482 return false;
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001483 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001484
Karl Schultze4dc75c2016-02-02 15:37:51 -07001485 for (int y = 0; y < *height; y++) {
Tony Barbour1a86d032015-09-21 15:17:33 -06001486 uint8_t *rowPtr = rgba_data;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001487 for (int x = 0; x < *width; x++) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001488 size_t s = fread(rowPtr, 3, 1, fPtr);
Karl Schultze4dc75c2016-02-02 15:37:51 -07001489 (void)s;
Tony Barbour1a86d032015-09-21 15:17:33 -06001490 rowPtr[3] = 255; /* Alpha of 1 */
1491 rowPtr += 4;
1492 }
1493 rgba_data += layout->rowPitch;
1494 }
1495 fclose(fPtr);
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001496 return true;
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001497#endif
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001498}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001499
Karl Schultze4dc75c2016-02-02 15:37:51 -07001500static void demo_prepare_texture_image(struct demo *demo, const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001501 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001502 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001503 VkImageUsageFlags usage,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001504 VkFlags required_props) {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001505 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001506 int32_t tex_width;
1507 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001508 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001509 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001510
Karl Schultze4dc75c2016-02-02 15:37:51 -07001511 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001512 ERR_EXIT("Failed to load textures", "Load Texture Failure");
David Pinedo30bd71d2015-04-23 08:16:57 -06001513 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001514
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001515 tex_obj->tex_width = tex_width;
1516 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001517
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001518 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001519 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001520 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -06001521 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001522 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001523 .extent = {tex_width, tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001524 .mipLevels = 1,
Courtney Goeltzenleuchterdff021f2015-10-21 17:57:31 -06001525 .arrayLayers = 1,
Chia-I Wu3ede9462015-10-31 00:31:16 +08001526 .samples = VK_SAMPLE_COUNT_1_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001527 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001528 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001529 .flags = 0,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001530 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001531 };
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001532
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001533 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001534
Karl Schultze4dc75c2016-02-02 15:37:51 -07001535 err =
1536 vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001537 assert(!err);
1538
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001539 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Piers Daniell735ee532015-02-23 16:23:13 -07001540
Chia-I Wuf48700b2015-11-10 16:21:09 +08001541 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001542 tex_obj->mem_alloc.pNext = NULL;
1543 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1544 tex_obj->mem_alloc.memoryTypeIndex = 0;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001545
Karl Schultze4dc75c2016-02-02 15:37:51 -07001546 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1547 required_props,
1548 &tex_obj->mem_alloc.memoryTypeIndex);
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001549 assert(pass);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001550
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001551 /* allocate memory */
Chia-I Wua8fe78a2015-10-27 18:04:07 +08001552 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001553 &(tex_obj->mem));
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001554 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001555
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001556 /* bind memory */
Karl Schultze4dc75c2016-02-02 15:37:51 -07001557 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001558 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001559
Tony Barbour8a9f62a2015-10-15 12:42:56 -06001560 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001561 const VkImageSubresource subres = {
Chia-I Wua0141432015-10-27 19:55:05 +08001562 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001563 .mipLevel = 0,
Courtney Goeltzenleuchterfc465b82015-09-10 16:38:41 -06001564 .arrayLayer = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001565 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001566 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001567 void *data;
1568
Karl Schultze4dc75c2016-02-02 15:37:51 -07001569 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres,
1570 &layout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001571
Karl Schultze4dc75c2016-02-02 15:37:51 -07001572 err = vkMapMemory(demo->device, tex_obj->mem, 0,
1573 tex_obj->mem_alloc.allocationSize, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001574 assert(!err);
1575
1576 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1577 fprintf(stderr, "Error loading texture: %s\n", filename);
1578 }
1579
Mark Lobodzinskie4c92a62015-09-07 13:59:43 -06001580 vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001581 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001582
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001583 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001584}
1585
Karl Schultze4dc75c2016-02-02 15:37:51 -07001586static void demo_destroy_texture_image(struct demo *demo,
1587 struct texture_object *tex_objs) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001588 /* clean up staging resources */
Chia-I Wu63636062015-10-26 21:10:41 +08001589 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1590 vkDestroyImage(demo->device, tex_objs->image, NULL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001591}
1592
Karl Schultze4dc75c2016-02-02 15:37:51 -07001593static void demo_prepare_textures(struct demo *demo) {
Tony Barbour72304ef2015-04-16 15:59:00 -06001594 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001595 VkFormatProperties props;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001596 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001597
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001598 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001599
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001600 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06001601 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001602
Karl Schultze4dc75c2016-02-02 15:37:51 -07001603 if ((props.linearTilingFeatures &
1604 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) &&
1605 !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001606 /* Device can texture using linear textures */
Tony Barbour5342ef52016-04-28 15:05:09 -06001607 demo_prepare_texture_image(
1608 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR,
1609 VK_IMAGE_USAGE_SAMPLED_BIT,
1610 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1611 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tony Barbour5ff13182016-10-19 13:58:29 -06001612 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1613 // shader to run until layout transition completes
1614 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT,
1615 VK_IMAGE_LAYOUT_PREINITIALIZED, demo->textures[i].imageLayout,
1616 VK_ACCESS_HOST_WRITE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1617 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Tony Barbour16156cb2016-10-19 14:15:49 -06001618 demo->staging_texture.image = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001619 } else if (props.optimalTilingFeatures &
1620 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001621 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001622
Tony Barbour16156cb2016-10-19 14:15:49 -06001623 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
Tony Barbour5342ef52016-04-28 15:05:09 -06001624 demo_prepare_texture_image(
Tony Barbour16156cb2016-10-19 14:15:49 -06001625 demo, tex_files[i], &demo->staging_texture, VK_IMAGE_TILING_LINEAR,
Tony Barbour5342ef52016-04-28 15:05:09 -06001626 VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1627 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1628 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001629
Karl Schultze4dc75c2016-02-02 15:37:51 -07001630 demo_prepare_texture_image(
1631 demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1632 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1633 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001634
Tony Barbour16156cb2016-10-19 14:15:49 -06001635 demo_set_image_layout(demo, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001636 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001637 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001638 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001639 VK_ACCESS_HOST_WRITE_BIT,
1640 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1641 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001642
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001643 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001644 VK_IMAGE_ASPECT_COLOR_BIT,
Tony Barbour5ff13182016-10-19 13:58:29 -06001645 VK_IMAGE_LAYOUT_PREINITIALIZED,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001646 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tony Barbour5ff13182016-10-19 13:58:29 -06001647 VK_ACCESS_HOST_WRITE_BIT,
1648 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1649 VK_PIPELINE_STAGE_TRANSFER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001650
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001651 VkImageCopy copy_region = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001652 .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1653 .srcOffset = {0, 0, 0},
1654 .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1655 .dstOffset = {0, 0, 0},
Tony Barbour16156cb2016-10-19 14:15:49 -06001656 .extent = {demo->staging_texture.tex_width,
1657 demo->staging_texture.tex_height, 1},
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001658 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07001659 vkCmdCopyImage(
Tony Barbour16156cb2016-10-19 14:15:49 -06001660 demo->cmd, demo->staging_texture.image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001661 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image,
1662 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001663
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001664 demo_set_image_layout(demo, demo->textures[i].image,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001665 VK_IMAGE_ASPECT_COLOR_BIT,
1666 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Tobin Ehlisf07d9552016-02-11 17:49:23 -07001667 demo->textures[i].imageLayout,
Tony Barbour5ff13182016-10-19 13:58:29 -06001668 VK_ACCESS_TRANSFER_WRITE_BIT,
1669 VK_PIPELINE_STAGE_TRANSFER_BIT,
1670 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001671
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001672 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001673 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1674 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001675 }
1676
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001677 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001678 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001679 .pNext = NULL,
Chia-I Wu6ef8c3b2015-10-26 16:49:32 +08001680 .magFilter = VK_FILTER_NEAREST,
1681 .minFilter = VK_FILTER_NEAREST,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07001682 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001683 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1684 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1685 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001686 .mipLodBias = 0.0f,
Jon Ashburn55e3fb02015-12-14 14:59:20 -07001687 .anisotropyEnable = VK_FALSE,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001688 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001689 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001690 .minLod = 0.0f,
1691 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001692 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Mark Lobodzinski644dc492015-09-01 15:42:56 -06001693 .unnormalizedCoordinates = VK_FALSE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001695
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001696 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001697 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001698 .pNext = NULL,
Chia-I Wu1c4086a2015-10-26 20:04:44 +08001699 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001700 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001701 .format = tex_format,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001702 .components =
1703 {
1704 VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G,
1705 VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A,
1706 },
1707 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06001708 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001709 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001710
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001711 /* create sampler */
Chia-I Wu63636062015-10-26 21:10:41 +08001712 err = vkCreateSampler(demo->device, &sampler, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001713 &demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001714 assert(!err);
1715
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001716 /* create image view */
1717 view.image = demo->textures[i].image;
Chia-I Wu63636062015-10-26 21:10:41 +08001718 err = vkCreateImageView(demo->device, &view, NULL,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001719 &demo->textures[i].view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001720 assert(!err);
1721 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001722}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001723
Tony Barboura72d9492017-01-11 13:35:09 -07001724void demo_prepare_cube_data_buffers(struct demo *demo) {
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001725 VkBufferCreateInfo buf_info;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001726 VkMemoryRequirements mem_reqs;
Tony Barboura72d9492017-01-11 13:35:09 -07001727 VkMemoryAllocateInfo mem_alloc;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001728 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001729 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001730 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter87a0ef02015-10-22 11:03:31 -06001731 bool U_ASSERT_ONLY pass;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001732 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001733
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001734 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001735 mat4x4_mul(MVP, VP, demo->model_matrix);
1736 memcpy(data.mvp, MVP, sizeof(MVP));
Karl Schultze4dc75c2016-02-02 15:37:51 -07001737 // dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001738
Karl Schultza2615462017-01-20 13:19:20 -07001739 for (unsigned int i = 0; i < 12 * 3; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001740 data.position[i][0] = g_vertex_buffer_data[i * 3];
1741 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1742 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001743 data.position[i][3] = 1.0f;
Karl Schultze4dc75c2016-02-02 15:37:51 -07001744 data.attr[i][0] = g_uv_buffer_data[2 * i];
1745 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001746 data.attr[i][2] = 0;
1747 data.attr[i][3] = 0;
1748 }
1749
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001750 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001751 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001752 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Cody Northrop91807302015-07-16 10:29:32 -06001753 buf_info.size = sizeof(data);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001754
Tony Barboura72d9492017-01-11 13:35:09 -07001755 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1756 err =
1757 vkCreateBuffer(demo->device, &buf_info, NULL,
1758 &demo->swapchain_image_resources[i].uniform_buffer);
1759 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001760
Tony Barboura72d9492017-01-11 13:35:09 -07001761 vkGetBufferMemoryRequirements(demo->device,
1762 demo->swapchain_image_resources[i].uniform_buffer,
1763 &mem_reqs);
Dominik Witczak2fe1c702015-09-22 18:25:33 +02001764
Tony Barboura72d9492017-01-11 13:35:09 -07001765 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1766 mem_alloc.pNext = NULL;
1767 mem_alloc.allocationSize = mem_reqs.size;
1768 mem_alloc.memoryTypeIndex = 0;
1769
1770 pass = memory_type_from_properties(
1771 demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
Tony Barbour5342ef52016-04-28 15:05:09 -06001772 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
Tony Barboura72d9492017-01-11 13:35:09 -07001773 &mem_alloc.memoryTypeIndex);
1774 assert(pass);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001775
Tony Barboura72d9492017-01-11 13:35:09 -07001776 err = vkAllocateMemory(demo->device, &mem_alloc, NULL,
1777 &demo->swapchain_image_resources[i].uniform_memory);
1778 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001779
Tony Barboura72d9492017-01-11 13:35:09 -07001780 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0,
1781 VK_WHOLE_SIZE, 0, (void **)&pData);
1782 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001783
Tony Barboura72d9492017-01-11 13:35:09 -07001784 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001785
Tony Barboura72d9492017-01-11 13:35:09 -07001786 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001787
Tony Barboura72d9492017-01-11 13:35:09 -07001788 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1789 demo->swapchain_image_resources[i].uniform_memory, 0);
1790 assert(!err);
1791 }
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001792}
1793
Karl Schultze4dc75c2016-02-02 15:37:51 -07001794static void demo_prepare_descriptor_layout(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001795 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001796 [0] =
1797 {
1798 .binding = 0,
1799 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1800 .descriptorCount = 1,
1801 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1802 .pImmutableSamplers = NULL,
1803 },
1804 [1] =
1805 {
1806 .binding = 1,
1807 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1808 .descriptorCount = DEMO_TEXTURE_COUNT,
1809 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1810 .pImmutableSamplers = NULL,
1811 },
Chia-I Wua2aa8632015-03-26 15:04:41 +08001812 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001813 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001814 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001815 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001816 .bindingCount = 2,
Jon Ashburn238f3432015-12-30 18:01:16 -07001817 .pBindings = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001818 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001819 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820
Karl Schultze4dc75c2016-02-02 15:37:51 -07001821 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL,
1822 &demo->desc_layout);
Chia-I Wub58c24a2015-03-26 15:27:55 +08001823 assert(!err);
1824
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001825 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001826 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1827 .pNext = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001828 .setLayoutCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001829 .pSetLayouts = &demo->desc_layout,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001830 };
1831
Karl Schultze4dc75c2016-02-02 15:37:51 -07001832 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL,
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001833 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001834 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001835}
1836
Karl Schultze4dc75c2016-02-02 15:37:51 -07001837static void demo_prepare_render_pass(struct demo *demo) {
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001838 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1839 // because at the start of the renderpass, we don't care about their contents.
1840 // At the start of the subpass, the color attachment's layout will be transitioned
1841 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1842 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1843 // the renderpass, the color attachment's layout will be transitioned to
1844 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1845 // the renderpass, no barriers are necessary.
Chia-I Wua74c5b22015-07-07 11:50:03 +08001846 const VkAttachmentDescription attachments[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001847 [0] =
1848 {
1849 .format = demo->format,
Tony Barboura860ce12016-11-21 12:56:25 -07001850 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001851 .samples = VK_SAMPLE_COUNT_1_BIT,
1852 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1853 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1854 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1855 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001856 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
Tony Barbour98390392016-07-28 10:50:13 -06001857 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001858 },
1859 [1] =
1860 {
1861 .format = demo->depth.format,
Tony Barboura860ce12016-11-21 12:56:25 -07001862 .flags = 0,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001863 .samples = VK_SAMPLE_COUNT_1_BIT,
1864 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1865 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1866 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1867 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1868 .initialLayout =
Tony Barbourdb30e4b2016-10-11 11:41:05 -06001869 VK_IMAGE_LAYOUT_UNDEFINED,
Karl Schultze4dc75c2016-02-02 15:37:51 -07001870 .finalLayout =
1871 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1872 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001873 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001874 const VkAttachmentReference color_reference = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07001875 .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001876 };
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001877 const VkAttachmentReference depth_reference = {
1878 .attachment = 1,
1879 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1880 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001881 const VkSubpassDescription subpass = {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001882 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1883 .flags = 0,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001884 .inputAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001885 .pInputAttachments = NULL,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001886 .colorAttachmentCount = 1,
Cody Northrop945bb832015-08-04 11:16:41 -06001887 .pColorAttachments = &color_reference,
1888 .pResolveAttachments = NULL,
Chia-I Wu6555f3f2015-10-26 17:32:47 +08001889 .pDepthStencilAttachment = &depth_reference,
Chia-I Wu3dfc1342015-10-26 20:48:51 +08001890 .preserveAttachmentCount = 0,
Cody Northrop945bb832015-08-04 11:16:41 -06001891 .pPreserveAttachments = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001892 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001893 const VkRenderPassCreateInfo rp_info = {
1894 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1895 .pNext = NULL,
Tony Barboura860ce12016-11-21 12:56:25 -07001896 .flags = 0,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001897 .attachmentCount = 2,
1898 .pAttachments = attachments,
1899 .subpassCount = 1,
1900 .pSubpasses = &subpass,
1901 .dependencyCount = 0,
1902 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001903 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001904 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001905
Chia-I Wu63636062015-10-26 21:10:41 +08001906 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
Chia-I Wuf973e322015-07-08 13:34:24 +08001907 assert(!err);
1908}
1909
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001910//TODO: Merge shader reading
1911#ifndef __ANDROID__
Karl Schultze4dc75c2016-02-02 15:37:51 -07001912static VkShaderModule
1913demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) {
Chia-I Wu46176f12015-10-31 00:31:16 +08001914 VkShaderModule module;
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001915 VkShaderModuleCreateInfo moduleCreateInfo;
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07001916 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001917
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001918 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1919 moduleCreateInfo.pNext = NULL;
1920
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001921 moduleCreateInfo.codeSize = size;
1922 moduleCreateInfo.pCode = code;
1923 moduleCreateInfo.flags = 0;
1924 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1925 assert(!err);
Chia-I Wu46176f12015-10-31 00:31:16 +08001926
1927 return module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001928}
1929
Karl Schultze4dc75c2016-02-02 15:37:51 -07001930char *demo_read_spv(const char *filename, size_t *psize) {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001931 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001932 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001933 void *shader_code;
1934
Bill Hollingsf4352142017-02-14 22:58:56 -05001935#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
Tony Barbourc65cd752017-03-13 15:29:35 -06001936 filename =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @(filename)].UTF8String;
Bill Hollingsf4352142017-02-14 22:58:56 -05001937#endif
Tony Barbourc65cd752017-03-13 15:29:35 -06001938
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001939 FILE *fp = fopen(filename, "rb");
Karl Schultze4dc75c2016-02-02 15:37:51 -07001940 if (!fp)
1941 return NULL;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001942
1943 fseek(fp, 0L, SEEK_END);
1944 size = ftell(fp);
1945
1946 fseek(fp, 0L, SEEK_SET);
1947
1948 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001949 retval = fread(shader_code, size, 1, fp);
1950 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001951
1952 *psize = size;
1953
Mike Stroyan9e9792a2015-10-28 11:15:46 -06001954 fclose(fp);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001955 return shader_code;
1956}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001957#endif
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001958
Karl Schultze4dc75c2016-02-02 15:37:51 -07001959static VkShaderModule demo_prepare_vs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001960#ifdef __ANDROID__
1961 VkShaderModuleCreateInfo sh_info = {};
1962 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1963
1964#include "cube.vert.h"
1965 sh_info.codeSize = sizeof(cube_vert);
1966 sh_info.pCode = cube_vert;
1967 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module);
1968 assert(!err);
1969#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001970 void *vertShaderCode;
1971 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001972
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001973 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08001974 if (!vertShaderCode) {
1975 ERR_EXIT("Failed to load cube-vert.spv", "Load Shader Failure");
1976 }
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001977
Karl Schultze4dc75c2016-02-02 15:37:51 -07001978 demo->vert_shader_module =
1979 demo_prepare_shader_module(demo, vertShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001980
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001981 free(vertShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001982#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08001983
1984 return demo->vert_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001985}
1986
Karl Schultze4dc75c2016-02-02 15:37:51 -07001987static VkShaderModule demo_prepare_fs(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05001988#ifdef __ANDROID__
1989 VkShaderModuleCreateInfo sh_info = {};
1990 sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1991
1992#include "cube.frag.h"
1993 sh_info.codeSize = sizeof(cube_frag);
1994 sh_info.pCode = cube_frag;
1995 VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module);
1996 assert(!err);
1997#else
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06001998 void *fragShaderCode;
1999 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002000
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002001 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Robert Morell4ccc6522017-02-01 14:51:00 -08002002 if (!fragShaderCode) {
2003 ERR_EXIT("Failed to load cube-frag.spv", "Load Shader Failure");
2004 }
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06002005
Karl Schultze4dc75c2016-02-02 15:37:51 -07002006 demo->frag_shader_module =
2007 demo_prepare_shader_module(demo, fragShaderCode, size);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06002008
Courtney Goeltzenleuchtera95e3052015-10-30 15:19:27 -06002009 free(fragShaderCode);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002010#endif
Chia-I Wu46176f12015-10-31 00:31:16 +08002011
2012 return demo->frag_shader_module;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002013}
2014
Karl Schultze4dc75c2016-02-02 15:37:51 -07002015static void demo_prepare_pipeline(struct demo *demo) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002016 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002017 VkPipelineCacheCreateInfo pipelineCache;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002018 VkPipelineVertexInputStateCreateInfo vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002019 VkPipelineInputAssemblyStateCreateInfo ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002020 VkPipelineRasterizationStateCreateInfo rs;
2021 VkPipelineColorBlendStateCreateInfo cb;
2022 VkPipelineDepthStencilStateCreateInfo ds;
2023 VkPipelineViewportStateCreateInfo vp;
2024 VkPipelineMultisampleStateCreateInfo ms;
2025 VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
2026 VkPipelineDynamicStateCreateInfo dynamicState;
Tony Barbourfdc2d352015-04-22 09:02:32 -06002027 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002028
Piers Daniellba839d12015-09-29 13:01:09 -06002029 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2030 memset(&dynamicState, 0, sizeof dynamicState);
2031 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2032 dynamicState.pDynamicStates = dynamicStateEnables;
2033
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002034 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002035 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05002036 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002037
Jason Ekstrand515b7ae2015-10-15 17:15:25 -07002038 memset(&vi, 0, sizeof(vi));
2039 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2040
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002041 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06002042 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002043 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002044
2045 memset(&rs, 0, sizeof(rs));
Chia-I Wuf051d262015-10-27 19:25:11 +08002046 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2047 rs.polygonMode = VK_POLYGON_MODE_FILL;
Chia-I Wu6555f3f2015-10-26 17:32:47 +08002048 rs.cullMode = VK_CULL_MODE_BACK_BIT;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002049 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
Courtney Goeltzenleuchter0db2c912015-10-15 12:57:38 -06002050 rs.depthClampEnable = VK_FALSE;
Cody Northrop709c1742015-08-17 11:10:49 -06002051 rs.rasterizerDiscardEnable = VK_FALSE;
2052 rs.depthBiasEnable = VK_FALSE;
Mark Young8749e2e2016-03-31 14:56:43 -06002053 rs.lineWidth = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002054
2055 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06002056 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2057 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07002058 memset(att_state, 0, sizeof(att_state));
Chia-I Wuf051d262015-10-27 19:25:11 +08002059 att_state[0].colorWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002060 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002061 cb.attachmentCount = 1;
2062 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002063
Tony Barbour29645d02015-01-16 14:27:35 -07002064 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06002065 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06002066 vp.viewportCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002067 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2068 VK_DYNAMIC_STATE_VIEWPORT;
Piers Daniellba839d12015-09-29 13:01:09 -06002069 vp.scissorCount = 1;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002070 dynamicStateEnables[dynamicState.dynamicStateCount++] =
2071 VK_DYNAMIC_STATE_SCISSOR;
Tony Barbour29645d02015-01-16 14:27:35 -07002072
2073 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06002074 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002075 ds.depthTestEnable = VK_TRUE;
2076 ds.depthWriteEnable = VK_TRUE;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002077 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
Cody Northrop5e4125d2015-08-26 10:01:32 -06002078 ds.depthBoundsTestEnable = VK_FALSE;
Chia-I Wuf051d262015-10-27 19:25:11 +08002079 ds.back.failOp = VK_STENCIL_OP_KEEP;
2080 ds.back.passOp = VK_STENCIL_OP_KEEP;
2081 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002082 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07002083 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002084
Tony Barbour29645d02015-01-16 14:27:35 -07002085 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06002086 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Cody Northrop011546b2015-08-04 14:34:54 -06002087 ms.pSampleMask = NULL;
Chia-I Wu3ede9462015-10-31 00:31:16 +08002088 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002089
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002090 // Two stages: vs and fs
2091 pipeline.stageCount = 2;
2092 VkPipelineShaderStageCreateInfo shaderStages[2];
2093 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2094
Karl Schultze4dc75c2016-02-02 15:37:51 -07002095 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2096 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002097 shaderStages[0].module = demo_prepare_vs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002098 shaderStages[0].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002099
Karl Schultze4dc75c2016-02-02 15:37:51 -07002100 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2101 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
Chia-I Wu46176f12015-10-31 00:31:16 +08002102 shaderStages[1].module = demo_prepare_fs(demo);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002103 shaderStages[1].pName = "main";
Mark Lobodzinski521d7762015-06-23 15:11:57 -06002104
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002105 memset(&pipelineCache, 0, sizeof(pipelineCache));
2106 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002107
Karl Schultze4dc75c2016-02-02 15:37:51 -07002108 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL,
2109 &demo->pipelineCache);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06002110 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06002111
Karl Schultze4dc75c2016-02-02 15:37:51 -07002112 pipeline.pVertexInputState = &vi;
Tony Barbour194bf282015-07-10 15:29:03 -06002113 pipeline.pInputAssemblyState = &ia;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002114 pipeline.pRasterizationState = &rs;
2115 pipeline.pColorBlendState = &cb;
2116 pipeline.pMultisampleState = &ms;
2117 pipeline.pViewportState = &vp;
2118 pipeline.pDepthStencilState = &ds;
2119 pipeline.pStages = shaderStages;
2120 pipeline.renderPass = demo->render_pass;
2121 pipeline.pDynamicState = &dynamicState;
Tony Barbour194bf282015-07-10 15:29:03 -06002122
Courtney Goeltzenleuchterd311fd02015-08-13 16:55:04 -06002123 pipeline.renderPass = demo->render_pass;
2124
Karl Schultze4dc75c2016-02-02 15:37:51 -07002125 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1,
2126 &pipeline, NULL, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002127 assert(!err);
2128
Chia-I Wu63636062015-10-26 21:10:41 +08002129 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2130 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002131}
2132
Karl Schultze4dc75c2016-02-02 15:37:51 -07002133static void demo_prepare_descriptor_pool(struct demo *demo) {
Chia-I Wuf051d262015-10-27 19:25:11 +08002134 const VkDescriptorPoolSize type_counts[2] = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002135 [0] =
2136 {
2137 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Tony Barboura72d9492017-01-11 13:35:09 -07002138 .descriptorCount = demo->swapchainImageCount,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002139 },
2140 [1] =
2141 {
2142 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Tony Barboura72d9492017-01-11 13:35:09 -07002143 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002144 },
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002145 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002146 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002147 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002148 .pNext = NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002149 .maxSets = demo->swapchainImageCount,
Chia-I Wuf051d262015-10-27 19:25:11 +08002150 .poolSizeCount = 2,
2151 .pPoolSizes = type_counts,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002152 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06002153 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002154
Karl Schultze4dc75c2016-02-02 15:37:51 -07002155 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL,
2156 &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002157 assert(!err);
2158}
2159
Karl Schultze4dc75c2016-02-02 15:37:51 -07002160static void demo_prepare_descriptor_set(struct demo *demo) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002161 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
Chia-I Wuae721ba2015-05-25 16:27:55 +08002162 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06002163 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002164
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002165 VkDescriptorSetAllocateInfo alloc_info = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002166 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Courtney Goeltzenleuchter014ded82015-10-23 14:21:05 -06002167 .pNext = NULL,
2168 .descriptorPool = demo->desc_pool,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002169 .descriptorSetCount = 1,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002170 .pSetLayouts = &demo->desc_layout};
Tony Barboura72d9492017-01-11 13:35:09 -07002171
2172 VkDescriptorBufferInfo buffer_info;
2173 buffer_info.offset = 0;
2174 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002175
Chia-I Wuae721ba2015-05-25 16:27:55 +08002176 memset(&tex_descs, 0, sizeof(tex_descs));
Karl Schultza2615462017-01-20 13:19:20 -07002177 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002178 tex_descs[i].sampler = demo->textures[i].sampler;
2179 tex_descs[i].imageView = demo->textures[i].view;
2180 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002181 }
2182
2183 memset(&writes, 0, sizeof(writes));
2184
2185 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002186 writes[0].descriptorCount = 1;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002187 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Tony Barboura72d9492017-01-11 13:35:09 -07002188 writes[0].pBufferInfo = &buffer_info;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002189
2190 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002191 writes[1].dstBinding = 1;
Chia-I Wu3dfc1342015-10-26 20:48:51 +08002192 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002193 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
Courtney Goeltzenleuchterc3ad65e2015-10-23 13:38:14 -06002194 writes[1].pImageInfo = tex_descs;
Chia-I Wuae721ba2015-05-25 16:27:55 +08002195
Tony Barboura72d9492017-01-11 13:35:09 -07002196 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2197 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2198 assert(!err);
2199 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2200 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2201 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2202 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2203 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002204}
2205
Karl Schultze4dc75c2016-02-02 15:37:51 -07002206static void demo_prepare_framebuffers(struct demo *demo) {
Courtney Goeltzenleuchter41866db2015-09-01 17:30:39 -06002207 VkImageView attachments[2];
Cody Northrop2e11cad2015-08-04 10:47:08 -06002208 attachments[1] = demo->depth.view;
2209
Chia-I Wuf973e322015-07-08 13:34:24 +08002210 const VkFramebufferCreateInfo fb_info = {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002211 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2212 .pNext = NULL,
2213 .renderPass = demo->render_pass,
2214 .attachmentCount = 2,
2215 .pAttachments = attachments,
2216 .width = demo->width,
2217 .height = demo->height,
2218 .layers = 1,
Chia-I Wuf973e322015-07-08 13:34:24 +08002219 };
2220 VkResult U_ASSERT_ONLY err;
2221 uint32_t i;
2222
Tony Barbourd8e504f2015-10-08 14:26:24 -06002223 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002224 attachments[0] = demo->swapchain_image_resources[i].view;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002225 err = vkCreateFramebuffer(demo->device, &fb_info, NULL,
Tony Barboura72d9492017-01-11 13:35:09 -07002226 &demo->swapchain_image_resources[i].framebuffer);
Chia-I Wuf973e322015-07-08 13:34:24 +08002227 assert(!err);
2228 }
2229}
2230
Karl Schultze4dc75c2016-02-02 15:37:51 -07002231static void demo_prepare(struct demo *demo) {
Cody Northrop91e221b2015-07-09 18:08:32 -06002232 VkResult U_ASSERT_ONLY err;
2233
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002234 const VkCommandPoolCreateInfo cmd_pool_info = {
2235 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Cody Northrop91e221b2015-07-09 18:08:32 -06002236 .pNext = NULL,
Tony Barboura2a67812016-07-18 13:21:06 -06002237 .queueFamilyIndex = demo->graphics_queue_family_index,
Cody Northrop91e221b2015-07-09 18:08:32 -06002238 .flags = 0,
2239 };
Karl Schultze4dc75c2016-02-02 15:37:51 -07002240 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL,
2241 &demo->cmd_pool);
Cody Northrop91e221b2015-07-09 18:08:32 -06002242 assert(!err);
2243
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002244 const VkCommandBufferAllocateInfo cmd = {
Chia-I Wuf48700b2015-11-10 16:21:09 +08002245 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002246 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08002247 .commandPool = demo->cmd_pool,
2248 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
Jon Ashburn0bec67e2016-01-11 13:12:43 -07002249 .commandBufferCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002250 };
Tony Barbour6db4fcb2016-10-19 13:41:16 -06002251 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2252 assert(!err);
2253 VkCommandBufferBeginInfo cmd_buf_info = {
2254 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2255 .pNext = NULL,
2256 .flags = 0,
2257 .pInheritanceInfo = NULL,
2258 };
2259 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2260 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002261
2262 demo_prepare_buffers(demo);
2263 demo_prepare_depth(demo);
2264 demo_prepare_textures(demo);
Tony Barboura72d9492017-01-11 13:35:09 -07002265 demo_prepare_cube_data_buffers(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002266
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002267 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08002268 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002269 demo_prepare_pipeline(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002270
Ian Elliotta0781972015-08-21 15:09:33 -06002271 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07002272 err =
Tony Barboura72d9492017-01-11 13:35:09 -07002273 vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002274 assert(!err);
2275 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002276
Tony Barbour22e06272016-09-07 16:07:56 -06002277 if (demo->separate_present_queue) {
Karl Schultza2615462017-01-20 13:19:20 -07002278 const VkCommandPoolCreateInfo present_cmd_pool_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002279 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2280 .pNext = NULL,
2281 .queueFamilyIndex = demo->present_queue_family_index,
2282 .flags = 0,
2283 };
Karl Schultza2615462017-01-20 13:19:20 -07002284 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL,
Tony Barbour22e06272016-09-07 16:07:56 -06002285 &demo->present_cmd_pool);
2286 assert(!err);
Karl Schultza2615462017-01-20 13:19:20 -07002287 const VkCommandBufferAllocateInfo present_cmd_info = {
Tony Barbour22e06272016-09-07 16:07:56 -06002288 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2289 .pNext = NULL,
2290 .commandPool = demo->present_cmd_pool,
2291 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2292 .commandBufferCount = 1,
2293 };
2294 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2295 err = vkAllocateCommandBuffers(
Karl Schultza2615462017-01-20 13:19:20 -07002296 demo->device, &present_cmd_info, &demo->swapchain_image_resources[i].graphics_to_present_cmd);
Tony Barbour22e06272016-09-07 16:07:56 -06002297 assert(!err);
2298 demo_build_image_ownership_cmd(demo, i);
2299 }
2300 }
2301
Chia-I Wu63ea9262015-03-26 13:14:16 +08002302 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002303 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002304
Chia-I Wuf973e322015-07-08 13:34:24 +08002305 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002306
Ian Elliotta0781972015-08-21 15:09:33 -06002307 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002308 demo->current_buffer = i;
Tony Barboura72d9492017-01-11 13:35:09 -07002309 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002310 }
2311
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002312 /*
2313 * Prepare functions above may generate pipeline commands
2314 * that need to be flushed before beginning the render loop.
2315 */
2316 demo_flush_init_cmd(demo);
Tony Barbour16156cb2016-10-19 14:15:49 -06002317 if (demo->staging_texture.image) {
2318 demo_destroy_texture_image(demo, &demo->staging_texture);
2319 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002320
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07002321 demo->current_buffer = 0;
Mike Stroyanbb60b382015-10-28 09:46:57 -06002322 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002323}
2324
Karl Schultze4dc75c2016-02-02 15:37:51 -07002325static void demo_cleanup(struct demo *demo) {
David Pinedo2bb7c932015-06-18 17:03:14 -06002326 uint32_t i;
2327
2328 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002329 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06002330
Tony Barbour66a56c32016-09-21 13:10:56 -06002331 // Wait for fences from present operations
2332 for (i = 0; i < FRAME_LAG; i++) {
szdarkhackd322ff02016-10-08 09:51:22 +03002333 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
Tony Barbour66a56c32016-09-21 13:10:56 -06002334 vkDestroyFence(demo->device, demo->fences[i], NULL);
2335 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2336 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2337 if (demo->separate_present_queue) {
2338 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2339 }
2340 }
2341
Tony Barbourd8e504f2015-10-08 14:26:24 -06002342 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002343 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Tony Barbour155cce82015-07-03 10:33:54 -06002344 }
Chia-I Wu63636062015-10-26 21:10:41 +08002345 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Chia-I Wuf973e322015-07-08 13:34:24 +08002346
Chia-I Wu63636062015-10-26 21:10:41 +08002347 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2348 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2349 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2350 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2351 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002352
2353 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002354 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2355 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2356 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2357 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002358 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002359 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002360
Chia-I Wu63636062015-10-26 21:10:41 +08002361 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2362 vkDestroyImage(demo->device, demo->depth.image, NULL);
2363 vkFreeMemory(demo->device, demo->depth.mem, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002364
Ian Elliotta0781972015-08-21 15:09:33 -06002365 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002366 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002367 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002368 &demo->swapchain_image_resources[i].cmd);
2369 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2370 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2371 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002372 }
Tony Barboura72d9492017-01-11 13:35:09 -07002373 free(demo->swapchain_image_resources);
Courtney Goeltzenleuchter874eb8d2015-09-24 17:16:48 -06002374 free(demo->queue_props);
Chia-I Wu63636062015-10-26 21:10:41 +08002375 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour66a56c32016-09-21 13:10:56 -06002376
Tony Barbour22e06272016-09-07 16:07:56 -06002377 if (demo->separate_present_queue) {
2378 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002379 }
Tony Barbourffa9a422016-11-10 16:45:15 -07002380 vkDeviceWaitIdle(demo->device);
Chia-I Wu63636062015-10-26 21:10:41 +08002381 vkDestroyDevice(demo->device, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002382 if (demo->validate) {
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07002383 demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL);
Tony Barbourd70b42c2015-06-30 14:14:19 -06002384 }
Ian Elliottb08e0d92015-11-20 11:55:46 -07002385 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
Chia-I Wu63636062015-10-26 21:10:41 +08002386 vkDestroyInstance(demo->inst, NULL);
David Pinedo2bb7c932015-06-18 17:03:14 -06002387
Tony Barbour153cb062016-12-07 13:43:36 -07002388#if defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour78d6b572016-11-14 14:46:33 -07002389 XDestroyWindow(demo->display, demo->xlib_window);
2390 XCloseDisplay(demo->display);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002391#elif defined(VK_USE_PLATFORM_XCB_KHR)
Pavol Klacansky573a19d2016-05-10 03:08:19 -06002392 xcb_destroy_window(demo->connection, demo->xcb_window);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002393 xcb_disconnect(demo->connection);
2394 free(demo->atom_wm_delete_window);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002395#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2396 wl_shell_surface_destroy(demo->shell_surface);
2397 wl_surface_destroy(demo->window);
2398 wl_shell_destroy(demo->shell);
2399 wl_compositor_destroy(demo->compositor);
2400 wl_registry_destroy(demo->registry);
2401 wl_display_disconnect(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002402#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002403#endif
David Pinedo2bb7c932015-06-18 17:03:14 -06002404}
2405
Karl Schultze4dc75c2016-02-02 15:37:51 -07002406static void demo_resize(struct demo *demo) {
Ian Elliottcf074d32015-10-16 18:02:43 -06002407 uint32_t i;
2408
Mike Stroyanbb60b382015-10-28 09:46:57 -06002409 // Don't react to resize until after first initialization.
2410 if (!demo->prepared) {
2411 return;
2412 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002413 // In order to properly resize the window, we must re-create the swapchain
2414 // AND redo the command buffers, etc.
2415 //
2416 // First, perform part of the demo_cleanup() function:
2417 demo->prepared = false;
Tony Barbource7524e2016-07-28 12:01:45 -06002418 vkDeviceWaitIdle(demo->device);
Ian Elliottcf074d32015-10-16 18:02:43 -06002419
2420 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002421 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002422 }
Chia-I Wu63636062015-10-26 21:10:41 +08002423 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002424
Chia-I Wu63636062015-10-26 21:10:41 +08002425 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2426 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2427 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2428 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2429 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002430
2431 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Chia-I Wu63636062015-10-26 21:10:41 +08002432 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2433 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2434 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2435 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002436 }
Ian Elliottcf074d32015-10-16 18:02:43 -06002437
Chia-I Wu63636062015-10-26 21:10:41 +08002438 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2439 vkDestroyImage(demo->device, demo->depth.image, NULL);
2440 vkFreeMemory(demo->device, demo->depth.mem, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002441
Ian Elliottcf074d32015-10-16 18:02:43 -06002442 for (i = 0; i < demo->swapchainImageCount; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07002443 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
Karl Schultze4dc75c2016-02-02 15:37:51 -07002444 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1,
Tony Barboura72d9492017-01-11 13:35:09 -07002445 &demo->swapchain_image_resources[i].cmd);
2446 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2447 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2448 vkDestroyFence(demo->device, demo->swapchain_image_resources[i].fence, NULL);
Ian Elliottcf074d32015-10-16 18:02:43 -06002449 }
Chia-I Wu63636062015-10-26 21:10:41 +08002450 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
Tony Barbour22e06272016-09-07 16:07:56 -06002451 if (demo->separate_present_queue) {
2452 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2453 }
Tony Barboura72d9492017-01-11 13:35:09 -07002454 free(demo->swapchain_image_resources);
Ian Elliottcf074d32015-10-16 18:02:43 -06002455
Ian Elliottcf074d32015-10-16 18:02:43 -06002456 // Second, re-perform the demo_prepare() function, which will re-create the
2457 // swapchain:
2458 demo_prepare(demo);
2459}
2460
David Pinedo2bb7c932015-06-18 17:03:14 -06002461// On MS-Windows, make this a global, so it's available to WndProc()
2462struct demo demo;
2463
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002464#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07002465static void demo_run(struct demo *demo) {
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002466 if (!demo->prepared)
2467 return;
Tony Barbource7524e2016-07-28 12:01:45 -06002468
Ian Elliott639ca472015-04-16 15:23:05 -06002469 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002470 demo->curFrame++;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002471 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) {
Karl Schultz8a663912016-03-24 18:43:41 -06002472 PostQuitMessage(validation_error);
David Pinedo2bb7c932015-06-18 17:03:14 -06002473 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002474}
Ian Elliott639ca472015-04-16 15:23:05 -06002475
2476// MS-Windows event handling function:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002477LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2478 switch (uMsg) {
Ian Elliottaaae5352015-07-06 14:27:58 -06002479 case WM_CLOSE:
Karl Schultz0218c002016-03-22 17:06:13 -06002480 PostQuitMessage(validation_error);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002481 break;
Ian Elliottaaae5352015-07-06 14:27:58 -06002482 case WM_PAINT:
Tony Barbour602ca4f2016-09-12 14:02:43 -06002483 // The validation callback calls MessageBox which can generate paint
2484 // events - don't make more Vulkan calls if we got here from the
2485 // callback
2486 if (!in_callback) {
2487 demo_run(&demo);
2488 }
Tony Barbourc8a59892015-10-20 12:49:46 -06002489 break;
Rene Lindsayb9403da2016-07-01 18:00:30 -06002490 case WM_GETMINMAXINFO: // set window's minimum size
2491 ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize;
2492 return 0;
Ian Elliott5ef45e92015-10-21 15:14:07 -06002493 case WM_SIZE:
Piers Daniellc0b6e432016-02-18 10:54:15 -07002494 // Resize the application to the new window size, except when
2495 // it was minimized. Vulkan doesn't support images or swapchains
2496 // with width=0 and height=0.
2497 if (wParam != SIZE_MINIMIZED) {
2498 demo.width = lParam & 0xffff;
Tony Barbourb3237202016-08-10 13:39:36 -06002499 demo.height = (lParam & 0xffff0000) >> 16;
Piers Daniellc0b6e432016-02-18 10:54:15 -07002500 demo_resize(&demo);
2501 }
Ian Elliott5ef45e92015-10-21 15:14:07 -06002502 break;
Ian Elliott639ca472015-04-16 15:23:05 -06002503 default:
2504 break;
2505 }
2506 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2507}
2508
Karl Schultze4dc75c2016-02-02 15:37:51 -07002509static void demo_create_window(struct demo *demo) {
2510 WNDCLASSEX win_class;
Ian Elliott639ca472015-04-16 15:23:05 -06002511
2512 // Initialize the window class structure:
2513 win_class.cbSize = sizeof(WNDCLASSEX);
2514 win_class.style = CS_HREDRAW | CS_VREDRAW;
2515 win_class.lpfnWndProc = WndProc;
2516 win_class.cbClsExtra = 0;
2517 win_class.cbWndExtra = 0;
2518 win_class.hInstance = demo->connection; // hInstance
2519 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2520 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2521 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2522 win_class.lpszMenuName = NULL;
2523 win_class.lpszClassName = demo->name;
2524 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2525 // Register window class:
2526 if (!RegisterClassEx(&win_class)) {
2527 // It didn't work, so try to give a useful error:
2528 printf("Unexpected error trying to start the application!\n");
2529 fflush(stdout);
2530 exit(1);
2531 }
2532 // Create window with the registered class:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002533 RECT wr = {0, 0, demo->width, demo->height};
Mike Stroyanb46779e2015-06-15 14:20:13 -06002534 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06002535 demo->window = CreateWindowEx(0,
2536 demo->name, // class name
2537 demo->name, // app name
2538 WS_OVERLAPPEDWINDOW | // window style
Karl Schultze4dc75c2016-02-02 15:37:51 -07002539 WS_VISIBLE | WS_SYSMENU,
2540 100, 100, // x/y coords
2541 wr.right - wr.left, // width
2542 wr.bottom - wr.top, // height
2543 NULL, // handle to parent
2544 NULL, // handle to menu
2545 demo->connection, // hInstance
2546 NULL); // no extra parameters
Ian Elliott639ca472015-04-16 15:23:05 -06002547 if (!demo->window) {
2548 // It didn't work, so try to give a useful error:
2549 printf("Cannot create a window in which to draw!\n");
2550 fflush(stdout);
2551 exit(1);
2552 }
Rene Lindsayb9403da2016-07-01 18:00:30 -06002553 // Window client area size must be at least 1 pixel high, to prevent crash.
2554 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2555 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1;
Ian Elliott639ca472015-04-16 15:23:05 -06002556}
Tony Barbour8295ac42016-08-08 11:04:17 -06002557#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002558static void demo_create_xlib_window(struct demo *demo) {
2559
Tony Barbouree9cd372017-01-31 16:09:58 -07002560 XInitThreads();
Tony Barbour2593b182016-04-19 10:57:58 -06002561 demo->display = XOpenDisplay(NULL);
2562 long visualMask = VisualScreenMask;
2563 int numberOfVisuals;
Rene Lindsay11612722016-06-13 17:20:39 -06002564 XVisualInfo vInfoTemplate={};
Tony Barbour2593b182016-04-19 10:57:58 -06002565 vInfoTemplate.screen = DefaultScreen(demo->display);
2566 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask,
2567 &vInfoTemplate, &numberOfVisuals);
2568
2569 Colormap colormap = XCreateColormap(
2570 demo->display, RootWindow(demo->display, vInfoTemplate.screen),
2571 visualInfo->visual, AllocNone);
2572
Rene Lindsay11612722016-06-13 17:20:39 -06002573 XSetWindowAttributes windowAttributes={};
Tony Barbour2593b182016-04-19 10:57:58 -06002574 windowAttributes.colormap = colormap;
2575 windowAttributes.background_pixel = 0xFFFFFFFF;
2576 windowAttributes.border_pixel = 0;
2577 windowAttributes.event_mask =
2578 KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2579
2580 demo->xlib_window = XCreateWindow(
2581 demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0,
2582 demo->width, demo->height, 0, visualInfo->depth, InputOutput,
2583 visualInfo->visual,
2584 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2585
2586 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2587 XMapWindow(demo->display, demo->xlib_window);
2588 XFlush(demo->display);
2589 demo->xlib_wm_delete_window =
2590 XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2591}
2592static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2593 switch(event->type) {
2594 case ClientMessage:
2595 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window)
2596 demo->quit = true;
2597 break;
2598 case KeyPress:
2599 switch (event->xkey.keycode) {
2600 case 0x9: // Escape
2601 demo->quit = true;
2602 break;
2603 case 0x71: // left arrow key
Tony Barbour2593b182016-04-19 10:57:58 -06002604 demo->spin_angle -= demo->spin_increment;
2605 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002606 case 0x72: // right arrow key
2607 demo->spin_angle += demo->spin_increment;
2608 break;
2609 case 0x41: // space bar
Tony Barbour2593b182016-04-19 10:57:58 -06002610 demo->pause = !demo->pause;
2611 break;
2612 }
2613 break;
2614 case ConfigureNotify:
2615 if ((demo->width != event->xconfigure.width) ||
2616 (demo->height != event->xconfigure.height)) {
2617 demo->width = event->xconfigure.width;
2618 demo->height = event->xconfigure.height;
2619 demo_resize(demo);
2620 }
2621 break;
2622 default:
2623 break;
2624 }
2625
2626}
2627
2628static void demo_run_xlib(struct demo *demo) {
2629
2630 while (!demo->quit) {
2631 XEvent event;
2632
2633 if (demo->pause) {
2634 XNextEvent(demo->display, &event);
2635 demo_handle_xlib_event(demo, &event);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002636 }
2637 while (XPending(demo->display) > 0) {
2638 XNextEvent(demo->display, &event);
2639 demo_handle_xlib_event(demo, &event);
Tony Barbour2593b182016-04-19 10:57:58 -06002640 }
2641
Tony Barbour2593b182016-04-19 10:57:58 -06002642 demo_draw(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06002643 demo->curFrame++;
2644 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2645 demo->quit = true;
2646 }
2647}
Tony Barbour153cb062016-12-07 13:43:36 -07002648#elif defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06002649static void demo_handle_xcb_event(struct demo *demo,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002650 const xcb_generic_event_t *event) {
Piers Daniell735ee532015-02-23 16:23:13 -07002651 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002652 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002653 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002654 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002655 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002656 case XCB_CLIENT_MESSAGE:
Karl Schultze4dc75c2016-02-02 15:37:51 -07002657 if ((*(xcb_client_message_event_t *)event).data.data32[0] ==
2658 (*demo->atom_wm_delete_window).atom) {
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002659 demo->quit = true;
2660 }
2661 break;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002662 case XCB_KEY_RELEASE: {
2663 const xcb_key_release_event_t *key =
2664 (const xcb_key_release_event_t *)event;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002665
Karl Schultze4dc75c2016-02-02 15:37:51 -07002666 switch (key->detail) {
2667 case 0x9: // Escape
2668 demo->quit = true;
2669 break;
2670 case 0x71: // left arrow key
Karl Schultze4dc75c2016-02-02 15:37:51 -07002671 demo->spin_angle -= demo->spin_increment;
2672 break;
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002673 case 0x72: // right arrow key
2674 demo->spin_angle += demo->spin_increment;
2675 break;
2676 case 0x41: // space bar
Karl Schultze4dc75c2016-02-02 15:37:51 -07002677 demo->pause = !demo->pause;
2678 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002679 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002680 } break;
2681 case XCB_CONFIGURE_NOTIFY: {
2682 const xcb_configure_notify_event_t *cfg =
2683 (const xcb_configure_notify_event_t *)event;
2684 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
Damien Leone745a0b42016-01-26 14:34:12 -08002685 demo->width = cfg->width;
2686 demo->height = cfg->height;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002687 demo_resize(demo);
Ian Elliottcf074d32015-10-16 18:02:43 -06002688 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07002689 } break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002690 default:
2691 break;
2692 }
2693}
2694
Tony Barbour2593b182016-04-19 10:57:58 -06002695static void demo_run_xcb(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002696 xcb_flush(demo->connection);
2697
2698 while (!demo->quit) {
2699 xcb_generic_event_t *event;
2700
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002701 if (demo->pause) {
2702 event = xcb_wait_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002703 }
2704 else {
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002705 event = xcb_poll_for_event(demo->connection);
Karl Schultz6ddf1e02017-01-04 10:31:20 -07002706 }
2707 while (event) {
2708 demo_handle_xcb_event(demo, event);
2709 free(event);
2710 event = xcb_poll_for_event(demo->connection);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002711 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07002712
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07002713 demo_draw(demo);
David Pinedo2bb7c932015-06-18 17:03:14 -06002714 demo->curFrame++;
Tony Barbour1a86d032015-09-21 15:17:33 -06002715 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
David Pinedo2bb7c932015-06-18 17:03:14 -06002716 demo->quit = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002717 }
2718}
2719
Tony Barbour2593b182016-04-19 10:57:58 -06002720static void demo_create_xcb_window(struct demo *demo) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002721 uint32_t value_mask, value_list[32];
2722
Tony Barbour2593b182016-04-19 10:57:58 -06002723 demo->xcb_window = xcb_generate_id(demo->connection);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002724
2725 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2726 value_list[0] = demo->screen->black_pixel;
Karl Schultze4dc75c2016-02-02 15:37:51 -07002727 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
Ian Elliottcf074d32015-10-16 18:02:43 -06002728 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002729
Tony Barbour2593b182016-04-19 10:57:58 -06002730 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002731 demo->screen->root, 0, 0, demo->width, demo->height, 0,
2732 XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual,
2733 value_mask, value_list);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002734
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002735 /* Magic code that will send notification when window is destroyed */
Karl Schultze4dc75c2016-02-02 15:37:51 -07002736 xcb_intern_atom_cookie_t cookie =
2737 xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2738 xcb_intern_atom_reply_t *reply =
2739 xcb_intern_atom_reply(demo->connection, cookie, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002740
Karl Schultze4dc75c2016-02-02 15:37:51 -07002741 xcb_intern_atom_cookie_t cookie2 =
2742 xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2743 demo->atom_wm_delete_window =
2744 xcb_intern_atom_reply(demo->connection, cookie2, 0);
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002745
Tony Barbour2593b182016-04-19 10:57:58 -06002746 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002747 (*reply).atom, 4, 32, 1,
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07002748 &(*demo->atom_wm_delete_window).atom);
2749 free(reply);
2750
Tony Barbour2593b182016-04-19 10:57:58 -06002751 xcb_map_window(demo->connection, demo->xcb_window);
David Pinedo2bb7c932015-06-18 17:03:14 -06002752
Karl Schultze4dc75c2016-02-02 15:37:51 -07002753 // Force the x/y coordinates to 100,100 results are identical in consecutive
2754 // runs
2755 const uint32_t coords[] = {100, 100};
Tony Barbour2593b182016-04-19 10:57:58 -06002756 xcb_configure_window(demo->connection, demo->xcb_window,
David Pinedo2bb7c932015-06-18 17:03:14 -06002757 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002758}
Tony Barbour6b131662016-08-25 13:14:45 -06002759// VK_USE_PLATFORM_XCB_KHR
2760#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002761static void demo_run(struct demo *demo) {
2762 while (!demo->quit) {
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002763 demo_draw(demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09002764 demo->curFrame++;
2765 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount)
2766 demo->quit = true;
2767 }
2768}
2769
2770static void handle_ping(void *data UNUSED,
2771 struct wl_shell_surface *shell_surface,
2772 uint32_t serial) {
2773 wl_shell_surface_pong(shell_surface, serial);
2774}
2775
2776static void handle_configure(void *data UNUSED,
2777 struct wl_shell_surface *shell_surface UNUSED,
2778 uint32_t edges UNUSED, int32_t width UNUSED,
2779 int32_t height UNUSED) {}
2780
2781static void handle_popup_done(void *data UNUSED,
2782 struct wl_shell_surface *shell_surface UNUSED) {}
2783
2784static const struct wl_shell_surface_listener shell_surface_listener = {
2785 handle_ping, handle_configure, handle_popup_done};
2786
2787static void demo_create_window(struct demo *demo) {
2788 demo->window = wl_compositor_create_surface(demo->compositor);
2789 if (!demo->window) {
2790 printf("Can not create wayland_surface from compositor!\n");
2791 fflush(stdout);
2792 exit(1);
2793 }
2794
2795 demo->shell_surface = wl_shell_get_shell_surface(demo->shell, demo->window);
2796 if (!demo->shell_surface) {
2797 printf("Can not get shell_surface from wayland_surface!\n");
2798 fflush(stdout);
2799 exit(1);
2800 }
2801 wl_shell_surface_add_listener(demo->shell_surface, &shell_surface_listener,
2802 demo);
2803 wl_shell_surface_set_toplevel(demo->shell_surface);
2804 wl_shell_surface_set_title(demo->shell_surface, APP_SHORT_NAME);
2805}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002806#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
2807static void demo_run(struct demo *demo) {
2808 if (!demo->prepared)
2809 return;
2810
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002811 demo_draw(demo);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002812 demo->curFrame++;
2813}
Tony Barbourefd0c5a2016-12-07 14:45:12 -07002814#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07002815#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
2816static VkResult demo_create_display_surface(struct demo *demo) {
2817 VkResult U_ASSERT_ONLY err;
2818 uint32_t display_count;
2819 uint32_t mode_count;
2820 uint32_t plane_count;
2821 VkDisplayPropertiesKHR display_props;
2822 VkDisplayKHR display;
2823 VkDisplayModePropertiesKHR mode_props;
2824 VkDisplayPlanePropertiesKHR *plane_props;
2825 VkBool32 found_plane = VK_FALSE;
2826 uint32_t plane_index;
2827 VkExtent2D image_extent;
2828 VkDisplaySurfaceCreateInfoKHR create_info;
2829
2830 // Get the first display
2831 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, NULL);
2832 assert(!err);
2833
2834 if (display_count == 0) {
2835 printf("Cannot find any display!\n");
2836 fflush(stdout);
2837 exit(1);
2838 }
2839
2840 display_count = 1;
2841 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2842 assert(!err || (err == VK_INCOMPLETE));
2843
2844 display = display_props.display;
2845
2846 // Get the first mode of the display
2847 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2848 assert(!err);
2849
2850 if (mode_count == 0) {
2851 printf("Cannot find any mode for the display!\n");
2852 fflush(stdout);
2853 exit(1);
2854 }
2855
2856 mode_count = 1;
2857 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2858 assert(!err || (err == VK_INCOMPLETE));
2859
2860 // Get the list of planes
2861 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2862 assert(!err);
2863
2864 if (plane_count == 0) {
2865 printf("Cannot find any plane!\n");
2866 fflush(stdout);
2867 exit(1);
2868 }
2869
2870 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2871 assert(plane_props);
2872
2873 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2874 assert(!err);
2875
2876 // Find a plane compatible with the display
2877 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2878 uint32_t supported_count;
2879 VkDisplayKHR *supported_displays;
2880
2881 // Disqualify planes that are bound to a different display
2882 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) &&
2883 (plane_props[plane_index].currentDisplay != display)) {
2884 continue;
2885 }
2886
2887 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
2888 assert(!err);
2889
2890 if (supported_count == 0) {
2891 continue;
2892 }
2893
2894 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
2895 assert(supported_displays);
2896
2897 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
2898 assert(!err);
2899
2900 for (uint32_t i = 0; i < supported_count; i++) {
2901 if (supported_displays[i] == display) {
2902 found_plane = VK_TRUE;
2903 break;
2904 }
2905 }
2906
2907 free(supported_displays);
2908
2909 if (found_plane) {
2910 break;
2911 }
2912 }
2913
2914 if (!found_plane) {
2915 printf("Cannot find a plane compatible with the display!\n");
2916 fflush(stdout);
2917 exit(1);
2918 }
2919
2920 free(plane_props);
2921
Tony Barbour820b55b2017-03-14 08:55:46 -06002922 VkDisplayPlaneCapabilitiesKHR planeCaps;
2923 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
2924 // Find a supported alpha mode
2925 VkCompositeAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
2926 VkCompositeAlphaFlagBitsKHR alphaModes[4] = {
2927 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
2928 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
2929 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
2930 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
2931 };
2932 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
2933 if (planeCaps.supportedAlpha & alphaModes[i]) {
2934 alphaMode = alphaModes[i];
2935 break;
2936 }
2937 }
Damien Leone600c3052017-01-31 10:26:07 -07002938 image_extent.width = mode_props.parameters.visibleRegion.width;
2939 image_extent.height = mode_props.parameters.visibleRegion.height;
2940
2941 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
2942 create_info.pNext = NULL;
2943 create_info.flags = 0;
2944 create_info.displayMode = mode_props.displayMode;
2945 create_info.planeIndex = plane_index;
2946 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
2947 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
Tony Barbour820b55b2017-03-14 08:55:46 -06002948 create_info.alphaMode = alphaMode;
Damien Leone600c3052017-01-31 10:26:07 -07002949 create_info.globalAlpha = 1.0f;
2950 create_info.imageExtent = image_extent;
2951
2952 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
2953}
2954
2955static void demo_run_display(struct demo *demo)
2956{
2957 while (!demo->quit) {
2958 demo_draw(demo);
2959 demo->curFrame++;
2960
2961 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2962 demo->quit = true;
2963 }
2964 }
2965}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05002966#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002967
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002968/*
2969 * Return 1 (true) if all layer names specified in check_names
2970 * can be found in given layer properties.
2971 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002972static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Karl Schultze4dc75c2016-02-02 15:37:51 -07002973 uint32_t layer_count,
2974 VkLayerProperties *layers) {
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002975 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002976 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002977 for (uint32_t j = 0; j < layer_count; j++) {
2978 if (!strcmp(check_names[i], layers[j].layerName)) {
2979 found = 1;
Dustin Graves7c287352016-01-27 13:48:06 -07002980 break;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002981 }
2982 }
2983 if (!found) {
2984 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
2985 return 0;
2986 }
2987 }
2988 return 1;
2989}
2990
Karl Schultze4dc75c2016-02-02 15:37:51 -07002991static void demo_init_vk(struct demo *demo) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002992 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002993 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002994 uint32_t instance_layer_count = 0;
Karl Schultz5b423ea2016-06-20 19:08:43 -06002995 uint32_t validation_layer_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06002996 char **instance_validation_layers = NULL;
2997 demo->enabled_extension_count = 0;
2998 demo->enabled_layer_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002999
Karl Schultz7c50ad62016-04-01 12:07:03 -06003000 char *instance_validation_layers_alt1[] = {
3001 "VK_LAYER_LUNARG_standard_validation"
3002 };
3003
3004 char *instance_validation_layers_alt2[] = {
Mark Lobodzinski19ed2db2017-02-15 14:43:21 -07003005 "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation",
3006 "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation",
3007 "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects"
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003008 };
3009
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003010 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06003011 VkBool32 validation_found = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003012 if (demo->validate) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003013
Karl Schultz7c50ad62016-04-01 12:07:03 -06003014 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
Dustin Graves7c287352016-01-27 13:48:06 -07003015 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06003016
Karl Schultz7c50ad62016-04-01 12:07:03 -06003017 instance_validation_layers = instance_validation_layers_alt1;
3018 if (instance_layer_count > 0) {
3019 VkLayerProperties *instance_layers =
3020 malloc(sizeof (VkLayerProperties) * instance_layer_count);
3021 err = vkEnumerateInstanceLayerProperties(&instance_layer_count,
3022 instance_layers);
3023 assert(!err);
Dustin Graves7c287352016-01-27 13:48:06 -07003024
Karl Schultz7c50ad62016-04-01 12:07:03 -06003025
3026 validation_found = demo_check_layers(
3027 ARRAY_SIZE(instance_validation_layers_alt1),
3028 instance_validation_layers, instance_layer_count,
3029 instance_layers);
3030 if (validation_found) {
3031 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003032 demo->enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation";
3033 validation_layer_count = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003034 } else {
3035 // use alternative set of validation layers
3036 instance_validation_layers = instance_validation_layers_alt2;
3037 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2);
3038 validation_found = demo_check_layers(
3039 ARRAY_SIZE(instance_validation_layers_alt2),
3040 instance_validation_layers, instance_layer_count,
3041 instance_layers);
Karl Schultz5b423ea2016-06-20 19:08:43 -06003042 validation_layer_count =
3043 ARRAY_SIZE(instance_validation_layers_alt2);
3044 for (uint32_t i = 0; i < validation_layer_count; i++) {
3045 demo->enabled_layers[i] = instance_validation_layers[i];
Karl Schultz7c50ad62016-04-01 12:07:03 -06003046 }
3047 }
3048 free(instance_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003049 }
Dustin Graves7c287352016-01-27 13:48:06 -07003050
Karl Schultz7c50ad62016-04-01 12:07:03 -06003051 if (!validation_found) {
Karl Schultzad7bf022016-04-07 09:40:30 -06003052 ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find "
Karl Schultz7c50ad62016-04-01 12:07:03 -06003053 "required validation layer.\n\n"
3054 "Please look at the Getting Started guide for additional "
3055 "information.\n",
3056 "vkCreateInstance Failure");
3057 }
Dustin Graves7c287352016-01-27 13:48:06 -07003058 }
3059
3060 /* Look for instance extensions */
3061 VkBool32 surfaceExtFound = 0;
3062 VkBool32 platformSurfaceExtFound = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003063 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003064
Karl Schultze4dc75c2016-02-02 15:37:51 -07003065 err = vkEnumerateInstanceExtensionProperties(
3066 NULL, &instance_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003067 assert(!err);
3068
Dustin Graves7c287352016-01-27 13:48:06 -07003069 if (instance_extension_count > 0) {
3070 VkExtensionProperties *instance_extensions =
3071 malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3072 err = vkEnumerateInstanceExtensionProperties(
3073 NULL, &instance_extension_count, instance_extensions);
3074 assert(!err);
3075 for (uint32_t i = 0; i < instance_extension_count; i++) {
3076 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME,
3077 instance_extensions[i].extensionName)) {
3078 surfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003079 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003080 VK_KHR_SURFACE_EXTENSION_NAME;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003081 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003082#if defined(VK_USE_PLATFORM_WIN32_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003083 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
3084 instance_extensions[i].extensionName)) {
3085 platformSurfaceExtFound = 1;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003086 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003087 VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
3088 }
Tony Barbour153cb062016-12-07 13:43:36 -07003089#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003090 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
3091 instance_extensions[i].extensionName)) {
3092 platformSurfaceExtFound = 1;
Tony Barbour2593b182016-04-19 10:57:58 -06003093 demo->extension_names[demo->enabled_extension_count++] =
3094 VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
3095 }
Tony Barbour153cb062016-12-07 13:43:36 -07003096#elif defined(VK_USE_PLATFORM_XCB_KHR)
Dustin Graves7c287352016-01-27 13:48:06 -07003097 if (!strcmp(VK_KHR_XCB_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_XCB_SURFACE_EXTENSION_NAME;
3102 }
Tony Barbour153cb062016-12-07 13:43:36 -07003103#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003104 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
3105 instance_extensions[i].extensionName)) {
3106 platformSurfaceExtFound = 1;
3107 demo->extension_names[demo->enabled_extension_count++] =
3108 VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
3109 }
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003110#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003111#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3112 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME,
3113 instance_extensions[i].extensionName)) {
3114 platformSurfaceExtFound = 1;
3115 demo->extension_names[demo->enabled_extension_count++] =
3116 VK_KHR_DISPLAY_EXTENSION_NAME;
3117 }
Tony Barbour153cb062016-12-07 13:43:36 -07003118#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003119 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
3120 instance_extensions[i].extensionName)) {
3121 platformSurfaceExtFound = 1;
3122 demo->extension_names[demo->enabled_extension_count++] =
3123 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
3124 }
Bill Hollingsf4352142017-02-14 22:58:56 -05003125#elif defined(VK_USE_PLATFORM_IOS_MVK)
3126 if (!strcmp(VK_MVK_IOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3127 platformSurfaceExtFound = 1;
3128 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_IOS_SURFACE_EXTENSION_NAME;
3129 }
3130#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3131 if (!strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3132 platformSurfaceExtFound = 1;
3133 demo->extension_names[demo->enabled_extension_count++] = VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
3134 }
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003135#endif
Dustin Graves7c287352016-01-27 13:48:06 -07003136 if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
3137 instance_extensions[i].extensionName)) {
3138 if (demo->validate) {
Karl Schultz7c50ad62016-04-01 12:07:03 -06003139 demo->extension_names[demo->enabled_extension_count++] =
Dustin Graves7c287352016-01-27 13:48:06 -07003140 VK_EXT_DEBUG_REPORT_EXTENSION_NAME;
3141 }
3142 }
Karl Schultz7c50ad62016-04-01 12:07:03 -06003143 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003144 }
Dustin Graves7c287352016-01-27 13:48:06 -07003145
3146 free(instance_extensions);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06003147 }
Dustin Graves7c287352016-01-27 13:48:06 -07003148
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003149 if (!surfaceExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003150 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3151 "the " VK_KHR_SURFACE_EXTENSION_NAME
3152 " extension.\n\nDo you have a compatible "
Ian Elliott65152912015-04-28 13:22:33 -06003153 "Vulkan installable client driver (ICD) installed?\nPlease "
3154 "look at the Getting Started guide for additional "
3155 "information.\n",
3156 "vkCreateInstance Failure");
3157 }
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003158 if (!platformSurfaceExtFound) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003159#if defined(VK_USE_PLATFORM_WIN32_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003160 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3161 "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3162 " extension.\n\nDo you have a compatible "
Ian Elliottd3f7dcc2015-11-20 13:08:34 -07003163 "Vulkan installable client driver (ICD) installed?\nPlease "
3164 "look at the Getting Started guide for additional "
3165 "information.\n",
3166 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003167#elif defined(VK_USE_PLATFORM_IOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003168 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3169 VK_MVK_IOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3170 "Vulkan installable client driver (ICD) installed?\nPlease "
3171 "look at the Getting Started guide for additional "
3172 "information.\n",
3173 "vkCreateInstance Failure");
Bill Hollingsf4352142017-02-14 22:58:56 -05003174#elif defined(VK_USE_PLATFORM_MACOS_MVK)
Tony Barbourc65cd752017-03-13 15:29:35 -06003175 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the "
3176 VK_MVK_MACOS_SURFACE_EXTENSION_NAME" extension.\n\nDo you have a compatible "
3177 "Vulkan installable client driver (ICD) installed?\nPlease "
3178 "look at the Getting Started guide for additional "
3179 "information.\n",
3180 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003181#elif defined(VK_USE_PLATFORM_XCB_KHR)
Karl Schultze4dc75c2016-02-02 15:37:51 -07003182 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3183 "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3184 " extension.\n\nDo you have a compatible "
Mark Lobodzinski6f7a6032015-11-24 12:04:15 -07003185 "Vulkan installable client driver (ICD) installed?\nPlease "
3186 "look at the Getting Started guide for additional "
3187 "information.\n",
3188 "vkCreateInstance Failure");
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003189#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3190 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3191 "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3192 " extension.\n\nDo you have a compatible "
3193 "Vulkan installable client driver (ICD) installed?\nPlease "
3194 "look at the Getting Started guide for additional "
3195 "information.\n",
3196 "vkCreateInstance Failure");
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003197#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07003198#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3199 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3200 "the " VK_KHR_DISPLAY_EXTENSION_NAME
3201 " extension.\n\nDo you have a compatible "
3202 "Vulkan installable client driver (ICD) installed?\nPlease "
3203 "look at the Getting Started guide for additional "
3204 "information.\n",
3205 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003206#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3207 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3208 "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3209 " extension.\n\nDo you have a compatible "
3210 "Vulkan installable client driver (ICD) installed?\nPlease "
3211 "look at the Getting Started guide for additional "
3212 "information.\n",
3213 "vkCreateInstance Failure");
Tony Barbour153cb062016-12-07 13:43:36 -07003214#elif defined(VK_USE_PLATFORM_XLIB_KHR)
Tony Barbour2593b182016-04-19 10:57:58 -06003215 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find "
3216 "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3217 " extension.\n\nDo you have a compatible "
3218 "Vulkan installable client driver (ICD) installed?\nPlease "
3219 "look at the Getting Started guide for additional "
3220 "information.\n",
3221 "vkCreateInstance Failure");
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003222#endif
Tony Barbour153cb062016-12-07 13:43:36 -07003223 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06003224 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003225 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003226 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003227 .pApplicationName = APP_SHORT_NAME,
3228 .applicationVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06003229 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003230 .engineVersion = 0,
Jon Ashburn7f4bc2c2016-03-22 13:57:46 -06003231 .apiVersion = VK_API_VERSION_1_0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003232 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003233 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003234 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06003235 .pNext = NULL,
Chia-I Wua8fe78a2015-10-27 18:04:07 +08003236 .pApplicationInfo = &app,
Karl Schultz7c50ad62016-04-01 12:07:03 -06003237 .enabledLayerCount = demo->enabled_layer_count,
3238 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3239 .enabledExtensionCount = demo->enabled_extension_count,
3240 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06003241 };
Karl Schultzcd9887d2016-03-25 14:25:16 -06003242
3243 /*
3244 * This is info for a temp callback to use during CreateInstance.
3245 * After the instance is created, we use the instance-based
3246 * function to register the final callback.
3247 */
Karl Schultza2615462017-01-20 13:19:20 -07003248 VkDebugReportCallbackCreateInfoEXT dbgCreateInfoTemp;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003249 VkValidationFlagsEXT val_flags;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003250 if (demo->validate) {
Karl Schultza2615462017-01-20 13:19:20 -07003251 dbgCreateInfoTemp.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
3252 dbgCreateInfoTemp.pNext = NULL;
3253 dbgCreateInfoTemp.pfnCallback = demo->use_break ? BreakCallback : dbgFunc;
3254 dbgCreateInfoTemp.pUserData = demo;
3255 dbgCreateInfoTemp.flags =
Ian Elliott5959bbd2016-03-25 09:07:19 -06003256 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003257 if (demo->validate_checks_disabled) {
3258 val_flags.sType = VK_STRUCTURE_TYPE_VALIDATION_FLAGS_EXT;
3259 val_flags.pNext = NULL;
3260 val_flags.disabledValidationCheckCount = 1;
3261 VkValidationCheckEXT disabled_check = VK_VALIDATION_CHECK_ALL_EXT;
3262 val_flags.pDisabledValidationChecks = &disabled_check;
3263 dbgCreateInfoTemp.pNext = (void*)&val_flags;
3264 }
Karl Schultza2615462017-01-20 13:19:20 -07003265 inst_info.pNext = &dbgCreateInfoTemp;
Ian Elliott5959bbd2016-03-25 09:07:19 -06003266 }
Ian Elliott097d9f32015-04-28 11:35:02 -06003267
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06003268 uint32_t gpu_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003269
Chia-I Wu63636062015-10-26 21:10:41 +08003270 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06003271 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
3272 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06003273 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06003274 "additional information.\n",
3275 "vkCreateInstance Failure");
Courtney Goeltzenleuchter1e47e4b2015-09-14 18:01:17 -06003276 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003277 ERR_EXIT("Cannot find a specified extension library"
Dustin Graves7c287352016-01-27 13:48:06 -07003278 ".\nMake sure your layers path is set appropriately.\n",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003279 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06003280 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06003281 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
3282 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06003283 "the Getting Started guide for additional information.\n",
3284 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06003285 }
Jon Ashburnab46b362015-04-04 14:52:07 -06003286
Tobin Ehlis8a724d82015-09-07 15:16:39 -06003287 /* Make initial call to query gpu_count, then second call for gpu info*/
3288 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3289 assert(!err && gpu_count > 0);
Dustin Graves7c287352016-01-27 13:48:06 -07003290
3291 if (gpu_count > 0) {
3292 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3293 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3294 assert(!err);
3295 /* For cube demo we just grab the first physical device */
3296 demo->gpu = physical_devices[0];
3297 free(physical_devices);
3298 } else {
3299 ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3300 "Do you have a compatible Vulkan installable client driver (ICD) "
3301 "installed?\nPlease look at the Getting Started guide for "
3302 "additional information.\n",
3303 "vkEnumeratePhysicalDevices Failure");
3304 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003305
Dustin Graves7c287352016-01-27 13:48:06 -07003306 /* Look for device extensions */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003307 uint32_t device_extension_count = 0;
Dustin Graves7c287352016-01-27 13:48:06 -07003308 VkBool32 swapchainExtFound = 0;
3309 demo->enabled_extension_count = 0;
Karl Schultz7c50ad62016-04-01 12:07:03 -06003310 memset(demo->extension_names, 0, sizeof(demo->extension_names));
Dustin Graves7c287352016-01-27 13:48:06 -07003311
Karl Schultze4dc75c2016-02-02 15:37:51 -07003312 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL,
3313 &device_extension_count, NULL);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003314 assert(!err);
3315
Dustin Graves7c287352016-01-27 13:48:06 -07003316 if (device_extension_count > 0) {
3317 VkExtensionProperties *device_extensions =
3318 malloc(sizeof(VkExtensionProperties) * device_extension_count);
3319 err = vkEnumerateDeviceExtensionProperties(
3320 demo->gpu, NULL, &device_extension_count, device_extensions);
3321 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003322
Dustin Graves7c287352016-01-27 13:48:06 -07003323 for (uint32_t i = 0; i < device_extension_count; i++) {
3324 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME,
3325 device_extensions[i].extensionName)) {
3326 swapchainExtFound = 1;
3327 demo->extension_names[demo->enabled_extension_count++] =
3328 VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3329 }
3330 assert(demo->enabled_extension_count < 64);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003331 }
Dustin Graves7c287352016-01-27 13:48:06 -07003332
Ian Elliottd940ca22017-03-22 08:31:27 -06003333 if (demo->VK_GOOGLE_display_timing_enabled) {
3334 // Even though the user "enabled" the extension via the command
3335 // line, we must make sure that it's enumerated for use with the
3336 // device. Therefore, disable it here, and re-enable it again if
3337 // enumerated.
3338 demo->VK_GOOGLE_display_timing_enabled = false;
3339 for (uint32_t i = 0; i < device_extension_count; i++) {
3340 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
3341 device_extensions[i].extensionName)) {
3342 demo->extension_names[demo->enabled_extension_count++] =
3343 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
3344 demo->VK_GOOGLE_display_timing_enabled = true;
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003345 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003346 }
3347 assert(demo->enabled_extension_count < 64);
3348 }
3349 if (!demo->VK_GOOGLE_display_timing_enabled) {
Ian Elliott0c7b3c92017-03-27 14:38:52 -06003350 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
Ian Elliottd940ca22017-03-22 08:31:27 -06003351 }
3352 }
3353
Dustin Graves7c287352016-01-27 13:48:06 -07003354 free(device_extensions);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003355 }
Dustin Graves7c287352016-01-27 13:48:06 -07003356
Tony Barbourcc69f452015-10-08 13:59:42 -06003357 if (!swapchainExtFound) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003358 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find "
3359 "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3360 " extension.\n\nDo you have a compatible "
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06003361 "Vulkan installable client driver (ICD) installed?\nPlease "
3362 "look at the Getting Started guide for additional "
3363 "information.\n",
3364 "vkCreateInstance Failure");
3365 }
3366
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003367 if (demo->validate) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003368 demo->CreateDebugReportCallback =
3369 (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(
3370 demo->inst, "vkCreateDebugReportCallbackEXT");
3371 demo->DestroyDebugReportCallback =
3372 (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(
3373 demo->inst, "vkDestroyDebugReportCallbackEXT");
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003374 if (!demo->CreateDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003375 ERR_EXIT(
3376 "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n",
3377 "vkGetProcAddr Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003378 }
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003379 if (!demo->DestroyDebugReportCallback) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003380 ERR_EXIT(
3381 "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n",
3382 "vkGetProcAddr Failure");
Tony Barbourd70b42c2015-06-30 14:14:19 -06003383 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003384 demo->DebugReportMessage =
3385 (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr(
3386 demo->inst, "vkDebugReportMessageEXT");
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003387 if (!demo->DebugReportMessage) {
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003388 ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n",
Courtney Goeltzenleuchteraf1951c2015-12-01 14:11:38 -07003389 "vkGetProcAddr Failure");
3390 }
Jon Ashburn9549c8e2015-12-15 08:47:46 -07003391
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003392 VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
Karl Schultzcd9887d2016-03-25 14:25:16 -06003393 PFN_vkDebugReportCallbackEXT callback;
3394 callback = demo->use_break ? BreakCallback : dbgFunc;
Courtney Goeltzenleuchter0f060df2015-12-09 15:48:16 -07003395 dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003396 dbgCreateInfo.pNext = NULL;
3397 dbgCreateInfo.pfnCallback = callback;
lenny-lunargfbe22392016-06-06 11:07:53 -06003398 dbgCreateInfo.pUserData = demo;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003399 dbgCreateInfo.flags =
Mark Lobodzinskicf9784e2016-02-11 09:26:16 -07003400 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003401 err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL,
3402 &demo->msg_callback);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003403 switch (err) {
3404 case VK_SUCCESS:
3405 break;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003406 case VK_ERROR_OUT_OF_HOST_MEMORY:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003407 ERR_EXIT("CreateDebugReportCallback: out of host memory\n",
3408 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003409 break;
3410 default:
Courtney Goeltzenleuchter2dafae42015-11-30 12:13:14 -07003411 ERR_EXIT("CreateDebugReportCallback: unknown failure\n",
3412 "CreateDebugReportCallback Failure");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06003413 break;
3414 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003415 }
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003416 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003417
3418 /* Call with NULL data to get count */
Tony Barbourab2a0362016-09-06 11:40:12 -06003419 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu,
3420 &demo->queue_family_count, NULL);
3421 assert(demo->queue_family_count >= 1);
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003422
Karl Schultze4dc75c2016-02-02 15:37:51 -07003423 demo->queue_props = (VkQueueFamilyProperties *)malloc(
Tony Barbourab2a0362016-09-06 11:40:12 -06003424 demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3425 vkGetPhysicalDeviceQueueFamilyProperties(
3426 demo->gpu, &demo->queue_family_count, demo->queue_props);
3427
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003428 // Query fine-grained feature support for this device.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003429 // If app has specific feature requirements it should check supported
3430 // features based on this query
Tobin Ehlis8d03b7c2015-09-29 11:22:37 -06003431 VkPhysicalDeviceFeatures physDevFeatures;
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003432 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
Tobin Ehlisbf3020e2015-09-24 15:18:22 -06003433
Tony Barbourda2bad52016-01-22 14:36:40 -07003434 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3435 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3436 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3437 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3438 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3439}
3440
Karl Schultze4dc75c2016-02-02 15:37:51 -07003441static void demo_create_device(struct demo *demo) {
Tony Barbourda2bad52016-01-22 14:36:40 -07003442 VkResult U_ASSERT_ONLY err;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003443 float queue_priorities[1] = {0.0};
Tony Barbour22e06272016-09-07 16:07:56 -06003444 VkDeviceQueueCreateInfo queues[2];
3445 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3446 queues[0].pNext = NULL;
3447 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3448 queues[0].queueCount = 1;
3449 queues[0].pQueuePriorities = queue_priorities;
3450 queues[0].flags = 0;
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003451
3452 VkDeviceCreateInfo device = {
3453 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3454 .pNext = NULL,
Chia-I Wu8b497442015-11-06 06:42:02 +08003455 .queueCreateInfoCount = 1,
Tony Barbour22e06272016-09-07 16:07:56 -06003456 .pQueueCreateInfos = queues,
Karl Schultz5b423ea2016-06-20 19:08:43 -06003457 .enabledLayerCount = 0,
3458 .ppEnabledLayerNames = NULL,
Tony Barbourda2bad52016-01-22 14:36:40 -07003459 .enabledExtensionCount = demo->enabled_extension_count,
Karl Schultze4dc75c2016-02-02 15:37:51 -07003460 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3461 .pEnabledFeatures =
3462 NULL, // If specific features are required, pass them in here
Tobin Ehlis9626ba62015-09-22 13:52:37 -06003463 };
Tony Barbour22e06272016-09-07 16:07:56 -06003464 if (demo->separate_present_queue) {
3465 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3466 queues[1].pNext = NULL;
3467 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3468 queues[1].queueCount = 1;
3469 queues[1].pQueuePriorities = queue_priorities;
3470 queues[1].flags = 0;
3471 device.queueCreateInfoCount = 2;
3472 }
Chia-I Wu63636062015-10-26 21:10:41 +08003473 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003474 assert(!err);
Courtney Goeltzenleuchter3a35c822015-07-15 17:45:38 -06003475}
3476
Karl Schultze4dc75c2016-02-02 15:37:51 -07003477static void demo_init_vk_swapchain(struct demo *demo) {
Courtney Goeltzenleuchterd3cdaaf2015-12-17 09:53:29 -07003478 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003479
Karl Schultze4dc75c2016-02-02 15:37:51 -07003480// Create a WSI surface for the window:
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003481#if defined(VK_USE_PLATFORM_WIN32_KHR)
Ian Elliotta7a731c2015-12-11 15:52:12 -07003482 VkWin32SurfaceCreateInfoKHR createInfo;
3483 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3484 createInfo.pNext = NULL;
3485 createInfo.flags = 0;
Jon Ashburnb90f50d2015-12-24 17:08:01 -07003486 createInfo.hinstance = demo->connection;
3487 createInfo.hwnd = demo->window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003488
Karl Schultze4dc75c2016-02-02 15:37:51 -07003489 err =
3490 vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003491#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003492 VkWaylandSurfaceCreateInfoKHR createInfo;
3493 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3494 createInfo.pNext = NULL;
3495 createInfo.flags = 0;
3496 createInfo.display = demo->display;
3497 createInfo.surface = demo->window;
3498
3499 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL,
3500 &demo->surface);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003501#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003502#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3503 VkAndroidSurfaceCreateInfoKHR createInfo;
3504 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3505 createInfo.pNext = NULL;
3506 createInfo.flags = 0;
3507 createInfo.window = (ANativeWindow*)(demo->window);
Ian Elliottb08e0d92015-11-20 11:55:46 -07003508
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003509 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003510#elif defined(VK_USE_PLATFORM_XLIB_KHR)
3511 VkXlibSurfaceCreateInfoKHR createInfo;
3512 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3513 createInfo.pNext = NULL;
3514 createInfo.flags = 0;
3515 createInfo.dpy = demo->display;
3516 createInfo.window = demo->xlib_window;
Ian Elliotta7a731c2015-12-11 15:52:12 -07003517
Tony Barbour153cb062016-12-07 13:43:36 -07003518 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL,
Tony Barbour2593b182016-04-19 10:57:58 -06003519 &demo->surface);
Tony Barbour153cb062016-12-07 13:43:36 -07003520#elif defined(VK_USE_PLATFORM_XCB_KHR)
3521 VkXcbSurfaceCreateInfoKHR createInfo;
3522 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3523 createInfo.pNext = NULL;
3524 createInfo.flags = 0;
3525 createInfo.connection = demo->connection;
3526 createInfo.window = demo->xcb_window;
Tony Barbour2593b182016-04-19 10:57:58 -06003527
Tony Barbour153cb062016-12-07 13:43:36 -07003528 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
Damien Leone600c3052017-01-31 10:26:07 -07003529#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3530 err = demo_create_display_surface(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003531#elif defined(VK_USE_PLATFORM_IOS_MVK)
3532 VkIOSSurfaceCreateInfoMVK surface;
3533 surface.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
3534 surface.pNext = NULL;
3535 surface.flags = 0;
3536 surface.pView = demo->window;
3537
3538 err = vkCreateIOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
3539#elif defined(VK_USE_PLATFORM_MACOS_MVK)
3540 VkMacOSSurfaceCreateInfoMVK surface;
3541 surface.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
3542 surface.pNext = NULL;
3543 surface.flags = 0;
3544 surface.pView = demo->window;
3545
3546 err = vkCreateMacOSSurfaceMVK(demo->inst, &surface, NULL, &demo->surface);
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003547#endif
Tony Barbour2593b182016-04-19 10:57:58 -06003548 assert(!err);
Ian Elliottaaae5352015-07-06 14:27:58 -06003549
Tony Barbourcc69f452015-10-08 13:59:42 -06003550 // Iterate over each queue to learn whether it supports presenting:
Karl Schultze4dc75c2016-02-02 15:37:51 -07003551 VkBool32 *supportsPresent =
Tony Barbourab2a0362016-09-06 11:40:12 -06003552 (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
Karl Schultza2615462017-01-20 13:19:20 -07003553 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003554 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface,
Ian Elliottaaae5352015-07-06 14:27:58 -06003555 &supportsPresent[i]);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003556 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003557
3558 // Search for a graphics and a present queue in the array of queue
3559 // families, try to find one that supports both
Tony Barbourab2a0362016-09-06 11:40:12 -06003560 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3561 uint32_t presentQueueFamilyIndex = UINT32_MAX;
Karl Schultza2615462017-01-20 13:19:20 -07003562 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003563 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3564 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3565 graphicsQueueFamilyIndex = i;
3566 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003567
Tony Barbourab2a0362016-09-06 11:40:12 -06003568 if (supportsPresent[i] == VK_TRUE) {
3569 graphicsQueueFamilyIndex = i;
3570 presentQueueFamilyIndex = i;
3571 break;
3572 }
3573 }
3574 }
3575
3576 if (presentQueueFamilyIndex == UINT32_MAX) {
3577 // If didn't find a queue that supports both graphics and present, then
3578 // find a separate present queue.
Karl Schultza2615462017-01-20 13:19:20 -07003579 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
Tony Barbourab2a0362016-09-06 11:40:12 -06003580 if (supportsPresent[i] == VK_TRUE) {
3581 presentQueueFamilyIndex = i;
3582 break;
3583 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003584 }
3585 }
Ian Elliottaaae5352015-07-06 14:27:58 -06003586
3587 // Generate error if could not find both a graphics and a present queue
Tony Barbourab2a0362016-09-06 11:40:12 -06003588 if (graphicsQueueFamilyIndex == UINT32_MAX ||
3589 presentQueueFamilyIndex == UINT32_MAX) {
Tony Barboura2a67812016-07-18 13:21:06 -06003590 ERR_EXIT("Could not find both graphics and present queues\n",
Tony Barbourcc69f452015-10-08 13:59:42 -06003591 "Swapchain Initialization Failure");
Ian Elliottaaae5352015-07-06 14:27:58 -06003592 }
3593
Tony Barbourab2a0362016-09-06 11:40:12 -06003594 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3595 demo->present_queue_family_index = presentQueueFamilyIndex;
Tony Barbour22e06272016-09-07 16:07:56 -06003596 demo->separate_present_queue =
3597 (demo->graphics_queue_family_index != demo->present_queue_family_index);
Tony Barbourab2a0362016-09-06 11:40:12 -06003598 free(supportsPresent);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07003599
Tony Barbourda2bad52016-01-22 14:36:40 -07003600 demo_create_device(demo);
3601
3602 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3603 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3604 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3605 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3606 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
Ian Elliottd940ca22017-03-22 08:31:27 -06003607 if (demo->VK_GOOGLE_display_timing_enabled) {
3608 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3609 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3610 }
Tony Barbourda2bad52016-01-22 14:36:40 -07003611
Tony Barboura2a67812016-07-18 13:21:06 -06003612 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0,
3613 &demo->graphics_queue);
3614
Tony Barbour22e06272016-09-07 16:07:56 -06003615 if (!demo->separate_present_queue) {
Tony Barboura2a67812016-07-18 13:21:06 -06003616 demo->present_queue = demo->graphics_queue;
3617 } else {
3618 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0,
3619 &demo->present_queue);
3620 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003621
Ian Elliottaaae5352015-07-06 14:27:58 -06003622 // Get the list of VkFormat's that are supported:
Ian Elliott8528eff2015-08-07 15:56:59 -06003623 uint32_t formatCount;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003624 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003625 &formatCount, NULL);
Ian Elliottaaae5352015-07-06 14:27:58 -06003626 assert(!err);
Ian Elliotta0781972015-08-21 15:09:33 -06003627 VkSurfaceFormatKHR *surfFormats =
3628 (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
Karl Schultze4dc75c2016-02-02 15:37:51 -07003629 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface,
Ian Elliottf2ff8022015-11-23 12:48:15 -07003630 &formatCount, surfFormats);
Ian Elliottaaae5352015-07-06 14:27:58 -06003631 assert(!err);
3632 // If the format list includes just one entry of VK_FORMAT_UNDEFINED,
3633 // the surface has no preferred format. Otherwise, at least one
3634 // supported format will be returned.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003635 if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) {
Ian Elliottaaae5352015-07-06 14:27:58 -06003636 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003637 } else {
Ian Elliottaaae5352015-07-06 14:27:58 -06003638 assert(formatCount >= 1);
3639 demo->format = surfFormats[0].format;
3640 }
Ian Elliott8528eff2015-08-07 15:56:59 -06003641 demo->color_space = surfFormats[0].colorSpace;
Ian Elliotte4602cd2015-04-21 16:41:02 -06003642
David Pinedo2bb7c932015-06-18 17:03:14 -06003643 demo->quit = false;
3644 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003645
Tony Barbource7524e2016-07-28 12:01:45 -06003646 // Create semaphores to synchronize acquiring presentable buffers before
3647 // rendering and waiting for drawing to be complete before presenting
3648 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3649 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3650 .pNext = NULL,
3651 .flags = 0,
3652 };
Tony Barbource7524e2016-07-28 12:01:45 -06003653
Tony Barbour66a56c32016-09-21 13:10:56 -06003654 // Create fences that we can use to throttle if we get too far
3655 // ahead of the image presents
3656 VkFenceCreateInfo fence_ci = {
3657 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
3658 .pNext = NULL,
szdarkhackd322ff02016-10-08 09:51:22 +03003659 .flags = VK_FENCE_CREATE_SIGNALED_BIT
Tony Barbour66a56c32016-09-21 13:10:56 -06003660 };
3661 for (uint32_t i = 0; i < FRAME_LAG; i++) {
Tony Barboura72d9492017-01-11 13:35:09 -07003662 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3663 assert(!err);
3664
Tony Barbour22e06272016-09-07 16:07:56 -06003665 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
Tony Barbour66a56c32016-09-21 13:10:56 -06003666 &demo->image_acquired_semaphores[i]);
Tony Barbour22e06272016-09-07 16:07:56 -06003667 assert(!err);
Tony Barbour66a56c32016-09-21 13:10:56 -06003668
3669 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3670 &demo->draw_complete_semaphores[i]);
3671 assert(!err);
3672
3673 if (demo->separate_present_queue) {
3674 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL,
3675 &demo->image_ownership_semaphores[i]);
3676 assert(!err);
3677 }
Tony Barbour22e06272016-09-07 16:07:56 -06003678 }
Tony Barbour66a56c32016-09-21 13:10:56 -06003679 demo->frame_index = 0;
Tony Barbour22e06272016-09-07 16:07:56 -06003680
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06003681 // Get Memory information and properties
Courtney Goeltzenleuchterbd5c1742015-10-20 16:40:38 -06003682 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003683}
3684
Tony Barbour153cb062016-12-07 13:43:36 -07003685#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003686static void registry_handle_global(void *data, struct wl_registry *registry,
3687 uint32_t name, const char *interface,
3688 uint32_t version UNUSED) {
3689 struct demo *demo = data;
3690 if (strcmp(interface, "wl_compositor") == 0) {
3691 demo->compositor =
3692 wl_registry_bind(registry, name, &wl_compositor_interface, 3);
3693 /* Todo: When xdg_shell protocol has stablized, we should move wl_shell
3694 * tp xdg_shell */
3695 } else if (strcmp(interface, "wl_shell") == 0) {
3696 demo->shell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
3697 }
3698}
3699
3700static void registry_handle_global_remove(void *data UNUSED,
3701 struct wl_registry *registry UNUSED,
3702 uint32_t name UNUSED) {}
3703
3704static const struct wl_registry_listener registry_listener = {
3705 registry_handle_global, registry_handle_global_remove};
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003706#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003707#endif
3708
Karl Schultze4dc75c2016-02-02 15:37:51 -07003709static void demo_init_connection(struct demo *demo) {
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003710#if defined(VK_USE_PLATFORM_XCB_KHR)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003711 const xcb_setup_t *setup;
3712 xcb_screen_iterator_t iter;
3713 int scr;
3714
3715 demo->connection = xcb_connect(NULL, &scr);
Lenny Komow022bc2a2016-08-11 10:57:38 -06003716 if (xcb_connection_has_error(demo->connection) > 0) {
Ian Elliott3979e282015-04-03 15:24:55 -06003717 printf("Cannot find a compatible Vulkan installable client driver "
3718 "(ICD).\nExiting ...\n");
3719 fflush(stdout);
3720 exit(1);
3721 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003722
3723 setup = xcb_get_setup(demo->connection);
3724 iter = xcb_setup_roots_iterator(setup);
3725 while (scr-- > 0)
3726 xcb_screen_next(&iter);
3727
3728 demo->screen = iter.data;
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09003729#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3730 demo->display = wl_display_connect(NULL);
3731
3732 if (demo->display == NULL) {
3733 printf("Cannot find a compatible Vulkan installable client driver "
3734 "(ICD).\nExiting ...\n");
3735 fflush(stdout);
3736 exit(1);
3737 }
3738
3739 demo->registry = wl_display_get_registry(demo->display);
3740 wl_registry_add_listener(demo->registry, &registry_listener, demo);
3741 wl_display_dispatch(demo->display);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07003742#elif defined(VK_USE_PLATFORM_MIR_KHR)
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003743#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003744}
3745
Karl Schultze4dc75c2016-02-02 15:37:51 -07003746static void demo_init(struct demo *demo, int argc, char **argv) {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003747 vec3 eye = {0.0f, 3.0f, 5.0f};
3748 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08003749 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003750
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003751 memset(demo, 0, sizeof(*demo));
Tony Barbour291d57b2016-10-21 14:47:07 -06003752 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
Tony Barbour1a86d032015-09-21 15:17:33 -06003753 demo->frameCount = INT32_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003754
Piers Daniell735ee532015-02-23 16:23:13 -07003755 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003756 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003757 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003758 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06003759 }
Tony Barbour291d57b2016-10-21 14:47:07 -06003760 if ((strcmp(argv[i], "--present_mode") == 0) &&
3761 (i < argc - 1)) {
3762 demo->presentMode = atoi(argv[i+1]);
3763 i++;
3764 continue;
3765 }
Courtney Goeltzenleuchter03f4b772015-07-22 11:03:51 -06003766 if (strcmp(argv[i], "--break") == 0) {
3767 demo->use_break = true;
3768 continue;
3769 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003770 if (strcmp(argv[i], "--validate") == 0) {
3771 demo->validate = true;
3772 continue;
3773 }
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003774 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3775 demo->validate = true;
3776 demo->validate_checks_disabled = true;
3777 continue;
3778 }
Tony Barbour2593b182016-04-19 10:57:58 -06003779 if (strcmp(argv[i], "--xlib") == 0) {
Tony Barbour153cb062016-12-07 13:43:36 -07003780 fprintf(stderr, "--xlib is deprecated and no longer does anything");
Tony Barbour2593b182016-04-19 10:57:58 -06003781 continue;
3782 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003783 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX &&
3784 i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 &&
3785 demo->frameCount >= 0) {
David Pinedo2bb7c932015-06-18 17:03:14 -06003786 i++;
3787 continue;
3788 }
lenny-lunargfbe22392016-06-06 11:07:53 -06003789 if (strcmp(argv[i], "--suppress_popups") == 0) {
3790 demo->suppress_popups = true;
3791 continue;
3792 }
Ian Elliottd940ca22017-03-22 08:31:27 -06003793 if (strcmp(argv[i], "--display_timing") == 0) {
3794 demo->VK_GOOGLE_display_timing_enabled = true;
3795 continue;
3796 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003797
Cody Northropaf28a532016-09-27 10:50:57 -06003798#if defined(ANDROID)
3799 ERR_EXIT("Usage: cube [--validate]\n", "Usage");
3800#else
Tobin Ehlis4eb29d62017-03-14 11:29:01 -06003801 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--validate-checks-disabled] [--break] "
Ian Elliottd940ca22017-03-22 08:31:27 -06003802 "[--c <framecount>] [--suppress_popups] [--display_timing] [--present_mode <present mode enum>]\n"
Tony Barbour291d57b2016-10-21 14:47:07 -06003803 "VK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
3804 "VK_PRESENT_MODE_MAILBOX_KHR = %d\n"
3805 "VK_PRESENT_MODE_FIFO_KHR = %d\n"
3806 "VK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n",
3807 APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
3808 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
Ian Elliott639ca472015-04-16 15:23:05 -06003809 fflush(stderr);
3810 exit(1);
Cody Northropaf28a532016-09-27 10:50:57 -06003811#endif
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07003812 }
3813
Tony Barbour153cb062016-12-07 13:43:36 -07003814 demo_init_connection(demo);
Tony Barbour2593b182016-04-19 10:57:58 -06003815
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06003816 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003817
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003818 demo->width = 500;
3819 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003820
Tony Barbour66a56c32016-09-21 13:10:56 -06003821 demo->spin_angle = 4.0f;
3822 demo->spin_increment = 0.2f;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003823 demo->pause = false;
3824
Karl Schultze4dc75c2016-02-02 15:37:51 -07003825 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f),
3826 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06003827 mat4x4_look_at(demo->view_matrix, eye, origin, up);
3828 mat4x4_identity(demo->model_matrix);
Rene Lindsaybcccdea2016-08-12 17:56:09 -06003829
3830 demo->projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation.
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06003831}
3832
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003833#if defined(VK_USE_PLATFORM_WIN32_KHR)
Mark Young418b9d12016-01-06 14:26:04 -07003834// Include header required for parsing the command line options.
3835#include <shellapi.h>
Ian Elliottf9cf78c2015-04-28 10:33:11 -06003836
Karl Schultze4dc75c2016-02-02 15:37:51 -07003837int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
3838 int nCmdShow) {
3839 MSG msg; // message
3840 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003841 int argc;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003842 char **argv;
Ian Elliott639ca472015-04-16 15:23:05 -06003843
Jamie Madillb2ff6502017-03-15 16:17:46 -04003844 // Ensure wParam is initialized.
3845 msg.wParam = 0;
3846
Karl Schultze4dc75c2016-02-02 15:37:51 -07003847 // Use the CommandLine functions to get the command line arguments.
3848 // Unfortunately, Microsoft outputs
3849 // this information as wide characters for Unicode, and we simply want the
3850 // Ascii version to be compatible
3851 // with the non-Windows side. So, we have to convert the information to
3852 // Ascii character strings.
3853 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
3854 if (NULL == commandLineArgs) {
Mark Young418b9d12016-01-06 14:26:04 -07003855 argc = 0;
3856 }
3857
Karl Schultze4dc75c2016-02-02 15:37:51 -07003858 if (argc > 0) {
3859 argv = (char **)malloc(sizeof(char *) * argc);
3860 if (argv == NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003861 argc = 0;
Karl Schultze4dc75c2016-02-02 15:37:51 -07003862 } else {
3863 for (int iii = 0; iii < argc; iii++) {
3864 size_t wideCharLen = wcslen(commandLineArgs[iii]);
Mark Young418b9d12016-01-06 14:26:04 -07003865 size_t numConverted = 0;
3866
Karl Schultze4dc75c2016-02-02 15:37:51 -07003867 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
3868 if (argv[iii] != NULL) {
3869 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1,
3870 commandLineArgs[iii], wideCharLen + 1);
Mark Young418b9d12016-01-06 14:26:04 -07003871 }
3872 }
3873 }
Karl Schultze4dc75c2016-02-02 15:37:51 -07003874 } else {
Mark Young418b9d12016-01-06 14:26:04 -07003875 argv = NULL;
3876 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003877
3878 demo_init(&demo, argc, argv);
Mark Young418b9d12016-01-06 14:26:04 -07003879
3880 // Free up the items we had to allocate for the command line arguments.
Karl Schultze4dc75c2016-02-02 15:37:51 -07003881 if (argc > 0 && argv != NULL) {
3882 for (int iii = 0; iii < argc; iii++) {
3883 if (argv[iii] != NULL) {
Mark Young418b9d12016-01-06 14:26:04 -07003884 free(argv[iii]);
3885 }
3886 }
3887 free(argv);
3888 }
3889
Tony Barbour3dddd5d2015-04-29 16:19:20 -06003890 demo.connection = hInstance;
3891 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06003892 demo_create_window(&demo);
Tony Barbourcc69f452015-10-08 13:59:42 -06003893 demo_init_vk_swapchain(&demo);
Ian Elliott639ca472015-04-16 15:23:05 -06003894
3895 demo_prepare(&demo);
3896
Karl Schultze4dc75c2016-02-02 15:37:51 -07003897 done = false; // initialize loop condition variable
Mark Young418b9d12016-01-06 14:26:04 -07003898
3899 // main message loop
Karl Schultze4dc75c2016-02-02 15:37:51 -07003900 while (!done) {
Ian Elliotta748eaf2015-04-28 15:50:36 -06003901 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Karl Schultze4dc75c2016-02-02 15:37:51 -07003902 if (msg.message == WM_QUIT) // check for a quit message
Ian Elliott639ca472015-04-16 15:23:05 -06003903 {
Karl Schultze4dc75c2016-02-02 15:37:51 -07003904 done = true; // if found, quit app
3905 } else {
Ian Elliott639ca472015-04-16 15:23:05 -06003906 /* Translate and dispatch to event queue*/
Karl Schultze4dc75c2016-02-02 15:37:51 -07003907 TranslateMessage(&msg);
Ian Elliott639ca472015-04-16 15:23:05 -06003908 DispatchMessage(&msg);
3909 }
Tony Barbourc8a59892015-10-20 12:49:46 -06003910 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
Ian Elliott639ca472015-04-16 15:23:05 -06003911 }
3912
3913 demo_cleanup(&demo);
3914
Karl Schultze4dc75c2016-02-02 15:37:51 -07003915 return (int)msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06003916}
Bill Hollingsf4352142017-02-14 22:58:56 -05003917
3918#elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)
3919static void demo_main(struct demo *demo, void* view) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003920 const char* argv[] = { "CubeSample" };
3921 int argc = sizeof(argv) / sizeof(char*);
Bill Hollingsf4352142017-02-14 22:58:56 -05003922
Tony Barbourc65cd752017-03-13 15:29:35 -06003923 demo_init(demo, argc, (char**)argv);
3924 demo->window = view;
3925 demo_init_vk_swapchain(demo);
3926 demo_prepare(demo);
3927 demo->spin_angle = 0.4f;
Bill Hollingsf4352142017-02-14 22:58:56 -05003928}
3929
3930static void demo_update_and_draw(struct demo *demo) {
Tony Barbourc65cd752017-03-13 15:29:35 -06003931 // Wait for work to finish before updating MVP.
3932 vkDeviceWaitIdle(demo->device);
3933 demo_update_data_buffer(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003934
Tony Barbourc65cd752017-03-13 15:29:35 -06003935 demo_draw(demo);
Bill Hollingsf4352142017-02-14 22:58:56 -05003936}
3937
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003938#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3939#include <android/log.h>
3940#include <android_native_app_glue.h>
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003941#include "android_util.h"
3942
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003943static bool initialized = false;
3944static bool active = false;
3945struct demo demo;
3946
3947static int32_t processInput(struct android_app* app, AInputEvent* event) {
3948 return 0;
3949}
3950
3951static void processCommand(struct android_app* app, int32_t cmd) {
3952 switch(cmd) {
3953 case APP_CMD_INIT_WINDOW: {
3954 if (app->window) {
Ian Elliottf05d5d12016-05-17 12:03:35 -06003955 // We're getting a new window. If the app is starting up, we
3956 // need to initialize. If the app has already been
3957 // initialized, that means that we lost our previous window,
3958 // which means that we have a lot of work to do. At a minimum,
3959 // we need to destroy the swapchain and surface associated with
3960 // the old window, and create a new surface and swapchain.
3961 // However, since there are a lot of other objects/state that
3962 // is tied to the swapchain, it's easiest to simply cleanup and
3963 // start over (i.e. use a brute-force approach of re-starting
3964 // the app)
3965 if (demo.prepared) {
3966 demo_cleanup(&demo);
3967 }
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003968
3969 // Parse Intents into argc, argv
3970 // Use the following key to send arguments, i.e.
3971 // --es args "--validate"
3972 const char key[] = "args";
Cody Northropaf28a532016-09-27 10:50:57 -06003973 char* appTag = (char*) APP_SHORT_NAME;
Cody Northropc5e5a8c2016-09-26 21:05:00 -06003974 int argc = 0;
3975 char** argv = get_args(app, key, appTag, &argc);
3976
3977 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
3978 for (int i = 0; i < argc; i++)
3979 __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
3980
3981 demo_init(&demo, argc, argv);
3982
3983 // Free the argv malloc'd by get_args
3984 for (int i = 0; i < argc; i++)
3985 free(argv[i]);
3986
Michael Lentinec6cde4b2016-04-18 13:20:45 -05003987 demo.window = (void*)app->window;
3988 demo_init_vk_swapchain(&demo);
3989 demo_prepare(&demo);
3990 initialized = true;
3991 }
3992 break;
3993 }
3994 case APP_CMD_GAINED_FOCUS: {
3995 active = true;
3996 break;
3997 }
3998 case APP_CMD_LOST_FOCUS: {
3999 active = false;
4000 break;
4001 }
4002 }
4003}
4004
4005void android_main(struct android_app *app)
4006{
4007 app_dummy();
4008
Cody Northrop8707a6e2016-04-26 19:59:19 -06004009#ifdef ANDROID
4010 int vulkanSupport = InitVulkan();
4011 if (vulkanSupport == 0)
4012 return;
4013#endif
4014
Ian Elliottf05d5d12016-05-17 12:03:35 -06004015 demo.prepared = false;
4016
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004017 app->onAppCmd = processCommand;
4018 app->onInputEvent = processInput;
4019
4020 while(1) {
4021 int events;
4022 struct android_poll_source* source;
4023 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) {
4024 if (source) {
4025 source->process(app, source);
4026 }
4027
4028 if (app->destroyRequested != 0) {
4029 demo_cleanup(&demo);
4030 return;
4031 }
4032 }
4033 if (initialized && active) {
4034 demo_run(&demo);
4035 }
4036 }
4037
4038}
Tobin Ehlis43ed2982016-04-28 10:17:33 -06004039#else
Karl Schultze4dc75c2016-02-02 15:37:51 -07004040int main(int argc, char **argv) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004041 struct demo demo;
4042
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07004043 demo_init(&demo, argc, argv);
Tony Barbour153cb062016-12-07 13:43:36 -07004044#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004045 demo_create_xcb_window(&demo);
4046#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4047 demo_create_xlib_window(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004048#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4049 demo_create_window(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004050#elif defined(VK_USE_PLATFORM_MIR_KHR)
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004051#endif
Tony Barbour2593b182016-04-19 10:57:58 -06004052
Tony Barbourcc69f452015-10-08 13:59:42 -06004053 demo_init_vk_swapchain(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004054
4055 demo_prepare(&demo);
Tony Barbour2593b182016-04-19 10:57:58 -06004056
Tony Barbour153cb062016-12-07 13:43:36 -07004057#if defined(VK_USE_PLATFORM_XCB_KHR)
Tony Barbour8295ac42016-08-08 11:04:17 -06004058 demo_run_xcb(&demo);
4059#elif defined(VK_USE_PLATFORM_XLIB_KHR)
4060 demo_run_xlib(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004061#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4062 demo_run(&demo);
Tony Barbourefd0c5a2016-12-07 14:45:12 -07004063#elif defined(VK_USE_PLATFORM_MIR_KHR)
Damien Leone600c3052017-01-31 10:26:07 -07004064#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4065 demo_run_display(&demo);
Mun Gwan-gyeong6f01c552016-06-27 05:34:44 +09004066#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004067
4068 demo_cleanup(&demo);
4069
Karl Schultz0218c002016-03-22 17:06:13 -06004070 return validation_error;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06004071}
Michael Lentinec6cde4b2016-04-18 13:20:45 -05004072#endif